send method

  1. @override
Future<Response> send(
  1. Request request
)
override

Sends the given request and returns the response.

Implementation

@override
Future<Response> send(Request request) async {
  var client = _client;

  if (client == null) {
    throw Exception('Client is already closed.');
  }

  Future<Response> handler(Request request) async {
    var ioRequest = await client.openUrl(
      request.method,
      request.requestedUri,
    );

    ioRequest.contentLength = request.contentLength ?? -1;
    request.headers.forEach(ioRequest.headers.set);
    await ioRequest.addStream(request.read());

    var ioResponse = await ioRequest.close();
    var headers = <String, List<String>>{};

    void setHeaders(String name, List<String> values) {
      headers[name] = <String>[
        for (var value in values) value.trimRight(),
      ];
    }

    ioResponse.headers.forEach(setHeaders);

    return Response(
      ioResponse.statusCode,
      body: ioResponse,
      headers: headers,
      encoding: request.encoding,
    );
  }

  if (_pipeline case var pipeline?) {
    return await pipeline.addHandler(handler)(request);
  }

  return await handler(request);
}