httpResponseHandler function

dynamic httpResponseHandler(
  1. dynamic payload,
  2. HttpRequest request
)

Implementation

dynamic httpResponseHandler(dynamic payload, HttpRequest request) {
  /// Websocket handler will return websocket payload
  /// and in this case we need to remain the connection open for
  /// websocket communication. So there is no `res.close()` required.
  if (payload is WebSocket) {
    return;
  }

  ResponseHandlerInterface? customResponseHandler = DoxServer().responseHandler;

  /// a plain value (text, map, list...) with no custom handler needs no
  /// DoxResponse: it would set no header and no status. files keep the
  /// DoxResponse path, which sets their content type.
  if (customResponseHandler == null &&
      payload is! DoxResponse &&
      payload is! File &&
      payload is! StreamFile &&
      payload is! DownloadableFile) {
    responseDataHandler(payload, request);
    return;
  }

  DoxResponse doxResponse = payload is DoxResponse
      ? payload
      : DoxResponse(payload);

  /// If there is responseHandler set, handle responseHandler
  /// before DoxResponse process. The responseHandler will return
  /// DoxResponse to process
  if (customResponseHandler != null) {
    doxResponse = customResponseHandler.handle(doxResponse);
  }

  /// DoxResponse have process function
  /// which will set header and status code in the response
  /// and will pass payload/content to `responseDataHandler` function.
  doxResponse.process(request);
}