Line data Source code
1 : /*
2 : * Famedly Matrix SDK
3 : * Copyright (C) 2021 Famedly GmbH
4 : *
5 : * This program is free software: you can redistribute it and/or modify
6 : * it under the terms of the GNU Affero General Public License as
7 : * published by the Free Software Foundation, either version 3 of the
8 : * License, or (at your option) any later version.
9 : *
10 : * This program is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : * GNU Affero General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Affero General Public License
16 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 : */
18 :
19 : import 'dart:async';
20 :
21 : import 'package:http/http.dart' as http;
22 :
23 35 : http.StreamedResponse replaceStream(
24 : http.StreamedResponse base,
25 : Stream<List<int>> stream,
26 : ) =>
27 35 : http.StreamedResponse(
28 35 : http.ByteStream(stream),
29 35 : base.statusCode,
30 35 : contentLength: base.contentLength,
31 35 : request: base.request,
32 35 : headers: base.headers,
33 35 : isRedirect: base.isRedirect,
34 35 : persistentConnection: base.persistentConnection,
35 35 : reasonPhrase: base.reasonPhrase,
36 : );
37 :
38 : /// Http Client that enforces a timeout on requests.
39 : /// Timeout calculation is done in a subclass.
40 : abstract class TimeoutHttpClient extends http.BaseClient {
41 39 : TimeoutHttpClient(this.inner);
42 :
43 : http.Client inner;
44 :
45 : Duration get timeout;
46 :
47 35 : @override
48 : Future<http.StreamedResponse> send(http.BaseRequest request) async {
49 70 : final response = await inner.send(request);
50 140 : return replaceStream(response, response.stream.timeout(timeout));
51 : }
52 : }
53 :
54 : class FixedTimeoutHttpClient extends TimeoutHttpClient {
55 39 : FixedTimeoutHttpClient(super.inner, this.timeout);
56 : @override
57 : Duration timeout;
58 : }
|