Skip to content

Commit

Permalink
🔄️ Turn the world up-side-down
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Oct 24, 2023
1 parent c8d75d4 commit 74e4d07
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class IOHttpClientAdapter implements HttpClientAdapter {
request.headers.set(
key,
value,
preserveHeaderCase: !options.caseInsensitiveHeaders,
preserveHeaderCase: options.caseSensitiveHeaders,
);
}
});
Expand Down
10 changes: 8 additions & 2 deletions dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,10 @@ abstract class DioMixin implements Dio {
stream,
cancelToken?.whenCancel,
);
final headers = Headers.fromMap(responseBody.headers);
final headers = Headers.fromMap(
responseBody.headers,
caseSensitive: reqOpt.caseSensitiveHeaders,
);
// Make sure headers and [ResponseBody.headers] are the same instance.
responseBody.headers = headers.map;
final ret = Response<dynamic>(
Expand Down Expand Up @@ -707,7 +710,10 @@ abstract class DioMixin implements Dio {
final T? data = response.data as T?;
final Headers headers;
if (data is ResponseBody) {
headers = Headers.fromMap(data.headers);
headers = Headers.fromMap(
data.headers,
caseSensitive: requestOptions.caseSensitiveHeaders,
);
} else {
headers = response.headers;
}
Expand Down
16 changes: 5 additions & 11 deletions dio/lib/src/headers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ typedef HeaderForEachCallback = void Function(String name, List<String> values);
/// The headers class for requests and responses.
class Headers {
Headers({
this.caseInsensitive = true,
this.caseSensitive = false,
}) : _map = caseInsensitiveKeyMap<List<String>>();

/// Create the [Headers] from a [Map] instance.
Headers.fromMap(
Map<String, List<String>> map, {
this.caseInsensitive = true,
this.caseSensitive = false,
}) : _map = caseInsensitiveKeyMap<List<String>>(
map.map((k, v) => MapEntry(k.trim(), v)),
);
Expand All @@ -32,10 +32,10 @@ class Headers {

static final jsonMimeType = MediaType.parse(jsonContentType);

/// Whether the header key should be case-insensitive.
/// Whether the header key should be case-sensitive.
///
/// Defaults to true.
final bool caseInsensitive;
/// Defaults to false.
final bool caseSensitive;

final Map<String, List<String>> _map;

Expand All @@ -44,9 +44,6 @@ class Headers {
/// Returns the list of values for the header named [name]. If there
/// is no header with the provided name, [:null:] will be returned.
List<String>? operator [](String name) {
if (caseInsensitive) {
name = name.toLowerCase();
}
return _map[name.trim()];
}

Expand Down Expand Up @@ -75,9 +72,6 @@ class Headers {
/// cleared before the value [value] is added as its value.
void set(String name, dynamic value) {
if (value == null) return;
if (caseInsensitive) {
name = name.toLowerCase();
}
name = name.trim();
if (value is List) {
_map[name] = value.map<String>((e) => '$e').toList();
Expand Down
45 changes: 22 additions & 23 deletions dio/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class BaseOptions extends _RequestConfig with OptionsMixin {
Map<String, dynamic>? queryParameters,
Map<String, dynamic>? extra,
Map<String, dynamic>? headers,
bool? caseInsensitiveHeaders,
bool caseSensitiveHeaders = false,
ResponseType? responseType = ResponseType.json,
String? contentType,
ValidateStatus? validateStatus,
Expand All @@ -147,7 +147,7 @@ class BaseOptions extends _RequestConfig with OptionsMixin {
sendTimeout: sendTimeout,
extra: extra,
headers: headers,
caseInsensitiveHeaders: caseInsensitiveHeaders,
caseSensitiveHeaders: caseSensitiveHeaders,
responseType: responseType,
contentType: contentType,
validateStatus: validateStatus,
Expand Down Expand Up @@ -175,7 +175,7 @@ class BaseOptions extends _RequestConfig with OptionsMixin {
Duration? sendTimeout,
Map<String, Object?>? extra,
Map<String, Object?>? headers,
bool? caseInsensitiveHeaders,
bool? caseSensitiveHeaders,
ResponseType? responseType,
String? contentType,
ValidateStatus? validateStatus,
Expand All @@ -196,8 +196,8 @@ class BaseOptions extends _RequestConfig with OptionsMixin {
sendTimeout: sendTimeout ?? this.sendTimeout,
extra: extra ?? Map.from(this.extra),
headers: headers ?? Map.from(this.headers),
caseInsensitiveHeaders:
caseInsensitiveHeaders ?? this.caseInsensitiveHeaders,
caseSensitiveHeaders:
caseSensitiveHeaders ?? this.caseSensitiveHeaders,
responseType: responseType ?? this.responseType,
contentType: contentType ?? this.contentType,
validateStatus: validateStatus ?? this.validateStatus,
Expand All @@ -221,7 +221,7 @@ class Options {
Duration? receiveTimeout,
this.extra,
this.headers,
this.caseInsensitiveHeaders,
this.caseSensitiveHeaders,
this.responseType,
this.contentType,
this.validateStatus,
Expand All @@ -244,7 +244,7 @@ class Options {
Duration? receiveTimeout,
Map<String, Object?>? extra,
Map<String, Object?>? headers,
bool? caseInsensitiveHeaders,
bool? caseSensitiveHeaders,
ResponseType? responseType,
String? contentType,
ValidateStatus? validateStatus,
Expand Down Expand Up @@ -281,8 +281,8 @@ class Options {
receiveTimeout: receiveTimeout ?? this.receiveTimeout,
extra: extra ?? effectiveExtra,
headers: headers ?? effectiveHeaders,
caseInsensitiveHeaders:
caseInsensitiveHeaders ?? this.caseInsensitiveHeaders,
caseSensitiveHeaders:
caseSensitiveHeaders ?? this.caseSensitiveHeaders,
responseType: responseType ?? this.responseType,
contentType: contentType ?? this.contentType,
validateStatus: validateStatus ?? this.validateStatus,
Expand Down Expand Up @@ -331,8 +331,8 @@ class Options {
baseUrl: baseOpt.baseUrl,
path: path,
data: data,
caseInsensitiveHeaders:
caseInsensitiveHeaders ?? baseOpt.caseInsensitiveHeaders,
caseSensitiveHeaders:
caseSensitiveHeaders ?? baseOpt.caseSensitiveHeaders,
sourceStackTrace: sourceStackTrace ?? StackTrace.current,
connectTimeout: baseOpt.connectTimeout,
sendTimeout: sendTimeout ?? baseOpt.sendTimeout,
Expand Down Expand Up @@ -367,15 +367,14 @@ class Options {
/// e.g.: `content-type` and `Content-Type` will be treated as the same key.
Map<String, dynamic>? headers;

/// Whether the header keys should be case-insensitive.
/// e.g.: `Content-Type` will be sent as `content-type` eventually.
/// Whether the header keys should be case-sensitive.
///
/// Defaults to true.
/// Defaults to false.
///
/// This option WILL NOT take effect on these circumstances:
/// - XHR ([HttpRequest]) does not support handling this explicitly.
/// - The HTTP/2 standard only supports lowercase header keys.
bool? caseInsensitiveHeaders;
bool? caseSensitiveHeaders;

/// Timeout when sending data.
///
Expand Down Expand Up @@ -493,7 +492,7 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
String? baseUrl,
Map<String, dynamic>? extra,
Map<String, dynamic>? headers,
bool caseInsensitiveHeaders = true,
bool? caseSensitiveHeaders,
ResponseType? responseType,
String? contentType,
ValidateStatus? validateStatus,
Expand All @@ -513,7 +512,7 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
receiveTimeout: receiveTimeout,
extra: extra,
headers: headers,
caseInsensitiveHeaders: caseInsensitiveHeaders,
caseSensitiveHeaders: caseSensitiveHeaders,
responseType: responseType,
contentType: contentType,
validateStatus: validateStatus,
Expand Down Expand Up @@ -546,7 +545,7 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
CancelToken? cancelToken,
Map<String, dynamic>? extra,
Map<String, dynamic>? headers,
bool? caseInsensitiveHeaders,
bool? caseSensitiveHeaders,
ResponseType? responseType,
String? contentType,
ValidateStatus? validateStatus,
Expand Down Expand Up @@ -583,8 +582,8 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
cancelToken: cancelToken ?? this.cancelToken,
extra: extra ?? Map.from(this.extra),
headers: headers ?? Map.from(this.headers),
caseInsensitiveHeaders:
caseInsensitiveHeaders ?? this.caseInsensitiveHeaders,
caseSensitiveHeaders:
caseSensitiveHeaders ?? this.caseSensitiveHeaders,
responseType: responseType ?? this.responseType,
validateStatus: validateStatus ?? this.validateStatus,
receiveDataWhenStatusError:
Expand Down Expand Up @@ -662,7 +661,7 @@ class _RequestConfig {
String? method,
Map<String, dynamic>? extra,
Map<String, dynamic>? headers,
bool? caseInsensitiveHeaders,
bool? caseSensitiveHeaders,
String? contentType,
ListFormat? listFormat,
bool? followRedirects,
Expand All @@ -678,7 +677,7 @@ class _RequestConfig {
assert(sendTimeout == null || !sendTimeout.isNegative),
_sendTimeout = sendTimeout,
method = method ?? 'GET',
caseInsensitiveHeaders = caseInsensitiveHeaders ?? true,
caseSensitiveHeaders = caseSensitiveHeaders ?? false,
listFormat = listFormat ?? ListFormat.multi,
extra = extra ?? {},
followRedirects = followRedirects ?? true,
Expand Down Expand Up @@ -718,7 +717,7 @@ class _RequestConfig {
}
}

late bool caseInsensitiveHeaders;
late bool caseSensitiveHeaders;

Duration? get sendTimeout => _sendTimeout;
Duration? _sendTimeout;
Expand Down
3 changes: 2 additions & 1 deletion dio/lib/src/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Response<T> {
this.redirects = const [],
Map<String, dynamic>? extra,
Headers? headers,
}) : headers = headers ?? Headers(),
}) : headers = headers ??
Headers(caseSensitive: requestOptions.caseSensitiveHeaders),
extra = extra ?? <String, dynamic>{};

/// The response payload in specific type.
Expand Down
4 changes: 3 additions & 1 deletion dio/test/basic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ void main() {
'content-length': ['200'],
'Test': ['1', '2'],
},
caseInsensitive: false,
caseSensitive: true,
);
expect(headers['SET-COOKIE']?.length, 2);
// Although it's case-sensitive, we still use case-insensitive map.
expect(headers['set-cookie']?.length, 2);
expect(headers['content-length']?.length, 1);
expect(headers['Test']?.length, 2);
});
Expand Down

0 comments on commit 74e4d07

Please sign in to comment.