feat(http): add new middleware feature

This commit is contained in:
2022-06-24 15:58:48 +02:00
parent 14cb3485e4
commit 81367e5f3a
42 changed files with 1021 additions and 2146 deletions
@@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:convert';
class Convert {
static String toHex(List<int> bytes, {bool upperCase = false}) {
final buffer = StringBuffer();
@@ -29,6 +31,15 @@ class Convert {
return buffer.toString();
}
}
static String mapToQuery(Map<String, String> map, {Encoding? encoding}) {
final pairs = <List<String>>[];
map.forEach((key, value) => pairs.add([
Uri.encodeQueryComponent(key, encoding: encoding ?? utf8),
Uri.encodeQueryComponent(value, encoding: encoding ?? utf8)
]),);
return pairs.map((pair) => '${pair[0]}=${pair[1]}').join('&');
}
}
extension UriX on Uri {
@@ -0,0 +1,36 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'dart:core';
import 'dart:math';
abstract class Delay {
static Duration getRetryDelay(int attempt) {
assert(attempt >= 0, 'attempt cannot be negative');
if (attempt <= 0) {
return Duration.zero;
}
final rand = Random();
final Duration delayFactor = const Duration(milliseconds: 200);
final double randomizationFactor = 0.25;
final Duration maxDelay = const Duration(seconds: 30);
final rf = randomizationFactor * (rand.nextDouble() * 2 - 1) + 1;
final exp = min(attempt, 31); // prevent overflows.
final delay = delayFactor * pow(2.0, exp) * rf;
return delay < maxDelay ? delay : maxDelay;
}
}
@@ -18,6 +18,5 @@ enum Protocols {
http,
https;
String get name => toString().split('.').last;
String get scheme => '$name://';
}
@@ -0,0 +1,89 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:http/http.dart';
abstract class RequestUtils {
static Request _copyNormalRequestWith(
Request original, {
String? method,
Uri? url,
Map<String, String>? headers,
int? maxRedirects,
bool? followRedirects,
bool? persistentConnection,
String? body,
}) {
final request = Request(method ?? original.method, url ?? original.url)
..followRedirects = followRedirects ?? original.followRedirects
..headers.addAll(headers ?? original.headers)
..maxRedirects = maxRedirects ?? original.maxRedirects
..persistentConnection =
persistentConnection ?? original.persistentConnection
..body = body ?? original.body;
return request;
}
static BaseRequest copyRequestWith(
BaseRequest original, {
String? method,
Uri? url,
Map<String, String>? headers,
int? maxRedirects,
bool? followRedirects,
bool? persistentConnection,
String? body,
}) {
if (original is Request) {
return _copyNormalRequestWith(
original,
method: method,
url: url,
headers: headers,
maxRedirects: maxRedirects,
followRedirects: followRedirects,
persistentConnection: persistentConnection,
body: body,
);
} else {
throw UnimplementedError(
'Cannot handle requests of type ${original.runtimeType}',
);
}
}
static Request _copyNormalRequest(Request original) {
final request = Request(original.method, original.url)
..followRedirects = original.followRedirects
..headers.addAll(original.headers)
..maxRedirects = original.maxRedirects
..persistentConnection = original.persistentConnection
..body = original.body;
return request;
}
static BaseRequest copyRequest(BaseRequest original) {
if (original is Request) {
return _copyNormalRequest(original);
} else {
throw UnimplementedError(
'Cannot handle requests of type ${original.runtimeType}',
);
}
}
}
@@ -1,89 +1,23 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:http/http.dart';
abstract class Utils {
static Request _copyNormalRequest(Request original) {
final request = Request(original.method, original.url)
..followRedirects = original.followRedirects
..headers.addAll(original.headers)
..maxRedirects = original.maxRedirects
..persistentConnection = original.persistentConnection
..body = original.body;
return request;
}
static Request _copyNormalRequestWith(
Request original, {
String? method,
Uri? url,
Map<String, String>? headers,
int? maxRedirects,
bool? followRedirects,
bool? persistentConnection,
String? body,
}) {
final request = Request(method ?? original.method, url ?? original.url)
..followRedirects = followRedirects ?? original.followRedirects
..headers.addAll(headers ?? original.headers)
..maxRedirects = maxRedirects ?? original.maxRedirects
..persistentConnection =
persistentConnection ?? original.persistentConnection
..body = body ?? original.body;
return request;
}
static BaseRequest copyRequest(BaseRequest original) {
if (original is Request) {
return _copyNormalRequest(original);
} else {
throw UnimplementedError(
'Cannot handle requests of type ${original.runtimeType}',
);
}
}
static BaseRequest copyRequestWith(
BaseRequest original, {
String? method,
Uri? url,
Map<String, String>? headers,
int? maxRedirects,
bool? followRedirects,
bool? persistentConnection,
String? body,
}) {
if (original is Request) {
return _copyNormalRequestWith(
original,
method: method,
url: url,
headers: headers,
maxRedirects: maxRedirects,
followRedirects: followRedirects,
persistentConnection: persistentConnection,
body: body,
);
} else {
throw UnimplementedError(
'Cannot handle requests of type ${original.runtimeType}',
);
}
}
}
export 'authentication_methods.dart';
export 'digest_auth.dart';
export 'header_keys.dart';
export 'http_methods.dart';
export 'http_status.dart';
export 'protocols.dart';
export 'request_utils.dart';