in lib/shelf_io.dart [182:231]
Future _writeResponse(Response response, HttpResponse httpResponse) {
if (response.context.containsKey('shelf.io.buffer_output')) {
httpResponse.bufferOutput =
response.context['shelf.io.buffer_output'] as bool;
}
httpResponse.statusCode = response.statusCode;
// An adapter must not add or modify the `Transfer-Encoding` parameter, but
// the Dart SDK sets it by default. Set this before we fill in
// [response.headers] so that the user or Shelf can explicitly override it if
// necessary.
httpResponse.headers.chunkedTransferEncoding = false;
response.headersAll.forEach((header, value) {
httpResponse.headers.set(header, value);
});
var coding = response.headers['transfer-encoding'];
if (coding != null && !equalsIgnoreAsciiCase(coding, 'identity')) {
// If the response is already in a chunked encoding, de-chunk it because
// otherwise `dart:io` will try to add another layer of chunking.
//
// TODO(nweiz): Do this more cleanly when sdk#27886 is fixed.
response = response.change(
body: chunkedCoding.decoder.bind(response.read()),
);
httpResponse.headers.set(HttpHeaders.transferEncodingHeader, 'chunked');
} else if (response.statusCode >= 200 &&
response.statusCode != 204 &&
response.statusCode != 304 &&
response.contentLength == null &&
response.mimeType != 'multipart/byteranges') {
// If the response isn't chunked yet and there's no other way to tell its
// length, enable `dart:io`'s chunked encoding.
httpResponse.headers.set(HttpHeaders.transferEncodingHeader, 'chunked');
}
if (!response.headers.containsKey(HttpHeaders.serverHeader)) {
httpResponse.headers.set(HttpHeaders.serverHeader, 'dart:io with Shelf');
}
if (!response.headers.containsKey(HttpHeaders.dateHeader)) {
httpResponse.headers.date = DateTime.now().toUtc();
}
return httpResponse
.addStream(response.read())
.then((_) => httpResponse.close());
}