Class: Excon::Response
- Inherits:
-
Object
- Object
- Excon::Response
- Defined in:
- lib/excon/response.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #body ⇒ Object
-
#body=(new_body) ⇒ Object
backwards compatability reader/writers.
-
#get_header(name) ⇒ Object
Retrieve a specific header value.
- #headers ⇒ Object
- #headers=(new_headers) ⇒ Object
-
#initialize(params = {}) ⇒ Response
constructor
A new instance of Response.
- #local_address ⇒ Object
- #local_port ⇒ Object
- #params ⇒ Object
- #pp ⇒ Object
- #remote_ip ⇒ Object
- #remote_ip=(new_remote_ip) ⇒ Object
- #status ⇒ Object
- #status=(new_status) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Response
Returns a new instance of Response.
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/excon/response.rb', line 160 def initialize(params={}) @data = { :body => '', :headers => Excon::Headers.new }.merge(params) @body = @data[:body] @headers = @data[:headers] @status = @data[:status] @remote_ip = @data[:remote_ip] @local_port = @data[:local_port] @local_address = @data[:local_address] end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/excon/response.rb', line 4 def data @data end |
Class Method Details
.parse(socket, datum) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/excon/response.rb', line 38 def self.parse(socket, datum) # this will discard any trailing lines from the previous response if any. until status = socket.readline[9,11].to_i end datum[:response] = { :body => '', :headers => Excon::Headers.new, :status => status } unless datum[:scheme] == UNIX datum[:response].merge!( :remote_ip => socket.remote_ip, :local_port => socket.local_port, :local_address => socket.local_address ) end parse_headers(socket, datum) unless (['HEAD', 'CONNECT'].include?(datum[:method].to_s.upcase)) || NO_ENTITY.include?(datum[:response][:status]) if key = datum[:response][:headers].keys.detect {|k| k.casecmp('Transfer-Encoding') == 0 } encodings = Utils.split_header_value(datum[:response][:headers][key]) if (encoding = encodings.last) && encoding.casecmp('chunked') == 0 transfer_encoding_chunked = true if encodings.length == 1 datum[:response][:headers].delete(key) else datum[:response][:headers][key] = encodings[0...-1].join(', ') end end end # use :response_block unless :expects would fail if response_block = datum[:response_block] if datum[:middlewares].include?(Excon::Middleware::Expects) && datum[:expects] && !Array(datum[:expects]).include?(datum[:response][:status]) response_block = nil end end if transfer_encoding_chunked if response_block while (chunk_size = socket.readline.chomp!.to_i(16)) > 0 while chunk_size > 0 chunk = socket.read(chunk_size) chunk_size -= chunk.bytesize response_block.call(chunk, nil, nil) end new_line_size = 2 # 2 == "\r\n".length while new_line_size > 0 new_line_size -= socket.read(new_line_size).length end end else while (chunk_size = socket.readline.chomp!.to_i(16)) > 0 while chunk_size > 0 chunk = socket.read(chunk_size) chunk_size -= chunk.bytesize datum[:response][:body] << chunk end new_line_size = 2 # 2 == "\r\n".length while new_line_size > 0 new_line_size -= socket.read(new_line_size).length end end end parse_headers(socket, datum) # merge trailers into headers else if key = datum[:response][:headers].keys.detect {|k| k.casecmp('Content-Length') == 0 } content_length = datum[:response][:headers][key].to_i end if remaining = content_length if response_block while remaining > 0 chunk = socket.read([datum[:chunk_size], remaining].min) response_block.call(chunk, [remaining - chunk.bytesize, 0].max, content_length) remaining -= chunk.bytesize end else while remaining > 0 chunk = socket.read([datum[:chunk_size], remaining].min) datum[:response][:body] << chunk remaining -= chunk.bytesize end end else if response_block while chunk = socket.read(datum[:chunk_size]) response_block.call(chunk, nil, nil) end else while chunk = socket.read(datum[:chunk_size]) datum[:response][:body] << chunk end end end end end datum end |
.parse_headers(socket, datum) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/excon/response.rb', line 143 def self.parse_headers(socket, datum) last_key = nil until (data = socket.readline.chomp!).empty? if !data.lstrip!.nil? raise Excon::Errors::ResponseParseError, 'malformed header' unless last_key # append to last_key's last value datum[:response][:headers][last_key] << ' ' << data.rstrip else key, value = data.split(':', 2) raise Excon::Errors::ResponseParseError, 'malformed header' unless value # add key/value or append value to existing values datum[:response][:headers][key] = ([datum[:response][:headers][key]] << value.strip).compact.join(', ') last_key = key end end end |
Instance Method Details
#[](key) ⇒ Object
173 174 175 |
# File 'lib/excon/response.rb', line 173 def [](key) @data[key] end |
#body ⇒ Object
10 11 12 |
# File 'lib/excon/response.rb', line 10 def body @data[:body] end |
#body=(new_body) ⇒ Object
backwards compatability reader/writers
7 8 9 |
# File 'lib/excon/response.rb', line 7 def body=(new_body) @data[:body] = new_body end |
#get_header(name) ⇒ Object
Retrieve a specific header value. Header names are treated case-insensitively.
@param [String] name Header name
188 189 190 |
# File 'lib/excon/response.rb', line 188 def get_header(name) headers[name] end |
#headers ⇒ Object
16 17 18 |
# File 'lib/excon/response.rb', line 16 def headers @data[:headers] end |
#headers=(new_headers) ⇒ Object
13 14 15 |
# File 'lib/excon/response.rb', line 13 def headers=(new_headers) @data[:headers] = new_headers end |
#local_address ⇒ Object
34 35 36 |
# File 'lib/excon/response.rb', line 34 def local_address @data[:local_address] end |
#local_port ⇒ Object
31 32 33 |
# File 'lib/excon/response.rb', line 31 def local_port @data[:local_port] end |
#params ⇒ Object
177 178 179 180 |
# File 'lib/excon/response.rb', line 177 def params Excon.display_warning('Excon::Response#params is deprecated use Excon::Response#data instead.') data end |
#pp ⇒ Object
182 183 184 |
# File 'lib/excon/response.rb', line 182 def pp Excon::PrettyPrinter.pp($stdout, @data) end |
#remote_ip ⇒ Object
28 29 30 |
# File 'lib/excon/response.rb', line 28 def remote_ip @data[:remote_ip] end |
#remote_ip=(new_remote_ip) ⇒ Object
25 26 27 |
# File 'lib/excon/response.rb', line 25 def remote_ip=(new_remote_ip) @data[:remote_ip] = new_remote_ip end |
#status ⇒ Object
22 23 24 |
# File 'lib/excon/response.rb', line 22 def status @data[:status] end |
#status=(new_status) ⇒ Object
19 20 21 |
# File 'lib/excon/response.rb', line 19 def status=(new_status) @data[:status] = new_status end |