determineContentTypes method

Stream<UrlInfoContentTypeResult> determineContentTypes()

Resolve content type from UrlInfo and export result.

The condition of returned ContentTypeCategory will be depended on given extendion from Uri.path first. If not offered, it will try to find Content-Type in HTTP response header by making HTTP HEAD request.

This features will not be availabled during test and UnsupportedError will be thrown if attempted.

Implementation

Stream<UrlInfoContentTypeResult> determineContentTypes() async* {
  Client c = MetaFetch.instance.createClient(false);

  try {
    for (var ui in this) {
      ContentTypeCategory httpCT;
      Set<ContentTypeCategory> httpExtCt = ui.url!.extensionContentType;

      if (httpExtCt.isNotEmpty) {
        httpCT = httpExtCt.first;
      } else {
        httpCT =
            await c.head(ui.url!).then((value) => value.contentTypeCategory);
      }

      ContentTypeCategory? httpsCT;
      if (ui.secureUrl != null) {
        Set<ContentTypeCategory> httpsExtCt =
            ui.secureUrl!.extensionContentType;
        if (httpsExtCt.isNotEmpty) {
          httpsCT = httpsExtCt.first;
        } else {
          httpsCT = await c
              .head(ui.secureUrl!)
              .then((value) => value.contentTypeCategory);
        }
      }

      yield UrlInfoContentTypeResult._(ui, httpCT, httpsCT);
    }
  } finally {
    c.close();
  }
}