Class: Rbkb::Http::BoundBody

Inherits:
Body show all
Defined in:
lib/rbkb/http/body.rb

Overview

BoundBody is designed for handling an HTTP body when using the usual “Content-Length: NNN” HTTP header.

Instance Attribute Summary

Attributes inherited from Body

#base, #expect_length

Instance Method Summary collapse

Methods inherited from Body

#capture_complete?, #data, #data=, #get_content_length, #initialize, parse, #reset_capture, #reset_capture!

Methods included from CommonInterface

#_common_init, #opts, #opts=

Methods inherited from String

#^, #b64, #bgrep, #blit, #camelize, #camelize_meth, #class_name, #const_lookup, #crc32, #cstring, #d64, #dat_to_num, #decamelize, #dehexdump, #entropy, #hex_to_num, #hexdump, #hexify, #ishex?, #lalign, #pipe_magick, #ralign, #randomize, #randomize!, #rotate_bytes, #starts_with?, #strings, #to_stringio, #unhexify, #urldec, #urlenc, #xor

Constructor Details

This class inherits a constructor from Rbkb::Http::Body

Instance Method Details

#capture(str) ⇒ Object

This method may throw :expect_length with one of the following values to indicate certain content-length conditions:

> 0 :   Got incomplete data in this capture. The object expects
        capture to be called again with more body data.

< 0 :   Got more data than expected, the caller should truncate and 
        handle the extra data in some way. Note: Calling capture again
        on this instance will start a fresh body capture.

Caller can also detect the above conditions by checking the expect_length attribute but should still be prepared handle the throw().

0/nil:  Got exactly what was expected. Caller can proceed with fresh
        captures on this or other Body objects.

See also reset_capture and reset_capture!



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rbkb/http/body.rb', line 108

def capture(str)
  raise "arg 0 must be a string" unless String === str

  # Start fresh unless we're expecting more data
  self.data="" unless @expect_length and @expect_length > 0

  if not clen=get_content_length()
    raise "content-length is unknown. aborting capture"
  else
    @expect_length = clen - (self.size + str.size)
    self << str[0, clen - self.size]
    if @expect_length > 0
      throw(:expect_length, @expect_length)
    elsif @expect_length < 0
      throw(:expect_length, @expect_length)
    else
      reset_capture()
    end
  end
  return self
end

#to_raw(*args) ⇒ Object



130
131
132
133
134
135
# File 'lib/rbkb/http/body.rb', line 130

def to_raw(*args)
  if @base
    @base.headers.set_header("Content-Length", self.size)
  end
  super(*args)
end