Bug 184645 - Adjust XMLHttpRequest Content-Type handling
Summary: Adjust XMLHttpRequest Content-Type handling
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Rob Buis
URL:
Keywords: InRadar
Depends on: 180526 191085
Blocks:
  Show dependency treegraph
 
Reported: 2018-04-16 02:49 PDT by Anne van Kesteren
Modified: 2019-03-01 13:33 PST (History)
9 users (show)

See Also:


Attachments
Patch (8.60 KB, patch)
2019-02-27 12:38 PST, Rob Buis
no flags Details | Formatted Diff | Diff
Archive of layout-test-results from ews102 for mac-highsierra (2.42 MB, application/zip)
2019-02-27 13:40 PST, EWS Watchlist
no flags Details
Patch (10.96 KB, patch)
2019-02-27 13:46 PST, Rob Buis
no flags Details | Formatted Diff | Diff
Patch (10.96 KB, patch)
2019-02-28 00:34 PST, Rob Buis
no flags Details | Formatted Diff | Diff
Patch (12.08 KB, patch)
2019-03-01 02:26 PST, Rob Buis
no flags Details | Formatted Diff | Diff
Patch (12.02 KB, patch)
2019-03-01 09:37 PST, Rob Buis
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Anne van Kesteren 2018-04-16 02:49:42 PDT
See https://github.com/whatwg/xhr/pull/176 and https://github.com/w3c/web-platform-tests/pull/8422.

In particular, XMLHttpRequest Content-Type manipulation is now to be done in terms of the MIME type parser defined in MIME Sniffing.
Comment 1 Anne van Kesteren 2018-10-30 02:50:07 PDT
https://github.com/whatwg/xhr/pull/224 and https://github.com/web-platform-tests/wpt/pull/13779 have minor tweaks to this to make this more web-compatible.
Comment 2 Chris Dumez 2018-10-30 15:38:15 PDT
Test is xhr/send-content-type-charset.htm.
Comment 3 Chris Dumez 2018-10-30 15:49:30 PDT
Quite a few failures on imported/w3c/web-platform-tests/xhr/send-content-type-charset.htm:
FAIL header with invalid MIME type is not changed assert_equals: expected "text; charset=ascii" but got "text; charset=UTF-8"
PASS header with invalid MIME type (empty string) is not changed
PASS known charset but bogus header - missing MIME type
PASS bogus charset and bogus header - missing MIME type
FAIL If charset= param is UTF-8 (case-insensitive), it should not be changed assert_equals: expected "text/plain;charset=utf-8" but got "text/plain;charset=UTF-8"
PASS If no charset= param is given, implementation should not add one - unknown MIME
PASS If no charset= param is given, implementation should not add one - known MIME
PASS If no charset= param is given, implementation should not add one - known MIME, unknown param, two spaces
FAIL charset given but wrong, fix it (unknown MIME, bogus charset) assert_equals: expected "text/x-thepiano;charset=UTF-8" but got "text/x-thepiano;charset= UTF-8"
FAIL If charset= param is UTF-8 (case-insensitive), it should not be changed (bogus charset) assert_equals: expected "text/plain;charset=utf-8;charset=waddup" but got "text/plain;charset=UTF-8;charset=UTF-8"
PASS charset given but wrong, fix it (known MIME, actual charset)
FAIL Multiple non-UTF-8 charset parameters deduplicate, bogus parameter dropped assert_equals: expected "text/x-pink-unicorn;charset=UTF-8" but got "text/x-pink-unicorn; charset=UTF-8; charset=UTF-8; notrelated; charset=UTF-8"
PASS No content type set, give MIME and charset
FAIL charset with space that is UTF-8 does not change assert_equals: expected "text/plain;charset= utf-8" but got "text/plain;charset= UTF-8"
FAIL charset in double quotes that is UTF-8 does not change assert_equals: expected "text/plain;charset=\"utf-8\"" but got "text/plain;charset=\"UTF-8\""
FAIL charset in double quotes with space assert_equals: expected "text/plain;charset=UTF-8" but got "text/plain;charset=\" UTF-8\""
FAIL charset in double quotes with backslashes that is UTF-8 does not change assert_equals: expected "text/plain;charset=\"u\\t\f-8\"" but got "text/plain;charset=\"UTF-8\""
FAIL unknown parameters need to be preserved assert_equals: expected "yo/yo;charset=UTF-8;yo=YO;x=y" but got "YO/yo;charset=UTF-8;yo=YO; X=y"
Comment 4 Alexey Proskuryakov 2018-10-30 16:17:54 PDT
I think that the challenge here is to separate cases that have the potential of fixing or breaking websites from irrelevant subtests.
Comment 5 Rob Buis 2019-02-27 12:38:49 PST
Created attachment 363117 [details]
Patch
Comment 6 EWS Watchlist 2019-02-27 13:40:41 PST
Comment on attachment 363117 [details]
Patch

Attachment 363117 [details] did not pass mac-ews (mac):
Output: https://webkit-queues.webkit.org/results/11306680

