#ifndef FormData_h
#define FormData_h
#include "BlobData.h"
#include "URL.h"
#include <wtf/Forward.h>
#include <wtf/RefCounted.h>
#include <wtf/Vector.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
class Document;
class FormDataList;
class TextEncoding;
class FormDataElement {
public:
enum class Type {
Data,
EncodedFile,
EncodedBlob,
};
FormDataElement()
: m_type(Type::Data)
{
}
explicit FormDataElement(const Vector<char>& array)
: m_type(Type::Data)
, m_data(array)
{
}
FormDataElement(const String& filename, long long fileStart, long long fileLength, double expectedFileModificationTime, bool shouldGenerateFile)
: m_type(Type::EncodedFile)
, m_filename(filename)
, m_fileStart(fileStart)
, m_fileLength(fileLength)
, m_expectedFileModificationTime(expectedFileModificationTime)
, m_shouldGenerateFile(shouldGenerateFile)
, m_ownsGeneratedFile(false)
{
}
explicit FormDataElement(const URL& blobURL)
: m_type(Type::EncodedBlob)
, m_url(blobURL)
{
}
FormDataElement isolatedCopy() const;
template<typename Encoder>
void encode(Encoder&) const;
template<typename Decoder>
static bool decode(Decoder&, FormDataElement& result);
Type m_type;
Vector<char> m_data;
String m_filename;
URL m_url; int64_t m_fileStart;
int64_t m_fileLength;
double m_expectedFileModificationTime;
String m_generatedFilename;
bool m_shouldGenerateFile;
bool m_ownsGeneratedFile;
};
inline bool operator==(const FormDataElement& a, const FormDataElement& b)
{
if (&a == &b)
return true;
if (a.m_type != b.m_type)
return false;
if (a.m_type == FormDataElement::Type::Data)
return a.m_data == b.m_data;
if (a.m_type == FormDataElement::Type::EncodedFile)
return a.m_filename == b.m_filename && a.m_fileStart == b.m_fileStart && a.m_fileLength == b.m_fileLength && a.m_expectedFileModificationTime == b.m_expectedFileModificationTime;
if (a.m_type == FormDataElement::Type::EncodedBlob)
return a.m_url == b.m_url;
return true;
}
inline bool operator!=(const FormDataElement& a, const FormDataElement& b)
{
return !(a == b);
}
template<typename Encoder>
void FormDataElement::encode(Encoder& encoder) const
{
encoder.encodeEnum(m_type);
switch (m_type) {
case Type::Data:
encoder << m_data;
break;
case Type::EncodedFile:
encoder << m_filename;
encoder << m_generatedFilename;
encoder << m_shouldGenerateFile;
encoder << m_fileStart;
encoder << m_fileLength;
encoder << m_expectedFileModificationTime;
break;
case Type::EncodedBlob:
encoder << m_url.string();
break;
}
}
template<typename Decoder>
bool FormDataElement::decode(Decoder& decoder, FormDataElement& result)
{
if (!decoder.decodeEnum(result.m_type))
return false;
switch (result.m_type) {
case Type::Data:
if (!decoder.decode(result.m_data))
return false;
return true;
case Type::EncodedFile:
if (!decoder.decode(result.m_filename))
return false;
if (!decoder.decode(result.m_generatedFilename))
return false;
if (!decoder.decode(result.m_shouldGenerateFile))
return false;
result.m_ownsGeneratedFile = false;
if (!decoder.decode(result.m_fileStart))
return false;
if (!decoder.decode(result.m_fileLength))
return false;
if (result.m_fileLength != BlobDataItem::toEndOfFile && result.m_fileLength < result.m_fileStart)
return false;
if (!decoder.decode(result.m_expectedFileModificationTime))
return false;
return true;
case Type::EncodedBlob: {
String blobURLString;
if (!decoder.decode(blobURLString))
return false;
result.m_url = URL(URL(), blobURLString);
return true;
}
}
return false;
}
class FormData : public RefCounted<FormData> {
public:
enum EncodingType {
FormURLEncoded, TextPlain, MultipartFormData };
WEBCORE_EXPORT static Ref<FormData> create();
WEBCORE_EXPORT static Ref<FormData> create(const void*, size_t);
static Ref<FormData> create(const CString&);
static Ref<FormData> create(const Vector<char>&);
static Ref<FormData> create(const FormDataList&, const TextEncoding&, EncodingType = FormURLEncoded);
static Ref<FormData> createMultiPart(const FormDataList&, const TextEncoding&, Document*);
WEBCORE_EXPORT ~FormData();
Ref<FormData> copy() const;
Ref<FormData> isolatedCopy() const;
template<typename Encoder>
void encode(Encoder&) const;
template<typename Decoder>
static RefPtr<FormData> decode(Decoder&);
WEBCORE_EXPORT void appendData(const void* data, size_t);
void appendFile(const String& filePath, bool shouldGenerateFile = false);
WEBCORE_EXPORT void appendFileRange(const String& filename, long long start, long long length, double expectedModificationTime, bool shouldGenerateFile = false);
WEBCORE_EXPORT void appendBlob(const URL& blobURL);
char* expandDataStore(size_t);
void flatten(Vector<char>&) const; String flattenToString() const;
Ref<FormData> resolveBlobReferences();
bool isEmpty() const { return m_elements.isEmpty(); }
const Vector<FormDataElement>& elements() const { return m_elements; }
const Vector<char>& boundary() const { return m_boundary; }
void generateFiles(Document*);
void removeGeneratedFilesIfNeeded();
bool alwaysStream() const { return m_alwaysStream; }
void setAlwaysStream(bool alwaysStream) { m_alwaysStream = alwaysStream; }
void setIdentifier(int64_t identifier) { m_identifier = identifier; }
int64_t identifier() const { return m_identifier; }
bool containsPasswordData() const { return m_containsPasswordData; }
void setContainsPasswordData(bool containsPasswordData) { m_containsPasswordData = containsPasswordData; }
static EncodingType parseEncodingType(const String& type)
{
if (equalLettersIgnoringASCIICase(type, "text/plain"))
return TextPlain;
if (equalLettersIgnoringASCIICase(type, "multipart/form-data"))
return MultipartFormData;
return FormURLEncoded;
}
private:
FormData();
FormData(const FormData&);
void appendKeyValuePairItems(const FormDataList&, const TextEncoding&, bool isMultiPartForm, Document*, EncodingType = FormURLEncoded);
bool hasGeneratedFiles() const;
bool hasOwnedGeneratedFiles() const;
Vector<FormDataElement> m_elements;
int64_t m_identifier { 0 };
bool m_alwaysStream { false };
Vector<char> m_boundary;
bool m_containsPasswordData { false };
};
inline bool operator==(const FormData& a, const FormData& b)
{
return a.elements() == b.elements();
}
inline bool operator!=(const FormData& a, const FormData& b)
{
return !(a == b);
}
template<typename Encoder>
void FormData::encode(Encoder& encoder) const
{
encoder << m_alwaysStream;
encoder << m_boundary;
encoder << m_elements;
encoder << m_identifier;
}
template<typename Decoder>
RefPtr<FormData> FormData::decode(Decoder& decoder)
{
RefPtr<FormData> data = FormData::create();
if (!decoder.decode(data->m_alwaysStream))
return nullptr;
if (!decoder.decode(data->m_boundary))
return nullptr;
if (!decoder.decode(data->m_elements))
return nullptr;
if (!decoder.decode(data->m_identifier))
return nullptr;
return data;
}
}
#endif