addChunkedEncoding top-level property

Middleware addChunkedEncoding
final

Middleware that adds chunked transfer coding to responses if none of the following conditions are true:

  • A Content-Length header is provided.
  • The Content-Type header indicates the MIME type multipart/byteranges.
  • The Transfer-Encoding header already includes the chunked coding.

This is intended for use by Shelf adapters rather than end-users.

Implementation

final addChunkedEncoding = createMiddleware(responseHandler: (response) {
  if (response.contentLength != null) return response;
  if (response.statusCode < 200) return response;
  if (response.statusCode == 204) return response;
  if (response.statusCode == 304) return response;
  if (response.mimeType == 'multipart/byteranges') return response;

  // We only check the last coding here because HTTP requires that the chunked
  // encoding be listed last.
  var coding = response.headers['transfer-encoding'];
  if (coding != null && !equalsIgnoreAsciiCase(coding, 'identity')) {
    return response;
  }

  return response.change(
      headers: {'transfer-encoding': 'chunked'},
      body: chunkedCoding.encoder.bind(response.read()));
});