Sha256: 2e09e8c8297cf1aac42715daf678825ca1ceb32d540658aac431a0049516b96e

Contents?: true

Size: 1.55 KB

Versions: 26

Compression:

Stored size: 1.55 KB

Contents

# -*- encoding: binary -*-
# :enddoc:
#
# This is only used for chunked request bodies, which are rare
class Rainbows::MaxBody::Wrapper
  def initialize(rack_input, limit)
    @input, @limit, @rbuf = rack_input, limit, ''
  end

  def each
    while line = gets
      yield line
    end
  end

  # chunked encoding means this method behaves more like readpartial,
  # since Rack does not support a method named "readpartial"
  def read(length = nil, rv = '')
    if length
      if length <= @rbuf.size
        length < 0 and raise ArgumentError, "negative length #{length} given"
        rv.replace(@rbuf.slice!(0, length))
      elsif @rbuf.empty?
        checked_read(length, rv) or return
      else
        rv.replace(@rbuf.slice!(0, @rbuf.size))
      end
      rv.empty? && length != 0 ? nil : rv
    else
      rv.replace(read_all)
    end
  end

  def gets
    sep = $/
    if sep.nil?
      rv = read_all
      return rv.empty? ? nil : rv
    end
    re = /\A(.*?#{Regexp.escape(sep)})/

    begin
      @rbuf.sub!(re, '') and return $1

      if tmp = checked_read(16384)
        @rbuf << tmp
      elsif @rbuf.empty? # EOF
        return nil
      else # EOF, return whatever is left
        return @rbuf.slice!(0, @rbuf.size)
      end
    end while true
  end

  def checked_read(length = 16384, buf = '')
    if @input.read(length, buf)
      throw :rainbows_EFBIG if ((@limit -= buf.size) < 0)
      return buf
    end
  end

  def read_all
    rv = @rbuf.slice!(0, @rbuf.size)
    tmp = ''
    while checked_read(16384, tmp)
      rv << tmp
    end
    rv
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
rainbows-5.2.1 lib/rainbows/max_body/wrapper.rb
rainbows-5.2.0 lib/rainbows/max_body/wrapper.rb
rainbows-5.1.1 lib/rainbows/max_body/wrapper.rb
rainbows-5.1.0 lib/rainbows/max_body/wrapper.rb
rainbows-5.0.0.5.ge717 lib/rainbows/max_body/wrapper.rb
rainbows-5.0.0 lib/rainbows/max_body/wrapper.rb
rainbows-4.7.0 lib/rainbows/max_body/wrapper.rb
rainbows-4.6.2 lib/rainbows/max_body/wrapper.rb
rainbows-4.6.1 lib/rainbows/max_body/wrapper.rb
rainbows-4.6.0.4.g4108 lib/rainbows/max_body/wrapper.rb
rainbows-4.6.0 lib/rainbows/max_body/wrapper.rb
rainbows-4.5.0 lib/rainbows/max_body/wrapper.rb
rainbows-4.4.3 lib/rainbows/max_body/wrapper.rb
rainbows-4.4.2 lib/rainbows/max_body/wrapper.rb
rainbows-4.4.1.1.gd5c8c lib/rainbows/max_body/wrapper.rb
rainbows-4.4.1 lib/rainbows/max_body/wrapper.rb
rainbows-4.4.0 lib/rainbows/max_body/wrapper.rb
rainbows-4.3.1 lib/rainbows/max_body/wrapper.rb
rainbows-4.3.0 lib/rainbows/max_body/wrapper.rb
rainbows-4.2.0 lib/rainbows/max_body/wrapper.rb