Class: Excon::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/excon/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Response

Returns a new instance of Response.



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/excon/response.rb', line 213

def initialize(params={})
  @data = {
    :body     => ''
  }.merge(params)
  @data[:headers] = Excon::Headers.new.merge!(params[:headers] || {})

  @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

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/excon/response.rb', line 5

def data
  @data
end

Class Method Details

.parse(socket, datum) ⇒ Object



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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/excon/response.rb', line 69

def self.parse(socket, datum)
  # this will discard any trailing lines from the previous response if any.
  line = nil
  loop do
    line = socket.readline
    break if line[9,3].to_i != 0
  end

  status = line[9, 3].to_i
  reason_phrase = line[13..-3] # -3 strips the trailing "\r\n"

  datum[:response] = {
    :body          => String.new,
    :cookies       => [],
    :host          => datum[:host],
    :scheme        => datum[:scheme],
    :method        => datum[:method],
    :headers       => Excon::Headers.new,
    :path          => datum[:path],
    :query         => datum[:query],
    :port          => datum[:port],
    :omit_default_port => datum[:omit_default_port],
    :status        => status,
    :status_line   => line,
    :reason_phrase => reason_phrase
  }

  unix_proxy = datum[:proxy] ? datum[:proxy][:scheme] == UNIX : false
  unless datum[:scheme] == UNIX || unix_proxy
    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') })
      encodings = Utils.split_header_value(datum[:response][:headers][key])
      if (encoding = encodings.last) && encoding.casecmp?('chunked')
        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) || raise(EOFError)
            chunk_size -= chunk.bytesize
            response_block.call(chunk, nil, nil)
          end
          new_line_size = 2 # 2 == "\r\n".length
          while new_line_size > 0
            chunk = socket.read(new_line_size) || raise(EOFError)
            new_line_size -= chunk.length
          end
        end
      else
        while (chunk_size = socket.readline.chomp!.to_i(16)) > 0
          while chunk_size > 0
            chunk = socket.read(chunk_size) || raise(EOFError)
            chunk_size -= chunk.bytesize
            datum[:response][:body] << chunk
          end
          new_line_size = 2 # 2 == "\r\n".length
          while new_line_size > 0
            chunk = socket.read(new_line_size) || raise(EOFError)
            new_line_size -= chunk.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') })
        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) || raise(EOFError)
            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) || raise(EOFError)
            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



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/excon/response.rb', line 193

def self.parse_headers(socket, datum)
  last_key = nil
  until (data = socket.readline.chomp).empty?
    if !data.lstrip!.nil?
      raise Excon::Error::ResponseParse, '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::Error::ResponseParse, '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(', ')
      if key.casecmp?('Set-Cookie')
        datum[:response][:cookies] << value.strip
      end
      last_key = key
    end
  end
end

Instance Method Details

#[](key) ⇒ Object



227
228
229
# File 'lib/excon/response.rb', line 227

def [](key)
  @data[key]
end

#bodyObject



11
12
13
# File 'lib/excon/response.rb', line 11

def body
  @data[:body]
end

#body=(new_body) ⇒ Object

backwards compatability reader/writers



8
9
10
# File 'lib/excon/response.rb', line 8

def body=(new_body)
  @data[:body] = new_body
end

#get_header(name) ⇒ Object

Retrieve a specific header value. Header names are treated case-insensitively.

Parameters:

  • name (String)

    Header name



242
243
244
# File 'lib/excon/response.rb', line 242

def get_header(name)
  headers[name]
end

#headersObject



17
18
19
# File 'lib/excon/response.rb', line 17

def headers
  @data[:headers]
end

#headers=(new_headers) ⇒ Object



14
15
16
# File 'lib/excon/response.rb', line 14

def headers=(new_headers)
  @data[:headers] = new_headers
end

#hostObject



20
21
22
# File 'lib/excon/response.rb', line 20

def host
  @data[:host]
end

#http_methodObject

can’t be named “method”



32
33
34
# File 'lib/excon/response.rb', line 32

def http_method # can't be named "method"
  @data[:method]
end

#local_addressObject



26
27
28
# File 'lib/excon/response.rb', line 26

def local_address
  @data[:local_address]
end

#local_portObject



29
30
31
# File 'lib/excon/response.rb', line 29

def local_port
  @data[:local_port]
end

#paramsObject



231
232
233
234
# File 'lib/excon/response.rb', line 231

def params
  Excon.display_warning('Excon::Response#params is deprecated use Excon::Response#data instead.')
  data
end

#pathObject



35
36
37
# File 'lib/excon/response.rb', line 35

def path
  @data[:path]
end

#portObject



41
42
43
# File 'lib/excon/response.rb', line 41

def port
  @data[:port]
end

#ppObject



236
237
238
# File 'lib/excon/response.rb', line 236

def pp
  Excon::PrettyPrinter.pp($stdout, @data)
end

#queryObject



38
39
40
# File 'lib/excon/response.rb', line 38

def query
  @data[:query]
end

#reason_phraseObject



47
48
49
# File 'lib/excon/response.rb', line 47

def reason_phrase
  @data[:reason_phrase]
end

#reason_phrase=(new_reason_phrase) ⇒ Object



44
45
46
# File 'lib/excon/response.rb', line 44

def reason_phrase=(new_reason_phrase)
  @data[:reason_phrase] = new_reason_phrase
end

#remote_ipObject



53
54
55
# File 'lib/excon/response.rb', line 53

def remote_ip
  @data[:remote_ip]
end

#remote_ip=(new_remote_ip) ⇒ Object



50
51
52
# File 'lib/excon/response.rb', line 50

def remote_ip=(new_remote_ip)
  @data[:remote_ip] = new_remote_ip
end

#schemeObject



23
24
25
# File 'lib/excon/response.rb', line 23

def scheme
  @data[:scheme]
end

#statusObject



59
60
61
# File 'lib/excon/response.rb', line 59

def status
  @data[:status]
end

#status=(new_status) ⇒ Object



56
57
58
# File 'lib/excon/response.rb', line 56

def status=(new_status)
  @data[:status] = new_status
end

#status_lineObject



62
63
64
# File 'lib/excon/response.rb', line 62

def status_line
  @data[:status_line]
end

#status_line=(new_status_line) ⇒ Object



65
66
67
# File 'lib/excon/response.rb', line 65

def status_line=(new_status_line)
  @data[:status_line] = new_status_line
end