Class: EventMachine::HttpConnection
- Inherits:
-
Object
- Object
- EventMachine::HttpConnection
show all
- Includes:
- Connectify, HTTPMethods, Socksify
- Defined in:
- lib/em-http/http_connection.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#delete, #get, #head, #options, #patch, #post, #put
Constructor Details
Returns a new instance of HttpConnection.
105
106
107
108
|
# File 'lib/em-http/http_connection.rb', line 105
def initialize
@deferred = true
@middleware = []
end
|
Instance Attribute Details
#conn ⇒ Object
Returns the value of attribute conn.
102
103
104
|
# File 'lib/em-http/http_connection.rb', line 102
def conn
@conn
end
|
#connopts ⇒ Object
Returns the value of attribute connopts.
103
104
105
|
# File 'lib/em-http/http_connection.rb', line 103
def connopts
@connopts
end
|
#deferred ⇒ Object
Returns the value of attribute deferred.
102
103
104
|
# File 'lib/em-http/http_connection.rb', line 102
def deferred
@deferred
end
|
#error ⇒ Object
Returns the value of attribute error.
103
104
105
|
# File 'lib/em-http/http_connection.rb', line 103
def error
@error
end
|
#uri ⇒ Object
Returns the value of attribute uri.
103
104
105
|
# File 'lib/em-http/http_connection.rb', line 103
def uri
@uri
end
|
Instance Method Details
#activate_connection(client) ⇒ Object
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
|
# File 'lib/em-http/http_connection.rb', line 115
def activate_connection(client)
begin
EventMachine.bind_connect(@connopts.bind, @connopts.bind_port,
@connopts.host, @connopts.port,
HttpStubConnection) do |conn|
post_init
@deferred = false
@conn = conn
conn.parent = self
conn.pending_connect_timeout = @connopts.connect_timeout
conn.comm_inactivity_timeout = @connopts.inactivity_timeout
end
finalize_request(client)
rescue EventMachine::ConnectionError => e
EM.next_tick{client.close(e.message)}
end
end
|
#connection_completed ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/em-http/http_connection.rb', line 226
def connection_completed
@peer = @conn.get_peername
if @connopts.socks_proxy?
socksify(client.req.uri.hostname, client.req.uri.inferred_port, *@connopts.proxy[:authorization]) { start }
elsif @connopts.connect_proxy?
connectify(client.req.uri.hostname, client.req.uri.inferred_port, *@connopts.proxy[:authorization]) { start }
else
start
end
end
|
#finalize_request(c) ⇒ Object
158
159
160
161
162
163
164
165
166
|
# File 'lib/em-http/http_connection.rb', line 158
def finalize_request(c)
@conn.callback { c.connection_completed }
middleware.each do |m|
c.callback(&m.method(:response)) if m.respond_to?(:response)
end
@clients.push c
end
|
#middleware ⇒ Object
168
169
170
|
# File 'lib/em-http/http_connection.rb', line 168
def middleware
[HttpRequest.middleware, @middleware].flatten
end
|
#peer ⇒ Object
213
214
215
|
# File 'lib/em-http/http_connection.rb', line 213
def peer
Socket.unpack_sockaddr_in(@peer)[1] rescue nil
end
|
#post_init ⇒ Object
172
173
174
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
205
206
207
|
# File 'lib/em-http/http_connection.rb', line 172
def post_init
@clients = []
@pending = []
@p = Http::Parser.new
@p. = :mixed
@p. = proc do |h|
if client
if @p.status_code == 100
client.send_request_body
@p.reset!
else
client.(h, @p.http_version, @p.status_code)
:reset if client.req.no_body?
end
else
@p.reset!
unbind
end
end
@p.on_body = proc do |b|
client.on_body_data(b)
end
@p.on_message_complete = proc do
if !client.continue?
c = @clients.shift
c.state = :finished
c.on_request_complete
end
end
end
|
#receive_data(data) ⇒ Object
217
218
219
220
221
222
223
224
|
# File 'lib/em-http/http_connection.rb', line 217
def receive_data(data)
begin
@p << data
rescue HTTP::Parser::Error => e
c = @clients.shift
c.nil? ? unbind(e.message) : c.on_error(e.message)
end
end
|
#redirect(client, new_location) ⇒ Object
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# File 'lib/em-http/http_connection.rb', line 243
def redirect(client, new_location)
old_location = client.req.uri
new_location = client.req.set_uri(new_location)
if client.req.keepalive
if old_location.origin != new_location.origin
conn = HttpConnection.new
client.conn = conn
conn.connopts = @connopts
conn.connopts.https = new_location.scheme == "https"
conn.uri = client.req.uri
conn.activate_connection(client)
else
@clients.push client
client.connection_completed
end
else
@pending.push client
end
end
|
#send_data(data) ⇒ Object
303
304
305
|
# File 'lib/em-http/http_connection.rb', line 303
def send_data(data)
@conn.send_data data
end
|
#setup_request(method, options = {}, c = nil) ⇒ Object
152
153
154
155
156
|
# File 'lib/em-http/http_connection.rb', line 152
def setup_request(method, options = {}, c = nil)
c ||= HttpClient.new(self, HttpClientOptions.new(@uri, options, method))
@deferred ? activate_connection(c) : finalize_request(c)
c
end
|
#start ⇒ Object
238
239
240
241
|
# File 'lib/em-http/http_connection.rb', line 238
def start
@conn.start_tls(@connopts.tls) if client && client.req.ssl?
@conn.succeed
end
|
#stream_data(io, opts = {}) ⇒ Object
311
312
313
|
# File 'lib/em-http/http_connection.rb', line 311
def stream_data(io, opts = {})
EventMachine::IOStreamer.new(self, io, opts)
end
|
#stream_file_data(filename, args = {}) ⇒ Object
307
308
309
|
# File 'lib/em-http/http_connection.rb', line 307
def stream_file_data(filename, args = {})
@conn.stream_file_data filename, args
end
|
#unbind(reason = nil) ⇒ Object
Also known as:
close
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
|
# File 'lib/em-http/http_connection.rb', line 272
def unbind(reason = nil)
@clients.map { |c| c.unbind(reason) }
if r = @pending.shift
@clients.push r
r.reset!
@p.reset!
begin
@conn.set_deferred_status :unknown
if @connopts.proxy
@conn.reconnect(@connopts.host, @connopts.port)
else
@conn.reconnect(r.req.host, r.req.port)
end
@conn.pending_connect_timeout = @connopts.connect_timeout
@conn.comm_inactivity_timeout = @connopts.inactivity_timeout
@conn.callback { r.connection_completed }
rescue EventMachine::ConnectionError => e
@clients.pop.close(e.message)
end
else
@deferred = true
@conn.close_connection
end
end
|
#use(klass, *args, &block) ⇒ Object
209
210
211
|
# File 'lib/em-http/http_connection.rb', line 209
def use(klass, *args, &block)
@middleware << klass.new(*args, &block)
end
|