2 * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 #include "platform/blob/BlobData.h"
24 #include "platform/weborigin/KURL.h"
25 #include "wtf/Forward.h"
26 #include "wtf/RefCounted.h"
27 #include "wtf/Vector.h"
28 #include "wtf/text/WTFString.h"
34 class PLATFORM_EXPORT FormDataElement {
36 FormDataElement() : m_type(data) { }
37 explicit FormDataElement(const Vector<char>& array) : m_type(data), m_data(array) { }
39 FormDataElement(const String& filename, long long fileStart, long long fileLength, double expectedFileModificationTime) : m_type(encodedFile), m_filename(filename), m_fileStart(fileStart), m_fileLength(fileLength), m_expectedFileModificationTime(expectedFileModificationTime) { }
40 explicit FormDataElement(const String& blobUUID, PassRefPtr<BlobDataHandle> optionalHandle) : m_type(encodedBlob), m_blobUUID(blobUUID), m_optionalBlobDataHandle(optionalHandle) { }
41 FormDataElement(const KURL& fileSystemURL, long long start, long long length, double expectedFileModificationTime) : m_type(encodedFileSystemURL), m_fileSystemURL(fileSystemURL), m_fileStart(start), m_fileLength(length), m_expectedFileModificationTime(expectedFileModificationTime) { }
52 RefPtr<BlobDataHandle> m_optionalBlobDataHandle;
54 long long m_fileStart;
55 long long m_fileLength;
56 double m_expectedFileModificationTime;
59 inline bool operator==(const FormDataElement& a, const FormDataElement& b)
64 if (a.m_type != b.m_type)
66 if (a.m_type == FormDataElement::data)
67 return a.m_data == b.m_data;
68 if (a.m_type == FormDataElement::encodedFile)
69 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;
70 if (a.m_type == FormDataElement::encodedBlob)
71 return a.m_blobUUID == b.m_blobUUID;
72 if (a.m_type == FormDataElement::encodedFileSystemURL)
73 return a.m_fileSystemURL == b.m_fileSystemURL;
78 inline bool operator!=(const FormDataElement& a, const FormDataElement& b)
83 class PLATFORM_EXPORT FormData : public RefCounted<FormData> {
86 FormURLEncoded, // for application/x-www-form-urlencoded
87 TextPlain, // for text/plain
88 MultipartFormData // for multipart/form-data
91 static PassRefPtr<FormData> create();
92 static PassRefPtr<FormData> create(const void*, size_t);
93 static PassRefPtr<FormData> create(const CString&);
94 static PassRefPtr<FormData> create(const Vector<char>&);
95 PassRefPtr<FormData> copy() const;
96 PassRefPtr<FormData> deepCopy() const;
99 void appendData(const void* data, size_t);
100 void appendFile(const String& filePath);
101 void appendFileRange(const String& filename, long long start, long long length, double expectedModificationTime);
102 void appendBlob(const String& blobUUID, PassRefPtr<BlobDataHandle> optionalHandle);
103 void appendFileSystemURL(const KURL&);
104 void appendFileSystemURLRange(const KURL&, long long start, long long length, double expectedModificationTime);
106 void flatten(Vector<char>&) const; // omits files
107 String flattenToString() const; // omits files
109 bool isEmpty() const { return m_elements.isEmpty(); }
110 const Vector<FormDataElement>& elements() const { return m_elements; }
112 const Vector<char>& boundary() const { return m_boundary; }
113 void setBoundary(Vector<char> boundary) { m_boundary = boundary; }
115 bool alwaysStream() const { return m_alwaysStream; }
116 void setAlwaysStream(bool alwaysStream) { m_alwaysStream = alwaysStream; }
118 // Identifies a particular form submission instance. A value of 0 is used
119 // to indicate an unspecified identifier.
120 void setIdentifier(int64_t identifier) { m_identifier = identifier; }
121 int64_t identifier() const { return m_identifier; }
123 bool containsPasswordData() const { return m_containsPasswordData; }
124 void setContainsPasswordData(bool containsPasswordData) { m_containsPasswordData = containsPasswordData; }
126 static EncodingType parseEncodingType(const String& type)
128 if (equalIgnoringCase(type, "text/plain"))
130 if (equalIgnoringCase(type, "multipart/form-data"))
131 return MultipartFormData;
132 return FormURLEncoded;
135 // Size of the elements making up the FormData.
136 unsigned long long sizeInBytes() const;
140 FormData(const FormData&);
142 Vector<FormDataElement> m_elements;
144 int64_t m_identifier;
146 Vector<char> m_boundary;
147 bool m_containsPasswordData;
150 inline bool operator==(const FormData& a, const FormData& b)
152 return a.elements() == b.elements();
155 inline bool operator!=(const FormData& a, const FormData& b)