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(attrs = {}) ⇒ Response

Returns a new instance of Response.



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

def initialize(attrs={})
  @body    = attrs[:body]    || ''
  @headers = attrs[:headers] || {}
  @status  = attrs[:status]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/excon/response.rb', line 3

def body
  @body
end

#headersObject

Returns the value of attribute headers.



3
4
5
# File 'lib/excon/response.rb', line 3

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



3
4
5
# File 'lib/excon/response.rb', line 3

def status
  @status
end

Class Method Details

.parse(socket, params = {}) ⇒ Object



11
12
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
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
# File 'lib/excon/response.rb', line 11

def self.parse(socket, params={})
  response = new(:status => socket.readline[9, 11].to_i)
  block_given = block_given?

  while true
    (data = socket.readline).chop!

    unless data.empty?
      key, value = data.split(': ')
      response.headers[key] = value
    else
      break
    end
  end

  unless params[:method].to_s.casecmp('HEAD') == 0

    # don't pass stuff into a block if there was an error
    if params[:expects] && ![*params[:expects]].include?(response.status)
      block_given = false
    end

    if block_given
      if response.headers.has_key?('Transfer-Encoding') && response.headers['Transfer-Encoding'].casecmp('chunked') == 0
        while true
          chunk_size = socket.readline.chop!.to_i(16)
          break if chunk_size < 1
          yield socket.read(chunk_size+2).chop! # 2 == "/r/n".length
        end
      elsif response.headers.has_key?('Connection') && response.headers['Connection'].casecmp('close') == 0
        yield socket.read
      elsif response.headers.has_key?('Content-Length')
        remaining = response.headers['Content-Length'].to_i
        while remaining > 0
          yield socket.read([CHUNK_SIZE, remaining].min)
          remaining -= CHUNK_SIZE
        end
      end
    else
      if response.headers.has_key?('Transfer-Encoding') && response.headers['Transfer-Encoding'].casecmp('chunked') == 0
        while true
          chunk_size = socket.readline.chop!.to_i(16)
          break if chunk_size < 1
          response.body << socket.read(chunk_size + 2).chop! # 2 == "/r/n".length
        end
      elsif response.headers.has_key?('Connection') && response.headers['Connection'].casecmp('close') == 0
        response.body << socket.read
      elsif response.headers.has_key?('Content-Length')
        remaining = response.headers['Content-Length'].to_i
        while remaining > 0
          response.body << socket.read([CHUNK_SIZE, remaining].min)
          remaining -= CHUNK_SIZE
        end
      end
    end
  end

  response
end