Class: UV::HttpEndpoint
- Inherits:
-
Object
- Object
- UV::HttpEndpoint
- 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
-
#cookiejar ⇒ Object
readonly
Returns the value of attribute cookiejar.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#inactivity_timeout ⇒ Object
readonly
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.
-
#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
-
#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.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/uv-rays/http_endpoint.rb', line 81 def initialize(host, = {}) @queue = [] @parser = Http::Parser.new @thread = reactor @connection = nil @options = @@defaults.merge() @tls_options = [:tls_options] || {} @inactivity_timeout = [:inactivity_timeout] || 10000 @idle_timeout_method = method(:idle_timeout) if @inactivity_timeout > 0 @timer = @thread.timer end uri = URI.parse host @port = uri.port @host = uri.host @scheme = uri.scheme @tls = @scheme == 'https' @cookiejar = CookieJar.new @middleware = [] end |
Instance Attribute Details
#cookiejar ⇒ Object (readonly)
Returns the value of attribute cookiejar.
108 109 110 |
# File 'lib/uv-rays/http_endpoint.rb', line 108 def @cookiejar end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
107 108 109 |
# File 'lib/uv-rays/http_endpoint.rb', line 107 def host @host end |
#inactivity_timeout ⇒ Object (readonly)
Returns the value of attribute inactivity_timeout.
106 107 108 |
# File 'lib/uv-rays/http_endpoint.rb', line 106 def inactivity_timeout @inactivity_timeout end |
#middleware ⇒ Object (readonly)
Returns the value of attribute middleware.
108 109 110 |
# File 'lib/uv-rays/http_endpoint.rb', line 108 def middleware @middleware end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
107 108 109 |
# File 'lib/uv-rays/http_endpoint.rb', line 107 def port @port end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
107 108 109 |
# File 'lib/uv-rays/http_endpoint.rb', line 107 def scheme @scheme end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
106 107 108 |
# File 'lib/uv-rays/http_endpoint.rb', line 106 def thread @thread end |
#tls ⇒ Object (readonly)
Returns the value of attribute tls.
107 108 109 |
# File 'lib/uv-rays/http_endpoint.rb', line 107 def tls @tls end |
#tls_options ⇒ Object (readonly)
Returns the value of attribute tls_options.
107 108 109 |
# File 'lib/uv-rays/http_endpoint.rb', line 107 def @tls_options end |
Instance Method Details
#cancel_all ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/uv-rays/http_endpoint.rb', line 177 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
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/uv-rays/http_endpoint.rb', line 159 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
150 151 152 153 154 155 156 157 |
# File 'lib/uv-rays/http_endpoint.rb', line 150 def connection_ready if @queue.length > 0 restart_timer next_request else close_connection end end |
#data_received(data) ⇒ Object
172 173 174 175 |
# File 'lib/uv-rays/http_endpoint.rb', line 172 def data_received(data) restart_timer close_connection if @parser.received(data) end |
#delete(options = {}) ⇒ Object
113 |
# File 'lib/uv-rays/http_endpoint.rb', line 113 def delete( = {}); request(:delete, ); end |
#get(options = {}) ⇒ Object
111 |
# File 'lib/uv-rays/http_endpoint.rb', line 111 def get( = {}); request(:get, ); end |
#head(options = {}) ⇒ Object
112 |
# File 'lib/uv-rays/http_endpoint.rb', line 112 def head( = {}); request(:head, ); end |
#options(options = {}) ⇒ Object
117 |
# File 'lib/uv-rays/http_endpoint.rb', line 117 def ( = {}); request(:options, ); end |
#patch(options = {}) ⇒ Object
116 |
# File 'lib/uv-rays/http_endpoint.rb', line 116 def patch( = {}); request(:patch, ); end |
#post(options = {}) ⇒ Object
115 |
# File 'lib/uv-rays/http_endpoint.rb', line 115 def post( = {}); request(:post, ); end |
#put(options = {}) ⇒ Object
114 |
# File 'lib/uv-rays/http_endpoint.rb', line 114 def put( = {}); request(:put, ); end |
#request(method, options = {}) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/uv-rays/http_endpoint.rb', line 120 def request(method, = {}) = @options.merge() [:method] = method # 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 |