class UV::HttpEndpoint

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

Attributes

cookiejar[R]
encoded_host[R]
host[R]
inactivity_timeout[RW]
middleware[R]
port[R]
proxy[R]
scheme[R]
thread[R]
tls[R]
tls_options[R]

Public Class Methods

new(host, options = {}) click to toggle source
# 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

Public Instance Methods

cancel_all() click to toggle source
# 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) click to toggle source
# 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_ready() click to toggle source

Callbacks

# 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) click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 252
def data_received(data)
    restart_timer
    close_connection if @parser.received(data)
end
delete(options = {}) click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 179
def delete(options = {});  request(:delete,  options); end
get(options = {}) click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 177
def get(options = {});     request(:get,     options); end
head(options = {}) click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 178
def head(options = {});    request(:head,    options); end
http_proxy?() click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 269
def http_proxy?
    @proxy && !@tls
end
options(options = {}) click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 183
def options(options = {}); request(:options, options); end
patch(options = {}) click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 182
def patch(options = {});   request(:patch,   options); end
post(options = {}) click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 181
def post(options = {});    request(:post,    options); end
put(options = {}) click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 180
def put(options = {});     request(:put,     options); end
request(method, options = {}) click to toggle source
# 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

Private Instance Methods

close_connection() click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 304
def close_connection
    # Close connection can be called while connecting
    return if @closing || @connection.nil?
    @closing = true
    @connection.close_connection
    stop_timer
    @connection = nil
end
idle_timeout() click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 329
def idle_timeout
    connection = @connection
    close_connection
    @parser.reason = :timeout if @parser.request
    connection.reason = :timeout if connection
end
new_connection() click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 293
def new_connection
    # no new connections while transitioning state
    return if @closing || @connecting
    if @queue.length > 0 && @connection.nil?
        @connecting = true
        @connection = Connection.new(@host, @port, @tls, @proxy, self)
        start_timer
    end
    @connection
end
next_request() click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 277
def next_request
    # Don't start a request while transitioning state
    return if @closing || @connecting
    return if @parser.request || @queue.length == 0

    if @connection
        req = @queue.pop
        @connection.request = req
        @parser.new_request(req)

        req.execute(@connection)
    else
        new_connection
    end
end
restart_timer()
Alias for: start_timer
start_timer() click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 313
def start_timer
    # Only start the timer if there is a connection starting or in place
    return if @closing || @connection.nil?
    @timer.cancel if @timer
    @timer = @thread.scheduler.in(@inactivity_timeout) do
        @timer = nil
        idle_timeout
    end
end
Also aliased as: restart_timer
stop_timer() click to toggle source
# File lib/uv-rays/http_endpoint.rb, line 324
def stop_timer
    @timer.cancel unless @timer.nil?
    @timer = nil
end