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 26eb326ae5b2eb7d7b66c4377a777e9c30538305..9c0f07367e8edc39ed4b76936061dc582297003c 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,48 @@ 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 { |
| + // "Let |reader| be the result of getting reader from |stream|." |
| + // "Read all bytes from |stream| with |reader|." |
| + if (BodyBuffer()) |
| + BodyBuffer()->CloseAndLockAndDisturb(); |
|
yhirano
2017/05/12 11:54:12
This is not spec-conformant when BodyBuffer() is m
e_hakkinen
2017/05/12 14:56:12
OK. I used the same code as blink::Request::Create
yhirano
2017/05/15 11:54:48
Please note that "the same code" in Request::Creat
|
| + } |
| + |
| + 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()) |