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

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

Issue 2292763002: [Fetch API] Implement Request.formData and Response.formData. (Closed)
Patch Set: Handle partial delimiter prefixes correctly and really test them Created 4 years, 3 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
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);

Powered by Google App Engine
This is Rietveld 408576698