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.



93
94
95
96
97
98
99
100
101
102
# File 'lib/excon/response.rb', line 93

def initialize(params={})
  @data = {
    :body     => '',
    :headers  => {}
  }.merge(params)
  @body      = @data[:body]
  @headers   = @data[:headers]
  @status    = @data[:status]
  @remote_ip = @data[:remote_ip]
end

Instance Attribute Details

#dataObject

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



32
33
34
35
36
37
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
# File 'lib/excon/response.rb', line 32

def self.parse(socket, datum)
  datum[:response] = {
    :body       => '',
    :headers    => {},
    :status     => socket.read(12)[9, 11].to_i,
    :remote_ip  => socket.respond_to?(:remote_ip) && socket.remote_ip
  }
  socket.readline # read the rest of the status line and CRLF

  until ((data = socket.readline).chop!).empty?
    key, value = data.split(/:\s*/, 2)
    datum[:response][:headers][key] = ([*datum[:response][:headers][key]] << value).compact.join(', ')
    if key.casecmp('Content-Length') == 0
      content_length = value.to_i
    elsif (key.casecmp('Transfer-Encoding') == 0) && (value.casecmp('chunked') == 0)
      transfer_encoding_chunked = true
    end
  end

  unless (['HEAD', 'CONNECT'].include?(datum[:method].to_s.upcase)) || NO_ENTITY.include?(datum[:response][:status])

    # check to see if expects was set and matched
    expected_status = !datum.has_key?(:expects) || [*datum[:expects]].include?(datum[:response][:status])

    # if expects matched and there is a block, use it
    if expected_status && datum.has_key?(:response_block)
      if transfer_encoding_chunked
        # 2 == "/r/n".length
        while (chunk_size = socket.readline.chop!.to_i(16)) > 0
          datum[:response_block].call(socket.read(chunk_size + 2).chop!, nil, nil)
        end
        socket.read(2)
      elsif remaining = content_length
        while remaining > 0
          datum[:response_block].call(socket.read([datum[:chunk_size], remaining].min), [remaining - datum[:chunk_size], 0].max, content_length)
          remaining -= datum[:chunk_size]
        end
      else
        while remaining = socket.read(datum[:chunk_size])
          datum[:response_block].call(remaining, remaining.length, content_length)
        end
      end
    else # no block or unexpected status
      if transfer_encoding_chunked
        while (chunk_size = socket.readline.chop!.to_i(16)) > 0
          datum[:response][:body] << socket.read(chunk_size + 2).chop! # 2 == "/r/n".length
        end
        socket.read(2) # 2 == "/r/n".length
      elsif remaining = content_length
        while remaining > 0
          datum[:response][:body] << socket.read([datum[:chunk_size], remaining].min)
          remaining -= datum[:chunk_size]
        end
      else
        datum[:response][:body] << socket.read
      end
    end
  end
  datum
end

Instance Method Details

#[](key) ⇒ Object



104
105
106
# File 'lib/excon/response.rb', line 104

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

#bodyObject



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


115
116
117
118
119
120
121
122
# File 'lib/excon/response.rb', line 115

def get_header(name)
  headers.each do |key,value|
    if key.casecmp(name) == 0
      return value
    end
  end
  nil
end

#headersObject



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

#paramsObject



108
109
110
111
# File 'lib/excon/response.rb', line 108

def params
  Excon.display_warning("Excon::Response#params is deprecated use Excon::Response#data instead (#{caller.first})")
  data
end

#remote_ipObject



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

#statusObject



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