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 33 : http.StreamedResponse replaceStream( 24 : http.StreamedResponse base, Stream<List<int>> stream) => 25 33 : http.StreamedResponse( 26 33 : http.ByteStream(stream), 27 33 : base.statusCode, 28 33 : contentLength: base.contentLength, 29 33 : request: base.request, 30 33 : headers: base.headers, 31 33 : isRedirect: base.isRedirect, 32 33 : persistentConnection: base.persistentConnection, 33 33 : reasonPhrase: base.reasonPhrase, 34 : ); 35 : 36 : /// Http Client that enforces a timeout on requests. 37 : /// Timeout calculation is done in a subclass. 38 : abstract class TimeoutHttpClient extends http.BaseClient { 39 38 : TimeoutHttpClient(this.inner); 40 : 41 : http.Client inner; 42 : 43 : Duration get timeout; 44 : 45 33 : @override 46 : Future<http.StreamedResponse> send(http.BaseRequest request) async { 47 66 : final response = await inner.send(request); 48 132 : return replaceStream(response, response.stream.timeout(timeout)); 49 : } 50 : } 51 : 52 : class FixedTimeoutHttpClient extends TimeoutHttpClient { 53 38 : FixedTimeoutHttpClient(super.inner, this.timeout); 54 : @override 55 : Duration timeout; 56 : }