Skip to content

Commit

Permalink
🚀 options
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Oct 23, 2023
1 parent 7430df2 commit c8d75d4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
10 changes: 8 additions & 2 deletions dio/lib/src/adapters/io_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,14 @@ class IOHttpClientAdapter implements HttpClientAdapter {
}

// Set Headers
options.headers.forEach((k, v) {
if (v != null) request.headers.set(k, v);
options.headers.forEach((key, value) {
if (value != null) {
request.headers.set(
key,
value,
preserveHeaderCase: !options.caseInsensitiveHeaders,
);
}
});
} on SocketException catch (e) {
if (e.message.contains('timed out')) {
Expand Down
34 changes: 32 additions & 2 deletions dio/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class BaseOptions extends _RequestConfig with OptionsMixin {
Map<String, dynamic>? queryParameters,
Map<String, dynamic>? extra,
Map<String, dynamic>? headers,
bool? caseInsensitiveHeaders,
ResponseType? responseType = ResponseType.json,
String? contentType,
ValidateStatus? validateStatus,
Expand All @@ -146,6 +147,7 @@ class BaseOptions extends _RequestConfig with OptionsMixin {
sendTimeout: sendTimeout,
extra: extra,
headers: headers,
caseInsensitiveHeaders: caseInsensitiveHeaders,
responseType: responseType,
contentType: contentType,
validateStatus: validateStatus,
Expand Down Expand Up @@ -173,6 +175,7 @@ class BaseOptions extends _RequestConfig with OptionsMixin {
Duration? sendTimeout,
Map<String, Object?>? extra,
Map<String, Object?>? headers,
bool? caseInsensitiveHeaders,
ResponseType? responseType,
String? contentType,
ValidateStatus? validateStatus,
Expand All @@ -193,6 +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,
responseType: responseType ?? this.responseType,
contentType: contentType ?? this.contentType,
validateStatus: validateStatus ?? this.validateStatus,
Expand All @@ -216,6 +221,7 @@ class Options {
Duration? receiveTimeout,
this.extra,
this.headers,
this.caseInsensitiveHeaders,
this.responseType,
this.contentType,
this.validateStatus,
Expand All @@ -238,6 +244,7 @@ class Options {
Duration? receiveTimeout,
Map<String, Object?>? extra,
Map<String, Object?>? headers,
bool? caseInsensitiveHeaders,
ResponseType? responseType,
String? contentType,
ValidateStatus? validateStatus,
Expand Down Expand Up @@ -274,6 +281,8 @@ class Options {
receiveTimeout: receiveTimeout ?? this.receiveTimeout,
extra: extra ?? effectiveExtra,
headers: headers ?? effectiveHeaders,
caseInsensitiveHeaders:
caseInsensitiveHeaders ?? this.caseInsensitiveHeaders,
responseType: responseType ?? this.responseType,
contentType: contentType ?? this.contentType,
validateStatus: validateStatus ?? this.validateStatus,
Expand Down Expand Up @@ -322,6 +331,8 @@ class Options {
baseUrl: baseOpt.baseUrl,
path: path,
data: data,
caseInsensitiveHeaders:
caseInsensitiveHeaders ?? baseOpt.caseInsensitiveHeaders,
sourceStackTrace: sourceStackTrace ?? StackTrace.current,
connectTimeout: baseOpt.connectTimeout,
sendTimeout: sendTimeout ?? baseOpt.sendTimeout,
Expand Down Expand Up @@ -352,10 +363,20 @@ class Options {

/// HTTP request headers.
///
/// The keys of the header are case-insensitive,
/// e.g.: content-type and Content-Type will be treated as the same key.
/// The equality of the header keys is case-insensitive,
/// 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.
///
/// Defaults to true.
///
/// 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;

/// Timeout when sending data.
///
/// [Dio] will throw the [DioException] with
Expand Down Expand Up @@ -472,6 +493,7 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
String? baseUrl,
Map<String, dynamic>? extra,
Map<String, dynamic>? headers,
bool caseInsensitiveHeaders = true,
ResponseType? responseType,
String? contentType,
ValidateStatus? validateStatus,
Expand All @@ -491,6 +513,7 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
receiveTimeout: receiveTimeout,
extra: extra,
headers: headers,
caseInsensitiveHeaders: caseInsensitiveHeaders,
responseType: responseType,
contentType: contentType,
validateStatus: validateStatus,
Expand Down Expand Up @@ -523,6 +546,7 @@ class RequestOptions extends _RequestConfig with OptionsMixin {
CancelToken? cancelToken,
Map<String, dynamic>? extra,
Map<String, dynamic>? headers,
bool? caseInsensitiveHeaders,
ResponseType? responseType,
String? contentType,
ValidateStatus? validateStatus,
Expand Down Expand Up @@ -559,6 +583,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,
responseType: responseType ?? this.responseType,
validateStatus: validateStatus ?? this.validateStatus,
receiveDataWhenStatusError:
Expand Down Expand Up @@ -636,6 +662,7 @@ class _RequestConfig {
String? method,
Map<String, dynamic>? extra,
Map<String, dynamic>? headers,
bool? caseInsensitiveHeaders,
String? contentType,
ListFormat? listFormat,
bool? followRedirects,
Expand All @@ -651,6 +678,7 @@ class _RequestConfig {
assert(sendTimeout == null || !sendTimeout.isNegative),
_sendTimeout = sendTimeout,
method = method ?? 'GET',
caseInsensitiveHeaders = caseInsensitiveHeaders ?? true,
listFormat = listFormat ?? ListFormat.multi,
extra = extra ?? {},
followRedirects = followRedirects ?? true,
Expand Down Expand Up @@ -690,6 +718,8 @@ class _RequestConfig {
}
}

late bool caseInsensitiveHeaders;

Duration? get sendTimeout => _sendTimeout;
Duration? _sendTimeout;

Expand Down

0 comments on commit c8d75d4

Please sign in to comment.