Class: EventMachine::AblyHttpRequest::HttpClient
- Inherits:
-
Object
- Object
- EventMachine::AblyHttpRequest::HttpClient
show all
- Includes:
- Deferrable, HttpEncoding, HttpStatus
- Defined in:
- lib/em-http/client.rb
Defined Under Namespace
Classes: CookieJar
Constant Summary
collapse
- TRANSFER_ENCODING =
"TRANSFER_ENCODING"
- CONTENT_ENCODING =
"CONTENT_ENCODING"
- CONTENT_LENGTH =
"CONTENT_LENGTH"
- CONTENT_TYPE =
"CONTENT_TYPE"
- LAST_MODIFIED =
"LAST_MODIFIED"
- KEEP_ALIVE =
"CONNECTION"
- SET_COOKIE =
"SET_COOKIE"
- LOCATION =
"LOCATION"
- HOST =
"HOST"
- ETAG =
"ETAG"
- CRLF =
"\r\n"
Constants included
from HttpStatus
EventMachine::AblyHttpRequest::HttpStatus::CODE
EventMachine::AblyHttpRequest::HttpEncoding::FIELD_ENCODING, EventMachine::AblyHttpRequest::HttpEncoding::HTTP_REQUEST_HEADER
Instance Attribute Summary collapse
Instance Method Summary
collapse
#bytesize, #encode_auth, #encode_cookie, #encode_field, #encode_headers, #encode_host, #encode_param, #encode_query, #encode_request, #escape, #form_encode_body, #munge_header_keys, #unescape
Constructor Details
#initialize(conn, options) ⇒ HttpClient
Returns a new instance of HttpClient.
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/em-http/client.rb', line 28
def initialize(conn, options)
@conn = conn
@req = options
@stream = nil
@headers = nil
@cookies = []
@cookiejar = CookieJar.new
reset!
end
|
Instance Attribute Details
#conn ⇒ Object
Returns the value of attribute conn.
25
26
27
|
# File 'lib/em-http/client.rb', line 25
def conn
@conn
end
|
#content_charset ⇒ Object
Returns the value of attribute content_charset.
26
27
28
|
# File 'lib/em-http/client.rb', line 26
def content_charset
@content_charset
end
|
#cookies ⇒ Object
Returns the value of attribute cookies.
26
27
28
|
# File 'lib/em-http/client.rb', line 26
def cookies
@cookies
end
|
#error ⇒ Object
Returns the value of attribute error.
26
27
28
|
# File 'lib/em-http/client.rb', line 26
def error
@error
end
|
#req ⇒ Object
Returns the value of attribute req.
26
27
28
|
# File 'lib/em-http/client.rb', line 26
def req
@req
end
|
#response ⇒ Object
Returns the value of attribute response.
25
26
27
|
# File 'lib/em-http/client.rb', line 25
def response
@response
end
|
Returns the value of attribute response_header.
26
27
28
|
# File 'lib/em-http/client.rb', line 26
def
@response_header
end
|
#state ⇒ Object
Returns the value of attribute state.
25
26
27
|
# File 'lib/em-http/client.rb', line 25
def state
@state
end
|
Instance Method Details
#build_request ⇒ Object
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
|
# File 'lib/em-http/client.rb', line 135
def build_request
head = @req. ? (@req.) : {}
if @conn.connopts.http_proxy?
proxy = @conn.connopts.proxy
head['proxy-authorization'] = proxy[:authorization] if proxy[:authorization]
end
if cookie = head['cookie']
@cookies << encode_cookie(cookie) if cookie
end
head['cookie'] = @cookies.compact.uniq.join("; ").squeeze(";") unless @cookies.empty?
if !@req.keepalive
head['connection'] = 'close'
end
head['host'] ||= encode_host
if !head.key?('user-agent')
head['user-agent'] = 'EventMachine HttpClient'
elsif head['user-agent'].nil?
head.delete('user-agent')
end
if !head.key?('accept-encoding') && req.compressed
head['accept-encoding'] = 'gzip, compressed'
end
head['Authorization'] = @req.uri.userinfo.split(/:/, 2) if @req.uri.userinfo
head
end
|
#connection_completed ⇒ Object
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/em-http/client.rb', line 54
def connection_completed
@state = :response_header
head, body = build_request, @req.body
@conn.middleware.each do |m|
head, body = m.request(self, head, body) if m.respond_to?(:request)
end
send_request(head, body)
end
|
#continue? ⇒ Boolean
75
76
77
|
# File 'lib/em-http/client.rb', line 75
def continue?
@response_header.status == 100 && (@req.method == 'POST' || @req.method == 'PUT')
end
|
#finished? ⇒ Boolean
79
80
81
|
# File 'lib/em-http/client.rb', line 79
def finished?
@state == :finished || (@state == :body && @response_header.content_length.nil?)
end
|
129
|
# File 'lib/em-http/client.rb', line 129
def (&blk); @headers = blk; end
|
#last_effective_url ⇒ Object
50
|
# File 'lib/em-http/client.rb', line 50
def last_effective_url; @req.uri; end
|
#normalize_body(body) ⇒ Object
131
132
133
|
# File 'lib/em-http/client.rb', line 131
def normalize_body(body)
body.is_a?(Hash) ? form_encode_body(body) : body
end
|
#on_body_data(data) ⇒ Object
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/em-http/client.rb', line 206
def on_body_data(data)
if @content_decoder
begin
@content_decoder << data
rescue HttpDecoders::DecoderError
on_error "Content-decoder error"
end
else
on_decoded_body_data(data)
end
end
|
#on_decoded_body_data(data) ⇒ Object
218
219
220
221
222
223
224
225
|
# File 'lib/em-http/client.rb', line 218
def on_decoded_body_data(data)
data.force_encoding @content_charset if @content_charset
if @stream
@stream.call(data)
else
@response << data
end
end
|
#on_error(msg = nil) ⇒ Object
Also known as:
close
122
123
124
125
|
# File 'lib/em-http/client.rb', line 122
def on_error(msg = nil)
@error = msg
fail(self)
end
|
#on_request_complete ⇒ Object
65
66
67
68
69
70
71
72
73
|
# File 'lib/em-http/client.rb', line 65
def on_request_complete
begin
@content_decoder.finalize! if @content_decoder
rescue HttpDecoders::DecoderError
on_error "Content-decoder error"
end
unbind
end
|
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
# File 'lib/em-http/client.rb', line 249
def (, version, status)
@response_header.raw =
.each do |key, val|
@response_header[key.upcase.gsub('-','_')] = val
end
@response_header.http_version = version.join('.')
@response_header.http_status = status
@response_header.http_reason = CODE[status] || 'unknown'
@headers.call(@response_header) if @headers
unless @response_header.http_status and @response_header.http_reason
@state = :invalid
on_error "no HTTP response"
return
end
if @response_header.cookie && @req.pass_cookies
[@response_header.cookie].flatten.each {|cookie| @cookiejar.set(cookie, @req.uri)}
end
if @response_header.location
begin
location = Addressable::URI.parse(@response_header.location)
location.path = "/" if location.path.empty?
if location.relative?
location = @req.uri.join(location)
else
raise if location.host.nil?
end
@response_header[LOCATION] = location.to_s
rescue
on_error "Location header format error"
return
end
end
if @req.method == "HEAD"
@state = :finished
return
end
if @response_header.chunked_encoding?
@state = :chunk_header
elsif @response_header.content_length
@state = :body
else
@state = :body
end
if @req.decoding && decoder_class = HttpDecoders.decoder_for_encoding([CONTENT_ENCODING])
begin
@content_decoder = decoder_class.new do |s| on_decoded_body_data(s) end
rescue HttpDecoders::DecoderError
on_error "Content-decoder error"
end
end
content_type = [[CONTENT_TYPE]].flatten.first
if String.method_defined?(:force_encoding) && /;\s*charset=\s*(.+?)\s*(;|$)/.match(content_type)
@content_charset = Encoding.find($1.gsub(/^\"|\"$/, '')) rescue Encoding.default_external
end
end
|
#peer ⇒ Object
52
|
# File 'lib/em-http/client.rb', line 52
def peer; @conn.peer; end
|
#redirect? ⇒ Boolean
83
84
85
|
# File 'lib/em-http/client.rb', line 83
def redirect?
@response_header.redirection? && @req.follow_redirect?
end
|
#redirects ⇒ Object
51
|
# File 'lib/em-http/client.rb', line 51
def redirects; @req.followed; end
|
#request_body_pending? ⇒ Boolean
227
228
229
|
# File 'lib/em-http/client.rb', line 227
def request_body_pending?
!!@req_body
end
|
#reset! ⇒ Object
40
41
42
43
44
45
46
47
48
|
# File 'lib/em-http/client.rb', line 40
def reset!
@response_header = HttpResponseHeader.new
@state = :response_header
@response = ''
@error = nil
@content_decoder = nil
@content_charset = nil
end
|
#send_request(head, body) ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/em-http/client.rb', line 175
def send_request(head, body)
body = normalize_body(body)
file = @req.file
query = @req.query
head['content-length'] = File.size(file) if file
if body
head['content-length'] ||= body.respond_to?(:bytesize) ? body.bytesize : body.size
elsif @req.method == 'POST' or @req.method == 'PUT'
head['content-length'] ||= 0
end
if !head['content-type'] and @req.body.is_a? Hash
head['content-type'] = 'application/x-www-form-urlencoded'
end
||= encode_request(@req.method, @req.uri, query, @conn.connopts)
<< (head)
<< CRLF
@conn.send_data
@req_body = body || (@req.file && Pathname.new(@req.file))
send_request_body unless @req.['expect'] == '100-continue'
end
|
#send_request_body ⇒ Object
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/em-http/client.rb', line 231
def send_request_body
return if @req_body.nil?
if @req_body.is_a?(String)
@conn.send_data @req_body
elsif @req_body.is_a?(Pathname)
@conn.stream_file_data @req_body.to_path, http_chunks: false
elsif @req_body.respond_to?(:read) && @req_body.respond_to?(:eof?) @conn.stream_data @req_body
else
raise "Don't know how to send request body: #{@req_body.inspect}"
end
@req_body = nil
end
|
#stream(&blk) ⇒ Object
128
|
# File 'lib/em-http/client.rb', line 128
def stream(&blk); @stream = blk; end
|
#unbind(reason = nil) ⇒ Object
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
|
# File 'lib/em-http/client.rb', line 87
def unbind(reason = nil)
if finished?
if redirect?
begin
@conn.middleware.each do |m|
m.response(self) if m.respond_to?(:response)
end
if redirect?
@req.followed += 1
@cookies.clear
@cookies = @cookiejar.get(@response_header.location).map(&:to_s) if @req.pass_cookies
@conn.redirect(self, @response_header.location)
else
succeed(self)
end
rescue => e
on_error(e.message)
end
else
succeed(self)
end
else
on_error(reason || 'connection closed by server')
end
end
|