limit static method
pass the body through, but throw 413 once it passes max bytes.
this catches clients that lie about or skip Content-Length.
A body that stops part way (the client went away, or the native server already answered 413 or 408 for it) is a 400: the client's fault, so it is not reported as an app error.
Implementation
static Stream<List<int>> limit(Stream<List<int>> body, int max) async* {
int total = 0;
try {
await for (List<int> chunk in body) {
total += chunk.length;
if (total > max) throw PayloadTooLargeException();
yield chunk;
}
} on HttpException {
throw BadRequestException(message: 'The request body did not come whole');
}
}