class Amber::Router::Parsers::BoundedIO

Overview

Wraps an IO and raises Amber::Exceptions::ParameterLimitExceeded once more than limit bytes have been consumed through it.

A segment limit can be opened on top of the total, for example around a multipart part's headers, so one long header line is rejected as soon as it passes the segment allowance.

Defined in:

amber/router/parsers/bounded_io.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(io : IO, limit : Int64, limit_name : String) #

[View source]

Instance Method Detail

def begin_segment(limit : Int, limit_name : String) : Nil #

Starts counting a segment that may consume at most limit bytes.


[View source]
def bytes_read : Int64 #

[View source]
def end_segment : Nil #

[View source]
def peek : Bytes | Nil #

Peeking does not consume, so it is not charged; #skip is. The peeked bytes are trimmed to one byte past the remaining allowance, so a reader that scans the peek buffer (such as gets) cannot copy more than that before its #skip trips the limit.


[View source]
def read(slice : Bytes) : Int32 #
Description copied from class IO

Reads at most slice.size bytes from this IO into slice. Returns the number of bytes read, which is 0 if and only if there is no more data to read (so checking for 0 is the way to detect end of file).

io = IO::Memory.new "hello"
slice = Bytes.new(4)
io.read(slice) # => 4
slice          # => Bytes[104, 101, 108, 108]
io.read(slice) # => 1
slice          # => Bytes[111, 101, 108, 108]
io.read(slice) # => 0

[View source]
def skip(bytes_count : Int) : Nil #
Description copied from class IO

Reads and discards exactly bytes_count bytes. Raises IO::EOFError if there aren't at least bytes_count bytes.

io = IO::Memory.new "hello world"
io.skip(6)
io.gets    # => "world"
io.skip(1) # raises IO::EOFError

[View source]
def write(slice : Bytes) : Nil #
Description copied from class IO

Writes the contents of slice into this IO.

io = IO::Memory.new
slice = Bytes.new(4) { |i| ('a'.ord + i).to_u8 }
io.write(slice)
io.to_s # => "abcd"

[View source]