Chromium Code Reviews| 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
| 18 * | 18 * |
| 19 */ | 19 */ |
| 20 | 20 |
| 21 #include "config.h" | 21 #include "config.h" |
| 22 #include "core/html/FormDataList.h" | 22 #include "core/html/FormDataList.h" |
| 23 | 23 |
| 24 #include "core/fileapi/File.h" | 24 #include "core/fileapi/File.h" |
| 25 #include "platform/network/FormDataBuilder.h" | 25 #include "platform/network/FormDataBuilder.h" |
| 26 #include "platform/text/LineEnding.h" | 26 #include "platform/text/LineEnding.h" |
| 27 #include "wtf/CurrentTime.h" | |
| 27 | 28 |
| 28 namespace blink { | 29 namespace blink { |
| 29 | 30 |
| 30 FormDataList::FormDataList(const WTF::TextEncoding& c) | 31 FormDataList::FormDataList(const WTF::TextEncoding& c) |
| 31 : m_encoding(c) | 32 : m_encoding(c) |
| 32 { | 33 { |
| 33 } | 34 } |
| 34 | 35 |
| 35 void FormDataList::appendString(const String& string) | 36 void FormDataList::appendString(const String& string) |
| 36 { | 37 { |
| 37 CString encodedString = m_encoding.encode(string, WTF::EntitiesForUnencodabl es); | 38 m_items.append(encodeAndNormalize(string)); |
| 38 m_items.append(normalizeLineEndingsToCRLF(encodedString)); | |
| 39 } | 39 } |
| 40 | 40 |
| 41 void FormDataList::appendString(const CString& string) | 41 void FormDataList::appendString(const CString& string) |
| 42 { | 42 { |
| 43 m_items.append(string); | 43 m_items.append(string); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void FormDataList::appendBlob(PassRefPtrWillBeRawPtr<Blob> blob, const String& f ilename) | 46 void FormDataList::appendBlob(PassRefPtrWillBeRawPtr<Blob> blob, const String& f ilename) |
| 47 { | 47 { |
| 48 m_items.append(Item(blob, filename)); | 48 m_items.append(Item(blob, filename)); |
| 49 } | 49 } |
| 50 | 50 |
| 51 void FormDataList::deleteItem(const String& key) | |
| 52 { | |
| 53 const CString keyData = encodeAndNormalize(key); | |
| 54 ASSERT(!(m_items.size() % 2)); | |
| 55 size_t i = 0; | |
| 56 while (i < m_items.size()) { | |
| 57 if (m_items[i].data() == keyData) { | |
| 58 m_items.remove(i, 2); | |
| 59 } else { | |
| 60 i += 2; | |
| 61 } | |
| 62 } | |
| 63 ASSERT(!(m_items.size() % 2)); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 FormDataList::EntryValue FormDataList::getItem(const String& key) const | |
| 68 { | |
| 69 const CString keyData = encodeAndNormalize(key); | |
| 70 const WillBeHeapVector<Item>& items = this->items(); | |
| 71 size_t formDataListSize = items.size(); | |
| 72 ASSERT(!(formDataListSize % 2)); | |
| 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 itemToEntryValue(value); | |
| 79 } | |
| 80 return EntryValue(); | |
| 81 } | |
| 82 | |
| 83 Vector<FormDataList::EntryValue> FormDataList::getAll(const String& key) const | |
| 84 { | |
| 85 Vector<FormDataList::EntryValue> matches; | |
|
sof
2014/09/12 15:21:26
Vector => WillBeHeapVector
jsbell
2014/09/12 16:30:09
Done.
| |
| 86 | |
| 87 const CString keyData = encodeAndNormalize(key); | |
| 88 const WillBeHeapVector<Item>& items = this->items(); | |
| 89 size_t formDataListSize = items.size(); | |
| 90 ASSERT(!(formDataListSize % 2)); | |
| 91 for (size_t i = 0; i < formDataListSize; i += 2) { | |
| 92 const FormDataList::Item& key = items[i]; | |
| 93 if (key.data() != keyData) | |
| 94 continue; | |
| 95 const FormDataList::Item& value = items[i + 1]; | |
| 96 matches.append(itemToEntryValue(value)); | |
| 97 } | |
| 98 | |
| 99 return matches; | |
| 100 } | |
| 101 | |
| 102 FormDataList::EntryValue FormDataList::itemToEntryValue(const FormDataList::Item & value) const | |
| 103 { | |
| 104 if (!value.blob()) { | |
| 105 const CString data = value.data(); | |
| 106 return EntryValue(m_encoding.decode(data.data(), data.length())); | |
| 107 } | |
| 108 | |
| 109 if (value.blob()->isFile()) { | |
| 110 // FIXME: Overwrite file's name with value.filename(). | |
| 111 return EntryValue(toFile(value.blob())); | |
| 112 } | |
| 113 | |
| 114 String filename = value.filename(); | |
| 115 if (filename.isNull()) | |
| 116 filename = "blob"; | |
| 117 return EntryValue(File::create(filename, currentTimeMS(), value.blob()->blob DataHandle())); | |
|
tyoshino (SeeGerritForStatus)
2014/09/12 07:29:32
this looks equivalent to what's specified in the s
jsbell
2014/09/12 16:30:09
Thanks!
| |
| 118 } | |
| 119 | |
| 120 bool FormDataList::hasItem(const String& key) const | |
| 121 { | |
| 122 const CString keyData = encodeAndNormalize(key); | |
| 123 const WillBeHeapVector<Item>& items = this->items(); | |
| 124 size_t formDataListSize = items.size(); | |
| 125 ASSERT(!(formDataListSize % 2)); | |
| 126 for (size_t i = 0; i < formDataListSize; i += 2) { | |
| 127 const FormDataList::Item& key = items[i]; | |
| 128 if (key.data() == keyData) | |
| 129 return true; | |
| 130 } | |
| 131 return false; | |
| 132 } | |
| 133 | |
| 134 void FormDataList::setBlob(const String& key, PassRefPtrWillBeRawPtr<Blob> blob, const String& filename) | |
| 135 { | |
| 136 setItem(key, Item(blob, filename)); | |
| 137 } | |
| 138 | |
| 139 void FormDataList::setData(const String& key, const String& value) | |
| 140 { | |
| 141 setItem(key, encodeAndNormalize(value)); | |
| 142 } | |
| 143 | |
| 144 void FormDataList::setItem(const String& key, const Item& item) | |
| 145 { | |
| 146 const CString keyData = encodeAndNormalize(key); | |
| 147 ASSERT(!(m_items.size() % 2)); | |
| 148 bool found = false; | |
| 149 size_t i = 0; | |
| 150 while (i < m_items.size()) { | |
| 151 if (m_items[i].data() != keyData) { | |
| 152 i += 2; | |
| 153 } else if (found) { | |
| 154 m_items.remove(i, 2); | |
| 155 } else { | |
| 156 found = true; | |
| 157 m_items[i + 1] = item; | |
| 158 i += 2; | |
| 159 } | |
| 160 } | |
| 161 if (!found) { | |
| 162 m_items.append(keyData); | |
| 163 m_items.append(item); | |
| 164 } | |
| 165 ASSERT(!(m_items.size() % 2)); | |
| 166 return; | |
| 167 } | |
| 168 | |
| 51 PassRefPtr<FormData> FormDataList::createFormData(FormData::EncodingType encodin gType) | 169 PassRefPtr<FormData> FormDataList::createFormData(FormData::EncodingType encodin gType) |
| 52 { | 170 { |
| 53 RefPtr<FormData> result = FormData::create(); | 171 RefPtr<FormData> result = FormData::create(); |
| 54 appendKeyValuePairItemsTo(result.get(), m_encoding, false, encodingType); | 172 appendKeyValuePairItemsTo(result.get(), m_encoding, false, encodingType); |
| 55 return result.release(); | 173 return result.release(); |
| 56 } | 174 } |
| 57 | 175 |
| 58 PassRefPtr<FormData> FormDataList::createMultiPartFormData() | 176 PassRefPtr<FormData> FormDataList::createMultiPartFormData() |
| 59 { | 177 { |
| 60 RefPtr<FormData> result = FormData::create(); | 178 RefPtr<FormData> result = FormData::create(); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 FormDataBuilder::addKeyValuePairAsFormData(encodedData, key.data(), value.data(), encodingType); | 251 FormDataBuilder::addKeyValuePairAsFormData(encodedData, key.data(), value.data(), encodingType); |
| 134 } | 252 } |
| 135 } | 253 } |
| 136 | 254 |
| 137 if (isMultiPartForm) | 255 if (isMultiPartForm) |
| 138 FormDataBuilder::addBoundaryToMultiPartHeader(encodedData, formData->bou ndary().data(), true); | 256 FormDataBuilder::addBoundaryToMultiPartHeader(encodedData, formData->bou ndary().data(), true); |
| 139 | 257 |
| 140 formData->appendData(encodedData.data(), encodedData.size()); | 258 formData->appendData(encodedData.data(), encodedData.size()); |
| 141 } | 259 } |
| 142 | 260 |
| 261 CString FormDataList::encodeAndNormalize(const String& string) const | |
| 262 { | |
| 263 CString encodedString = m_encoding.encode(string, WTF::EntitiesForUnencodabl es); | |
| 264 return normalizeLineEndingsToCRLF(encodedString); | |
| 265 } | |
| 266 | |
| 143 void FormDataList::trace(Visitor* visitor) | 267 void FormDataList::trace(Visitor* visitor) |
| 144 { | 268 { |
| 145 visitor->trace(m_items); | 269 visitor->trace(m_items); |
| 146 } | 270 } |
| 147 | 271 |
| 148 void FormDataList::Item::trace(Visitor* visitor) | 272 void FormDataList::Item::trace(Visitor* visitor) |
| 149 { | 273 { |
| 150 visitor->trace(m_blob); | 274 visitor->trace(m_blob); |
| 151 } | 275 } |
| 152 | 276 |
| 153 } // namespace | 277 } // namespace |
| OLD | NEW |