Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(394)

Side by Side Diff: Source/core/html/FormDataList.cpp

Issue 564963002: New FormData methods: get, getAll, has, set, delete and iterable (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Stuff new methods behind a flag Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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::deleteEntry(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);
arv (Not doing code reviews) 2014/09/17 19:49:38 I'm not sure what your plan is to fix this to make
jsbell 2014/09/18 16:18:59 I don't have a firm plan yet. IMHO we should remov
arv (Not doing code reviews) 2014/09/18 17:33:45 There are two ways to do this. 1. Keep a list if
59 } else {
60 i += 2;
61 }
62 }
63 ASSERT(!(m_items.size() % 2));
64 return;
65 }
66
67 FormDataList::Entry FormDataList::getEntry(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 itemsToEntry(key, value);
79 }
80 return Entry();
81 }
82
83 FormDataList::Entry FormDataList::getEntry(size_t index) const
84 {
85 const WillBeHeapVector<Item>& items = this->items();
86 size_t formDataListSize = items.size();
87 ASSERT(!(formDataListSize % 2));
88 if (index >= formDataListSize / 2)
89 return Entry();
90 const FormDataList::Item& key = items[index * 2];
91 const FormDataList::Item& value = items[index * 2 + 1];
92 return itemsToEntry(key, value);
93 }
94
95 WillBeHeapVector<FormDataList::Entry> FormDataList::getAll(const String& key) co nst
96 {
97 WillBeHeapVector<FormDataList::Entry> matches;
98
99 const CString keyData = encodeAndNormalize(key);
100 const WillBeHeapVector<Item>& items = this->items();
101 size_t formDataListSize = items.size();
102 ASSERT(!(formDataListSize % 2));
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 }
110
111 return matches;
112 }
113
114 FormDataList::Entry FormDataList::itemsToEntry(const FormDataList::Item& key, co nst FormDataList::Item& value) const
115 {
116 const CString nameData = key.data();
117 const String name = m_encoding.decode(nameData.data(), nameData.length());
118
119 if (!value.blob()) {
120 const CString valueData = value.data();
121 return Entry(name, m_encoding.decode(valueData.data(), valueData.length( )));
122 }
123
124 // 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
126 // items.
127 // FIXME: Consider applying the name during insertion.
128
129 if (value.blob()->isFile()) {
130 RefPtrWillBeRawPtr<File> file = toFile(value.blob());
131 if (value.filename().isNull())
132 return Entry(name, file);
133 return Entry(name, file->clone(value.filename()));
134 }
135
136 String filename = value.filename();
137 if (filename.isNull())
138 filename = "blob";
139 return Entry(name, File::create(filename, currentTime(), value.blob()->blobD ataHandle()));
140 }
141
142 bool FormDataList::hasEntry(const String& key) const
143 {
144 const CString keyData = encodeAndNormalize(key);
145 const WillBeHeapVector<Item>& items = this->items();
146 size_t formDataListSize = items.size();
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;
152 }
153 return false;
154 }
155
156 void FormDataList::setBlob(const String& key, PassRefPtrWillBeRawPtr<Blob> blob, const String& filename)
157 {
158 setEntry(key, Item(blob, filename));
159 }
160
161 void FormDataList::setData(const String& key, const String& value)
162 {
163 setEntry(key, encodeAndNormalize(value));
164 }
165
166 void FormDataList::setEntry(const String& key, const Item& item)
167 {
168 const CString keyData = encodeAndNormalize(key);
169 ASSERT(!(m_items.size() % 2));
170 bool found = false;
171 size_t i = 0;
172 while (i < m_items.size()) {
173 if (m_items[i].data() != keyData) {
174 i += 2;
175 } else if (found) {
176 m_items.remove(i, 2);
177 } else {
178 found = true;
179 m_items[i + 1] = item;
180 i += 2;
181 }
182 }
183 if (!found) {
184 m_items.append(keyData);
185 m_items.append(item);
186 }
187 ASSERT(!(m_items.size() % 2));
188 return;
189 }
190
51 PassRefPtr<FormData> FormDataList::createFormData(FormData::EncodingType encodin gType) 191 PassRefPtr<FormData> FormDataList::createFormData(FormData::EncodingType encodin gType)
52 { 192 {
53 RefPtr<FormData> result = FormData::create(); 193 RefPtr<FormData> result = FormData::create();
54 appendKeyValuePairItemsTo(result.get(), m_encoding, false, encodingType); 194 appendKeyValuePairItemsTo(result.get(), m_encoding, false, encodingType);
55 return result.release(); 195 return result.release();
56 } 196 }
57 197
58 PassRefPtr<FormData> FormDataList::createMultiPartFormData() 198 PassRefPtr<FormData> FormDataList::createMultiPartFormData()
59 { 199 {
60 RefPtr<FormData> result = FormData::create(); 200 RefPtr<FormData> result = FormData::create();
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 FormDataBuilder::addKeyValuePairAsFormData(encodedData, key.data(), value.data(), encodingType); 273 FormDataBuilder::addKeyValuePairAsFormData(encodedData, key.data(), value.data(), encodingType);
134 } 274 }
135 } 275 }
136 276
137 if (isMultiPartForm) 277 if (isMultiPartForm)
138 FormDataBuilder::addBoundaryToMultiPartHeader(encodedData, formData->bou ndary().data(), true); 278 FormDataBuilder::addBoundaryToMultiPartHeader(encodedData, formData->bou ndary().data(), true);
139 279
140 formData->appendData(encodedData.data(), encodedData.size()); 280 formData->appendData(encodedData.data(), encodedData.size());
141 } 281 }
142 282
283 CString FormDataList::encodeAndNormalize(const String& string) const
284 {
285 CString encodedString = m_encoding.encode(string, WTF::EntitiesForUnencodabl es);
286 return normalizeLineEndingsToCRLF(encodedString);
287 }
288
143 void FormDataList::trace(Visitor* visitor) 289 void FormDataList::trace(Visitor* visitor)
144 { 290 {
145 visitor->trace(m_items); 291 visitor->trace(m_items);
146 } 292 }
147 293
294
295 void FormDataList::Entry::trace(Visitor* visitor)
296 {
297 visitor->trace(m_file);
298 }
299
148 void FormDataList::Item::trace(Visitor* visitor) 300 void FormDataList::Item::trace(Visitor* visitor)
149 { 301 {
150 visitor->trace(m_blob); 302 visitor->trace(m_blob);
151 } 303 }
152 304
153 } // namespace 305 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698