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.
24 #include "PlatformString.h"
25 #include <wtf/Forward.h>
26 #include <wtf/RefCounted.h>
27 #include <wtf/Vector.h>
35 class FormDataElement {
37 FormDataElement() : m_type(data) { }
38 explicit FormDataElement(const Vector<char>& array) : m_type(data), m_data(array) { }
41 FormDataElement(const String& filename, long long fileStart, long long fileLength, double expectedFileModificationTime, bool shouldGenerateFile) : m_type(encodedFile), m_filename(filename), m_fileStart(fileStart), m_fileLength(fileLength), m_expectedFileModificationTime(expectedFileModificationTime), m_shouldGenerateFile(shouldGenerateFile) { }
42 explicit FormDataElement(const KURL& blobURL) : m_type(encodedBlob), m_blobURL(blobURL) { }
44 FormDataElement(const String& filename, bool shouldGenerateFile) : m_type(encodedFile), m_filename(filename), m_shouldGenerateFile(shouldGenerateFile) { }
57 long long m_fileStart;
58 long long m_fileLength;
59 double m_expectedFileModificationTime;
62 String m_generatedFilename;
63 bool m_shouldGenerateFile;
66 inline bool operator==(const FormDataElement& a, const FormDataElement& b)
71 if (a.m_type != b.m_type)
73 if (a.m_type == FormDataElement::data)
74 return a.m_data == b.m_data;
75 if (a.m_type == FormDataElement::encodedFile)
77 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;
78 if (a.m_type == FormDataElement::encodedBlob)
79 return a.m_blobURL == b.m_blobURL;
81 return a.m_filename == b.m_filename;
87 inline bool operator!=(const FormDataElement& a, const FormDataElement& b)
92 class FormData : public RefCounted<FormData> {
95 FormURLEncoded, // for application/x-www-form-urlencoded
96 TextPlain, // for text/plain
97 MultipartFormData // for multipart/form-data
100 static PassRefPtr<FormData> create();
101 static PassRefPtr<FormData> create(const void*, size_t);
102 static PassRefPtr<FormData> create(const CString&);
103 static PassRefPtr<FormData> create(const Vector<char>&);
104 static PassRefPtr<FormData> create(const FormDataList&, const TextEncoding&, EncodingType = FormURLEncoded);
105 static PassRefPtr<FormData> createMultiPart(const FormDataList&, const TextEncoding&, Document*);
106 PassRefPtr<FormData> copy() const;
107 PassRefPtr<FormData> deepCopy() const;
110 void encodeForBackForward(Encoder&) const;
111 static PassRefPtr<FormData> decodeForBackForward(Decoder&);
113 void appendData(const void* data, size_t);
114 void appendFile(const String& filePath, bool shouldGenerateFile = false);
116 void appendFileRange(const String& filename, long long start, long long length, double expectedModificationTime, bool shouldGenerateFile = false);
117 void appendBlob(const KURL& blobURL);
120 void flatten(Vector<char>&) const; // omits files
121 String flattenToString() const; // omits files
123 bool isEmpty() const { return m_elements.isEmpty(); }
124 const Vector<FormDataElement>& elements() const { return m_elements; }
125 const Vector<char>& boundary() const { return m_boundary; }
127 void generateFiles(Document*);
128 void removeGeneratedFilesIfNeeded();
130 bool alwaysStream() const { return m_alwaysStream; }
131 void setAlwaysStream(bool alwaysStream) { m_alwaysStream = alwaysStream; }
133 // Identifies a particular form submission instance. A value of 0 is used
134 // to indicate an unspecified identifier.
135 void setIdentifier(int64_t identifier) { m_identifier = identifier; }
136 int64_t identifier() const { return m_identifier; }
138 static EncodingType parseEncodingType(const String& type)
140 if (equalIgnoringCase(type, "text/plain"))
142 if (equalIgnoringCase(type, "multipart/form-data"))
143 return MultipartFormData;
144 return FormURLEncoded;
149 FormData(const FormData&);
151 void appendKeyValuePairItems(const FormDataList&, const TextEncoding&, bool isMultiPartForm, Document*, EncodingType = FormURLEncoded);
153 Vector<FormDataElement> m_elements;
155 int64_t m_identifier;
156 bool m_hasGeneratedFiles;
158 Vector<char> m_boundary;
161 inline bool operator==(const FormData& a, const FormData& b)
163 return a.elements() == b.elements();
166 inline bool operator!=(const FormData& a, const FormData& b)
171 } // namespace WebCore