New failing tests:
http/tests/xmlhttprequest/request-encoding2.html
Comment 7 EWS Watchlist 2019-02-27 13:40:44 PST
Created attachment 363123 [details]
Archive of layout-test-results from ews102 for mac-highsierra

The attached test failures were seen while running run-webkit-tests on the mac-ews.
Bot: ews102  Port: mac-highsierra  Platform: Mac OS X 10.13.6
Comment 8 Rob Buis 2019-02-27 13:46:38 PST
Created attachment 363126 [details]
Patch
Comment 9 Rob Buis 2019-02-28 00:34:17 PST
Created attachment 363208 [details]
Patch
Comment 10 youenn fablet 2019-02-28 14:00:11 PST
Comment on attachment 363208 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=363208&action=review

> Source/WebCore/platform/network/ParsedContentType.cpp:351
> +    m_parameterValues.set("charset", charset);

s/"charset"/"charset"_s/

Could also be String&&/WTFMove

> Source/WebCore/xml/XMLHttpRequest.cpp:77
>  static void replaceCharsetInMediaType(String& mediaType, const String& charsetValue)

Can we make replaceCharsetInMediaType closer to spec by no longer passing charsetValue, since it is always UTF-8?
We could then change replaceCharsetInMediaType prototype so that it takes a const String& and return a String.

> Source/WebCore/xml/XMLHttpRequest.cpp:79
> +    Optional<ParsedContentType> parsedContentType = ParsedContentType::create(mediaType);

s/Optional<ParsedContentType>/auto/
Comment 11 Rob Buis 2019-03-01 02:26:44 PST
Created attachment 363318 [details]
Patch
Comment 12 Rob Buis 2019-03-01 08:08:36 PST
Comment on attachment 363208 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=363208&action=review

>> Source/WebCore/platform/network/ParsedContentType.cpp:351
>> +    m_parameterValues.set("charset", charset);
> 
> s/"charset"/"charset"_s/
> 
> Could also be String&&/WTFMove

Done.

>> Source/WebCore/xml/XMLHttpRequest.cpp:77
>>  static void replaceCharsetInMediaType(String& mediaType, const String& charsetValue)
> 
> Can we make replaceCharsetInMediaType closer to spec by no longer passing charsetValue, since it is always UTF-8?
> We could then change replaceCharsetInMediaType prototype so that it takes a const String& and return a String.

Good idea, done.

>> Source/WebCore/xml/XMLHttpRequest.cpp:79
>> +    Optional<ParsedContentType> parsedContentType = ParsedContentType::create(mediaType);
> 
> s/Optional<ParsedContentType>/auto/

Done.
Comment 13 youenn fablet 2019-03-01 08:23:22 PST
Comment on attachment 363318 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=363318&action=review

> Source/WebCore/xml/XMLHttpRequest.cpp:77
> +static String replaceCharsetInMediaType(const String& mediaType)

I would rename it to replaceCharsetInMediaTypeIfNeeded.
Thinking about it, chances are that mediaType will be returned without any change.
We could take a String&& and return a String. Or go back to void/String&.

> Source/WebCore/xml/XMLHttpRequest.cpp:84
> +    return parsedContentType->serialize();

"text/plain; charset=UTF-8" should remain as is and not become "text/plain;charset=UTF-8", right?
Can we add a test if we do not cover this case?

> LayoutTests/imported/w3c/ChangeLog:8
> +        Sync test and uodate test expectation (all PASSes).

s/uodate/update/
Comment 14 Rob Buis 2019-03-01 09:37:23 PST
Created attachment 363337 [details]
Patch
Comment 15 Rob Buis 2019-03-01 09:40:07 PST
Comment on attachment 363318 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=363318&action=review

>> Source/WebCore/xml/XMLHttpRequest.cpp:77
>> +static String replaceCharsetInMediaType(const String& mediaType)
> 
> I would rename it to replaceCharsetInMediaTypeIfNeeded.
> Thinking about it, chances are that mediaType will be returned without any change.
> We could take a String&& and return a String. Or go back to void/String&.

I thought about it but thought the name would get kind of long. Anyway fine with it :)

>> Source/WebCore/xml/XMLHttpRequest.cpp:84
>> +    return parsedContentType->serialize();
> 
> "text/plain; charset=UTF-8" should remain as is and not become "text/plain;charset=UTF-8", right?
> Can we add a test if we do not cover this case?

We pass that, I tried it locally. I would like to cover this case in send-content-type-charset.htm, but in a follow-up commit to WPT.

>> LayoutTests/imported/w3c/ChangeLog:8
>> +        Sync test and uodate test expectation (all PASSes).
> 
> s/uodate/update/

Fixed.
Comment 16 WebKit Commit Bot 2019-03-01 13:32:59 PST
Comment on attachment 363337 [details]
Patch

Clearing flags on attachment: 363337

Committed r242284: <https://trac.webkit.org/changeset/242284>
Comment 17 WebKit Commit Bot 2019-03-01 13:33:01 PST
All reviewed patches have been landed.  Closing bug.
Comment 18 Radar WebKit Bug Importer 2019-03-01 13:33:59 PST
<rdar://problem/48521015>