limit static method

Stream<List<int>> limit(
  1. Stream<List<int>> body,
  2. int max
)

pass the body through, but throw 413 once it passes max bytes. this catches clients that lie about or skip Content-Length.

Implementation

static Stream<List<int>> limit(Stream<List<int>> body, int max) async* {
  int total = 0;
  await for (List<int> chunk in body) {
    total += chunk.length;
    if (total > max) throw PayloadTooLargeException();
    yield chunk;
  }
}