Future respond(Response aqueductResponse)

Sends a Response to this Request's client.

Do not invoke this method directly.

RequestControllers invoke this method to respond to this request.

Once this method has executed, the Request is no longer valid. All headers from aqueductResponse are added to the HTTP response. If aqueductResponse has a Response.body, this request will attempt to encode the body data according to the Content-Type in the aqueductResponse's Response.headers.

Source

Future respond(Response aqueductResponse) {
  respondDate = new DateTime.now().toUtc();

  _Reference<String> compressionType = new _Reference(null);
  var body = aqueductResponse.body;
  if (body is! Stream) {
    // Note: this pre-encodes the body in memory, such that encoding fails this will throw and we can return a 500
    // because we have yet to write to the response.
    body = _responseBodyBytes(aqueductResponse, compressionType);
  }

  response.statusCode = aqueductResponse.statusCode;
  aqueductResponse.headers?.forEach((k, v) {
    response.headers.add(k, v);
  });

  if (aqueductResponse.cachePolicy != null) {
    response.headers.add(HttpHeaders.CACHE_CONTROL, aqueductResponse.cachePolicy.headerValue);
  }

  if (body == null) {
    response.headers.removeAll(HttpHeaders.CONTENT_TYPE);
    return response.close();
  }

  response.headers.add(
      HttpHeaders.CONTENT_TYPE, aqueductResponse.contentType.toString());

  if (body is List) {
    if (compressionType.value != null) {
      response.headers.add(HttpHeaders.CONTENT_ENCODING, compressionType.value);
    }
    response.headers.add(HttpHeaders.CONTENT_LENGTH, body.length);

    response.add(body);

    return response.close();
  }

  // Otherwise, body is stream
  var bodyStream = _responseBodyStream(aqueductResponse, compressionType);
  if (compressionType.value != null) {
    response.headers.add(HttpHeaders.CONTENT_ENCODING, compressionType.value);
  }
  response.headers.add(HttpHeaders.TRANSFER_ENCODING, "chunked");

  return response.addStream(bodyStream).then((_) {
    return response.close();
  }).catchError((e, st) {
    throw new HTTPStreamingException(e, st);
  });
}