Class: UV::Http::Request
- Inherits:
-
Libuv::Q::DeferredPromise
- Object
- Libuv::Q::DeferredPromise
- UV::Http::Request
show all
- Includes:
- Encoding
- Defined in:
- lib/uv-rays/http/request.rb
Constant Summary
collapse
- COOKIE =
'cookie'
- CONNECTION =
:Connection
- CRLF =
"\r\n"
Constants included
from Encoding
Encoding::FIELD_ENCODING, Encoding::HTTP_REQUEST_HEADER
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Encoding
#bytesize, #encode_auth, #encode_cookie, #encode_field, #encode_headers, #encode_param, #encode_query, #encode_request, #escape, #form_encode_body, #munge_header_keys, #unescape
Constructor Details
#initialize(endpoint, options) ⇒ Request
Returns a new instance of Request.
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/uv-rays/http/request.rb', line 31
def initialize(endpoint, options)
super(endpoint.thread, endpoint.thread.defer)
@options = options
@endpoint = endpoint
@ntlm_creds = options[:ntlm]
@digest_creds = options[:digest]
@challenge_retries = 0
@path = options[:path]
@method = options[:method]
@uri = "#{endpoint.scheme}://#{encode_host(endpoint.host, endpoint.port)}#{@path}"
@error = proc { |reason| reject(reason) }
end
|
Instance Attribute Details
#method ⇒ Object
Returns the value of attribute method.
19
20
21
|
# File 'lib/uv-rays/http/request.rb', line 19
def method
@method
end
|
#options ⇒ Object
Returns the value of attribute options.
19
20
21
|
# File 'lib/uv-rays/http/request.rb', line 19
def options
@options
end
|
#path ⇒ Object
Returns the value of attribute path.
19
20
21
|
# File 'lib/uv-rays/http/request.rb', line 19
def path
@path
end
|
Instance Method Details
#cookies_hash ⇒ Object
22
23
24
|
# File 'lib/uv-rays/http/request.rb', line 22
def cookies_hash
@endpoint.cookiejar.get_hash(@uri)
end
|
#execute(transport) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
140
141
142
143
144
145
146
147
|
# File 'lib/uv-rays/http/request.rb', line 84
def execute(transport)
if @options[:ntlm]
@options[:headers] ||= {}
@options[:headers][:Authorization] ||=
end
head, body = build_request, @options[:body]
@transport = transport
@endpoint.middleware.each do |m|
begin
head, body = m.request(self, head, body) if m.respond_to?(:request)
rescue => e
reject e
return
end
end
body = body.is_a?(Hash) ? form_encode_body(body) : body
file = @options[:file]
query = @options[:query]
head['content-length'] = File.size(file) if file
if body
head['content-length'] = body.bytesize
elsif method == :post or method == :put
head['content-length'] ||= 0
end
if !head['content-type'] and @options[:body].is_a? Hash
head['content-type'] = 'application/x-www-form-urlencoded'
end
= encode_request(method, path, query)
<< (head)
<< CRLF
if body
<< body
transport.write().catch @error
elsif file
transport.write().catch @error
fileRef = @endpoint.reactor.file file, File::RDONLY do
pSend = fileRef.send_file(transport, using: :raw, wait: :promise)
pSend.catch @error
pSend.finally do
fileRef.close
end
end
fileRef.catch @error
else
transport.write().catch @error
end
end
|
#notify(*args) ⇒ Object
149
150
151
|
# File 'lib/uv-rays/http/request.rb', line 149
def notify(*args)
@defer.notify(*args)
end
|
159
160
161
|
# File 'lib/uv-rays/http/request.rb', line 159
def (callback, &blk)
@headers_callback = callback
end
|
#reject(reason) ⇒ Object
80
81
82
|
# File 'lib/uv-rays/http/request.rb', line 80
def reject(reason)
@defer.reject(reason)
end
|
#resolve(response, parser = nil) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/uv-rays/http/request.rb', line 49
def resolve(response, parser = nil)
if response.status == 401 && @challenge_retries == 0 && response[:"WWW-Authenticate"]
challenge = response[:"WWW-Authenticate"]
begin
if @ntlm_creds && challenge[0..3] == 'NTLM'
@options[:headers] ||= {}
@options[:headers][:Authorization] = (challenge)
@challenge_retries += 1
execute(@transport)
return false
elsif @digest_creds && challenge[0..5] == 'Digest'
@options[:headers] ||= {}
@options[:headers][:Authorization] = (challenge)
@challenge_retries += 1
execute(@transport)
return false
end
rescue => e
reject e
true
end
end
@transport = nil
@defer.resolve(response)
true
end
|
#set_cookie(value) ⇒ Object
26
27
28
|
# File 'lib/uv-rays/http/request.rb', line 26
def set_cookie(value)
@endpoint.cookiejar.set(@uri, value)
end
|
153
154
155
|
# File 'lib/uv-rays/http/request.rb', line 153
def (head)
@headers_callback.call(head) if @headers_callback
end
|
#streaming? ⇒ Boolean
163
164
165
|
# File 'lib/uv-rays/http/request.rb', line 163
def streaming?
@options[:streaming]
end
|