-
Notifications
You must be signed in to change notification settings - Fork 363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Default Encoding for application/json Content-Type in HTTP Responses #1422
base: master
Are you sure you want to change the base?
Changes from 5 commits
0820553
8c267c4
7223e9d
7b3c383
ee56aa0
7ba26e1
5d6c1ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,10 @@ class Response extends BaseResponse { | |
/// | ||
/// This is converted from [bodyBytes] using the `charset` parameter of the | ||
/// `Content-Type` header field, if available. If it's unavailable or if the | ||
/// encoding name is unknown, [latin1] is used by default, as per | ||
/// [RFC 2616][]. | ||
/// encoding name is unknown, [utf8] is used by default, as per | ||
/// [RFC3629][]. | ||
/// | ||
/// [RFC 2616]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html | ||
/// [RFC3629]:https://www.rfc-editor.org/rfc/rfc3629. | ||
String get body => _encodingForHeaders(headers).decode(bodyBytes); | ||
|
||
/// Creates a new HTTP response with a string body. | ||
|
@@ -66,10 +66,12 @@ class Response extends BaseResponse { | |
|
||
/// Returns the encoding to use for a response with the given headers. | ||
/// | ||
/// Defaults to [latin1] if the headers don't specify a charset or if that | ||
/// charset is unknown. | ||
/// If the `Content-Type` header specifies a charset, it will use that charset. | ||
/// If no charset is provided or the charset is unknown: | ||
/// - Defaults to [utf8] if the `Content-Type` is `application/json` (since JSON is defined to use UTF-8 by default). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrap to 80 columns |
||
/// - Otherwise, defaults to [latin1] for compatibility. | ||
Encoding _encodingForHeaders(Map<String, String> headers) => | ||
encodingForCharset(_contentTypeForHeaders(headers).parameters['charset']); | ||
encodingForContentTypeHeader(_contentTypeForHeaders(headers)); | ||
|
||
/// Returns the [MediaType] object for the given headers' content-type. | ||
/// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:http/http.dart' as http; | ||
import 'package:test/test.dart'; | ||
|
@@ -45,6 +46,23 @@ void main() { | |
headers: {'content-type': 'text/plain; charset=iso-8859-1'}); | ||
expect(response.body, equals('föøbãr')); | ||
}); | ||
test('test decoding with empty charset if content type is application/json', | ||
() { | ||
final utf8Bytes = utf8.encode('{"foo":"Привет, мир!"}'); | ||
var response = http.Response.bytes(utf8Bytes, 200, | ||
headers: {'content-type': 'application/json'}); | ||
expect(response.body, equals('{"foo":"Привет, мир!"}')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, there should be one Could you add tests for these additional scenarios:
|
||
|
||
final chineseUtf8Bytes = utf8.encode('{"foo":"你好,世界!"}'); | ||
var responseChinese = http.Response.bytes(chineseUtf8Bytes, 200, | ||
headers: {'content-type': 'application/json'}); | ||
expect(responseChinese.body, equals('{"foo":"你好,世界!"}')); | ||
|
||
final koreanUtf8Bytes = utf8.encode('{"foo":"안녕하세요, 세계!"}'); | ||
var responseKorean = http.Response.bytes(koreanUtf8Bytes, 200, | ||
headers: {'content-type': 'application/json'}); | ||
expect(responseKorean.body, equals('{"foo":"안녕하세요, 세계!"}')); | ||
}); | ||
}); | ||
|
||
group('.fromStream()', () { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you site a section? Is this only for JSON data for for all payloads?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, RFC3629 requires interpreting application/json as UTF-8, everything else is described as incorrect interpretation of the specification