net.dart 6.63 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

7 8
import 'package:meta/meta.dart';

9
import '../convert.dart';
10
import 'common.dart';
11
import 'file_system.dart';
12
import 'io.dart';
13
import 'logger.dart';
14
import 'platform.dart';
15 16

const int kNetworkProblemExitCode = 50;
17

18
typedef HttpClientFactory = HttpClient Function();
19

20 21
typedef UrlTunneller = Future<String> Function(String url);

22
/// If [httpClientFactory] is null, a default [HttpClient] is used.
23 24
class Net {
  Net({
25 26 27
    HttpClientFactory? httpClientFactory,
    required Logger logger,
    required Platform platform,
28
  }) :
29
    _httpClientFactory = httpClientFactory ?? (() => HttpClient()),
30 31
    _logger = logger,
    _platform = platform;
32

33
  final HttpClientFactory _httpClientFactory;
34

35 36 37 38 39 40 41 42 43 44 45 46
  final Logger _logger;

  final Platform _platform;

  /// Download a file from the given URL.
  ///
  /// If a destination file is not provided, returns the bytes.
  ///
  /// If a destination file is provided, streams the bytes to that file and
  /// returns an empty list.
  ///
  /// If [maxAttempts] is exceeded, returns null.
47 48 49 50
  Future<List<int>?> fetchUrl(Uri url, {
    int? maxAttempts,
    File? destFile,
    @visibleForTesting Duration? durationOverride,
51 52 53 54 55
  }) async {
    int attempts = 0;
    int durationSeconds = 1;
    while (true) {
      attempts += 1;
56
      _MemoryIOSink? memorySink;
57 58 59 60 61 62 63 64 65 66 67 68 69
      IOSink sink;
      if (destFile == null) {
        memorySink = _MemoryIOSink();
        sink = memorySink;
      } else {
        sink = destFile.openWrite();
      }

      final bool result = await _attempt(
        url,
        destSink: sink,
      );
      if (result) {
70
        return memorySink?.writes.takeBytes() ?? <int>[];
71 72 73 74 75 76 77 78 79 80
      }

      if (maxAttempts != null && attempts >= maxAttempts) {
        _logger.printStatus('Download failed -- retry $attempts');
        return null;
      }
      _logger.printStatus(
        'Download failed -- attempting retry $attempts in '
        '$durationSeconds second${ durationSeconds == 1 ? "" : "s"}...',
      );
81
      await Future<void>.delayed(durationOverride ?? Duration(seconds: durationSeconds));
82 83 84
      if (durationSeconds < 64) {
        durationSeconds *= 2;
      }
85
    }
86
  }
87

88 89 90 91 92
  /// Check if the given URL points to a valid endpoint.
  Future<bool> doesRemoteFileExist(Uri url) => _attempt(url, onlyHeaders: true);

  // Returns true on success and false on failure.
  Future<bool> _attempt(Uri url, {
93
    IOSink? destSink,
94 95 96 97
    bool onlyHeaders = false,
  }) async {
    assert(onlyHeaders || destSink != null);
    _logger.printTrace('Downloading: $url');
98
    final HttpClient httpClient = _httpClientFactory();
99
    HttpClientRequest request;
100
    HttpClientResponse? response;
101 102 103 104 105 106 107 108
    try {
      if (onlyHeaders) {
        request = await httpClient.headUrl(url);
      } else {
        request = await httpClient.getUrl(url);
      }
      response = await request.close();
    } on ArgumentError catch (error) {
109
      final String? overrideUrl = _platform.environment['FLUTTER_STORAGE_BASE_URL'];
110 111 112 113 114 115 116 117 118 119 120 121 122
      if (overrideUrl != null && url.toString().contains(overrideUrl)) {
        _logger.printError(error.toString());
        throwToolExit(
          'The value of FLUTTER_STORAGE_BASE_URL ($overrideUrl) could not be '
          'parsed as a valid url. Please see https://flutter.dev/community/china '
          'for an example of how to use it.\n'
          'Full URL: $url',
          exitCode: kNetworkProblemExitCode,);
      }
      _logger.printError(error.toString());
      rethrow;
    } on HandshakeException catch (error) {
      _logger.printTrace(error.toString());
123
      throwToolExit(
124 125 126
        'Could not authenticate download server. You may be experiencing a man-in-the-middle attack,\n'
        'your network may be compromised, or you may have malware installed on your computer.\n'
        'URL: $url',
127 128
        exitCode: kNetworkProblemExitCode,
      );
129 130 131 132 133 134 135 136 137 138 139 140
    } on SocketException catch (error) {
      _logger.printTrace('Download error: $error');
      return false;
    } on HttpException catch (error) {
      _logger.printTrace('Download error: $error');
      return false;
    }
    assert(response != null);

    // If we're making a HEAD request, we're only checking to see if the URL is
    // valid.
    if (onlyHeaders) {
141
      return response.statusCode == HttpStatus.ok;
142
    }
143
    if (response.statusCode != HttpStatus.ok) {
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      if (response.statusCode > 0 && response.statusCode < 500) {
        throwToolExit(
          'Download failed.\n'
          'URL: $url\n'
          'Error: ${response.statusCode} ${response.reasonPhrase}',
          exitCode: kNetworkProblemExitCode,
        );
      }
      // 5xx errors are server errors and we can try again
      _logger.printTrace('Download error: ${response.statusCode} ${response.reasonPhrase}');
      return false;
    }
    _logger.printTrace('Received response from server, collecting bytes...');
    try {
      assert(destSink != null);
159
      await response.forEach(destSink!.add);
160 161 162 163 164 165 166
      return true;
    } on IOException catch (error) {
      _logger.printTrace('Download error: $error');
      return false;
    } finally {
      await destSink?.flush();
      await destSink?.close();
167
    }
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  }
}

/// An IOSink that collects whatever is written to it.
class _MemoryIOSink implements IOSink {
  @override
  Encoding encoding = utf8;

  final BytesBuilder writes = BytesBuilder(copy: false);

  @override
  void add(List<int> data) {
    writes.add(data);
  }

  @override
  Future<void> addStream(Stream<List<int>> stream) {
    final Completer<void> completer = Completer<void>();
    stream.listen(add).onDone(completer.complete);
    return completer.future;
  }

  @override
  void writeCharCode(int charCode) {
    add(<int>[charCode]);
  }

  @override
196
  void write(Object? obj) {
197
    add(encoding.encode('$obj'));
198
  }
199 200

  @override
201
  void writeln([ Object? obj = '' ]) {
202 203 204 205 206 207
    add(encoding.encode('$obj\n'));
  }

  @override
  void writeAll(Iterable<dynamic> objects, [ String separator = '' ]) {
    bool addSeparator = false;
208
    for (final dynamic object in objects) {
209 210 211 212 213 214 215 216 217
      if (addSeparator) {
        write(separator);
      }
      write(object);
      addSeparator = true;
    }
  }

  @override
218
  void addError(dynamic error, [ StackTrace? stackTrace ]) {
219 220 221 222 223 224 225 226 227 228 229
    throw UnimplementedError();
  }

  @override
  Future<void> get done => close();

  @override
  Future<void> close() async { }

  @override
  Future<void> flush() async { }
230
}
231 232 233 234 235 236 237 238 239 240

/// Returns [true] if [address] is an IPv6 address.
bool isIPv6Address(String address) {
  try {
    Uri.parseIPv6Address(address);
    return true;
  } on FormatException {
    return false;
  }
}