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) ⇒ Object
-
#connection_ready ⇒ Object
Callbacks.
- #data_received(data) ⇒ Object
- #delete(options = {}, &blk) ⇒ Object
- #get(options = {}, &blk) ⇒ Object
- #head(options = {}, &blk) ⇒ Object
-
#initialize(host, options = {}) ⇒ HttpEndpoint
constructor
A new instance of HttpEndpoint.
- #options(options = {}, &blk) ⇒ Object
- #patch(options = {}, &blk) ⇒ Object
- #post(options = {}, &blk) ⇒ Object
- #put(options = {}, &blk) ⇒ Object
- #request(method, options = {}, &blk) ⇒ Object
Constructor Details
#initialize(host, options = {}) ⇒ HttpEndpoint
Returns a new instance of HttpEndpoint.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/uv-rays/http_endpoint.rb', line 75 def initialize(host, = {}) @queue = [] @parser = Http::Parser.new @thread = Libuv::Loop.current || Libuv::Loop.default @connection = nil = @@defaults.merge() = [: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.new @middleware = [] end |
Instance Attribute Details
#cookiejar ⇒ Object (readonly)
Returns the value of attribute cookiejar.
102 103 104 |
# File 'lib/uv-rays/http_endpoint.rb', line 102 def end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
101 102 103 |
# File 'lib/uv-rays/http_endpoint.rb', line 101 def host @host end |
#inactivity_timeout ⇒ Object (readonly)
Returns the value of attribute inactivity_timeout.
100 101 102 |
# File 'lib/uv-rays/http_endpoint.rb', line 100 def inactivity_timeout @inactivity_timeout end |
#middleware ⇒ Object (readonly)
Returns the value of attribute middleware.
102 103 104 |
# File 'lib/uv-rays/http_endpoint.rb', line 102 def middleware @middleware end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
101 102 103 |
# File 'lib/uv-rays/http_endpoint.rb', line 101 def port @port end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
101 102 103 |
# File 'lib/uv-rays/http_endpoint.rb', line 101 def scheme @scheme end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
100 101 102 |
# File 'lib/uv-rays/http_endpoint.rb', line 100 def thread @thread end |
#tls ⇒ Object (readonly)
Returns the value of attribute tls.
101 102 103 |
# File 'lib/uv-rays/http_endpoint.rb', line 101 def tls @tls end |
#tls_options ⇒ Object (readonly)
Returns the value of attribute tls_options.
101 102 103 |
# File 'lib/uv-rays/http_endpoint.rb', line 101 def end |
Instance Method Details
#cancel_all ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/uv-rays/http_endpoint.rb', line 171 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) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/uv-rays/http_endpoint.rb', line 153 def connection_closed(request) # 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 :connection_failure end end |
#connection_ready ⇒ Object
Callbacks
144 145 146 147 148 149 150 151 |
# File 'lib/uv-rays/http_endpoint.rb', line 144 def connection_ready if @queue.length > 0 restart_timer next_request else close_connection end end |
#data_received(data) ⇒ Object
166 167 168 169 |
# File 'lib/uv-rays/http_endpoint.rb', line 166 def data_received(data) restart_timer close_connection if @parser.received(data) end |
#delete(options = {}, &blk) ⇒ Object
107 |
# File 'lib/uv-rays/http_endpoint.rb', line 107 def delete = {}, &blk; request(:delete, , &blk); end |
#get(options = {}, &blk) ⇒ Object
105 |
# File 'lib/uv-rays/http_endpoint.rb', line 105 def get = {}, &blk; request(:get, , &blk); end |
#head(options = {}, &blk) ⇒ Object
106 |
# File 'lib/uv-rays/http_endpoint.rb', line 106 def head = {}, &blk; request(:head, , &blk); end |
#options(options = {}, &blk) ⇒ Object
111 |
# File 'lib/uv-rays/http_endpoint.rb', line 111 def = {}, &blk; request(:options, , &blk); end |
#patch(options = {}, &blk) ⇒ Object
110 |
# File 'lib/uv-rays/http_endpoint.rb', line 110 def patch = {}, &blk; request(:patch, , &blk); end |
#post(options = {}, &blk) ⇒ Object
109 |
# File 'lib/uv-rays/http_endpoint.rb', line 109 def post = {}, &blk; request(:post, , &blk); end |
#put(options = {}, &blk) ⇒ Object
108 |
# File 'lib/uv-rays/http_endpoint.rb', line 108 def put = {}, &blk; request(:put, , &blk); end |
#request(method, options = {}, &blk) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/uv-rays/http_endpoint.rb', line 114 def request(method, = {}, &blk) = .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| close_connection next_request ::Libuv::Q.reject(@thread, err) }) @queue.unshift(request) next_request request end |