Stream<List<int>> bytes

The stream of bytes to decode.

Source

@override
Stream<List<int>> get bytes {
  // If content-length is specified, then we can check it for maxSize
  // and just return the original stream.
  if (_hasContentLength) {
    if (_request.headers.contentLength > maxSize) {
      throw new HTTPBodyDecoderException(
          "entity length exceeds maximum",
          statusCode: HttpStatus.REQUEST_ENTITY_TOO_LARGE);
    }

    return _originalByteStream;
  }

  // If content-length is not specified (e.g., chunked),
  // then we need to check how many bytes we've read to ensure we haven't
  // crossed maxSize
  if (_bufferingController == null) {
    _bufferingController = new StreamController<List<int>>(sync: true);

    _originalByteStream.listen((chunk) {
      _bytesRead += chunk.length;
      if (_bytesRead > maxSize) {
        var exception = new HTTPBodyDecoderException(
            "entity length exceeds maximum",
            statusCode: HttpStatus.REQUEST_ENTITY_TOO_LARGE);
        _bufferingController.addError(exception);
        _bufferingController.close();
        return;
      }

      _bufferingController.add(chunk);
    }, onDone: () {
      _bufferingController.close();
    }, onError: (e, st) {
      if (!_bufferingController.isClosed) {
        _bufferingController.addError(e, st);
        _bufferingController.close();
      }
    }, cancelOnError: true);
  }

  return _bufferingController.stream;
}