| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2006, 2008, 2011 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Google Inc. All rights reserved. | 3 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 4 * Copyright (C) 2012 Digia Plc. and/or its subsidiary(-ies) | 4 * Copyright (C) 2012 Digia Plc. and/or its subsidiary(-ies) |
| 5 * | 5 * |
| 6 * This library is free software; you can redistribute it and/or | 6 * This library is free software; you can redistribute it and/or |
| 7 * modify it under the terms of the GNU Library General Public | 7 * modify it under the terms of the GNU Library General Public |
| 8 * License as published by the Free Software Foundation; either | 8 * License as published by the Free Software Foundation; either |
| 9 * version 2 of the License, or (at your option) any later version. | 9 * version 2 of the License, or (at your option) any later version. |
| 10 * | 10 * |
| 11 * This library is distributed in the hope that it will be useful, | 11 * This library is distributed in the hope that it will be useful, |
| 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 * Library General Public License for more details. | 14 * Library General Public License for more details. |
| 15 * | 15 * |
| 16 * You should have received a copy of the GNU Library General Public License | 16 * You should have received a copy of the GNU Library General Public License |
| 17 * along with this library; see the file COPYING.LIB. If not, write to | 17 * along with this library; see the file COPYING.LIB. If not, write to |
| 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 * Boston, MA 02110-1301, USA. | 19 * Boston, MA 02110-1301, USA. |
| 20 */ | 20 */ |
| 21 | 21 |
| 22 #include "config.h" | 22 #include "config.h" |
| 23 #include "core/platform/network/FormData.h" | 23 #include "core/platform/network/FormData.h" |
| 24 | 24 |
| 25 #include "core/fileapi/File.h" | |
| 26 #include "core/platform/network/FormDataBuilder.h" | 25 #include "core/platform/network/FormDataBuilder.h" |
| 27 #include "core/platform/network/FormDataList.h" | |
| 28 #include "platform/FileMetadata.h" | 26 #include "platform/FileMetadata.h" |
| 29 #include "platform/blob/BlobData.h" | 27 #include "platform/blob/BlobData.h" |
| 30 #include "wtf/text/TextEncoding.h" | 28 #include "wtf/text/TextEncoding.h" |
| 31 | 29 |
| 32 namespace WebCore { | 30 namespace WebCore { |
| 33 | 31 |
| 34 inline FormData::FormData() | 32 inline FormData::FormData() |
| 35 : m_identifier(0) | 33 : m_identifier(0) |
| 36 , m_alwaysStream(false) | 34 , m_alwaysStream(false) |
| 37 , m_containsPasswordData(false) | 35 , m_containsPasswordData(false) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 return result.release(); | 68 return result.release(); |
| 71 } | 69 } |
| 72 | 70 |
| 73 PassRefPtr<FormData> FormData::create(const Vector<char>& vector) | 71 PassRefPtr<FormData> FormData::create(const Vector<char>& vector) |
| 74 { | 72 { |
| 75 RefPtr<FormData> result = create(); | 73 RefPtr<FormData> result = create(); |
| 76 result->appendData(vector.data(), vector.size()); | 74 result->appendData(vector.data(), vector.size()); |
| 77 return result.release(); | 75 return result.release(); |
| 78 } | 76 } |
| 79 | 77 |
| 80 PassRefPtr<FormData> FormData::create(const FormDataList& list, const WTF::TextE
ncoding& encoding, EncodingType encodingType) | |
| 81 { | |
| 82 RefPtr<FormData> result = create(); | |
| 83 result->appendKeyValuePairItems(list, encoding, false, encodingType); | |
| 84 return result.release(); | |
| 85 } | |
| 86 | |
| 87 PassRefPtr<FormData> FormData::createMultiPart(const FormDataList& list, const W
TF::TextEncoding& encoding) | |
| 88 { | |
| 89 RefPtr<FormData> result = create(); | |
| 90 result->appendKeyValuePairItems(list, encoding, true); | |
| 91 return result.release(); | |
| 92 } | |
| 93 | |
| 94 PassRefPtr<FormData> FormData::copy() const | 78 PassRefPtr<FormData> FormData::copy() const |
| 95 { | 79 { |
| 96 return adoptRef(new FormData(*this)); | 80 return adoptRef(new FormData(*this)); |
| 97 } | 81 } |
| 98 | 82 |
| 99 PassRefPtr<FormData> FormData::deepCopy() const | 83 PassRefPtr<FormData> FormData::deepCopy() const |
| 100 { | 84 { |
| 101 RefPtr<FormData> formData(create()); | 85 RefPtr<FormData> formData(create()); |
| 102 | 86 |
| 103 formData->m_alwaysStream = m_alwaysStream; | 87 formData->m_alwaysStream = m_alwaysStream; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 void FormData::appendFileSystemURL(const KURL& url) | 136 void FormData::appendFileSystemURL(const KURL& url) |
| 153 { | 137 { |
| 154 m_elements.append(FormDataElement(url, 0, BlobDataItem::toEndOfFile, invalid
FileTime())); | 138 m_elements.append(FormDataElement(url, 0, BlobDataItem::toEndOfFile, invalid
FileTime())); |
| 155 } | 139 } |
| 156 | 140 |
| 157 void FormData::appendFileSystemURLRange(const KURL& url, long long start, long l
ong length, double expectedModificationTime) | 141 void FormData::appendFileSystemURLRange(const KURL& url, long long start, long l
ong length, double expectedModificationTime) |
| 158 { | 142 { |
| 159 m_elements.append(FormDataElement(url, start, length, expectedModificationTi
me)); | 143 m_elements.append(FormDataElement(url, start, length, expectedModificationTi
me)); |
| 160 } | 144 } |
| 161 | 145 |
| 162 void FormData::appendKeyValuePairItems(const FormDataList& list, const WTF::Text
Encoding& encoding, bool isMultiPartForm, EncodingType encodingType) | |
| 163 { | |
| 164 if (isMultiPartForm) | |
| 165 m_boundary = FormDataBuilder::generateUniqueBoundaryString(); | |
| 166 | |
| 167 Vector<char> encodedData; | |
| 168 | |
| 169 const Vector<FormDataList::Item>& items = list.items(); | |
| 170 size_t formDataListSize = items.size(); | |
| 171 ASSERT(!(formDataListSize % 2)); | |
| 172 for (size_t i = 0; i < formDataListSize; i += 2) { | |
| 173 const FormDataList::Item& key = items[i]; | |
| 174 const FormDataList::Item& value = items[i + 1]; | |
| 175 if (isMultiPartForm) { | |
| 176 Vector<char> header; | |
| 177 FormDataBuilder::beginMultiPartHeader(header, m_boundary.data(), key
.data()); | |
| 178 | |
| 179 // If the current type is blob, then we also need to include the fil
ename | |
| 180 if (value.blob()) { | |
| 181 String name; | |
| 182 if (value.blob()->isFile()) { | |
| 183 File* file = toFile(value.blob()); | |
| 184 // For file blob, use the filename (or relative path if it i
s present) as the name. | |
| 185 name = file->webkitRelativePath().isEmpty() ? file->name() :
file->webkitRelativePath(); | |
| 186 | |
| 187 // If a filename is passed in FormData.append(), use it inst
ead of the file blob's name. | |
| 188 if (!value.filename().isNull()) | |
| 189 name = value.filename(); | |
| 190 } else { | |
| 191 // For non-file blob, use the filename if it is passed in Fo
rmData.append(). | |
| 192 if (!value.filename().isNull()) | |
| 193 name = value.filename(); | |
| 194 else | |
| 195 name = "blob"; | |
| 196 } | |
| 197 | |
| 198 // We have to include the filename=".." part in the header, even
if the filename is empty | |
| 199 FormDataBuilder::addFilenameToMultiPartHeader(header, encoding,
name); | |
| 200 | |
| 201 // Add the content type if available, or "application/octet-stre
am" otherwise (RFC 1867). | |
| 202 String contentType; | |
| 203 if (value.blob()->type().isEmpty()) | |
| 204 contentType = "application/octet-stream"; | |
| 205 else | |
| 206 contentType = value.blob()->type(); | |
| 207 FormDataBuilder::addContentTypeToMultiPartHeader(header, content
Type.latin1()); | |
| 208 } | |
| 209 | |
| 210 FormDataBuilder::finishMultiPartHeader(header); | |
| 211 | |
| 212 // Append body | |
| 213 appendData(header.data(), header.size()); | |
| 214 if (value.blob()) { | |
| 215 if (value.blob()->isFile()) { | |
| 216 File* file = toFile(value.blob()); | |
| 217 // Do not add the file if the path is empty. | |
| 218 if (!file->path().isEmpty()) | |
| 219 appendFile(file->path()); | |
| 220 if (!file->fileSystemURL().isEmpty()) | |
| 221 appendFileSystemURL(file->fileSystemURL()); | |
| 222 } else { | |
| 223 appendBlob(value.blob()->uuid(), value.blob()->blobDataHandl
e()); | |
| 224 } | |
| 225 } else | |
| 226 appendData(value.data().data(), value.data().length()); | |
| 227 appendData("\r\n", 2); | |
| 228 } else { | |
| 229 // Omit the name "isindex" if it's the first form data element. | |
| 230 // FIXME: Why is this a good rule? Is this obsolete now? | |
| 231 if (encodedData.isEmpty() && key.data() == "isindex") | |
| 232 FormDataBuilder::encodeStringAsFormData(encodedData, value.data(
)); | |
| 233 else | |
| 234 FormDataBuilder::addKeyValuePairAsFormData(encodedData, key.data
(), value.data(), encodingType); | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 if (isMultiPartForm) | |
| 239 FormDataBuilder::addBoundaryToMultiPartHeader(encodedData, m_boundary.da
ta(), true); | |
| 240 | |
| 241 appendData(encodedData.data(), encodedData.size()); | |
| 242 } | |
| 243 | |
| 244 void FormData::flatten(Vector<char>& data) const | 146 void FormData::flatten(Vector<char>& data) const |
| 245 { | 147 { |
| 246 // Concatenate all the byte arrays, but omit any files. | 148 // Concatenate all the byte arrays, but omit any files. |
| 247 data.clear(); | 149 data.clear(); |
| 248 size_t n = m_elements.size(); | 150 size_t n = m_elements.size(); |
| 249 for (size_t i = 0; i < n; ++i) { | 151 for (size_t i = 0; i < n; ++i) { |
| 250 const FormDataElement& e = m_elements[i]; | 152 const FormDataElement& e = m_elements[i]; |
| 251 if (e.m_type == FormDataElement::data) | 153 if (e.m_type == FormDataElement::data) |
| 252 data.append(e.m_data.data(), static_cast<size_t>(e.m_data.size())); | 154 data.append(e.m_data.data(), static_cast<size_t>(e.m_data.size())); |
| 253 } | 155 } |
| 254 } | 156 } |
| 255 | 157 |
| 256 String FormData::flattenToString() const | 158 String FormData::flattenToString() const |
| 257 { | 159 { |
| 258 Vector<char> bytes; | 160 Vector<char> bytes; |
| 259 flatten(bytes); | 161 flatten(bytes); |
| 260 return Latin1Encoding().decode(reinterpret_cast<const char*>(bytes.data()),
bytes.size()); | 162 return Latin1Encoding().decode(reinterpret_cast<const char*>(bytes.data()),
bytes.size()); |
| 261 } | 163 } |
| 262 | 164 |
| 263 } // namespace WebCore | 165 } // namespace WebCore |
| OLD | NEW |