toIOHandler function

Future<void> Function(HttpRequest request) toIOHandler(
  1. Spry app
)

Create a new Dart IO handler for the Spry application.

Implementation

Future<void> Function(HttpRequest request) toIOHandler(Spry app) {
  final handler = toHandler(app);

  return (httpRequest) async {
    final spryRequest = Request(
      method: httpRequest.method.toUpperCase(),
      uri: httpRequest.requestedUri,
      headers: _createSpryHeaders(httpRequest.headers),
      body: httpRequest,
    );
    final event = createEvent(app, spryRequest);
    final httpResponse = httpRequest.response;

    if (httpRequest.connectionInfo != null) {
      setClientAddress(event,
          '${httpRequest.connectionInfo?.remoteAddress.address}:${httpRequest.connectionInfo?.remotePort}');
    }
    _registerUpgrade(httpRequest, event);

    final spryResponse = await handler(event);
    if (event.get(_kUpgraded) == true) {
      return;
    }

    httpResponse.statusCode = spryResponse.status;
    httpResponse.reasonPhrase = spryResponse.statusText;
    _writeSpryHeaders(httpResponse, spryResponse.headers);

    if (spryResponse.body != null) {
      await httpResponse.addStream(spryResponse.body!);
    }

    await httpResponse.close();
  };
}