Skip to content

Commit 19052d9

Browse files
committed
FetchResponse.formData() should not reject promise if the body is null and the MIME Type is "application/x-www-form-urlencoded"
https://bugs.webkit.org/show_bug.cgi?id=228085 Reviewed by Alex Christensen. LayoutTests/imported/w3c: Rebaseline WPT tests now that more checks are passing. * web-platform-tests/fetch/api/request/request-consume-empty.any-expected.txt: * web-platform-tests/fetch/api/request/request-consume-empty.any.worker-expected.txt: * web-platform-tests/fetch/api/response/response-consume-empty.any-expected.txt: * web-platform-tests/fetch/api/response/response-consume-empty.any.worker-expected.txt: Source/WebCore: FetchResponse.formData() should not reject promise if the body is null and the MIME Type is "application/x-www-form-urlencoded": - https://fetch.spec.whatwg.org/#concept-body-consume-body - https://fetch.spec.whatwg.org/#concept-body-package-data As per the specification, we should package an empty byte sequence in this case and resolve the promise with that. No new tests, rebaselined existing tests. * Modules/fetch/FetchBodyConsumer.cpp: (WebCore::FetchBodyConsumer::packageFormData): (WebCore::resolveWithTypeAndData): (WebCore::packageFormData): Deleted. * Modules/fetch/FetchBodyConsumer.h: * Modules/fetch/FetchBodyOwner.cpp: (WebCore::FetchBodyOwner::formData): Canonical link: https://commits.webkit.org/239781@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@280046 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 22c08a6 commit 19052d9

9 files changed

Lines changed: 61 additions & 10 deletions

File tree

LayoutTests/imported/w3c/ChangeLog

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
2021-07-19 Chris Dumez <[email protected]>
2+
3+
FetchResponse.formData() should not reject promise if the body is null and the MIME Type is "application/x-www-form-urlencoded"
4+
https://bugs.webkit.org/show_bug.cgi?id=228085
5+
6+
Reviewed by Alex Christensen.
7+
8+
Rebaseline WPT tests now that more checks are passing.
9+
10+
* web-platform-tests/fetch/api/request/request-consume-empty.any-expected.txt:
11+
* web-platform-tests/fetch/api/request/request-consume-empty.any.worker-expected.txt:
12+
* web-platform-tests/fetch/api/response/response-consume-empty.any-expected.txt:
13+
* web-platform-tests/fetch/api/response/response-consume-empty.any.worker-expected.txt:
14+
115
2021-07-19 Chris Dumez <[email protected]>
216

317
Unreviewed, land test baselines for cross-origin-opener-policy tests

LayoutTests/imported/w3c/web-platform-tests/fetch/api/request/request-consume-empty.any-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PASS Consume request's body as blob
44
PASS Consume request's body as arrayBuffer
55
PASS Consume request's body as json (error case)
66
PASS Consume request's body as formData with correct multipart type (error case)
7-
FAIL Consume request's body as formData with correct urlencoded type promise_test: Unhandled rejection with value: object "TypeError: Type error"
7+
PASS Consume request's body as formData with correct urlencoded type
88
PASS Consume request's body as formData without correct type (error case)
99
PASS Consume empty blob request body as arrayBuffer
1010
PASS Consume empty text request body as arrayBuffer

LayoutTests/imported/w3c/web-platform-tests/fetch/api/request/request-consume-empty.any.worker-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PASS Consume request's body as blob
44
PASS Consume request's body as arrayBuffer
55
PASS Consume request's body as json (error case)
66
PASS Consume request's body as formData with correct multipart type (error case)
7-
FAIL Consume request's body as formData with correct urlencoded type promise_test: Unhandled rejection with value: object "TypeError: Type error"
7+
PASS Consume request's body as formData with correct urlencoded type
88
PASS Consume request's body as formData without correct type (error case)
99
PASS Consume empty blob request body as arrayBuffer
1010
PASS Consume empty text request body as arrayBuffer

LayoutTests/imported/w3c/web-platform-tests/fetch/api/response/response-consume-empty.any-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PASS Consume response's body as blob
44
PASS Consume response's body as arrayBuffer
55
PASS Consume response's body as json (error case)
66
PASS Consume response's body as formData with correct multipart type (error case)
7-
FAIL Consume response's body as formData with correct urlencoded type promise_test: Unhandled rejection with value: object "TypeError: Type error"
7+
PASS Consume response's body as formData with correct urlencoded type
88
PASS Consume response's body as formData without correct type (error case)
99
PASS Consume empty blob response body as arrayBuffer
1010
PASS Consume empty text response body as arrayBuffer

LayoutTests/imported/w3c/web-platform-tests/fetch/api/response/response-consume-empty.any.worker-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PASS Consume response's body as blob
44
PASS Consume response's body as arrayBuffer
55
PASS Consume response's body as json (error case)
66
PASS Consume response's body as formData with correct multipart type (error case)
7-
FAIL Consume response's body as formData with correct urlencoded type promise_test: Unhandled rejection with value: object "TypeError: Type error"
7+
PASS Consume response's body as formData with correct urlencoded type
88
PASS Consume response's body as formData without correct type (error case)
99
PASS Consume empty blob response body as arrayBuffer
1010
PASS Consume empty text response body as arrayBuffer

