Class: Rbkb::Http::BoundBody

Inherits:
Body
  • Object
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, #get_content_type, #initialize, parse, #reset_capture, #reset_capture!

Methods included from CommonInterface

#_common_init, #opts, #opts=

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!



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rbkb/http/body.rb', line 114

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



136
137
138
139
140
141
# File 'lib/rbkb/http/body.rb', line 136

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