| 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 "platform/network/FormData.h" | 23 #include "platform/network/EncodedFormData.h" |
| 24 | 24 |
| 25 #include "platform/FileMetadata.h" | 25 #include "platform/FileMetadata.h" |
| 26 #include "platform/network/FormDataBuilder.h" | 26 #include "platform/network/FormDataEncoder.h" |
| 27 #include "wtf/text/CString.h" | 27 #include "wtf/text/CString.h" |
| 28 #include "wtf/text/TextEncoding.h" | 28 #include "wtf/text/TextEncoding.h" |
| 29 | 29 |
| 30 namespace blink { | 30 namespace blink { |
| 31 | 31 |
| 32 bool FormDataElement::isSafeToSendToAnotherThread() const | 32 bool FormDataElement::isSafeToSendToAnotherThread() const |
| 33 { | 33 { |
| 34 return m_filename.isSafeToSendToAnotherThread() | 34 return m_filename.isSafeToSendToAnotherThread() |
| 35 && m_blobUUID.isSafeToSendToAnotherThread() | 35 && m_blobUUID.isSafeToSendToAnotherThread() |
| 36 && m_fileSystemURL.isSafeToSendToAnotherThread(); | 36 && m_fileSystemURL.isSafeToSendToAnotherThread(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 inline FormData::FormData() | 39 inline EncodedFormData::EncodedFormData() |
| 40 : m_identifier(0) | 40 : m_identifier(0) |
| 41 , m_containsPasswordData(false) | 41 , m_containsPasswordData(false) |
| 42 { | 42 { |
| 43 } | 43 } |
| 44 | 44 |
| 45 inline FormData::FormData(const FormData& data) | 45 inline EncodedFormData::EncodedFormData(const EncodedFormData& data) |
| 46 : RefCounted<FormData>() | 46 : RefCounted<EncodedFormData>() |
| 47 , m_elements(data.m_elements) | 47 , m_elements(data.m_elements) |
| 48 , m_identifier(data.m_identifier) | 48 , m_identifier(data.m_identifier) |
| 49 , m_containsPasswordData(data.m_containsPasswordData) | 49 , m_containsPasswordData(data.m_containsPasswordData) |
| 50 { | 50 { |
| 51 } | 51 } |
| 52 | 52 |
| 53 FormData::~FormData() | 53 EncodedFormData::~EncodedFormData() |
| 54 { | 54 { |
| 55 } | 55 } |
| 56 | 56 |
| 57 PassRefPtr<FormData> FormData::create() | 57 PassRefPtr<EncodedFormData> EncodedFormData::create() |
| 58 { | 58 { |
| 59 return adoptRef(new FormData); | 59 return adoptRef(new EncodedFormData); |
| 60 } | 60 } |
| 61 | 61 |
| 62 PassRefPtr<FormData> FormData::create(const void* data, size_t size) | 62 PassRefPtr<EncodedFormData> EncodedFormData::create(const void* data, size_t siz
e) |
| 63 { | 63 { |
| 64 RefPtr<FormData> result = create(); | 64 RefPtr<EncodedFormData> result = create(); |
| 65 result->appendData(data, size); | 65 result->appendData(data, size); |
| 66 return result.release(); | 66 return result.release(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 PassRefPtr<FormData> FormData::create(const CString& string) | 69 PassRefPtr<EncodedFormData> EncodedFormData::create(const CString& string) |
| 70 { | 70 { |
| 71 RefPtr<FormData> result = create(); | 71 RefPtr<EncodedFormData> result = create(); |
| 72 result->appendData(string.data(), string.length()); | 72 result->appendData(string.data(), string.length()); |
| 73 return result.release(); | 73 return result.release(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 PassRefPtr<FormData> FormData::create(const Vector<char>& vector) | 76 PassRefPtr<EncodedFormData> EncodedFormData::create(const Vector<char>& vector) |
| 77 { | 77 { |
| 78 RefPtr<FormData> result = create(); | 78 RefPtr<EncodedFormData> result = create(); |
| 79 result->appendData(vector.data(), vector.size()); | 79 result->appendData(vector.data(), vector.size()); |
| 80 return result.release(); | 80 return result.release(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 PassRefPtr<FormData> FormData::copy() const | 83 PassRefPtr<EncodedFormData> EncodedFormData::copy() const |
| 84 { | 84 { |
| 85 return adoptRef(new FormData(*this)); | 85 return adoptRef(new EncodedFormData(*this)); |
| 86 } | 86 } |
| 87 | 87 |
| 88 PassRefPtr<FormData> FormData::deepCopy() const | 88 PassRefPtr<EncodedFormData> EncodedFormData::deepCopy() const |
| 89 { | 89 { |
| 90 RefPtr<FormData> formData(create()); | 90 RefPtr<EncodedFormData> formData(create()); |
| 91 | 91 |
| 92 formData->m_identifier = m_identifier; | 92 formData->m_identifier = m_identifier; |
| 93 formData->m_boundary = m_boundary; | 93 formData->m_boundary = m_boundary; |
| 94 formData->m_containsPasswordData = m_containsPasswordData; | 94 formData->m_containsPasswordData = m_containsPasswordData; |
| 95 | 95 |
| 96 size_t n = m_elements.size(); | 96 size_t n = m_elements.size(); |
| 97 formData->m_elements.reserveInitialCapacity(n); | 97 formData->m_elements.reserveInitialCapacity(n); |
| 98 for (size_t i = 0; i < n; ++i) { | 98 for (size_t i = 0; i < n; ++i) { |
| 99 const FormDataElement& e = m_elements[i]; | 99 const FormDataElement& e = m_elements[i]; |
| 100 switch (e.m_type) { | 100 switch (e.m_type) { |
| 101 case FormDataElement::data: | 101 case FormDataElement::data: |
| 102 formData->m_elements.uncheckedAppend(FormDataElement(e.m_data)); | 102 formData->m_elements.uncheckedAppend(FormDataElement(e.m_data)); |
| 103 break; | 103 break; |
| 104 case FormDataElement::encodedFile: | 104 case FormDataElement::encodedFile: |
| 105 formData->m_elements.uncheckedAppend(FormDataElement(e.m_filename.is
olatedCopy(), e.m_fileStart, e.m_fileLength, e.m_expectedFileModificationTime)); | 105 formData->m_elements.uncheckedAppend(FormDataElement(e.m_filename.is
olatedCopy(), e.m_fileStart, e.m_fileLength, e.m_expectedFileModificationTime)); |
| 106 break; | 106 break; |
| 107 case FormDataElement::encodedBlob: | 107 case FormDataElement::encodedBlob: |
| 108 formData->m_elements.uncheckedAppend(FormDataElement(e.m_blobUUID.is
olatedCopy(), e.m_optionalBlobDataHandle)); | 108 formData->m_elements.uncheckedAppend(FormDataElement(e.m_blobUUID.is
olatedCopy(), e.m_optionalBlobDataHandle)); |
| 109 break; | 109 break; |
| 110 case FormDataElement::encodedFileSystemURL: | 110 case FormDataElement::encodedFileSystemURL: |
| 111 formData->m_elements.uncheckedAppend(FormDataElement(e.m_fileSystemU
RL.copy(), e.m_fileStart, e.m_fileLength, e.m_expectedFileModificationTime)); | 111 formData->m_elements.uncheckedAppend(FormDataElement(e.m_fileSystemU
RL.copy(), e.m_fileStart, e.m_fileLength, e.m_expectedFileModificationTime)); |
| 112 break; | 112 break; |
| 113 } | 113 } |
| 114 } | 114 } |
| 115 return formData.release(); | 115 return formData.release(); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void FormData::appendData(const void* data, size_t size) | 118 void EncodedFormData::appendData(const void* data, size_t size) |
| 119 { | 119 { |
| 120 if (m_elements.isEmpty() || m_elements.last().m_type != FormDataElement::dat
a) | 120 if (m_elements.isEmpty() || m_elements.last().m_type != FormDataElement::dat
a) |
| 121 m_elements.append(FormDataElement()); | 121 m_elements.append(FormDataElement()); |
| 122 FormDataElement& e = m_elements.last(); | 122 FormDataElement& e = m_elements.last(); |
| 123 size_t oldSize = e.m_data.size(); | 123 size_t oldSize = e.m_data.size(); |
| 124 e.m_data.grow(oldSize + size); | 124 e.m_data.grow(oldSize + size); |
| 125 memcpy(e.m_data.data() + oldSize, data, size); | 125 memcpy(e.m_data.data() + oldSize, data, size); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void FormData::appendFile(const String& filename) | 128 void EncodedFormData::appendFile(const String& filename) |
| 129 { | 129 { |
| 130 m_elements.append(FormDataElement(filename, 0, BlobDataItem::toEndOfFile, in
validFileTime())); | 130 m_elements.append(FormDataElement(filename, 0, BlobDataItem::toEndOfFile, in
validFileTime())); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void FormData::appendFileRange(const String& filename, long long start, long lon
g length, double expectedModificationTime) | 133 void EncodedFormData::appendFileRange(const String& filename, long long start, l
ong long length, double expectedModificationTime) |
| 134 { | 134 { |
| 135 m_elements.append(FormDataElement(filename, start, length, expectedModificat
ionTime)); | 135 m_elements.append(FormDataElement(filename, start, length, expectedModificat
ionTime)); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void FormData::appendBlob(const String& uuid, PassRefPtr<BlobDataHandle> optiona
lHandle) | 138 void EncodedFormData::appendBlob(const String& uuid, PassRefPtr<BlobDataHandle>
optionalHandle) |
| 139 { | 139 { |
| 140 m_elements.append(FormDataElement(uuid, optionalHandle)); | 140 m_elements.append(FormDataElement(uuid, optionalHandle)); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void FormData::appendFileSystemURL(const KURL& url) | 143 void EncodedFormData::appendFileSystemURL(const KURL& url) |
| 144 { | 144 { |
| 145 m_elements.append(FormDataElement(url, 0, BlobDataItem::toEndOfFile, invalid
FileTime())); | 145 m_elements.append(FormDataElement(url, 0, BlobDataItem::toEndOfFile, invalid
FileTime())); |
| 146 } | 146 } |
| 147 | 147 |
| 148 void FormData::appendFileSystemURLRange(const KURL& url, long long start, long l
ong length, double expectedModificationTime) | 148 void EncodedFormData::appendFileSystemURLRange(const KURL& url, long long start,
long long length, double expectedModificationTime) |
| 149 { | 149 { |
| 150 m_elements.append(FormDataElement(url, start, length, expectedModificationTi
me)); | 150 m_elements.append(FormDataElement(url, start, length, expectedModificationTi
me)); |
| 151 } | 151 } |
| 152 | 152 |
| 153 void FormData::flatten(Vector<char>& data) const | 153 void EncodedFormData::flatten(Vector<char>& data) const |
| 154 { | 154 { |
| 155 // Concatenate all the byte arrays, but omit any files. | 155 // Concatenate all the byte arrays, but omit any files. |
| 156 data.clear(); | 156 data.clear(); |
| 157 size_t n = m_elements.size(); | 157 size_t n = m_elements.size(); |
| 158 for (size_t i = 0; i < n; ++i) { | 158 for (size_t i = 0; i < n; ++i) { |
| 159 const FormDataElement& e = m_elements[i]; | 159 const FormDataElement& e = m_elements[i]; |
| 160 if (e.m_type == FormDataElement::data) | 160 if (e.m_type == FormDataElement::data) |
| 161 data.append(e.m_data.data(), static_cast<size_t>(e.m_data.size())); | 161 data.append(e.m_data.data(), static_cast<size_t>(e.m_data.size())); |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 | 164 |
| 165 String FormData::flattenToString() const | 165 String EncodedFormData::flattenToString() const |
| 166 { | 166 { |
| 167 Vector<char> bytes; | 167 Vector<char> bytes; |
| 168 flatten(bytes); | 168 flatten(bytes); |
| 169 return Latin1Encoding().decode(reinterpret_cast<const char*>(bytes.data()),
bytes.size()); | 169 return Latin1Encoding().decode(reinterpret_cast<const char*>(bytes.data()),
bytes.size()); |
| 170 } | 170 } |
| 171 | 171 |
| 172 unsigned long long FormData::sizeInBytes() const | 172 unsigned long long EncodedFormData::sizeInBytes() const |
| 173 { | 173 { |
| 174 unsigned size = 0; | 174 unsigned size = 0; |
| 175 size_t n = m_elements.size(); | 175 size_t n = m_elements.size(); |
| 176 for (size_t i = 0; i < n; ++i) { | 176 for (size_t i = 0; i < n; ++i) { |
| 177 const FormDataElement& e = m_elements[i]; | 177 const FormDataElement& e = m_elements[i]; |
| 178 switch (e.m_type) { | 178 switch (e.m_type) { |
| 179 case FormDataElement::data: | 179 case FormDataElement::data: |
| 180 size += e.m_data.size(); | 180 size += e.m_data.size(); |
| 181 break; | 181 break; |
| 182 case FormDataElement::encodedFile: | 182 case FormDataElement::encodedFile: |
| 183 size += e.m_fileLength - e.m_fileStart; | 183 size += e.m_fileLength - e.m_fileStart; |
| 184 break; | 184 break; |
| 185 case FormDataElement::encodedBlob: | 185 case FormDataElement::encodedBlob: |
| 186 if (e.m_optionalBlobDataHandle) | 186 if (e.m_optionalBlobDataHandle) |
| 187 size += e.m_optionalBlobDataHandle->size(); | 187 size += e.m_optionalBlobDataHandle->size(); |
| 188 break; | 188 break; |
| 189 case FormDataElement::encodedFileSystemURL: | 189 case FormDataElement::encodedFileSystemURL: |
| 190 size += e.m_fileLength - e.m_fileStart; | 190 size += e.m_fileLength - e.m_fileStart; |
| 191 break; | 191 break; |
| 192 } | 192 } |
| 193 } | 193 } |
| 194 return size; | 194 return size; |
| 195 } | 195 } |
| 196 | 196 |
| 197 bool FormData::isSafeToSendToAnotherThread() const | 197 bool EncodedFormData::isSafeToSendToAnotherThread() const |
| 198 { | 198 { |
| 199 if (!hasOneRef()) | 199 if (!hasOneRef()) |
| 200 return false; | 200 return false; |
| 201 for (auto& element : m_elements) { | 201 for (auto& element : m_elements) { |
| 202 if (!element.isSafeToSendToAnotherThread()) | 202 if (!element.isSafeToSendToAnotherThread()) |
| 203 return false; | 203 return false; |
| 204 } | 204 } |
| 205 return true; | 205 return true; |
| 206 } | 206 } |
| 207 | 207 |
| 208 } // namespace blink | 208 } // namespace blink |
| OLD | NEW |