readBytes static method
read the whole body, but stop with 413 once it passes max bytes.
Implementation
static Future<Uint8List> readBytes(HttpRequest request, int max) async {
_checkContentLength(request, max);
/// the native server holds the whole body already, or streams a big
/// one: [NativeRequest.readBody] puts its parts in one buffer of
/// native memory, so a big body is no big list on the Dart heap.
if (request is NativeRequest) {
try {
return await request.readBody(max: max);
} on BodyTooLargeException {
throw PayloadTooLargeException();
} on HttpException {
throw BadRequestException(
message: 'The request body did not come whole',
);
}
}
BytesBuilder builder = BytesBuilder(copy: false);
await for (List<int> chunk in limit(request, max)) {
builder.add(chunk);
}
return builder.takeBytes();
}