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 34 : http.StreamedResponse replaceStream( 24 : http.StreamedResponse base, Stream<List<int>> stream) => 25 34 : http.StreamedResponse( 26 34 : http.ByteStream(stream), 27 34 : base.statusCode, 28 34 : contentLength: base.contentLength, 29 34 : request: base.request, 30 34 : headers: base.headers, 31 34 : isRedirect: base.isRedirect, 32 34 : persistentConnection: base.persistentConnection, 33 34 : 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 39 : TimeoutHttpClient(this.inner); 40 : 41 : http.Client inner; 42 : 43 : Duration get timeout; 44 : 45 34 : @override 46 : Future<http.StreamedResponse> send(http.BaseRequest request) async { 47 68 : final response = await inner.send(request); 48 136 : return replaceStream(response, response.stream.timeout(timeout)); 49 : } 50 : } 51 : 52 : class FixedTimeoutHttpClient extends TimeoutHttpClient { 53 39 : FixedTimeoutHttpClient(super.inner, this.timeout); 54 : @override 55 : Duration timeout; 56 : }