Chromium Code Reviews| 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 e23e91a5a9c58d55e2a2340e74e10b18f695ddc4..88dafca1ec8162deaa5915b1d7e4184c79c8fd0b 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()); |
|
horo
2016/09/27 10:45:49
nit: const
e_hakkinen
2016/09/28 15:15:04
Done.
|
| + String parsedType = parsedTypeWithParameters.mimeType().lower(); |
|
horo
2016/09/27 10:45:49
nit: const
e_hakkinen
2016/09/28 15:15:04
Done.
|
| + promise = resolver->promise(); |
| + if (parsedType == "multipart/form-data") { |
| + String boundary = parsedTypeWithParameters.parameterValueForName("boundary"); |
|
horo
2016/09/27 10:45:49
nit: const
e_hakkinen
2016/09/28 15:15:04
Done.
|
| + 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); |