1 /* 2 * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved. 3 * 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. 8 * 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. 13 * 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. 18 */ 19 20 #ifndef FormData_h 21 #define FormData_h 22 23 #include "weborigin/KURL.h" 24 #include "wtf/Forward.h" 25 #include "wtf/RefCounted.h" 26 #include "wtf/Vector.h" 27 #include "wtf/text/WTFString.h" 28 29 namespace WTF{ 30 class TextEncoding; 31 } 32 33 namespace WebCore { 34 35 class Document; 36 class FormDataList; 37 38 class FormDataElement { 39 public: 40 FormDataElement() : m_type(data) { } 41 explicit FormDataElement(const Vector<char>& array) : m_type(data), m_data(array) { } 42 43 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) { } 44 explicit FormDataElement(const KURL& blobURL) : m_type(encodedBlob), m_url(blobURL) { } 45 FormDataElement(const KURL& url, long long start, long long length, double expectedFileModificationTime) : m_type(encodedURL), m_url(url), m_fileStart(start), m_fileLength(length), m_expectedFileModificationTime(expectedFileModificationTime) { } 46 47 enum Type { 48 data, 49 encodedFile 50 , encodedBlob 51 , encodedURL 52 } m_type; 53 Vector<char> m_data; 54 String m_filename; 55 KURL m_url; // For Blob or URL. 56 long long m_fileStart; 57 long long m_fileLength; 58 double m_expectedFileModificationTime; 59 }; 60 61 inline bool operator==(const FormDataElement& a, const FormDataElement& b) 62 { 63 if (&a == &b) 64 return true; 65 66 if (a.m_type != b.m_type) 67 return false; 68 if (a.m_type == FormDataElement::data) 69 return a.m_data == b.m_data; 70 if (a.m_type == FormDataElement::encodedFile) 71 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; 72 if (a.m_type == FormDataElement::encodedBlob) 73 return a.m_url == b.m_url; 74 if (a.m_type == FormDataElement::encodedURL) 75 return a.m_url == b.m_url; 76 77 return true; 78 } 79 80 inline bool operator!=(const FormDataElement& a, const FormDataElement& b) 81 { 82 return !(a == b); 83 } 84 85 class FormData : public RefCounted<FormData> { 86 public: 87 enum EncodingType { 88 FormURLEncoded, // for application/x-www-form-urlencoded 89 TextPlain, // for text/plain 90 MultipartFormData // for multipart/form-data 91 }; 92 93 static PassRefPtr<FormData> create(); 94 static PassRefPtr<FormData> create(const void*, size_t); 95 static PassRefPtr<FormData> create(const CString&); 96 static PassRefPtr<FormData> create(const Vector<char>&); 97 static PassRefPtr<FormData> create(const FormDataList&, const WTF::TextEncoding&, EncodingType = FormURLEncoded); 98 static PassRefPtr<FormData> createMultiPart(const FormDataList&, const WTF::TextEncoding&, Document*); 99 PassRefPtr<FormData> copy() const; 100 PassRefPtr<FormData> deepCopy() const; 101 ~FormData(); 102 103 void appendData(const void* data, size_t); 104 void appendFile(const String& filePath); 105 void appendFileRange(const String& filename, long long start, long long length, double expectedModificationTime); 106 void appendBlob(const KURL& blobURL); 107 void appendURL(const KURL&); 108 void appendURLRange(const KURL&, long long start, long long length, double expectedModificationTime); 109 110 void flatten(Vector<char>&) const; // omits files 111 String flattenToString() const; // omits files 112 113 bool isEmpty() const { return m_elements.isEmpty(); } 114 const Vector<FormDataElement>& elements() const { return m_elements; } 115 const Vector<char>& boundary() const { return m_boundary; } 116 117 bool alwaysStream() const { return m_alwaysStream; } 118 void setAlwaysStream(bool alwaysStream) { m_alwaysStream = alwaysStream; } 119 120 // Identifies a particular form submission instance. A value of 0 is used 121 // to indicate an unspecified identifier. 122 void setIdentifier(int64_t identifier) { m_identifier = identifier; } 123 int64_t identifier() const { return m_identifier; } 124 125 bool containsPasswordData() const { return m_containsPasswordData; } 126 void setContainsPasswordData(bool containsPasswordData) { m_containsPasswordData = containsPasswordData; } 127 128 static EncodingType parseEncodingType(const String& type) 129 { 130 if (equalIgnoringCase(type, "text/plain")) 131 return TextPlain; 132 if (equalIgnoringCase(type, "multipart/form-data")) 133 return MultipartFormData; 134 return FormURLEncoded; 135 } 136 137 private: 138 FormData(); 139 FormData(const FormData&); 140 141 void appendKeyValuePairItems(const FormDataList&, const WTF::TextEncoding&, bool isMultiPartForm, Document*, EncodingType = FormURLEncoded); 142 143 Vector<FormDataElement> m_elements; 144 145 int64_t m_identifier; 146 bool m_alwaysStream; 147 Vector<char> m_boundary; 148 bool m_containsPasswordData; 149 }; 150 151 inline bool operator==(const FormData& a, const FormData& b) 152 { 153 return a.elements() == b.elements(); 154 } 155 156 inline bool operator!=(const FormData& a, const FormData& b) 157 { 158 return !(a == b); 159 } 160 161 } // namespace WebCore 162 163 #endif 164