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

Unified Diff: third_party/WebKit/Source/modules/fetch/Body.cpp

Issue 2292763002: [Fetch API] Implement Request.formData and Response.formData. (Closed)
Patch Set: More global-interface-listing*-expected.txt, urlencoded-parser-expected.txt Created 3 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/modules/fetch/Body.h ('k') | third_party/WebKit/Source/modules/fetch/Body.idl » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/modules/fetch/Body.cpp
diff --git a/third_party/WebKit/Source/modules/fetch/Body.cpp b/third_party/WebKit/Source/modules/fetch/Body.cpp
index 26eb326ae5b2eb7d7b66c4377a777e9c30538305..e91a271ebff17aeeca644843f825adaa9a815d20 100644
--- a/third_party/WebKit/Source/modules/fetch/Body.cpp
+++ b/third_party/WebKit/Source/modules/fetch/Body.cpp
@@ -10,11 +10,14 @@
#include "core/dom/DOMArrayBuffer.h"
#include "core/dom/DOMTypedArray.h"
#include "core/dom/ExecutionContext.h"
+#include "core/dom/URLSearchParams.h"
#include "core/fileapi/Blob.h"
+#include "core/html/FormData.h"
#include "modules/fetch/BodyStreamBuffer.h"
#include "modules/fetch/FetchDataLoader.h"
#include "platform/bindings/ScriptState.h"
#include "platform/bindings/V8ThrowException.h"
+#include "platform/network/ParsedContentType.h"
#include "platform/wtf/PassRefPtr.h"
#include "platform/wtf/RefPtr.h"
#include "public/platform/WebDataConsumerHandle.h"
@@ -72,6 +75,25 @@ class BodyArrayBufferConsumer final : public BodyConsumerBase {
}
};
+class BodyFormDataConsumer final : public BodyConsumerBase {
+ WTF_MAKE_NONCOPYABLE(BodyFormDataConsumer);
+
+ public:
+ explicit BodyFormDataConsumer(ScriptPromiseResolver* resolver)
+ : BodyConsumerBase(resolver) {}
+
+ void DidFetchDataLoadedFormData(FormData* formData) override {
+ Resolver()->Resolve(formData);
+ }
+
+ void DidFetchDataLoadedString(const String& string) override {
+ FormData* formData = FormData::Create();
+ for (const auto& pair : URLSearchParams::Create(string)->Params())
+ formData->append(pair.first, pair.second);
+ DidFetchDataLoadedFormData(formData);
+ }
+};
+
class BodyTextConsumer final : public BodyConsumerBase {
WTF_MAKE_NONCOPYABLE(BodyTextConsumer);
@@ -158,6 +180,49 @@ ScriptPromise Body::blob(ScriptState* script_state) {
return promise;
}
+ScriptPromise Body::formData(ScriptState* script_state) {
+ ScriptPromise promise = RejectInvalidConsumption(script_state);
+ if (!promise.IsEmpty())
+ return promise;
+
+ // See above comment.
+ if (!ExecutionContext::From(script_state))
+ return ScriptPromise();
+
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state);
+ const ParsedContentType parsedTypeWithParameters(ContentType());
+ const String parsedType = parsedTypeWithParameters.MimeType().LowerASCII();
+ promise = resolver->Promise();
+ if (parsedType == "multipart/form-data") {
+ const String boundary =
+ parsedTypeWithParameters.ParameterValueForName("boundary");
+ if (BodyBuffer() && !boundary.IsEmpty()) {
+ BodyBuffer()->StartLoading(
+ FetchDataLoader::CreateLoaderAsFormData(boundary),
+ new BodyFormDataConsumer(resolver));
+ return promise;
+ }
+ } else if (parsedType == "application/x-www-form-urlencoded") {
+ if (BodyBuffer()) {
+ BodyBuffer()->StartLoading(FetchDataLoader::CreateLoaderAsString(),
+ new BodyFormDataConsumer(resolver));
+ } else {
+ resolver->Resolve(FormData::Create());
+ }
+ return promise;
+ } else {
+ if (BodyBuffer()) {
+ BodyBuffer()->StartLoading(FetchDataLoader::CreateLoaderAsFailure(),
+ new BodyFormDataConsumer(resolver));
+ return promise;
+ }
+ }
+
+ resolver->Reject(V8ThrowException::CreateTypeError(script_state->GetIsolate(),
+ "Invalid MIME type"));
+ return promise;
+}
+
ScriptPromise Body::json(ScriptState* script_state) {
ScriptPromise promise = RejectInvalidConsumption(script_state);
if (!promise.IsEmpty())
« no previous file with comments | « third_party/WebKit/Source/modules/fetch/Body.h ('k') | third_party/WebKit/Source/modules/fetch/Body.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698