Style fixes to HttpForm

This commit is contained in:
John Scipione
2013-11-04 20:16:05 -05:00
parent e2183a14c4
commit f1e63b05cb
2 changed files with 165 additions and 160 deletions
+32 -36
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010 Haiku Inc. All rights reserved. * Copyright 2010-2013 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _B_HTTP_FORM_H_ #ifndef _B_HTTP_FORM_H_
@@ -33,9 +33,9 @@ private:
// check up) // check up)
BHttpFormData(); BHttpFormData();
friend class std::map<BString, BHttpFormData>; friend class std::map<BString, BHttpFormData>;
public: public:
BHttpFormData(const BString& name, BHttpFormData(const BString& name,
const BString& value); const BString& value);
BHttpFormData(const BString& name, BHttpFormData(const BString& name,
const BPath& file); const BPath& file);
@@ -43,27 +43,27 @@ public:
const void* buffer, ssize_t size); const void* buffer, ssize_t size);
BHttpFormData(const BHttpFormData& other); BHttpFormData(const BHttpFormData& other);
~BHttpFormData(); ~BHttpFormData();
// Retrieve data informations // Retrieve data informations
bool InitCheck() const; bool InitCheck() const;
const BString& Name() const; const BString& Name() const;
const BString& String() const; const BString& String() const;
const BPath& File() const; const BPath& File() const;
const void* Buffer() const; const void* Buffer() const;
ssize_t BufferSize() const; ssize_t BufferSize() const;
bool IsFile() const; bool IsFile() const;
const BString& Filename() const; const BString& Filename() const;
const BString& MimeType() const; const BString& MimeType() const;
form_content_type Type() const; form_content_type Type() const;
// Change behavior // Change behavior
status_t MarkAsFile(const BString& filename, status_t MarkAsFile(const BString& filename,
const BString& mimeType = ""); const BString& mimeType = "");
void UnmarkAsFile(); void UnmarkAsFile();
status_t CopyBuffer(); status_t CopyBuffer();
// Overloaded operators // Overloaded operators
BHttpFormData& operator=(const BHttpFormData& other); BHttpFormData& operator=(const BHttpFormData& other);
@@ -71,13 +71,13 @@ private:
form_content_type fDataType; form_content_type fDataType;
bool fCopiedBuffer; bool fCopiedBuffer;
bool fFileMark; bool fFileMark;
BString fName; BString fName;
BString fStringValue; BString fStringValue;
BPath fPathValue; BPath fPathValue;
const void* fBufferValue; const void* fBufferValue;
ssize_t fBufferSize; ssize_t fBufferSize;
BString fFilename; BString fFilename;
BString fMimeType; BString fMimeType;
}; };
@@ -88,28 +88,28 @@ public:
// Nested types // Nested types
class Iterator; class Iterator;
typedef std::map<BString, BHttpFormData> FormStorage; typedef std::map<BString, BHttpFormData> FormStorage;
public: public:
BHttpForm(); BHttpForm();
BHttpForm(const BHttpForm& other); BHttpForm(const BHttpForm& other);
BHttpForm(const BString& formString); BHttpForm(const BString& formString);
~BHttpForm(); ~BHttpForm();
// Form string parsing // Form string parsing
void ParseString(const BString& formString); void ParseString(const BString& formString);
BString RawData() const; BString RawData() const;
// Form add // Form add
status_t AddString(const BString& name, status_t AddString(const BString& name,
const BString& value); const BString& value);
status_t AddInt(const BString& name, int32 value); status_t AddInt(const BString& name, int32 value);
status_t AddFile(const BString& fieldName, status_t AddFile(const BString& fieldName,
const BPath& file); const BPath& file);
status_t AddBuffer(const BString& fieldName, status_t AddBuffer(const BString& fieldName,
const void* buffer, ssize_t size); const void* buffer, ssize_t size);
status_t AddBufferCopy(const BString& fieldName, status_t AddBufferCopy(const BString& fieldName,
const void* buffer, ssize_t size); const void* buffer, ssize_t size);
// Mark a field as a filename // Mark a field as a filename
void MarkAsFile(const BString& fieldName, void MarkAsFile(const BString& fieldName,
const BString& filename, const BString& filename,
@@ -117,52 +117,48 @@ public:
void MarkAsFile(const BString& fieldName, void MarkAsFile(const BString& fieldName,
const BString& filename); const BString& filename);
void UnmarkAsFile(const BString& fieldName); void UnmarkAsFile(const BString& fieldName);
// Change form type // Change form type
void SetFormType(form_type type); void SetFormType(form_type type);
// Form test // Form test
bool HasField(const BString& name) const; bool HasField(const BString& name) const;
// Form retrieve // Form retrieve
BString GetMultipartHeader(const BString& fieldName) BString GetMultipartHeader(const BString& fieldName) const;
const;
form_content_type GetType(const BString& fieldname) const; form_content_type GetType(const BString& fieldname) const;
// Form informations // Form informations
form_type GetFormType() const; form_type GetFormType() const;
const BString& GetMultipartBoundary() const; const BString& GetMultipartBoundary() const;
BString GetMultipartFooter() const; BString GetMultipartFooter() const;
ssize_t ContentLength() const; ssize_t ContentLength() const;
// Form iterator // Form iterator
Iterator GetIterator(); Iterator GetIterator();
// Form clear // Form clear
void Clear(); void Clear();
// Overloaded operators // Overloaded operators
BHttpFormData& operator[](const BString& name); BHttpFormData& operator[](const BString& name);
private: private:
void _ExtractNameValuePair(const BString& string, void _ExtractNameValuePair(const BString& string, int32* index);
int32* index);
void _GenerateMultipartBoundary(); void _GenerateMultipartBoundary();
BString _GetMultipartHeader( BString _GetMultipartHeader(const BHttpFormData* element) const;
const BHttpFormData* element) const;
form_content_type _GetType(FormStorage::const_iterator it) const; form_content_type _GetType(FormStorage::const_iterator it) const;
void _Erase(FormStorage::iterator it); void _Erase(FormStorage::iterator it);
private: private:
friend class Iterator; friend class Iterator;
FormStorage fFields; FormStorage fFields;
form_type fType; form_type fType;
BString fMultipartBoundary; BString fMultipartBoundary;
}; };
class BHttpForm::Iterator { class BHttpForm::Iterator {
public: public:
Iterator(const Iterator& other); Iterator(const Iterator& other);
@@ -171,7 +167,7 @@ public:
bool HasNext() const; bool HasNext() const;
void Remove(); void Remove();
BString MultipartHeader(); BString MultipartHeader();
Iterator& operator=(const Iterator& other); Iterator& operator=(const Iterator& other);
private: private:
@@ -179,7 +175,7 @@ private:
void _FindNext(); void _FindNext();
private: private:
friend class BHttpForm; friend class BHttpForm;
BHttpForm* fForm; BHttpForm* fForm;
BHttpForm::FormStorage::iterator BHttpForm::FormStorage::iterator
+133 -124
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010 Haiku Inc. All rights reserved. * Copyright 2010-2013 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -7,22 +7,24 @@
*/ */
#include <HttpForm.h>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include <File.h> #include <File.h>
#include <HttpForm.h>
#include <NodeInfo.h> #include <NodeInfo.h>
#include <TypeConstants.h> #include <TypeConstants.h>
#include <Url.h> #include <Url.h>
static int32 kBoundaryRandomSize = 16; static int32 kBoundaryRandomSize = 16;
using namespace std; using namespace std;
// #pragma mark -- BHttpFormData // #pragma mark - BHttpFormData
BHttpFormData::BHttpFormData() BHttpFormData::BHttpFormData()
@@ -62,7 +64,7 @@ BHttpFormData::BHttpFormData(const BString& name, const BPath& file)
} }
BHttpFormData::BHttpFormData(const BString& name, const void* buffer, BHttpFormData::BHttpFormData(const BString& name, const void* buffer,
ssize_t size) ssize_t size)
: :
fDataType(B_HTTPFORM_BUFFER), fDataType(B_HTTPFORM_BUFFER),
@@ -91,9 +93,9 @@ BHttpFormData::~BHttpFormData()
if (fCopiedBuffer) if (fCopiedBuffer)
delete[] reinterpret_cast<const char*>(fBufferValue); delete[] reinterpret_cast<const char*>(fBufferValue);
} }
// #pragma mark Retrieve data informations
// #pragma mark - Retrieve data informations
bool bool
@@ -101,7 +103,7 @@ BHttpFormData::InitCheck() const
{ {
if (fDataType == B_HTTPFORM_BUFFER) if (fDataType == B_HTTPFORM_BUFFER)
return fBufferValue != NULL; return fBufferValue != NULL;
return true; return true;
} }
@@ -169,17 +171,19 @@ BHttpFormData::Type() const
} }
// #pragma mark Change behavior // #pragma mark - Change behavior
status_t status_t
BHttpFormData::MarkAsFile(const BString& filename, const BString& mimeType) BHttpFormData::MarkAsFile(const BString& filename, const BString& mimeType)
{ {
if (fDataType == B_HTTPFORM_UNKNOWN || fDataType == B_HTTPFORM_FILE) if (fDataType == B_HTTPFORM_UNKNOWN || fDataType == B_HTTPFORM_FILE)
return B_ERROR; return B_ERROR;
fFilename = filename; fFilename = filename;
fMimeType = mimeType; fMimeType = mimeType;
fFileMark = true; fFileMark = true;
return B_OK; return B_OK;
} }
@@ -198,15 +202,15 @@ BHttpFormData::CopyBuffer()
{ {
if (fDataType != B_HTTPFORM_BUFFER) if (fDataType != B_HTTPFORM_BUFFER)
return B_ERROR; return B_ERROR;
char* copiedBuffer = new char[fBufferSize]; char* copiedBuffer = new char[fBufferSize];
if (copiedBuffer == NULL) if (copiedBuffer == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
memcpy(copiedBuffer, fBufferValue, fBufferSize); memcpy(copiedBuffer, fBufferValue, fBufferSize);
fBufferValue = copiedBuffer; fBufferValue = copiedBuffer;
fCopiedBuffer = true; fCopiedBuffer = true;
return B_OK; return B_OK;
} }
@@ -224,31 +228,34 @@ BHttpFormData::operator=(const BHttpFormData& other)
fBufferSize = other.fBufferSize; fBufferSize = other.fBufferSize;
fFilename = other.fFilename; fFilename = other.fFilename;
fMimeType = other.fMimeType; fMimeType = other.fMimeType;
if (other.fCopiedBuffer) if (other.fCopiedBuffer)
CopyBuffer(); CopyBuffer();
return *this; return *this;
} }
// #pragma mark -- BHttpForm // #pragma mark - BHttpForm
BHttpForm::BHttpForm() BHttpForm::BHttpForm()
: fType(B_HTTP_FORM_URL_ENCODED) :
fType(B_HTTP_FORM_URL_ENCODED)
{ {
} }
BHttpForm::BHttpForm(const BHttpForm&) BHttpForm::BHttpForm(const BHttpForm& other)
: fType(B_HTTP_FORM_URL_ENCODED) :
fType(B_HTTP_FORM_URL_ENCODED)
{ {
} }
BHttpForm::BHttpForm(const BString& formString) BHttpForm::BHttpForm(const BString& formString)
: fType(B_HTTP_FORM_URL_ENCODED) :
fType(B_HTTP_FORM_URL_ENCODED)
{ {
ParseString(formString); ParseString(formString);
} }
@@ -260,17 +267,16 @@ BHttpForm::~BHttpForm()
} }
// #pragma mark Form string parsing // #pragma mark - Form string parsing
void void
BHttpForm::ParseString(const BString& formString) BHttpForm::ParseString(const BString& formString)
{ {
int32 index = 0; int32 index = 0;
while (index < formString.Length()) { while (index < formString.Length())
_ExtractNameValuePair(formString, &index); _ExtractNameValuePair(formString, &index);
}
} }
@@ -278,87 +284,87 @@ BString
BHttpForm::RawData() const BHttpForm::RawData() const
{ {
BString result; BString result;
if (fType == B_HTTP_FORM_URL_ENCODED) { if (fType == B_HTTP_FORM_URL_ENCODED) {
for (FormStorage::const_iterator it = fFields.begin(); for (FormStorage::const_iterator it = fFields.begin();
it != fFields.end(); it++) { it != fFields.end(); it++) {
const BHttpFormData* currentField = &it->second; const BHttpFormData* currentField = &it->second;
switch (currentField->Type()) { switch (currentField->Type()) {
case B_HTTPFORM_UNKNOWN: case B_HTTPFORM_UNKNOWN:
break; break;
case B_HTTPFORM_STRING: case B_HTTPFORM_STRING:
result << '&' << BUrl::UrlEncode(currentField->Name()) result << '&' << BUrl::UrlEncode(currentField->Name())
<< '=' << BUrl::UrlEncode(currentField->String()); << '=' << BUrl::UrlEncode(currentField->String());
break; break;
case B_HTTPFORM_FILE: case B_HTTPFORM_FILE:
break; break;
case B_HTTPFORM_BUFFER: case B_HTTPFORM_BUFFER:
// Send the buffer only if its not marked as a file // Send the buffer only if its not marked as a file
if (!currentField->IsFile()) { if (!currentField->IsFile()) {
result << '&' << BUrl::UrlEncode(currentField->Name()) result << '&' << BUrl::UrlEncode(currentField->Name())
<< '='; << '=';
result.Append( result.Append(
reinterpret_cast<const char*>(currentField->Buffer()), reinterpret_cast<const char*>(currentField->Buffer()),
currentField->BufferSize()); currentField->BufferSize());
} }
break; break;
} }
} }
result.Remove(0, 1); result.Remove(0, 1);
} else if (fType == B_HTTP_FORM_MULTIPART) { } else if (fType == B_HTTP_FORM_MULTIPART) {
// Very slow and memory consuming method since we're caching the // Very slow and memory consuming method since we're caching the
// file content, this should be preferably handled by the protocol // file content, this should be preferably handled by the protocol
for (FormStorage::const_iterator it = fFields.begin(); for (FormStorage::const_iterator it = fFields.begin();
it != fFields.end(); it++) { it != fFields.end(); it++) {
const BHttpFormData* currentField = &it->second; const BHttpFormData* currentField = &it->second;
result << _GetMultipartHeader(currentField); result << _GetMultipartHeader(currentField);
switch (currentField->Type()) { switch (currentField->Type()) {
case B_HTTPFORM_UNKNOWN: case B_HTTPFORM_UNKNOWN:
break; break;
case B_HTTPFORM_STRING: case B_HTTPFORM_STRING:
result << currentField->String(); result << currentField->String();
break; break;
case B_HTTPFORM_FILE:
{
BFile upFile(currentField->File().Path(), B_READ_ONLY);
char readBuffer[1024];
ssize_t readSize;
case B_HTTPFORM_FILE:
{
BFile upFile(currentField->File().Path(), B_READ_ONLY);
char readBuffer[1024];
ssize_t readSize;
readSize = upFile.Read(readBuffer, 1024);
while (readSize > 0) {
result.Append(readBuffer, readSize);
readSize = upFile.Read(readBuffer, 1024); readSize = upFile.Read(readBuffer, 1024);
while (readSize > 0) {
result.Append(readBuffer, readSize);
readSize = upFile.Read(readBuffer, 1024);
}
} }
break; break;
}
case B_HTTPFORM_BUFFER: case B_HTTPFORM_BUFFER:
result.Append( result.Append(
reinterpret_cast<const char*>(currentField->Buffer()), reinterpret_cast<const char*>(currentField->Buffer()),
currentField->BufferSize()); currentField->BufferSize());
break; break;
} }
result << "\r\n"; result << "\r\n";
} }
result << "--" << fMultipartBoundary << "--\r\n"; result << "--" << fMultipartBoundary << "--\r\n";
} }
return result; return result;
} }
// #pragma mark Form add // #pragma mark - Form add
status_t status_t
@@ -367,7 +373,7 @@ BHttpForm::AddString(const BString& fieldName, const BString& value)
BHttpFormData formData(fieldName, value); BHttpFormData formData(fieldName, value);
if (!formData.InitCheck()) if (!formData.InitCheck())
return B_ERROR; return B_ERROR;
fFields.insert(pair<BString, BHttpFormData>(fieldName, formData)); fFields.insert(pair<BString, BHttpFormData>(fieldName, formData));
return B_OK; return B_OK;
} }
@@ -378,7 +384,7 @@ BHttpForm::AddInt(const BString& fieldName, int32 value)
{ {
BString strValue; BString strValue;
strValue << value; strValue << value;
return AddString(fieldName, strValue); return AddString(fieldName, strValue);
} }
@@ -389,9 +395,9 @@ BHttpForm::AddFile(const BString& fieldName, const BPath& file)
BHttpFormData formData(fieldName, file); BHttpFormData formData(fieldName, file);
if (!formData.InitCheck()) if (!formData.InitCheck())
return B_ERROR; return B_ERROR;
fFields.insert(pair<BString, BHttpFormData>(fieldName, formData)); fFields.insert(pair<BString, BHttpFormData>(fieldName, formData));
if (fType != B_HTTP_FORM_MULTIPART) if (fType != B_HTTP_FORM_MULTIPART)
SetFormType(B_HTTP_FORM_MULTIPART); SetFormType(B_HTTP_FORM_MULTIPART);
return B_OK; return B_OK;
@@ -405,7 +411,7 @@ BHttpForm::AddBuffer(const BString& fieldName, const void* buffer,
BHttpFormData formData(fieldName, buffer, size); BHttpFormData formData(fieldName, buffer, size);
if (!formData.InitCheck()) if (!formData.InitCheck())
return B_ERROR; return B_ERROR;
fFields.insert(pair<BString, BHttpFormData>(fieldName, formData)); fFields.insert(pair<BString, BHttpFormData>(fieldName, formData));
return B_OK; return B_OK;
} }
@@ -418,17 +424,17 @@ BHttpForm::AddBufferCopy(const BString& fieldName, const void* buffer,
BHttpFormData formData(fieldName, buffer, size); BHttpFormData formData(fieldName, buffer, size);
if (!formData.InitCheck()) if (!formData.InitCheck())
return B_ERROR; return B_ERROR;
// Copy the buffer of the inserted form data copy to // Copy the buffer of the inserted form data copy to
// avoid an unneeded copy of the buffer upon insertion // avoid an unneeded copy of the buffer upon insertion
pair<FormStorage::iterator, bool> insertResult pair<FormStorage::iterator, bool> insertResult
= fFields.insert(pair<BString, BHttpFormData>(fieldName, formData)); = fFields.insert(pair<BString, BHttpFormData>(fieldName, formData));
return insertResult.first->second.CopyBuffer(); return insertResult.first->second.CopyBuffer();
} }
// #pragma mark Mark a field as a filename // #pragma mark - Mark a field as a filename
void void
@@ -436,10 +442,10 @@ BHttpForm::MarkAsFile(const BString& fieldName, const BString& filename,
const BString& mimeType) const BString& mimeType)
{ {
FormStorage::iterator it = fFields.find(fieldName); FormStorage::iterator it = fFields.find(fieldName);
if (it == fFields.end()) if (it == fFields.end())
return; return;
it->second.MarkAsFile(filename, mimeType); it->second.MarkAsFile(filename, mimeType);
if (fType != B_HTTP_FORM_MULTIPART) if (fType != B_HTTP_FORM_MULTIPART)
SetFormType(B_HTTP_FORM_MULTIPART); SetFormType(B_HTTP_FORM_MULTIPART);
@@ -457,28 +463,28 @@ void
BHttpForm::UnmarkAsFile(const BString& fieldName) BHttpForm::UnmarkAsFile(const BString& fieldName)
{ {
FormStorage::iterator it = fFields.find(fieldName); FormStorage::iterator it = fFields.find(fieldName);
if (it == fFields.end()) if (it == fFields.end())
return; return;
it->second.UnmarkAsFile(); it->second.UnmarkAsFile();
} }
// #pragma mark Change form type // #pragma mark - Change form type
void void
BHttpForm::SetFormType(form_type type) BHttpForm::SetFormType(form_type type)
{ {
fType = type; fType = type;
if (fType == B_HTTP_FORM_MULTIPART) if (fType == B_HTTP_FORM_MULTIPART)
_GenerateMultipartBoundary(); _GenerateMultipartBoundary();
} }
// #pragma mark Form test // #pragma mark - Form test
bool bool
@@ -488,17 +494,17 @@ BHttpForm::HasField(const BString& name) const
} }
// #pragma mark Form retrieve // #pragma mark - Form retrieve
BString BString
BHttpForm::GetMultipartHeader(const BString& fieldName) const BHttpForm::GetMultipartHeader(const BString& fieldName) const
{ {
FormStorage::const_iterator it = fFields.find(fieldName); FormStorage::const_iterator it = fFields.find(fieldName);
if (it == fFields.end()) if (it == fFields.end())
return BString(""); return BString("");
return _GetMultipartHeader(&it->second); return _GetMultipartHeader(&it->second);
} }
@@ -528,43 +534,43 @@ BHttpForm::GetMultipartFooter() const
ssize_t ssize_t
BHttpForm::ContentLength() const BHttpForm::ContentLength() const
{ {
if (fType == B_HTTP_FORM_URL_ENCODED) if (fType == B_HTTP_FORM_URL_ENCODED)
return RawData().Length(); return RawData().Length();
ssize_t contentLength = 0; ssize_t contentLength = 0;
for (FormStorage::const_iterator it = fFields.begin(); for (FormStorage::const_iterator it = fFields.begin();
it != fFields.end(); it++) { it != fFields.end(); it++) {
const BHttpFormData* c = &it->second; const BHttpFormData* c = &it->second;
contentLength += _GetMultipartHeader(c).Length(); contentLength += _GetMultipartHeader(c).Length();
switch (c->Type()) { switch (c->Type()) {
case B_HTTPFORM_UNKNOWN: case B_HTTPFORM_UNKNOWN:
break; break;
case B_HTTPFORM_STRING: case B_HTTPFORM_STRING:
contentLength += c->String().Length(); contentLength += c->String().Length();
break; break;
case B_HTTPFORM_FILE: case B_HTTPFORM_FILE:
{ {
BFile upFile(c->File().Path(), B_READ_ONLY); BFile upFile(c->File().Path(), B_READ_ONLY);
upFile.Seek(0, SEEK_END); upFile.Seek(0, SEEK_END);
contentLength += upFile.Position(); contentLength += upFile.Position();
}
break; break;
}
case B_HTTPFORM_BUFFER: case B_HTTPFORM_BUFFER:
contentLength += c->BufferSize(); contentLength += c->BufferSize();
break; break;
} }
contentLength += 2; contentLength += 2;
} }
contentLength += fMultipartBoundary.Length() + 6; contentLength += fMultipartBoundary.Length() + 6;
return contentLength; return contentLength;
} }
@@ -579,7 +585,7 @@ BHttpForm::GetIterator()
} }
// #pragma mark Form clear // #pragma mark - Form clear
void void
@@ -589,7 +595,7 @@ BHttpForm::Clear()
} }
// #pragma mark Overloaded operators // #pragma mark - Overloaded operators
BHttpFormData& BHttpFormData&
@@ -597,28 +603,28 @@ BHttpForm::operator[](const BString& name)
{ {
if (!HasField(name)) if (!HasField(name))
AddString(name, ""); AddString(name, "");
return fFields[name]; return fFields[name];
} }
void void
BHttpForm::_ExtractNameValuePair(const BString& formString, int32* index) BHttpForm::_ExtractNameValuePair(const BString& formString, int32* index)
{ {
// Look for a name=value pair // Look for a name=value pair
int16 firstAmpersand = formString.FindFirst("&", *index); int16 firstAmpersand = formString.FindFirst("&", *index);
int16 firstEqual = formString.FindFirst("=", *index); int16 firstEqual = formString.FindFirst("=", *index);
BString name; BString name;
BString value; BString value;
if (firstAmpersand == -1) { if (firstAmpersand == -1) {
if (firstEqual != -1) { if (firstEqual != -1) {
formString.CopyInto(name, *index, firstEqual - *index); formString.CopyInto(name, *index, firstEqual - *index);
formString.CopyInto(value, firstEqual + 1, formString.CopyInto(value, firstEqual + 1,
formString.Length() - firstEqual - 1); formString.Length() - firstEqual - 1);
} else } else
formString.CopyInto(value, *index, formString.CopyInto(value, *index,
formString.Length() - *index); formString.Length() - *index);
*index = formString.Length() + 1; *index = formString.Length() + 1;
@@ -641,17 +647,17 @@ void
BHttpForm::_GenerateMultipartBoundary() BHttpForm::_GenerateMultipartBoundary()
{ {
fMultipartBoundary = "----------------------------"; fMultipartBoundary = "----------------------------";
srand(time(NULL)); srand(time(NULL));
// TODO: Maybe a more robust way to seed the random number // TODO: Maybe a more robust way to seed the random number
// generator is needed? // generator is needed?
for (int32 i = 0; i < kBoundaryRandomSize; i++) for (int32 i = 0; i < kBoundaryRandomSize; i++)
fMultipartBoundary << (char)(rand() % 10 + '0'); fMultipartBoundary << (char)(rand() % 10 + '0');
} }
// #pragma mark Field information access by std iterator // #pragma mark - Field information access by std iterator
BString BString
@@ -661,32 +667,33 @@ BHttpForm::_GetMultipartHeader(const BHttpFormData* element) const
result << "--" << fMultipartBoundary << "\r\n"; result << "--" << fMultipartBoundary << "\r\n";
result << "Content-Disposition: form-data; name=\"" << element->Name() result << "Content-Disposition: form-data; name=\"" << element->Name()
<< '"'; << '"';
switch (element->Type()) { switch (element->Type()) {
case B_HTTPFORM_UNKNOWN: case B_HTTPFORM_UNKNOWN:
break; break;
case B_HTTPFORM_FILE: case B_HTTPFORM_FILE:
{ {
result << "; filename=\"" << element->File().Leaf() << '"'; result << "; filename=\"" << element->File().Leaf() << '"';
BNode fileNode(element->File().Path()); BNode fileNode(element->File().Path());
BNodeInfo fileInfo(&fileNode); BNodeInfo fileInfo(&fileNode);
result << "\r\nContent-Type: "; result << "\r\nContent-Type: ";
char tempMime[128]; char tempMime[128];
if (fileInfo.GetType(tempMime) == B_OK) if (fileInfo.GetType(tempMime) == B_OK)
result << tempMime; result << tempMime;
else else
result << "application/octet-stream"; result << "application/octet-stream";
}
break; break;
}
case B_HTTPFORM_STRING: case B_HTTPFORM_STRING:
case B_HTTPFORM_BUFFER: case B_HTTPFORM_BUFFER:
if (element->IsFile()) { if (element->IsFile()) {
result << "; filename=\"" << element->Filename() << '"'; result << "; filename=\"" << element->Filename() << '"';
if (element->MimeType().Length() > 0) if (element->MimeType().Length() > 0)
result << "\r\nContent-Type: " << element->MimeType(); result << "\r\nContent-Type: " << element->MimeType();
else else
@@ -694,17 +701,19 @@ BHttpForm::_GetMultipartHeader(const BHttpFormData* element) const
} }
break; break;
} }
result << "\r\n\r\n"; result << "\r\n\r\n";
return result; return result;
} }
// #pragma mark -- Iterator // #pragma mark - Iterator
BHttpForm::Iterator::Iterator(BHttpForm* form) BHttpForm::Iterator::Iterator(BHttpForm* form)
: fElement(NULL) :
fElement(NULL)
{ {
fForm = form; fForm = form;
fStdIterator = form->fFields.begin(); fStdIterator = form->fFields.begin();
@@ -747,25 +756,25 @@ BHttpForm::Iterator::MultipartHeader()
{ {
return fForm->_GetMultipartHeader(fPrevElement); return fForm->_GetMultipartHeader(fPrevElement);
} }
BHttpForm::Iterator&
BHttpForm::Iterator&
BHttpForm::Iterator::operator=(const Iterator& other) BHttpForm::Iterator::operator=(const Iterator& other)
{ {
fForm = other.fForm; fForm = other.fForm;
fStdIterator = other.fStdIterator; fStdIterator = other.fStdIterator;
fElement = other.fElement; fElement = other.fElement;
fPrevElement = other.fPrevElement; fPrevElement = other.fPrevElement;
return *this; return *this;
} }
void void
BHttpForm::Iterator::_FindNext() BHttpForm::Iterator::_FindNext()
{ {
fPrevElement = fElement; fPrevElement = fElement;
if (fStdIterator != fForm->fFields.end()) { if (fStdIterator != fForm->fFields.end()) {
fElement = &fStdIterator->second; fElement = &fStdIterator->second;
fStdIterator++; fStdIterator++;