Class: Rbkb::Http::Response
Overview
A Response encapsulates all the entities in a HTTP response, including the status header, general headers, and body.
Instance Attribute Summary collapse
-
#status ⇒ Object
(also: #first_entity)
Returns the value of attribute status.
Attributes inherited from Base
Instance Method Summary collapse
-
#capture(str) {|_self, bstr| ... } ⇒ Object
Parses a raw HTTP response and captures data into the current instance.
-
#default_body_obj(*args) ⇒ Object
Returns a new BoundBody object.
-
#default_headers_obj(*args) ⇒ Object
Returns a new Headers object extended as ResponseHeaders.
-
#do_chunked_encoding?(hdrs = @headers) ⇒ Boolean
Indicates whether to use chunked encoding based on presence of the “Transfer-Encoding: chunked” header or the :ignore_chunked_encoding opts parameter.
-
#to_raw(raw_body = nil) {|_self| ... } ⇒ Object
Returns a raw HTTP response for this instance.
Methods inherited from Base
#attach_new_body, #attach_new_header, #capture_body, #capture_complete?, #capture_headers, #content_length, #content_type, #initialize, parse, #reset_capture, #reset_capture!
Methods included from CommonInterface
Constructor Details
This class inherits a constructor from Rbkb::Http::Base
Instance Attribute Details
#status ⇒ Object Also known as: first_entity
Returns the value of attribute status.
6 7 8 |
# File 'lib/rbkb/http/response.rb', line 6 def status @status end |
Instance Method Details
#capture(str) {|_self, bstr| ... } ⇒ Object
Parses a raw HTTP response and captures data into the current instance.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rbkb/http/response.rb', line 41 def capture(str) raise "arg 0 must be a string" unless String === str hstr, bstr = str.split(/\s*\r?\n\r?\n/, 2) capture_headers(hstr) yield(self, bstr) if block_given? unless @body and @body.capture_complete? @body = if do_chunked_encoding? ChunkedBody.new {|b| b.base = self } elsif content_length() BoundBody.new {|b| b.base = self } else Body.new {|b| b.base = self } end end capture_body(bstr) return self end |
#default_body_obj(*args) ⇒ Object
Returns a new BoundBody object. This is the default object which will be used when composing fresh Response body entities.
82 83 84 |
# File 'lib/rbkb/http/response.rb', line 82 def default_body_obj(*args) BoundBody.new(*args) end |
#default_headers_obj(*args) ⇒ Object
Returns a new Headers object extended as ResponseHeaders. This is the default object which will be used when composing fresh Response header entities.
76 77 78 |
# File 'lib/rbkb/http/response.rb', line 76 def default_headers_obj(*args) Headers.new(*args).extend(ResponseHeaders) end |
#do_chunked_encoding?(hdrs = @headers) ⇒ Boolean
Indicates whether to use chunked encoding based on presence of the “Transfer-Encoding: chunked” header or the :ignore_chunked_encoding opts parameter.
68 69 70 71 |
# File 'lib/rbkb/http/response.rb', line 68 def do_chunked_encoding?(hdrs=@headers) ( (not @opts[:ignore_chunked_encoding]) and (hdrs.get_header_value("Transfer-Encoding").to_s =~ /(?:^|\W)chunked(?:\W|$)/) ) end |
#to_raw(raw_body = nil) {|_self| ... } ⇒ Object
Returns a raw HTTP response for this instance. Must have a status element defined at a bare minimum.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rbkb/http/response.rb', line 13 def to_raw(raw_body=nil) raise "this response has no status" unless first_entity() self.headers ||= default_headers_obj() self.body = raw_body if raw_body if do_chunked_encoding?(@headers) unless @body.is_a? ChunkedBody @body = ChunkedBody.new(@body, @body.opts) end @headers.delete_header("Content-Length") elsif not opts[:ignore_content_length] unless @body.is_a? BoundBody @body = BoundBody.new(@body, @body.opts) end @headers.delete_header("Transfer-Encoding") else @body = Body.new(@body, @body.opts) end @body.base = self yield(self) if block_given? bstr = @body.to_raw hdrs = @headers.to_raw_array.unshift(self.first_entity.to_raw) return "#{hdrs.join("\r\n")}\r\n\r\n#{bstr}" end |