Class: UV::HttpEndpoint

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

Overview

CookieJar

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.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/uv-rays/http_endpoint.rb', line 87

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}"

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

Instance Attribute Details

#cookiejar ⇒ Object (readonly)

Returns the value of attribute cookiejar.



113
114
115
# File 'lib/uv-rays/http_endpoint.rb', line 113

def cookiejar
  @cookiejar
end

#encoded_host ⇒ Object (readonly)

Returns the value of attribute encoded_host.



112
113
114
# File 'lib/uv-rays/http_endpoint.rb', line 112

def encoded_host
  @encoded_host
end

#host ⇒ Object (readonly)

Returns the value of attribute host.



112
113
114
# File 'lib/uv-rays/http_endpoint.rb', line 112

def host
  @host
end

#inactivity_timeout ⇒ Object

Returns the value of attribute inactivity_timeout.



111
112
113
# File 'lib/uv-rays/http_endpoint.rb', line 111

def inactivity_timeout
  @inactivity_timeout
end

#middleware ⇒ Object (readonly)

Returns the value of attribute middleware.



113
114
115
# File 'lib/uv-rays/http_endpoint.rb', line 113

def middleware
  @middleware
end

#port ⇒ Object (readonly)

Returns the value of attribute port.



112
113
114
# File 'lib/uv-rays/http_endpoint.rb', line 112

def port
  @port
end

#scheme ⇒ Object (readonly)

Returns the value of attribute scheme.



112
113
114
# File 'lib/uv-rays/http_endpoint.rb', line 112

def scheme
  @scheme
end

#thread ⇒ Object (readonly)

Returns the value of attribute thread.



113
114
115
# File 'lib/uv-rays/http_endpoint.rb', line 113

def thread
  @thread
end

#tls ⇒ Object (readonly)

Returns the value of attribute tls.



112
113
114
# File 'lib/uv-rays/http_endpoint.rb', line 112

def tls
  @tls
end

#tls_options ⇒ Object (readonly)

Returns the value of attribute tls_options.



112
113
114
# File 'lib/uv-rays/http_endpoint.rb', line 112

def tls_options
  @tls_options
end

Instance Method Details

#cancel_all ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/uv-rays/http_endpoint.rb', line 182

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



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/uv-rays/http_endpoint.rb', line 164

def connection_closed(request, reason)
    # We may have closed a previous connection
    if @parser.request && (request.nil? || request == @parser.request)
        @connection = nil
        stop_timer

        @parser.eof
    elsif request.nil? && @parser.request.nil? && @queue.length > 0
        req = @queue.pop
        req.reject(reason || :connection_failure)
    end
end

#connection_ready ⇒ Object

Callbacks



155
156
157
158
159
160
161
162
# File 'lib/uv-rays/http_endpoint.rb', line 155

def connection_ready
    if @queue.length > 0
        restart_timer
        next_request
    else
        close_connection
    end
end

#data_received(data) ⇒ Object



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

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

#delete(options = {}) ⇒ Object



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

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

#get(options = {}) ⇒ Object



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

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

#head(options = {}) ⇒ Object



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

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

#options(options = {}) ⇒ Object



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

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

#patch(options = {}) ⇒ Object



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

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

#post(options = {}) ⇒ Object



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

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

#put(options = {}) ⇒ Object



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

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

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



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

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
            close_connection
        end

        next_request

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

    @queue.unshift(request)

    next_request
    request
end