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/connection.rb', line 94
def request_call(datum)
begin
if datum.has_key?(:response)
return datum
else
socket.data = datum
request = datum[:method].to_s.upcase + ' '
if datum[:proxy] && datum[:scheme] != HTTPS
request << datum[:scheme] << '://' << datum[:host] << port_string(datum)
end
request << datum[:path]
request << query_string(datum)
request << HTTP_1_1
if datum.has_key?(:request_block)
datum[:headers]['Transfer-Encoding'] = 'chunked'
else
body = datum[:body].is_a?(String) ? StringIO.new(datum[:body]) : datum[:body]
unless datum[:method].to_s.casecmp('GET') == 0 && body.nil?
unless datum[:headers].has_key?('Content-Length')
datum[:headers]['Content-Length'] = detect_content_length(body)
end
end
end
datum[:headers].each do |key, values|
[values].flatten.each do |value|
request << key.to_s << ': ' << value.to_s << CR_NL
end
end
request << CR_NL
if datum.has_key?(:request_block)
socket.write(request)
while true
chunk = datum[:request_block].call
if FORCE_ENC
chunk.force_encoding('BINARY')
end
if chunk.length > 0
socket.write(chunk.length.to_s(16) << CR_NL << chunk << CR_NL)
else
socket.write(String.new("0#{CR_NL}#{CR_NL}"))
break
end
end
elsif body.nil?
socket.write(request)
else
if body.respond_to?(:binmode)
body.binmode
end
if body.respond_to?(:rewind)
body.rewind rescue nil
end
if FORCE_ENC
request.force_encoding('BINARY')
end
chunk = body.read([datum[:chunk_size] - request.length, 0].max)
if chunk
if FORCE_ENC
chunk.force_encoding('BINARY')
end
socket.write(request << chunk)
else
socket.write(request)
end
while chunk = body.read(datum[:chunk_size])
socket.write(chunk)
end
end
end
rescue => error
case error
when Excon::Errors::StubNotFound, Excon::Errors::Timeout
raise(error)
else
raise_socket_error(error)
end
end
datum
end
|