Source/WebCore/ChangeLog

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
2021-07-19 Chris Dumez <[email protected]>
2+
3+
FetchResponse.formData() should not reject promise if the body is null and the MIME Type is "application/x-www-form-urlencoded"
4+
https://bugs.webkit.org/show_bug.cgi?id=228085
5+
6+
Reviewed by Alex Christensen.
7+
8+
FetchResponse.formData() should not reject promise if the body is null and the MIME Type is "application/x-www-form-urlencoded":
9+
- https://fetch.spec.whatwg.org/#concept-body-consume-body
10+
- https://fetch.spec.whatwg.org/#concept-body-package-data
11+
12+
As per the specification, we should package an empty byte sequence in this case and resolve the promise with that.
13+
14+
No new tests, rebaselined existing tests.
15+
16+
* Modules/fetch/FetchBodyConsumer.cpp:
17+
(WebCore::FetchBodyConsumer::packageFormData):
18+
(WebCore::resolveWithTypeAndData):
19+
(WebCore::packageFormData): Deleted.
20+
* Modules/fetch/FetchBodyConsumer.h:
21+
* Modules/fetch/FetchBodyOwner.cpp:
22+
(WebCore::FetchBodyOwner::formData):
23+
124
2021-07-19 Ziran Sun <[email protected]>
225

326
[css-grid] Images as grid items should use the overridingLogicalHeight when defined to compute the logical width

Source/WebCore/Modules/fetch/FetchBodyConsumer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static std::optional<MimeType> parseMIMEType(const String& contentType)
124124
}
125125

126126
// https://fetch.spec.whatwg.org/#concept-body-package-data
127-
static RefPtr<DOMFormData> packageFormData(ScriptExecutionContext* context, const String& contentType, const uint8_t* data, size_t length)
127+
RefPtr<DOMFormData> FetchBodyConsumer::packageFormData(ScriptExecutionContext* context, const String& contentType, const uint8_t* data, size_t length)
128128
{
129129
auto parseMultipartPart = [context] (const uint8_t* part, size_t partLength, DOMFormData& form) -> bool {
130130
const uint8_t* headerEnd = static_cast<const uint8_t*>(memmem(part, partLength, "\r\n\r\n", 4));
@@ -229,7 +229,7 @@ static void resolveWithTypeAndData(Ref<DeferredPromise>&& promise, FetchBodyCons
229229
promise->resolve<IDLDOMString>(TextResourceDecoder::textFromUTF8(data, length));
230230
return;
231231
case FetchBodyConsumer::Type::FormData:
232-
if (auto formData = packageFormData(context, contentType, data, length))
232+
if (auto formData = FetchBodyConsumer::packageFormData(context, contentType, data, length))
233233
promise->resolve<IDLInterface<DOMFormData>>(*formData);
234234
else
235235
promise->reject(TypeError);

Source/WebCore/Modules/fetch/FetchBodyConsumer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
namespace WebCore {
3838

3939
class Blob;
40+
class DOMFormData;
4041
class FetchBodySource;
4142
class ReadableStream;
4243

@@ -73,6 +74,8 @@ class FetchBodyConsumer {
7374

7475
void setAsLoading() { m_isLoading = true; }
7576

77+
static RefPtr<DOMFormData> packageFormData(ScriptExecutionContext*, const String& contentType, const uint8_t* data, size_t length);
78+
7679
private:
7780
Ref<Blob> takeAsBlob(ScriptExecutionContext*);
7881
void resetConsumePromise();

Source/WebCore/Modules/fetch/FetchBodyOwner.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "FetchLoader.h"
3535
#include "HTTPParsers.h"
3636
#include "JSBlob.h"
37+
#include "JSDOMFormData.h"
3738
#include "ResourceError.h"
3839
#include "ResourceResponse.h"
3940
#include "WindowEventLoop.h"
@@ -185,14 +186,24 @@ void FetchBodyOwner::formData(Ref<DeferredPromise>&& promise)
185186
return;
186187
}
187188

188-
if (isBodyNullOrOpaque()) {
189-
promise->reject(TypeError);
190-
return;
191-
}
192189
if (isDisturbedOrLocked()) {
193190
promise->reject(Exception { TypeError, "Body is disturbed or locked"_s });
194191
return;
195192
}
193+
194+
if (isBodyNullOrOpaque()) {
195+
if (isBodyNull()) {
196+
// If the content-type is 'application/x-www-form-urlencoded', a body is not required and we should package an empty byte sequence as per the specification.
197+
if (auto formData = FetchBodyConsumer::packageFormData(promise->scriptExecutionContext(), m_contentType, nullptr, 0)) {
198+
promise->resolve<IDLInterface<DOMFormData>>(*formData);
199+
return;
200+
}
201+
}
202+
203+
promise->reject(TypeError);
204+
return;
205+
}
206+
196207
m_isDisturbed = true;
197208
m_body->formData(*this, WTFMove(promise));
198209
}

0 commit comments

Comments
 (0)