Class: UV::HttpEndpoint
- Inherits:
-
Object
- Object
- UV::HttpEndpoint
- Defined in:
- lib/uv-rays/http_endpoint.rb
Overview
HTTP Proxy - connect to proxy GET #url_with_host HTTP/1.1\r\n" Proxy-Authorization: Basic #encoded_credentials\r\n \r\n
Defined Under Namespace
Classes: Connection
Constant Summary collapse
- @@defaults =
{ :path => '/', :keepalive => true }
Instance Attribute Summary collapse
-
#cookiejar ⇒ Object
readonly
Returns the value of attribute cookiejar.
-
#encoded_host ⇒ Object
readonly
Returns the value of attribute encoded_host.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#inactivity_timeout ⇒ Object
Returns the value of attribute inactivity_timeout.
-
#middleware ⇒ Object
readonly
Returns the value of attribute middleware.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#tls ⇒ Object
readonly
Returns the value of attribute tls.
-
#tls_options ⇒ Object
readonly
Returns the value of attribute tls_options.
Instance Method Summary collapse
- #cancel_all ⇒ Object
- #connection_closed(request, reason) ⇒ Object
-
#connection_ready ⇒ Object
Callbacks.
- #data_received(data) ⇒ Object
- #delete(options = {}) ⇒ Object
- #get(options = {}) ⇒ Object
- #head(options = {}) ⇒ Object
- #http_proxy? ⇒ Boolean
-
#initialize(host, options = {}) ⇒ HttpEndpoint
constructor
A new instance of HttpEndpoint.
- #options(options = {}) ⇒ Object
- #patch(options = {}) ⇒ Object
- #post(options = {}) ⇒ Object
- #put(options = {}) ⇒ Object
- #request(method, options = {}) ⇒ Object
Constructor Details
#initialize(host, options = {}) ⇒ HttpEndpoint
Returns a new instance of HttpEndpoint.
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 130 def initialize(host, = {}) @queue = [] @parser = Http::Parser.new @thread = reactor @connection = nil @options = @@defaults.merge() @tls_options = [:tls_options] || {} @inactivity_timeout = [: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 = [] end |
Instance Attribute Details
#cookiejar ⇒ Object (readonly)
Returns the value of attribute cookiejar.
157 158 159 |
# File 'lib/uv-rays/http_endpoint.rb', line 157 def @cookiejar end |
#encoded_host ⇒ Object (readonly)
Returns the value of attribute encoded_host.
156 157 158 |
# File 'lib/uv-rays/http_endpoint.rb', line 156 def encoded_host @encoded_host end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
156 157 158 |
# File 'lib/uv-rays/http_endpoint.rb', line 156 def host @host end |
#inactivity_timeout ⇒ Object
Returns the value of attribute inactivity_timeout.
155 156 157 |
# File 'lib/uv-rays/http_endpoint.rb', line 155 def inactivity_timeout @inactivity_timeout end |
#middleware ⇒ Object (readonly)
Returns the value of attribute middleware.
157 158 159 |
# File 'lib/uv-rays/http_endpoint.rb', line 157 def middleware @middleware end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
156 157 158 |
# File 'lib/uv-rays/http_endpoint.rb', line 156 def port @port end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
157 158 159 |
# File 'lib/uv-rays/http_endpoint.rb', line 157 def proxy @proxy end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
156 157 158 |
# File 'lib/uv-rays/http_endpoint.rb', line 156 def scheme @scheme end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
157 158 159 |
# File 'lib/uv-rays/http_endpoint.rb', line 157 def thread @thread end |
#tls ⇒ Object (readonly)
Returns the value of attribute tls.
156 157 158 |
# File 'lib/uv-rays/http_endpoint.rb', line 156 def tls @tls end |
#tls_options ⇒ Object (readonly)
Returns the value of attribute tls_options.
156 157 158 |
# File 'lib/uv-rays/http_endpoint.rb', line 156 def @tls_options end |
Instance Method Details
#cancel_all ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/uv-rays/http_endpoint.rb', line 226 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
208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/uv-rays/http_endpoint.rb', line 208 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
199 200 201 202 203 204 205 206 |
# File 'lib/uv-rays/http_endpoint.rb', line 199 def connection_ready if @queue.length > 0 restart_timer next_request else close_connection end end |
#data_received(data) ⇒ Object
221 222 223 224 |
# File 'lib/uv-rays/http_endpoint.rb', line 221 def data_received(data) restart_timer close_connection if @parser.received(data) end |
#delete(options = {}) ⇒ Object
162 |
# File 'lib/uv-rays/http_endpoint.rb', line 162 def delete( = {}); request(:delete, ); end |
#get(options = {}) ⇒ Object
160 |
# File 'lib/uv-rays/http_endpoint.rb', line 160 def get( = {}); request(:get, ); end |
#head(options = {}) ⇒ Object
161 |
# File 'lib/uv-rays/http_endpoint.rb', line 161 def head( = {}); request(:head, ); end |
#http_proxy? ⇒ Boolean
238 239 240 |
# File 'lib/uv-rays/http_endpoint.rb', line 238 def http_proxy? @proxy && !@tls end |
#options(options = {}) ⇒ Object
166 |
# File 'lib/uv-rays/http_endpoint.rb', line 166 def ( = {}); request(:options, ); end |
#patch(options = {}) ⇒ Object
165 |
# File 'lib/uv-rays/http_endpoint.rb', line 165 def patch( = {}); request(:patch, ); end |
#post(options = {}) ⇒ Object
164 |
# File 'lib/uv-rays/http_endpoint.rb', line 164 def post( = {}); request(:post, ); end |
#put(options = {}) ⇒ Object
163 |
# File 'lib/uv-rays/http_endpoint.rb', line 163 def put( = {}); request(:put, ); end |
#request(method, options = {}) ⇒ Object
169 170 171 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 |
# File 'lib/uv-rays/http_endpoint.rb', line 169 def request(method, = {}) = @options.merge() [:method] = method.to_sym # Setup the request with callbacks request = Http::Request.new(self, ) 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 |