Class: UV::HttpEndpoint
Constant Summary
collapse
- TRANSFER_ENCODING =
"TRANSFER_ENCODING".freeze
- CONTENT_ENCODING =
"CONTENT_ENCODING".freeze
- CONTENT_LENGTH =
"CONTENT_LENGTH".freeze
- CONTENT_TYPE =
"CONTENT_TYPE".freeze
- LAST_MODIFIED =
"LAST_MODIFIED".freeze
- KEEP_ALIVE =
"CONNECTION".freeze
- LOCATION =
"LOCATION".freeze
- HOST =
"HOST".freeze
- ETAG =
"ETAG".freeze
- CRLF =
"\r\n".freeze
- HTTPS =
"https://".freeze
- HTTP =
"http://".freeze
- @@defaults =
{
:path => '/',
:keepalive => true
}
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#close_connection(after_writing = false) ⇒ Object
-
#delete(options = {}, &blk) ⇒ Object
-
#get(options = {}, &blk) ⇒ Object
-
#head(options = {}, &blk) ⇒ Object
-
#initialize(uri, options = {}) ⇒ HttpEndpoint
constructor
A new instance of HttpEndpoint.
-
#middleware ⇒ Object
-
#next_request ⇒ Object
-
#ntlm_auth_header(challenge = nil) ⇒ Object
-
#on_close ⇒ Object
-
#on_connect(transport) ⇒ Object
-
#on_read(data, *args) ⇒ Object
-
#options(options = {}, &blk) ⇒ Object
-
#patch(options = {}, &blk) ⇒ Object
-
#post(options = {}, &blk) ⇒ Object
-
#process_request ⇒ Object
-
#put(options = {}, &blk) ⇒ Object
-
#request(method, options = {}, &blk) ⇒ Object
-
#try_send ⇒ Object
#reconnect, #use_tls
#keepalive, #stream_file, #write
Methods inherited from Connection
#pause, #paused?, #post_init, #resume
Constructor Details
#initialize(uri, options = {}) ⇒ HttpEndpoint
Returns a new instance of HttpEndpoint.
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/uv-rays/http_endpoint.rb', line 55
def initialize(uri, options = {})
@inactivity_timeout = options[:inactivity_timeout] ||= 10000 @ntlm_creds = options[:ntlm]
uri = uri.kind_of?(Addressable::URI) ? uri : Addressable::URI::parse(uri.to_s)
@https = uri.scheme == "https"
uri.port ||= (@https ? 443 : 80)
@scheme = @https ? HTTPS : HTTP
@loop = Libuv::Loop.current || Libuv::Loop.default
@host = uri.host
@port = uri.port
@closed = true
@closing = false
@connecting = false
@pending_requests = []
@staging_request = nil
@waiting_response = nil
@cookiejar = CookieJar.new
@idle_timeout_method = method(:idle_timeout)
@response = Http::Response.new
if @inactivity_timeout > 0
@timer = @loop.timer
end
end
|
Instance Attribute Details
#cookiejar ⇒ Object
Returns the value of attribute cookiejar.
52
53
54
|
# File 'lib/uv-rays/http_endpoint.rb', line 52
def cookiejar
@cookiejar
end
|
#host ⇒ Object
Returns the value of attribute host.
52
53
54
|
# File 'lib/uv-rays/http_endpoint.rb', line 52
def host
@host
end
|
#inactivity_timeout ⇒ Object
Returns the value of attribute inactivity_timeout.
53
54
55
|
# File 'lib/uv-rays/http_endpoint.rb', line 53
def inactivity_timeout
@inactivity_timeout
end
|
#loop ⇒ Object
Returns the value of attribute loop.
52
53
54
|
# File 'lib/uv-rays/http_endpoint.rb', line 52
def loop
@loop
end
|
#port ⇒ Object
Returns the value of attribute port.
52
53
54
|
# File 'lib/uv-rays/http_endpoint.rb', line 52
def port
@port
end
|
#scheme ⇒ Object
Returns the value of attribute scheme.
52
53
54
|
# File 'lib/uv-rays/http_endpoint.rb', line 52
def scheme
@scheme
end
|
#using_tls ⇒ Object
Returns the value of attribute using_tls.
52
53
54
|
# File 'lib/uv-rays/http_endpoint.rb', line 52
def using_tls
@using_tls
end
|
Instance Method Details
#close_connection(after_writing = false) ⇒ Object
248
249
250
251
252
253
254
255
256
|
# File 'lib/uv-rays/http_endpoint.rb', line 248
def close_connection(after_writing = false)
stop_timer
reqs = @pending_requests
@pending_requests.clear
reqs.each do |request|
request.reject(:close_connection)
end
super(after_writing) if @transport
end
|
#delete(options = {}, &blk) ⇒ Object
96
|
# File 'lib/uv-rays/http_endpoint.rb', line 96
def delete options = {}, &blk; request(:delete, options, &blk); end
|
#get(options = {}, &blk) ⇒ Object
94
|
# File 'lib/uv-rays/http_endpoint.rb', line 94
def get options = {}, &blk; request(:get, options, &blk); end
|
#head(options = {}, &blk) ⇒ Object
95
|
# File 'lib/uv-rays/http_endpoint.rb', line 95
def head options = {}, &blk; request(:head, options, &blk); end
|
#middleware ⇒ Object
233
234
235
236
|
# File 'lib/uv-rays/http_endpoint.rb', line 233
def middleware
[]
end
|
#next_request ⇒ Object
142
143
144
145
|
# File 'lib/uv-rays/http_endpoint.rb', line 142
def next_request
@staging_request = @pending_requests.shift
process_request unless @staging_request.nil?
end
|
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/uv-rays/http_endpoint.rb', line 258
def (challenge = nil)
if @ntlm_auth && challenge.nil?
return @ntlm_auth
elsif challenge
scheme, param_str = (challenge)
if param_str.nil?
@ntlm_auth = nil
return (creds)
else
t2 = Net::NTLM::Message.decode64(param_str)
t3 = t2.response(@ntlm_creds, ntlmv2: true)
@ntlm_auth = "NTLM #{t3.encode64}"
return @ntlm_auth
end
else
domain = @ntlm_creds[:domain]
t1 = Net::NTLM::Message::Type1.new()
t1.domain = domain if domain
@ntlm_auth = "NTLM #{t1.encode64}"
return @ntlm_auth
end
end
|
#on_close ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/uv-rays/http_endpoint.rb', line 179
def on_close
@closed = true
clear_staging = @connecting == @staging_request
@connecting = false
stop_timer
@loop.next_tick do
if @closing
@closing = false
@connecting = false
if @staging_request
process_request
else
next_request
end
else
if clear_staging
@staging_request.reject(:connection_refused)
elsif @waiting_response
@response.eof if @response.request
@waiting_response.reject(:disconnected)
elsif @staging_request
process_request
end
end
end
end
|
#on_connect(transport) ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/uv-rays/http_endpoint.rb', line 159
def on_connect(transport)
@connecting = false
@closed = false
use_tls() if @https
stop_timer
if @inactivity_timeout > 0
@timer.progress @idle_timeout_method
@timer.start @inactivity_timeout
end
@response.reset!
try_send end
|
#on_read(data, *args) ⇒ Object
238
239
240
241
242
243
244
245
246
|
# File 'lib/uv-rays/http_endpoint.rb', line 238
def on_read(data, *args)
@timer.again if @inactivity_timeout > 0
if @response.receive(data)
@transport.close
end
end
|
#options(options = {}, &blk) ⇒ Object
100
|
# File 'lib/uv-rays/http_endpoint.rb', line 100
def options options = {}, &blk; request(:options, options, &blk); end
|
#patch(options = {}, &blk) ⇒ Object
99
|
# File 'lib/uv-rays/http_endpoint.rb', line 99
def patch options = {}, &blk; request(:patch, options, &blk); end
|
#post(options = {}, &blk) ⇒ Object
98
|
# File 'lib/uv-rays/http_endpoint.rb', line 98
def post options = {}, &blk; request(:post, options, &blk); end
|
#process_request ⇒ Object
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/uv-rays/http_endpoint.rb', line 147
def process_request
if @closed && !@connecting
@transport = @loop.tcp
@connecting = @staging_request
::UV.try_connect(@transport, self, @host, @port)
elsif !@closing
try_send
end
end
|
#put(options = {}, &blk) ⇒ Object
97
|
# File 'lib/uv-rays/http_endpoint.rb', line 97
def put options = {}, &blk; request(:put, options, &blk); end
|
#request(method, options = {}, &blk) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
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
|
# File 'lib/uv-rays/http_endpoint.rb', line 103
def request(method, options = {}, &blk)
options = @@defaults.merge(options)
options[:method] = method
request = Http::Request.new(self, options, @ntlm_creds)
request.then(proc { |result|
@waiting_response = nil
if @closed || result[:headers].keep_alive
next_request
else
@closing = true
@transport.close
end
result
}, proc { |err|
@waiting_response = nil
next_request
::Libuv::Q.reject(@loop, err)
})
request.then blk if blk
@pending_requests << request
if !@waiting_response && !@staging_request
next_request
end
request
end
|
#try_send ⇒ Object
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
# File 'lib/uv-rays/http_endpoint.rb', line 213
def try_send
@waiting_response = @staging_request
@response.request = @staging_request
@staging_request = nil
if @ntlm_creds
opts = @waiting_response.options
opts[:headers] ||= {}
opts = opts[:headers]
opts[:Authorization] =
end
@timer.again if @inactivity_timeout > 0
@waiting_response.execute(@transport, proc { |err|
@transport.close
@waiting_response.reject(err)
})
end
|