OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2005, 2006, 2008 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
8 * | 8 * |
9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "platform/text/LineEnding.h" | 26 #include "platform/text/LineEnding.h" |
27 #include "wtf/CurrentTime.h" | 27 #include "wtf/CurrentTime.h" |
28 | 28 |
29 namespace blink { | 29 namespace blink { |
30 | 30 |
31 FormDataList::FormDataList(const WTF::TextEncoding& c) | 31 FormDataList::FormDataList(const WTF::TextEncoding& c) |
32 : m_encoding(c) | 32 : m_encoding(c) |
33 { | 33 { |
34 } | 34 } |
35 | 35 |
36 void FormDataList::appendString(const String& string) | 36 void FormDataList::appendItem(const FormDataList::Item& item) |
37 { | 37 { |
38 m_items.append(encodeAndNormalize(string)); | 38 m_items.append(item); |
39 } | |
40 | |
41 void FormDataList::appendString(const CString& string) | |
42 { | |
43 m_items.append(string); | |
44 } | |
45 | |
46 void FormDataList::appendBlob(Blob* blob, const String& filename) | |
47 { | |
48 m_items.append(Item(blob, filename)); | |
49 } | 39 } |
50 | 40 |
51 void FormDataList::deleteEntry(const String& key) | 41 void FormDataList::deleteEntry(const String& key) |
52 { | 42 { |
53 const CString keyData = encodeAndNormalize(key); | 43 const CString keyData = encodeAndNormalize(key); |
54 ASSERT(!(m_items.size() % 2)); | |
55 size_t i = 0; | 44 size_t i = 0; |
56 while (i < m_items.size()) { | 45 while (i < m_items.size()) { |
57 if (m_items[i].data() == keyData) { | 46 if (m_items[i].key() == keyData) { |
58 m_items.remove(i, 2); | 47 m_items.remove(i); |
59 } else { | 48 } else { |
60 i += 2; | 49 ++i; |
61 } | 50 } |
62 } | 51 } |
63 ASSERT(!(m_items.size() % 2)); | |
64 return; | 52 return; |
65 } | 53 } |
66 | 54 |
67 FormDataList::Entry FormDataList::getEntry(const String& key) const | 55 FormDataList::Entry FormDataList::getEntry(const String& key) const |
68 { | 56 { |
69 const CString keyData = encodeAndNormalize(key); | 57 const CString keyData = encodeAndNormalize(key); |
70 const FormDataListItems& items = this->items(); | 58 for (const Item& item : items()) { |
71 size_t formDataListSize = items.size(); | 59 if (item.key() == keyData) |
72 ASSERT(!(formDataListSize % 2)); | 60 return itemsToEntry(item); |
73 for (size_t i = 0; i < formDataListSize; i += 2) { | |
74 const FormDataList::Item& key = items[i]; | |
75 if (key.data() != keyData) | |
76 continue; | |
77 const FormDataList::Item& value = items[i + 1]; | |
78 return itemsToEntry(key, value); | |
79 } | 61 } |
80 return Entry(); | 62 return Entry(); |
81 } | 63 } |
82 | 64 |
83 FormDataList::Entry FormDataList::getEntry(size_t index) const | 65 FormDataList::Entry FormDataList::getEntry(size_t index) const |
84 { | 66 { |
85 const FormDataListItems& items = this->items(); | 67 const FormDataListItems& items = this->items(); |
86 size_t formDataListSize = items.size(); | 68 if (index >= items.size()) |
87 ASSERT(!(formDataListSize % 2)); | |
88 if (index >= formDataListSize / 2) | |
89 return Entry(); | 69 return Entry(); |
90 const FormDataList::Item& key = items[index * 2]; | 70 return itemsToEntry(items[index]); |
91 const FormDataList::Item& value = items[index * 2 + 1]; | |
92 return itemsToEntry(key, value); | |
93 } | 71 } |
94 | 72 |
95 HeapVector<FormDataList::Entry> FormDataList::getAll(const String& key) const | 73 HeapVector<FormDataList::Entry> FormDataList::getAll(const String& key) const |
96 { | 74 { |
97 HeapVector<FormDataList::Entry> matches; | 75 HeapVector<FormDataList::Entry> matches; |
98 | 76 |
99 const CString keyData = encodeAndNormalize(key); | 77 const CString keyData = encodeAndNormalize(key); |
100 const FormDataListItems& items = this->items(); | 78 for (const Item& item : items()) { |
101 size_t formDataListSize = items.size(); | 79 if (item.key() == keyData) |
102 ASSERT(!(formDataListSize % 2)); | 80 matches.append(itemsToEntry(item)); |
103 for (size_t i = 0; i < formDataListSize; i += 2) { | |
104 const FormDataList::Item& key = items[i]; | |
105 if (key.data() != keyData) | |
106 continue; | |
107 const FormDataList::Item& value = items[i + 1]; | |
108 matches.append(itemsToEntry(key, value)); | |
109 } | 81 } |
110 | 82 |
111 return matches; | 83 return matches; |
112 } | 84 } |
113 | 85 |
114 FormDataList::Entry FormDataList::itemsToEntry(const FormDataList::Item& key, co
nst FormDataList::Item& value) const | 86 FormDataList::Entry FormDataList::itemsToEntry(const FormDataList::Item& item) c
onst |
115 { | 87 { |
116 const CString nameData = key.data(); | 88 const CString nameData = item.key(); |
117 const String name = m_encoding.decode(nameData.data(), nameData.length()); | 89 const String name = m_encoding.decode(nameData.data(), nameData.length()); |
118 | 90 |
119 if (!value.blob()) { | 91 if (!item.blob()) { |
120 const CString valueData = value.data(); | 92 const CString valueData = item.data(); |
121 return Entry(name, m_encoding.decode(valueData.data(), valueData.length(
))); | 93 return Entry(name, m_encoding.decode(valueData.data(), valueData.length(
))); |
122 } | 94 } |
123 | 95 |
124 // The spec uses the passed filename when inserting entries into the list. | 96 // The spec uses the passed filename when inserting entries into the list. |
125 // Here, we apply the filename (if present) as an override when extracting | 97 // Here, we apply the filename (if present) as an override when extracting |
126 // items. | 98 // items. |
127 // FIXME: Consider applying the name during insertion. | 99 // FIXME: Consider applying the name during insertion. |
128 | 100 |
129 if (value.blob()->isFile()) { | 101 if (item.blob()->isFile()) { |
130 File* file = toFile(value.blob()); | 102 File* file = toFile(item.blob()); |
131 if (value.filename().isNull()) | 103 if (item.filename().isNull()) |
132 return Entry(name, file); | 104 return Entry(name, file); |
133 return Entry(name, file->clone(value.filename())); | 105 return Entry(name, file->clone(item.filename())); |
134 } | 106 } |
135 | 107 |
136 String filename = value.filename(); | 108 String filename = item.filename(); |
137 if (filename.isNull()) | 109 if (filename.isNull()) |
138 filename = "blob"; | 110 filename = "blob"; |
139 return Entry(name, File::create(filename, currentTimeMS(), value.blob()->blo
bDataHandle())); | 111 return Entry(name, File::create(filename, currentTimeMS(), item.blob()->blob
DataHandle())); |
140 } | 112 } |
141 | 113 |
142 bool FormDataList::hasEntry(const String& key) const | 114 bool FormDataList::hasEntry(const String& key) const |
143 { | 115 { |
144 const CString keyData = encodeAndNormalize(key); | 116 const CString keyData = encodeAndNormalize(key); |
145 const FormDataListItems& items = this->items(); | 117 for (const Item& item : items()) { |
146 size_t formDataListSize = items.size(); | 118 if (item.key() == keyData) |
147 ASSERT(!(formDataListSize % 2)); | |
148 for (size_t i = 0; i < formDataListSize; i += 2) { | |
149 const FormDataList::Item& key = items[i]; | |
150 if (key.data() == keyData) | |
151 return true; | 119 return true; |
152 } | 120 } |
153 return false; | 121 return false; |
154 } | 122 } |
155 | 123 |
156 void FormDataList::setBlob(const String& key, Blob* blob, const String& filename
) | 124 void FormDataList::setBlob(const String& key, Blob* blob, const String& filename
) |
157 { | 125 { |
158 setEntry(key, Item(blob, filename)); | 126 setEntry(Item(encodeAndNormalize(key), blob, filename)); |
159 } | 127 } |
160 | 128 |
161 void FormDataList::setData(const String& key, const String& value) | 129 void FormDataList::setData(const String& key, const String& value) |
162 { | 130 { |
163 setEntry(key, encodeAndNormalize(value)); | 131 setEntry(Item(encodeAndNormalize(key), encodeAndNormalize(value))); |
164 } | 132 } |
165 | 133 |
166 void FormDataList::setEntry(const String& key, const Item& item) | 134 void FormDataList::setEntry(const Item& item) |
167 { | 135 { |
168 const CString keyData = encodeAndNormalize(key); | 136 const CString keyData = item.key(); |
169 ASSERT(!(m_items.size() % 2)); | |
170 bool found = false; | 137 bool found = false; |
171 size_t i = 0; | 138 size_t i = 0; |
172 while (i < m_items.size()) { | 139 while (i < m_items.size()) { |
173 if (m_items[i].data() != keyData) { | 140 if (m_items[i].key() != keyData) { |
174 i += 2; | 141 ++i; |
175 } else if (found) { | 142 } else if (found) { |
176 m_items.remove(i, 2); | 143 m_items.remove(i); |
177 } else { | 144 } else { |
178 found = true; | 145 found = true; |
179 m_items[i + 1] = item; | 146 m_items[i] = item; |
180 i += 2; | 147 ++i; |
181 } | 148 } |
182 } | 149 } |
183 if (!found) { | 150 if (!found) |
184 m_items.append(keyData); | |
185 m_items.append(item); | 151 m_items.append(item); |
186 } | |
187 ASSERT(!(m_items.size() % 2)); | |
188 return; | 152 return; |
189 } | 153 } |
190 | 154 |
191 PassRefPtr<FormData> FormDataList::createFormData(FormData::EncodingType encodin
gType) | 155 PassRefPtr<FormData> FormDataList::createFormData(FormData::EncodingType encodin
gType) |
192 { | 156 { |
193 RefPtr<FormData> result = FormData::create(); | 157 RefPtr<FormData> result = FormData::create(); |
194 appendKeyValuePairItemsTo(result.get(), m_encoding, false, encodingType); | 158 appendKeyValuePairItemsTo(result.get(), m_encoding, false, encodingType); |
195 return result.release(); | 159 return result.release(); |
196 } | 160 } |
197 | 161 |
198 PassRefPtr<FormData> FormDataList::createMultiPartFormData() | 162 PassRefPtr<FormData> FormDataList::createMultiPartFormData() |
199 { | 163 { |
200 RefPtr<FormData> result = FormData::create(); | 164 RefPtr<FormData> result = FormData::create(); |
201 appendKeyValuePairItemsTo(result.get(), m_encoding, true); | 165 appendKeyValuePairItemsTo(result.get(), m_encoding, true); |
202 return result.release(); | 166 return result.release(); |
203 } | 167 } |
204 | 168 |
205 void FormDataList::appendKeyValuePairItemsTo(FormData* formData, const WTF::Text
Encoding& encoding, bool isMultiPartForm, FormData::EncodingType encodingType) | 169 void FormDataList::appendKeyValuePairItemsTo(FormData* formData, const WTF::Text
Encoding& encoding, bool isMultiPartForm, FormData::EncodingType encodingType) |
206 { | 170 { |
207 if (isMultiPartForm) | 171 if (isMultiPartForm) |
208 formData->setBoundary(FormDataBuilder::generateUniqueBoundaryString()); | 172 formData->setBoundary(FormDataBuilder::generateUniqueBoundaryString()); |
209 | 173 |
210 Vector<char> encodedData; | 174 Vector<char> encodedData; |
211 | 175 |
212 const FormDataListItems& items = this->items(); | 176 for (const Item& item : items()) { |
213 size_t formDataListSize = items.size(); | |
214 ASSERT(!(formDataListSize % 2)); | |
215 for (size_t i = 0; i < formDataListSize; i += 2) { | |
216 const FormDataList::Item& key = items[i]; | |
217 const FormDataList::Item& value = items[i + 1]; | |
218 if (isMultiPartForm) { | 177 if (isMultiPartForm) { |
219 Vector<char> header; | 178 Vector<char> header; |
220 FormDataBuilder::beginMultiPartHeader(header, formData->boundary().d
ata(), key.data()); | 179 FormDataBuilder::beginMultiPartHeader(header, formData->boundary().d
ata(), item.key()); |
221 | 180 |
222 // If the current type is blob, then we also need to include the fil
ename | 181 // If the current type is blob, then we also need to include the fil
ename |
223 if (value.blob()) { | 182 if (item.blob()) { |
224 String name; | 183 String name; |
225 if (value.blob()->isFile()) { | 184 if (item.blob()->isFile()) { |
226 File* file = toFile(value.blob()); | 185 File* file = toFile(item.blob()); |
227 // For file blob, use the filename (or relative path if it i
s present) as the name. | 186 // For file blob, use the filename (or relative path if it i
s present) as the name. |
228 name = file->webkitRelativePath().isEmpty() ? file->name() :
file->webkitRelativePath(); | 187 name = file->webkitRelativePath().isEmpty() ? file->name() :
file->webkitRelativePath(); |
229 | 188 |
230 // If a filename is passed in FormData.append(), use it inst
ead of the file blob's name. | 189 // If a filename is passed in FormData.append(), use it inst
ead of the file blob's name. |
231 if (!value.filename().isNull()) | 190 if (!item.filename().isNull()) |
232 name = value.filename(); | 191 name = item.filename(); |
233 } else { | 192 } else { |
234 // For non-file blob, use the filename if it is passed in Fo
rmData.append(). | 193 // For non-file blob, use the filename if it is passed in Fo
rmData.append(). |
235 if (!value.filename().isNull()) | 194 if (!item.filename().isNull()) |
236 name = value.filename(); | 195 name = item.filename(); |
237 else | 196 else |
238 name = "blob"; | 197 name = "blob"; |
239 } | 198 } |
240 | 199 |
241 // We have to include the filename=".." part in the header, even
if the filename is empty | 200 // We have to include the filename=".." part in the header, even
if the filename is empty |
242 FormDataBuilder::addFilenameToMultiPartHeader(header, encoding,
name); | 201 FormDataBuilder::addFilenameToMultiPartHeader(header, encoding,
name); |
243 | 202 |
244 // Add the content type if available, or "application/octet-stre
am" otherwise (RFC 1867). | 203 // Add the content type if available, or "application/octet-stre
am" otherwise (RFC 1867). |
245 String contentType; | 204 String contentType; |
246 if (value.blob()->type().isEmpty()) | 205 if (item.blob()->type().isEmpty()) |
247 contentType = "application/octet-stream"; | 206 contentType = "application/octet-stream"; |
248 else | 207 else |
249 contentType = value.blob()->type(); | 208 contentType = item.blob()->type(); |
250 FormDataBuilder::addContentTypeToMultiPartHeader(header, content
Type.latin1()); | 209 FormDataBuilder::addContentTypeToMultiPartHeader(header, content
Type.latin1()); |
251 } | 210 } |
252 | 211 |
253 FormDataBuilder::finishMultiPartHeader(header); | 212 FormDataBuilder::finishMultiPartHeader(header); |
254 | 213 |
255 // Append body | 214 // Append body |
256 formData->appendData(header.data(), header.size()); | 215 formData->appendData(header.data(), header.size()); |
257 if (value.blob()) { | 216 if (item.blob()) { |
258 if (value.blob()->hasBackingFile()) { | 217 if (item.blob()->hasBackingFile()) { |
259 File* file = toFile(value.blob()); | 218 File* file = toFile(item.blob()); |
260 // Do not add the file if the path is empty. | 219 // Do not add the file if the path is empty. |
261 if (!file->path().isEmpty()) | 220 if (!file->path().isEmpty()) |
262 formData->appendFile(file->path()); | 221 formData->appendFile(file->path()); |
263 if (!file->fileSystemURL().isEmpty()) | 222 if (!file->fileSystemURL().isEmpty()) |
264 formData->appendFileSystemURL(file->fileSystemURL()); | 223 formData->appendFileSystemURL(file->fileSystemURL()); |
265 } else { | 224 } else { |
266 formData->appendBlob(value.blob()->uuid(), value.blob()->blo
bDataHandle()); | 225 formData->appendBlob(item.blob()->uuid(), item.blob()->blobD
ataHandle()); |
267 } | 226 } |
268 } else { | 227 } else { |
269 formData->appendData(value.data().data(), value.data().length())
; | 228 formData->appendData(item.data().data(), item.data().length()); |
270 } | 229 } |
271 formData->appendData("\r\n", 2); | 230 formData->appendData("\r\n", 2); |
272 } else { | 231 } else { |
273 FormDataBuilder::addKeyValuePairAsFormData(encodedData, key.data(),
value.data(), encodingType); | 232 FormDataBuilder::addKeyValuePairAsFormData(encodedData, item.key(),
item.data(), encodingType); |
274 } | 233 } |
275 } | 234 } |
276 | 235 |
277 if (isMultiPartForm) | 236 if (isMultiPartForm) |
278 FormDataBuilder::addBoundaryToMultiPartHeader(encodedData, formData->bou
ndary().data(), true); | 237 FormDataBuilder::addBoundaryToMultiPartHeader(encodedData, formData->bou
ndary().data(), true); |
279 | 238 |
280 formData->appendData(encodedData.data(), encodedData.size()); | 239 formData->appendData(encodedData.data(), encodedData.size()); |
281 } | 240 } |
282 | 241 |
283 CString FormDataList::encodeAndNormalize(const String& string) const | 242 CString FormDataList::encodeAndNormalize(const String& string) const |
(...skipping 12 matching lines...) Expand all Loading... |
296 { | 255 { |
297 visitor->trace(m_file); | 256 visitor->trace(m_file); |
298 } | 257 } |
299 | 258 |
300 DEFINE_TRACE(FormDataList::Item) | 259 DEFINE_TRACE(FormDataList::Item) |
301 { | 260 { |
302 visitor->trace(m_blob); | 261 visitor->trace(m_blob); |
303 } | 262 } |
304 | 263 |
305 } // namespace blink | 264 } // namespace blink |
OLD | NEW |