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

Side by Side Diff: third_party/WebKit/Source/modules/fetch/Body.cpp

Issue 2292763002: [Fetch API] Implement Request.formData and Response.formData. (Closed)
Patch Set: Handle partial delimiter prefixes correctly and really test them Created 4 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/fetch/Body.h" 5 #include "modules/fetch/Body.h"
6 6
7 #include "bindings/core/v8/ScriptPromiseResolver.h" 7 #include "bindings/core/v8/ScriptPromiseResolver.h"
8 #include "bindings/core/v8/ScriptState.h" 8 #include "bindings/core/v8/ScriptState.h"
9 #include "bindings/core/v8/V8ArrayBuffer.h" 9 #include "bindings/core/v8/V8ArrayBuffer.h"
10 #include "bindings/core/v8/V8ThrowException.h" 10 #include "bindings/core/v8/V8ThrowException.h"
11 #include "core/dom/DOMArrayBuffer.h" 11 #include "core/dom/DOMArrayBuffer.h"
12 #include "core/dom/DOMTypedArray.h" 12 #include "core/dom/DOMTypedArray.h"
13 #include "core/dom/URLSearchParams.h"
13 #include "core/fileapi/Blob.h" 14 #include "core/fileapi/Blob.h"
14 #include "core/frame/UseCounter.h" 15 #include "core/frame/UseCounter.h"
16 #include "core/html/FormData.h"
15 #include "modules/fetch/BodyStreamBuffer.h" 17 #include "modules/fetch/BodyStreamBuffer.h"
16 #include "modules/fetch/FetchDataLoader.h" 18 #include "modules/fetch/FetchDataLoader.h"
19 #include "platform/network/ParsedContentType.h"
17 #include "public/platform/WebDataConsumerHandle.h" 20 #include "public/platform/WebDataConsumerHandle.h"
18 #include "wtf/PassRefPtr.h" 21 #include "wtf/PassRefPtr.h"
19 #include "wtf/RefPtr.h" 22 #include "wtf/RefPtr.h"
20 #include <memory> 23 #include <memory>
21 24
22 namespace blink { 25 namespace blink {
23 26
24 namespace { 27 namespace {
25 28
26 class BodyConsumerBase : public GarbageCollectedFinalized<BodyConsumerBase>, pub lic FetchDataLoader::Client { 29 class BodyConsumerBase : public GarbageCollectedFinalized<BodyConsumerBase>, pub lic FetchDataLoader::Client {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 WTF_MAKE_NONCOPYABLE(BodyArrayBufferConsumer); 63 WTF_MAKE_NONCOPYABLE(BodyArrayBufferConsumer);
61 public: 64 public:
62 explicit BodyArrayBufferConsumer(ScriptPromiseResolver* resolver) : BodyCons umerBase(resolver) {} 65 explicit BodyArrayBufferConsumer(ScriptPromiseResolver* resolver) : BodyCons umerBase(resolver) {}
63 66
64 void didFetchDataLoadedArrayBuffer(DOMArrayBuffer* arrayBuffer) override 67 void didFetchDataLoadedArrayBuffer(DOMArrayBuffer* arrayBuffer) override
65 { 68 {
66 resolver()->resolve(arrayBuffer); 69 resolver()->resolve(arrayBuffer);
67 } 70 }
68 }; 71 };
69 72
73 class BodyFormDataConsumer final : public BodyConsumerBase {
74 WTF_MAKE_NONCOPYABLE(BodyFormDataConsumer);
75
76 public:
77 explicit BodyFormDataConsumer(ScriptPromiseResolver* resolver)
78 : BodyConsumerBase(resolver)
79 {
80 }
81
82 void didFetchDataLoadedFormData(FormData* formData) override
83 {
84 resolver()->resolve(formData);
85 }
86
87 void didFetchDataLoadedString(const String& string) override
88 {
89 FormData* formData = FormData::create();
90 for (const auto& pair : URLSearchParams::create(string)->params())
91 formData->append(pair.first, pair.second);
92 didFetchDataLoadedFormData(formData);
93 }
94 };
95
70 class BodyTextConsumer final : public BodyConsumerBase { 96 class BodyTextConsumer final : public BodyConsumerBase {
71 WTF_MAKE_NONCOPYABLE(BodyTextConsumer); 97 WTF_MAKE_NONCOPYABLE(BodyTextConsumer);
72 public: 98 public:
73 explicit BodyTextConsumer(ScriptPromiseResolver* resolver) : BodyConsumerBas e(resolver) {} 99 explicit BodyTextConsumer(ScriptPromiseResolver* resolver) : BodyConsumerBas e(resolver) {}
74 100
75 void didFetchDataLoadedString(const String& string) override 101 void didFetchDataLoadedString(const String& string) override
76 { 102 {
77 resolver()->resolve(string); 103 resolver()->resolve(string);
78 } 104 }
79 }; 105 };
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsBlobHandle(mim eType()), new BodyBlobConsumer(resolver)); 168 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsBlobHandle(mim eType()), new BodyBlobConsumer(resolver));
143 } else { 169 } else {
144 std::unique_ptr<BlobData> blobData = BlobData::create(); 170 std::unique_ptr<BlobData> blobData = BlobData::create();
145 blobData->setContentType(mimeType()); 171 blobData->setContentType(mimeType());
146 resolver->resolve(Blob::create(BlobDataHandle::create(std::move(blobData ), 0))); 172 resolver->resolve(Blob::create(BlobDataHandle::create(std::move(blobData ), 0)));
147 } 173 }
148 return promise; 174 return promise;
149 175
150 } 176 }
151 177
178 ScriptPromise Body::formData(ScriptState* scriptState)
179 {
180 ScriptPromise promise = rejectInvalidConsumption(scriptState);
181 if (!promise.isEmpty())
182 return promise;
183
184 // See above comment.
185 if (!scriptState->getExecutionContext())
186 return ScriptPromise();
187
188 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
189 ParsedContentType parsedTypeWithParameters(contentType());
yhirano 2016/09/07 06:00:16 The spec[1] uses body's "MIME type", why don't you
e_hakkinen 2016/09/08 00:06:29 Because the return value of Body::mimeType does no
yhirano 2016/09/12 02:20:49 Then I think the existing Body::mimeType should be
190 String parsedType = parsedTypeWithParameters.mimeType().lower();
191 promise = resolver->promise();
192 if (parsedType == "multipart/form-data") {
193 String boundary = parsedTypeWithParameters.parameterValueForName("bounda ry");
194 if (bodyBuffer() && !boundary.isEmpty()) {
195 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsFormData(b oundary), new BodyFormDataConsumer(resolver));
196 return promise;
197 }
198 } else if (parsedType == "application/x-www-form-urlencoded") {
199 if (bodyBuffer()) {
200 bodyBuffer()->startLoading(FetchDataLoader::createLoaderAsString(), new BodyFormDataConsumer(resolver));
201 } else {
202 resolver->resolve(FormData::create());
203 }
204 return promise;
205 }
206
207 resolver->reject(V8ThrowException::createTypeError(scriptState->isolate(), " Invalid MIME type"));
208 return promise;
209 }
210
152 ScriptPromise Body::json(ScriptState* scriptState) 211 ScriptPromise Body::json(ScriptState* scriptState)
153 { 212 {
154 ScriptPromise promise = rejectInvalidConsumption(scriptState); 213 ScriptPromise promise = rejectInvalidConsumption(scriptState);
155 if (!promise.isEmpty()) 214 if (!promise.isEmpty())
156 return promise; 215 return promise;
157 216
158 // See above comment. 217 // See above comment.
159 if (!scriptState->getExecutionContext()) 218 if (!scriptState->getExecutionContext())
160 return ScriptPromise(); 219 return ScriptPromise();
161 220
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 Body::Body(ExecutionContext* context) : ActiveScriptWrappable(this) , ContextLif ecycleObserver(context) {} 280 Body::Body(ExecutionContext* context) : ActiveScriptWrappable(this) , ContextLif ecycleObserver(context) {}
222 281
223 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState) 282 ScriptPromise Body::rejectInvalidConsumption(ScriptState* scriptState)
224 { 283 {
225 if (isBodyLocked() || bodyUsed()) 284 if (isBodyLocked() || bodyUsed())
226 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read")); 285 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(scriptState->isolate(), "Already read"));
227 return ScriptPromise(); 286 return ScriptPromise();
228 } 287 }
229 288
230 } // namespace blink 289 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698