Jason Law

Fixed timeout issue

... ... @@ -205,6 +205,7 @@ class GetHttpClient {
if (authenticate) await _modifier.authenticator!(request);
final newRequest = await _modifier.modifyRequest<T>(request);
_httpClient.timeout = timeout;
var response = await _httpClient.send<T>(newRequest);
final newResponse =
... ...
... ... @@ -23,6 +23,9 @@ class HttpRequestImpl implements HttpRequestBase {
///on different sites. The default is false
final bool withCredentials;
@override
Duration? timeout;
/// Sends an HTTP request and asynchronously returns the response.
@override
Future<Response<T>> send<T>(Request<T> request) async {
... ... @@ -30,6 +33,7 @@ class HttpRequestImpl implements HttpRequestBase {
html.HttpRequest xhr;
xhr = html.HttpRequest()
..timeout = timeout?.inMilliseconds
..open(request.method, '${request.url}', async: true); // check this
_xhrs.add(xhr);
... ...
... ... @@ -8,4 +8,12 @@ abstract class HttpRequestBase {
/// Closes the [Request] and cleans up any resources associated with it.
void close();
/// Gets and sets the timeout.
///
/// For mobile, this value will be applied for both connection and request
/// timeout.
///
/// For web, this value will be the request timeout.
Duration? timeout;
}
... ...
... ... @@ -34,16 +34,20 @@ class HttpRequestImpl extends HttpRequestBase {
@override
Future<Response<T>> send<T>(Request<T> request) async {
var stream = request.bodyBytes.asBroadcastStream();
io.HttpClientRequest? ioRequest;
try {
var ioRequest = (await _httpClient!.openUrl(request.method, request.url))
_httpClient!.connectionTimeout = timeout;
ioRequest = (await _httpClient!.openUrl(request.method, request.url))
..followRedirects = request.followRedirects
..persistentConnection = request.persistentConnection
..maxRedirects = request.maxRedirects
..contentLength = request.contentLength ?? -1;
request.headers.forEach(ioRequest.headers.set);
var response = await stream.pipe(ioRequest) as io.HttpClientResponse;
var response = timeout == null
? await stream.pipe(ioRequest) as io.HttpClientResponse
: await stream.pipe(ioRequest).timeout(timeout!)
as io.HttpClientResponse;
var headers = <String, String>{};
response.headers.forEach((key, values) {
... ... @@ -68,6 +72,9 @@ class HttpRequestImpl extends HttpRequestBase {
body: body,
bodyString: stringBody,
);
} on TimeoutException catch (_) {
ioRequest?.abort();
rethrow;
} on io.HttpException catch (error) {
throw GetHttpException(error.message, error.uri);
}
... ...