This source file includes following definitions.
- appendString
- appendString
- appendBlob
- createFormData
- createMultiPartFormData
- appendKeyValuePairItemsTo
- trace
- trace
#include "config.h"
#include "core/html/FormDataList.h"
#include "core/fileapi/File.h"
#include "platform/network/FormDataBuilder.h"
#include "platform/text/LineEnding.h"
namespace WebCore {
FormDataList::FormDataList(const WTF::TextEncoding& c)
: m_encoding(c)
{
}
void FormDataList::appendString(const String& string)
{
CString encodedString = m_encoding.encode(string, WTF::EntitiesForUnencodables);
m_items.append(normalizeLineEndingsToCRLF(encodedString));
}
void FormDataList::appendString(const CString& string)
{
m_items.append(string);
}
void FormDataList::appendBlob(PassRefPtrWillBeRawPtr<Blob> blob, const String& filename)
{
m_items.append(Item(blob, filename));
}
PassRefPtr<FormData> FormDataList::createFormData(const WTF::TextEncoding& encoding, FormData::EncodingType encodingType)
{
RefPtr<FormData> result = FormData::create();
appendKeyValuePairItemsTo(result.get(), encoding, false, encodingType);
return result.release();
}
PassRefPtr<FormData> FormDataList::createMultiPartFormData(const WTF::TextEncoding& encoding)
{
RefPtr<FormData> result = FormData::create();
appendKeyValuePairItemsTo(result.get(), encoding, true);
return result.release();
}
void FormDataList::appendKeyValuePairItemsTo(FormData* formData, const WTF::TextEncoding& encoding, bool isMultiPartForm, FormData::EncodingType encodingType)
{
if (isMultiPartForm)
formData->setBoundary(FormDataBuilder::generateUniqueBoundaryString());
Vector<char> encodedData;
const WillBeHeapVector<Item>& items = this->items();
size_t formDataListSize = items.size();
ASSERT(!(formDataListSize % 2));
for (size_t i = 0; i < formDataListSize; i += 2) {
const FormDataList::Item& key = items[i];
const FormDataList::Item& value = items[i + 1];
if (isMultiPartForm) {
Vector<char> header;
FormDataBuilder::beginMultiPartHeader(header, formData->boundary().data(), key.data());
if (value.blob()) {
String name;
if (value.blob()->isFile()) {
File* file = toFile(value.blob());
name = file->webkitRelativePath().isEmpty() ? file->name() : file->webkitRelativePath();
if (!value.filename().isNull())
name = value.filename();
} else {
if (!value.filename().isNull())
name = value.filename();
else
name = "blob";
}
FormDataBuilder::addFilenameToMultiPartHeader(header, encoding, name);
String contentType;
if (value.blob()->type().isEmpty())
contentType = "application/octet-stream";
else
contentType = value.blob()->type();
FormDataBuilder::addContentTypeToMultiPartHeader(header, contentType.latin1());
}
FormDataBuilder::finishMultiPartHeader(header);
formData->appendData(header.data(), header.size());
if (value.blob()) {
if (value.blob()->hasBackingFile()) {
File* file = toFile(value.blob());
if (!file->path().isEmpty())
formData->appendFile(file->path());
if (!file->fileSystemURL().isEmpty())
formData->appendFileSystemURL(file->fileSystemURL());
} else {
formData->appendBlob(value.blob()->uuid(), value.blob()->blobDataHandle());
}
} else {
formData->appendData(value.data().data(), value.data().length());
}
formData->appendData("\r\n", 2);
} else {
FormDataBuilder::addKeyValuePairAsFormData(encodedData, key.data(), value.data(), encodingType);
}
}
if (isMultiPartForm)
FormDataBuilder::addBoundaryToMultiPartHeader(encodedData, formData->boundary().data(), true);
formData->appendData(encodedData.data(), encodedData.size());
}
void FormDataList::trace(Visitor* visitor)
{
visitor->trace(m_items);
}
void FormDataList::Item::trace(Visitor* visitor)
{
visitor->trace(m_blob);
}
}