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 d7d78ec5cbd95d21bc53dd1624f6d78023872445..41644d364740092227e2da89e1eaf457921adc93 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" |
@@ -67,6 +70,29 @@ public: |
} |
}; |
+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); |
public: |
@@ -149,6 +175,39 @@ ScriptPromise Body::blob(ScriptState* scriptState) |
} |
+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); |
+ 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
|
+ String parsedType = parsedTypeWithParameters.mimeType().lower(); |
+ promise = resolver->promise(); |
+ if (parsedType == "multipart/form-data") { |
+ 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; |
+ } |
+ |
+ resolver->reject(V8ThrowException::createTypeError(scriptState->isolate(), "Invalid MIME type")); |
+ return promise; |
+} |
+ |
ScriptPromise Body::json(ScriptState* scriptState) |
{ |
ScriptPromise promise = rejectInvalidConsumption(scriptState); |