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 ad7a829596abd4d44f99318530c6653bcc50f67f..26e1f57da666b70086ea190124afa61ab0af2dea 100644 |
--- a/third_party/WebKit/Source/modules/fetch/Body.cpp |
+++ b/third_party/WebKit/Source/modules/fetch/Body.cpp |
@@ -10,10 +10,13 @@ |
#include "bindings/core/v8/V8ThrowException.h" |
#include "core/dom/DOMArrayBuffer.h" |
#include "core/dom/DOMTypedArray.h" |
+#include "core/dom/URLSearchParams.h" |
#include "core/fileapi/Blob.h" |
#include "core/frame/UseCounter.h" |
+#include "core/html/FormData.h" |
#include "modules/fetch/BodyStreamBuffer.h" |
#include "modules/fetch/FetchDataLoader.h" |
+#include "platform/network/ParsedContentType.h" |
#include "public/platform/WebDataConsumerHandle.h" |
#include "wtf/PassRefPtr.h" |
#include "wtf/RefPtr.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,46 @@ ScriptPromise Body::blob(ScriptState* scriptState) { |
return promise; |
} |
+ScriptPromise Body::formData(ScriptState* scriptState) { |
+ ScriptPromise promise = rejectInvalidConsumption(scriptState); |
+ if (!promise.isEmpty()) |
+ return promise; |
+ |
+ // See above comment. |
+ if (!scriptState->getExecutionContext()) |
+ return ScriptPromise(); |
+ |
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
+ const ParsedContentType parsedTypeWithParameters(contentType()); |
+ const String parsedType = parsedTypeWithParameters.mimeType().lower(); |
+ 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()->closeAndLockAndDisturb(); |
+ } |
+ |
+ resolver->reject(V8ThrowException::createTypeError(scriptState->isolate(), |
+ "Invalid MIME type")); |
+ return promise; |
+} |
+ |
ScriptPromise Body::json(ScriptState* scriptState) { |
ScriptPromise promise = rejectInvalidConsumption(scriptState); |
if (!promise.isEmpty()) |