calcResponseSizeBytes static method

int calcResponseSizeBytes(
  1. StreamedResponse response
)

Calculate the response size depending on body of the response. As the content is stream, it can't be consumed twice, which means we need a content-length header to actually calculate the real response size bytes.

Implementation

static int calcResponseSizeBytes(StreamedResponse response) {
  try {
    // 4 Bytes coming from empty line after first line and headers
    var responseLength = 4;
    responseLength +=
        'HTTP/1.1 ${response.statusCode} ${HttpStatusCodeConverter.getStatusMessage(response.statusCode)}'.length;

    if (response.contentLength != null) {
      responseLength += response.contentLength!;
    }
    return responseLength += getHeaderLength(response.headers);
  } catch (error) {
    final urlString = response.request != null ? response.request!.url.toString() : 'url not available';
    Logger().d(
      _tag,
      "HttpClientUtils calcResponseSizeBytes($urlString): can't calculate response size.",
      logType: LogType.Warning,
    );
    return -1;
  }
}