diff --git a/pkgs/http/lib/http.dart b/pkgs/http/lib/http.dart index ccd7dab5ef..bd039c8519 100644 --- a/pkgs/http/lib/http.dart +++ b/pkgs/http/lib/http.dart @@ -16,7 +16,7 @@ import 'src/streamed_request.dart'; export 'src/base_client.dart'; export 'src/base_request.dart'; -export 'src/base_response.dart' show BaseResponse, HeadersWithFieldLists; +export 'src/base_response.dart' show BaseResponse, HeadersWithSplitValues; export 'src/byte_stream.dart'; export 'src/client.dart' hide zoneClient; export 'src/exception.dart'; diff --git a/pkgs/http/lib/src/base_response.dart b/pkgs/http/lib/src/base_response.dart index 3a4c380cf6..e1796e1b36 100644 --- a/pkgs/http/lib/src/base_response.dart +++ b/pkgs/http/lib/src/base_response.dart @@ -44,7 +44,7 @@ abstract class BaseResponse { /// ``` /// /// To retrieve the header values as a `List`, use - /// [HeadersWithFieldLists.headersFieldValueList]. + /// [HeadersWithSplitValues.headersSplitValues]. /// /// If a header value contains whitespace then that whitespace may be replaced /// by a single space. Leading and trailing whitespace in header values are @@ -71,36 +71,36 @@ abstract class BaseResponse { } } -// "token" as defined in RFC 2616, 2.2 -// See https://datatracker.ietf.org/doc/html/rfc2616#section-2.2 +/// "token" as defined in RFC 2616, 2.2 +/// See https://datatracker.ietf.org/doc/html/rfc2616#section-2.2 const _tokenChars = r"!#$%&'*+\-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`" 'abcdefghijklmnopqrstuvwxyz|~'; -// Splits comma-seperated header values. +/// Splits comma-seperated header values. var _headerSplitter = RegExp(r'[ \t]*,[ \t]*'); -// Splits comma-seperated "Set-Cookie" header values. -// -// Set-Cookie strings can contain commas. In particular, the following -// productions defined in RFC-6265, section 4.1.1: -// - e.g. "Expires=Sun, 06 Nov 1994 08:49:37 GMT" -// - e.g. "Path=somepath," -// - e.g. "AnyString,Really," -// -// Some values are ambiguous e.g. -// "Set-Cookie: lang=en; Path=/foo/" -// "Set-Cookie: SID=x23" -// and: -// "Set-Cookie: lang=en; Path=/foo/,SID=x23" -// would both be result in `response.headers` => "lang=en; Path=/foo/,SID=x23" -// -// The idea behind this regex is that ",=" is more likely to -// start a new then be part of or . -// -// See https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1 +/// Splits comma-seperated "Set-Cookie" header values. +/// +/// Set-Cookie strings can contain commas. In particular, the following +/// productions defined in RFC-6265, section 4.1.1: +/// - e.g. "Expires=Sun, 06 Nov 1994 08:49:37 GMT" +/// - e.g. "Path=somepath," +/// - e.g. "AnyString,Really," +/// +/// Some values are ambiguous e.g. +/// "Set-Cookie: lang=en; Path=/foo/" +/// "Set-Cookie: SID=x23" +/// and: +/// "Set-Cookie: lang=en; Path=/foo/,SID=x23" +/// would both be result in `response.headers` => "lang=en; Path=/foo/,SID=x23" +/// +/// The idea behind this regex is that ",=" is more likely to +/// start a new then be part of or . +/// +/// See https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1 var _setCookieSplitter = RegExp(r'[ \t]*,[ \t]*(?=[' + _tokenChars + r']+=)'); -extension HeadersWithFieldLists on BaseResponse { +extension HeadersWithSplitValues on BaseResponse { /// The HTTP headers returned by the server. /// /// The header names are converted to lowercase and stored with their @@ -116,10 +116,10 @@ extension HeadersWithFieldLists on BaseResponse { /// final response = await Client().get(Uri.https('example.com', '/')); /// final cookies = [ /// for (var value i - /// in response.headersFieldValueList['set-cookie'] ?? []) + /// in response.headersSplitValues['set-cookie'] ?? []) /// Cookie.fromSetCookieValue(value) /// ]; - Map> get headersFieldValueList { + Map> get headersSplitValues { var headersWithFieldLists = >{}; headers.forEach((key, value) { if (!value.contains(',')) { diff --git a/pkgs/http/test/response_test.dart b/pkgs/http/test/response_test.dart index aa38f4a143..1bd9fd8e38 100644 --- a/pkgs/http/test/response_test.dart +++ b/pkgs/http/test/response_test.dart @@ -71,16 +71,16 @@ void main() { }); }); - group('.headersFieldValueList', () { + group('.headersSplitValues', () { test('no headers', () async { var response = http.Response('Hello, world!', 200); - expect(response.headersFieldValueList, const >{}); + expect(response.headersSplitValues, const >{}); }); test('one header', () async { var response = http.Response('Hello, world!', 200, headers: {'fruit': 'apple'}); - expect(response.headersFieldValueList, const { + expect(response.headersSplitValues, const { 'fruit': ['apple'] }); }); @@ -88,7 +88,7 @@ void main() { test('two headers', () async { var response = http.Response('Hello, world!', 200, headers: {'fruit': 'apple,banana'}); - expect(response.headersFieldValueList, const { + expect(response.headersSplitValues, const { 'fruit': ['apple', 'banana'] }); }); @@ -96,7 +96,7 @@ void main() { test('two headers with lots of spaces', () async { var response = http.Response('Hello, world!', 200, headers: {'fruit': 'apple \t , \tbanana'}); - expect(response.headersFieldValueList, const { + expect(response.headersSplitValues, const { 'fruit': ['apple', 'banana'] }); }); @@ -105,7 +105,7 @@ void main() { var response = http.Response('Hello, world!', 200, headers: { 'set-cookie': 'id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT' }); - expect(response.headersFieldValueList, const { + expect(response.headersSplitValues, const { 'set-cookie': ['id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT'] }); }); @@ -116,7 +116,7 @@ void main() { 'set-cookie': 'id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT,' 'sessionId=e8bb43229de9; Domain=foo.example.com' }); - expect(response.headersFieldValueList, const { + expect(response.headersSplitValues, const { 'set-cookie': [ 'id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT', 'sessionId=e8bb43229de9; Domain=foo.example.com' @@ -131,7 +131,7 @@ void main() { 'id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Path=/,,HE,=L=LO,' 'sessionId=e8bb43229de9; Domain=foo.example.com' }); - expect(response.headersFieldValueList, const { + expect(response.headersSplitValues, const { 'set-cookie': [ 'id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Path=/,,HE,=L=LO', 'sessionId=e8bb43229de9; Domain=foo.example.com'