Class: EventMachine::HttpConnection

Inherits:
Object
  • Object
show all
Includes:
HTTPMethods, Socksify
Defined in:
lib/em-http/http_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTPMethods

#delete, #get, #head, #post, #put

Constructor Details

#initializeHttpConnection

Returns a new instance of HttpConnection.



40
41
42
43
# File 'lib/em-http/http_connection.rb', line 40

def initialize
  @deferred = true
  @middleware = []
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



38
39
40
# File 'lib/em-http/http_connection.rb', line 38

def conn
  @conn
end

#connoptsObject

Returns the value of attribute connopts.



38
39
40
# File 'lib/em-http/http_connection.rb', line 38

def connopts
  @connopts
end

#deferredObject (readonly)

Returns the value of attribute deferred.



37
38
39
# File 'lib/em-http/http_connection.rb', line 37

def deferred
  @deferred
end

#errorObject

Returns the value of attribute error.



38
39
40
# File 'lib/em-http/http_connection.rb', line 38

def error
  @error
end

#uriObject

Returns the value of attribute uri.



38
39
40
# File 'lib/em-http/http_connection.rb', line 38

def uri
  @uri
end

Instance Method Details

#activate_connection(client) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/em-http/http_connection.rb', line 50

def activate_connection(client)
  begin
    EventMachine.connect(@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
    #
    # Currently, this can only fire on initial connection setup
    # since #connect is a synchronous method. Hence, rescue the
    # exception, and return a failed deferred which will immediately
    # fail any client request.
    #
    # Once there is async-DNS, then we'll iterate over the outstanding
    # client requests and fail them in order.
    #
    # Net outcome: failed connection will invoke the same ConnectionError
    # message on the connection deferred, and on the client deferred.
    #
    client.close(e.message)
  end
end

#connection_completedObject



141
142
143
144
145
146
147
148
149
# File 'lib/em-http/http_connection.rb', line 141

def connection_completed
  @peer = @conn.get_peername

  if @connopts.proxy && @connopts.proxy[:type] == :socks5
    socksify(client.req.uri.host, client.req.uri.port, *@connopts.proxy[:authorization]) { start }
  else
    start
  end
end

#finalize_request(c) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/em-http/http_connection.rb', line 87

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

#middlewareObject



97
98
99
# File 'lib/em-http/http_connection.rb', line 97

def middleware
  [HttpRequest.middleware, @middleware].flatten
end

#peerObject



128
129
130
# File 'lib/em-http/http_connection.rb', line 128

def peer
  Socket.unpack_sockaddr_in(@peer)[1] rescue nil
end

#post_initObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/em-http/http_connection.rb', line 101

def post_init
  @clients = []
  @pending = []

  @p = Http::Parser.new
  @p.header_value_type = :mixed
  @p.on_headers_complete = proc do |h|
    client.parse_response_header(h, @p.http_version, @p.status_code)
  end

  @p.on_body = proc do |b|
    client.on_body_data(b)
  end

  @p.on_message_complete = proc do
    if not client.continue?
      c = @clients.shift
      c.state = :finished
      c.on_request_complete
    end
  end
end

#receive_data(data) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/em-http/http_connection.rb', line 132

def receive_data(data)
  begin
    @p << data
  rescue HTTP::Parser::Error => e
    c = @clients.shift
    c.nil? ? unbind : c.on_error(e.message)
  end
end

#redirect(client) ⇒ Object



156
157
158
# File 'lib/em-http/http_connection.rb', line 156

def redirect(client)
  @pending.push client
end

#send_data(data) ⇒ Object



180
181
182
# File 'lib/em-http/http_connection.rb', line 180

def send_data(data)
  @conn.send_data data
end

#setup_request(method, options = {}, c = nil) ⇒ Object



81
82
83
84
85
# File 'lib/em-http/http_connection.rb', line 81

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

#startObject



151
152
153
154
# File 'lib/em-http/http_connection.rb', line 151

def start
  @conn.start_tls(@connopts.tls) if client && client.req.ssl?
  @conn.succeed
end

#stream_file_data(filename, args = {}) ⇒ Object



184
185
186
# File 'lib/em-http/http_connection.rb', line 184

def stream_file_data(filename, args = {})
  @conn.stream_file_data filename, args
end

#unbind(reason) ⇒ Object Also known as: close



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/em-http/http_connection.rb', line 160

def unbind(reason)
  @clients.map { |c| c.unbind(reason) }

  if r = @pending.shift
    @clients.push r

    r.reset!
    @p.reset!

    begin
      @conn.set_deferred_status :unknown
      @conn.reconnect(r.req.host, r.req.port)
      @conn.callback { r.connection_completed }
    rescue EventMachine::ConnectionError => e
      @clients.pop.close(e.message)
    end
  end
end

#use(klass, *args, &block) ⇒ Object



124
125
126
# File 'lib/em-http/http_connection.rb', line 124

def use(klass, *args, &block)
  @middleware << klass.new(*args, &block)
end