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
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
|
# File 'lib/excon/response.rb', line 60
def self.parse(socket, datum)
begin
line = socket.readline
end until status = line[9, 3].to_i
reason_phrase = line[13..-3]
datum[:response] = {
:body => String.new,
:cookies => [],
:host => datum[:host],
:headers => Excon::.new,
:path => datum[:path],
:port => datum[: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
(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.(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
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
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
while new_line_size > 0
chunk = socket.read(new_line_size) || raise(EOFError)
new_line_size -= chunk.length
end
end
end
(socket, datum)
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) || 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
|