handleRelayBody function

Future<void> handleRelayBody(
  1. HttpRequest request, {
  2. required String? allowedOrigin,
  3. bool allowAnyHost = false,
  4. RelayLimits limits = relayDefaultLimits,
  5. void log(
    1. String line
    )?,
  6. void onUpstreamAbort()?,
})

The EXPENSIVE tail of handleRelayRequest — the body read and the upstream forward — everything that must run under a pool slot. The cheap verdicts (OPTIONS / bearer / origin) live in the hub's _relayGate, which runs BEFORE slot acquisition (issue #794 review): unauthenticated garbage never reaches this function through the hub.

Implementation

Future<void> handleRelayBody(
  HttpRequest request, {
  required String? allowedOrigin,
  bool allowAnyHost = false,
  RelayLimits limits = relayDefaultLimits,
  void Function(String line)? log,
  void Function()? onUpstreamAbort,
}) async {
  final parsed = await _relayEnvelope(request, allowedOrigin, limits, log: log);
  if (parsed == null) return;
  if (!relayDestinationAllowed(parsed.$1, allowAnyHost: allowAnyHost)) {
    request.response.headers.set(HttpHeaders.connectionHeader, 'close');
    _relayMarkRejection(request);
    request.response.statusCode = 403;
    _relayCors(request, allowedOrigin);
    request.response.write('{"error":"destination not allowed"}');
    await request.response.close();
    return;
  }
  await _relayForward(
    request,
    parsed.$1,
    parsed.$2,
    allowedOrigin,
    allowAnyHost: allowAnyHost,
    limits: limits,
    log: log,
    onUpstreamAbort: onUpstreamAbort,
  );
}