head method

Future<int> head(
  1. Uri uri, {
  2. Map<String, Object>? headers,
})

Sends a HEAD request to get the content length of the resource at uri.

Returns the content length as an int.

Implementation

Future<int> head(Uri uri, {Map<String, Object>? headers}) async {
  HttpClient client = VideoProxy.httpClientBuilderImpl.create();
  HttpClientRequest request = await client.headUrl(uri);
  if (headers != null) {
    headers.forEach((key, value) {
      if (key == 'host' && value == Config.serverUrl) return;
      request.headers.set(key, value);
    });
  }
  HttpClientResponse response = await request.close();
  client.close();
  // Get content-length from content-range, if failed get from content-length
  String? contentRange =
      response.headers.value(HttpHeaders.contentRangeHeader);
  if (contentRange != null) {
    final match = RegExp(r'bytes (\d+)-(\d+)/(\d+)').firstMatch(contentRange);
    if (match != null && match.group(3) != null) {
      String total = match.group(3)!;
      if (total.isNotEmpty && total != '0') {
        return int.parse(total);
      }
    }
  }
  return response.contentLength;
}