send method

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

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] = List<String>.of(values);
    }

    ioResponse.headers.forEach(setHeaders);

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

  FutureOr<Response> Function(Request) handle;

  if (_pipeline case var pipeline?) {
    handle = pipeline.addHandler(handler);
  } else {
    handle = handler;
  }

  return await handle(request);
}