Class: UV::HttpEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/uv-rays/http_endpoint.rb

Overview

HTTP Proxy - connect to proxy GET #url_with_host HTTP/1.1rn“ Proxy-Authorization: Basic #encoded_credentialsrn rn

Defined Under Namespace

Classes: Connection

Constant Summary collapse

@@defaults =
{
    :path => '/',
    :keepalive => true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ HttpEndpoint

Returns a new instance of HttpEndpoint.



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
# File 'lib/uv-rays/http_endpoint.rb', line 144

def initialize(host, options = {})
    @queue = []
    @parser = Http::Parser.new
    @thread = reactor
    @connection = nil

    @options = @@defaults.merge(options)
    @tls_options = options[:tls_options] || {}
    @inactivity_timeout = options[:inactivity_timeout] || 10000

    uri = host.is_a?(::URI) ? host : ::URI.parse(host)
    @port = uri.port
    @host = uri.host

    default_port = uri.port == uri.default_port
    @encoded_host = default_port ? @host : "#{@host}:#{@port}"
    @proxy = @options[:proxy]

    @scheme = uri.scheme
    @tls = @scheme == 'https'
    @cookiejar = CookieJar.new
    @middleware = []

    @closing = false
    @connecting = false
end

Instance Attribute Details

#cookiejarObject (readonly)

Returns the value of attribute cookiejar.



174
175
176
# File 'lib/uv-rays/http_endpoint.rb', line 174

def cookiejar
  @cookiejar
end

#encoded_hostObject (readonly)

Returns the value of attribute encoded_host.



173
174
175
# File 'lib/uv-rays/http_endpoint.rb', line 173

def encoded_host
  @encoded_host
end

#hostObject (readonly)

Returns the value of attribute host.



173
174
175
# File 'lib/uv-rays/http_endpoint.rb', line 173

def host
  @host
end

#inactivity_timeoutObject

Returns the value of attribute inactivity_timeout.



172
173
174
# File 'lib/uv-rays/http_endpoint.rb', line 172

def inactivity_timeout
  @inactivity_timeout
end

#middlewareObject (readonly)

Returns the value of attribute middleware.



174
175
176
# File 'lib/uv-rays/http_endpoint.rb', line 174

def middleware
  @middleware
end

#portObject (readonly)

Returns the value of attribute port.



173
174
175
# File 'lib/uv-rays/http_endpoint.rb', line 173

def port
  @port
end

#proxyObject (readonly)

Returns the value of attribute proxy.



174
175
176
# File 'lib/uv-rays/http_endpoint.rb', line 174

def proxy
  @proxy
end

#schemeObject (readonly)

Returns the value of attribute scheme.



173
174
175
# File 'lib/uv-rays/http_endpoint.rb', line 173

def scheme
  @scheme
end

#threadObject (readonly)

Returns the value of attribute thread.



174
175
176
# File 'lib/uv-rays/http_endpoint.rb', line 174

def thread
  @thread
end

#tlsObject (readonly)

Returns the value of attribute tls.



173
174
175
# File 'lib/uv-rays/http_endpoint.rb', line 173

def tls
  @tls
end

#tls_optionsObject (readonly)

Returns the value of attribute tls_options.



173
174
175
# File 'lib/uv-rays/http_endpoint.rb', line 173

def tls_options
  @tls_options
end

Instance Method Details

#cancel_allObject



257
258
259
260
261
262
263
264
265
266
267
# File 'lib/uv-rays/http_endpoint.rb', line 257

def cancel_all
    @queue.each do |request|
        request.reject(:cancelled)
    end
    if @parser.request
        @parser.request.reject(:cancelled)
        @parser.eof
    end
    @queue = []
    close_connection
end

#connection_closed(request, reason) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/uv-rays/http_endpoint.rb', line 232

def connection_closed(request, reason)
    # A connection might close due to a connection failure
    awaiting_close = @closing
    awaiting_connect = @connecting
    @closing = false
    @connecting = false
    @connection = nil

    # We may have closed a previous connection
    if @parser.request && (request.nil? || request == @parser.request)
        stop_timer
        @parser.eof
    elsif request.nil? && @parser.request.nil? && @queue.length > 0
        req = @queue.pop
        req.reject(reason || :connection_failure)
    end

    next_request if awaiting_close || awaiting_connect
end

#connection_readyObject

Callbacks



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/uv-rays/http_endpoint.rb', line 219

def connection_ready
    # A connection can be closed while still connecting
    return if @closing

    @connecting = false
    if @queue.length > 0
        restart_timer
        next_request
    else
        close_connection
    end
end

#data_received(data) ⇒ Object



252
253
254
255
# File 'lib/uv-rays/http_endpoint.rb', line 252

def data_received(data)
    restart_timer
    close_connection if @parser.received(data)
end

#delete(options = {}) ⇒ Object



179
# File 'lib/uv-rays/http_endpoint.rb', line 179

def delete(options = {});  request(:delete,  options); end

#get(options = {}) ⇒ Object



177
# File 'lib/uv-rays/http_endpoint.rb', line 177

def get(options = {});     request(:get,     options); end

#head(options = {}) ⇒ Object



178
# File 'lib/uv-rays/http_endpoint.rb', line 178

def head(options = {});    request(:head,    options); end

#http_proxy?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/uv-rays/http_endpoint.rb', line 269

def http_proxy?
    @proxy && !@tls
end

#options(options = {}) ⇒ Object



183
# File 'lib/uv-rays/http_endpoint.rb', line 183

def options(options = {}); request(:options, options); end

#patch(options = {}) ⇒ Object



182
# File 'lib/uv-rays/http_endpoint.rb', line 182

def patch(options = {});   request(:patch,   options); end

#post(options = {}) ⇒ Object



181
# File 'lib/uv-rays/http_endpoint.rb', line 181

def post(options = {});    request(:post,    options); end

#put(options = {}) ⇒ Object



180
# File 'lib/uv-rays/http_endpoint.rb', line 180

def put(options = {});     request(:put,     options); end

#request(method, options = {}) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/uv-rays/http_endpoint.rb', line 186

def request(method, options = {})
    options = @options.merge(options)
    options[:method] = method.to_sym

    # Setup the request with callbacks
    request = Http::Request.new(self, options)
    request.then(proc { |response|
        if response.keep_alive
            restart_timer
        else
            # We might have already started processing the next request
            # at this point. So don't want to disconnect if already
            # disconnected.
            close_connection unless @connecting
        end

        next_request

        response
    }, proc { |err|
        # @parser.eof
        close_connection unless @connecting
        next_request
        ::Libuv::Q.reject(@thread, err)
    })

    @queue.unshift(request)

    next_request
    request
end