Class: Rig::HTTPResponse
- Inherits:
-
Object
- Object
- Rig::HTTPResponse
- Defined in:
- lib/rig/http_response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Instance Method Summary collapse
-
#initialize(response) ⇒ HTTPResponse
constructor
A new instance of HTTPResponse.
- #parse_header ⇒ Object
- #read_chunked(dest) ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(response) ⇒ HTTPResponse
Returns a new instance of HTTPResponse.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rig/http_response.rb', line 7 def initialize response parts = response.split(CRLF + CRLF) @header = parts.delete_at( 0 ) @status = @header.match(/HTTP\/\d.\d\s(\d\d\d)/)[1] @body = parts.join parse_header if @header["Transfer-Encoding"] == "chunked" parsed_body = "" @body = StringIO.new( @body ) read_chunked( parsed_body ) @body = parsed_body end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
5 6 7 |
# File 'lib/rig/http_response.rb', line 5 def body @body end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
5 6 7 |
# File 'lib/rig/http_response.rb', line 5 def header @header end |
Instance Method Details
#parse_header ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rig/http_response.rb', line 28 def parse_header status_line = @header[/HTTP\/\d\.\d\s\d\d\d.+\r\n/] @header = @header.gsub(status_line, "Status: #{status_line}") @header = @header.split(CRLF) @header = @header.map { |element| element.split(": ") } @header = @header.inject({}) do |result, element| result[element.first] = element.last result end end |
#read_chunked(dest) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rig/http_response.rb', line 39 def read_chunked(dest) len = nil total = 0 while true line = @body.readline hexlen = line.slice(/[0-9a-fA-F]+/) or raise HTTPBadResponse, "wrong chunk size line: #{line}" len = hexlen.hex break if len == 0 @body.read len, dest; total += len @body.read 2 # \r\n end end |
#status ⇒ Object
24 25 26 |
# File 'lib/rig/http_response.rb', line 24 def status @status ? @status.to_i : 666 end |