responseDataHandler function
Handle response for different types of data such as string, stream, file, Model, List, encodable.
Implementation
void responseDataHandler(dynamic payload, HttpRequest request) {
HttpResponse res = request.response;
/// text is the usual body: send it before the type checks below. None
/// of them can match a String.
if (payload is String) {
_sendText(payload, res);
return;
}
if (payload == null) {
endResponse(res);
return;
}
/// file on disk: streamed, with Content-Length and Range support
if (payload is File) {
res.headers.set(_noSniffHeader, 'nosniff');
_pipe(request, _sendFile(payload, request));
return;
}
/// if payload is stream
if (payload is StreamFile) {
res.headers.contentType = payload.contentType;
_guardActiveFile(res, payload.contentType);
if (payload.length != null) res.contentLength = payload.length!;
_pipe(request, res.addStream(payload.stream));
return;
}
/// if payload is downloadable file,
/// we response contentDisposition header with stream data.
if (payload is DownloadableFile) {
res.headers.contentType = payload.contentType;
res.headers.add(FILE_DOWNLOAD_HEADER, payload.contentDisposition);
res.headers.set(_noSniffHeader, 'nosniff');
if (payload.length != null) res.contentLength = payload.length!;
_pipe(request, res.addStream(payload.stream));
return;
}
// if payload is stream, pipe it chunk by chunk (no buffering)
if (payload is Stream<List<int>>) {
_pipe(request, res.addStream(payload));
return;
}
/// if payload handle base Http Exception
/// we set http status from exception and return as map
/// which will parse into json and response as json
if (payload is IHttpException) {
res.statusCode = payload.code;
if (payload is HttpStatusException) {
payload.headers.forEach(res.headers.set);
}
payload = payload.toResponse();
}
/// the text of an error can hold SQL, file paths or hosts. send it only
/// in development. it is reported by the `ExceptionHandler` in every env.
if (payload is Exception || payload is Error) {
res.statusCode = HttpStatus.internalServerError;
payload = showErrors()
? payload.toString()
: InternalErrorException().toResponse();
}
if (payload is DateTime) {
payload = payload.toIso8601String();
}
List<int> bytes;
/// the response, when [bytes] is a part of the buffer of the JSON
/// encoder
NativeResponse? view;
/// if payload is Map or List, parse into json
/// and response as json
if (payload is String) {
// an error or a date that became text
_sendText(payload, res);
return;
} else if (payload is TextBytes) {
// a charset that is not UTF-8 encodes the text again, as a String
// body does
Encoding encoding = res.encoding;
bytes = encoding is Utf8Codec
? payload.bytes
: encoding.encode(utf8.decode(payload.bytes, allowMalformed: true));
} else if (payload is JsonBytes) {
bytes = payload.bytes;
res.headers.contentType = ContentType.json;
} else {
// The native response copies the body into the memory of the server
// when it ends, a few lines down. So the encoder does not make its
// own copy first.
if (res is NativeResponse) {
bytes = jsonBytesView(payload);
view = res;
} else {
bytes = JSON.bytes(payload);
res.headers.contentType = ContentType.json;
}
}
/// finally write and close http connection.
/// known length lets the client skip chunked decoding.
/// dart:io only gzips when the length is not set, so skip it then.
if (view == null) {
if (!Dox().config.compress) res.contentLength = bytes.length;
res.add(bytes);
endResponse(res);
return;
}
// The bytes are a part of the encoder buffer, which is lent to this
// response: until it is given back, any other JSON encode (also one
// from code that runs inside end(), like the toString() of a cookie)
// writes into a new buffer. The response copies the body when it ends.
// On every path out of here, also an error or a closed server, the
// response lets go of the bytes, and the buffer is given back.
try {
res.headers.contentType = ContentType.json;
if (!Dox().config.compress) res.contentLength = bytes.length;
res.add(bytes);
endResponse(res);
} finally {
view.takeBody();
giveBackJsonBytes(bytes);
}
}