Class: UV::Http::Request

Inherits:
Libuv::Q::DeferredPromise
  • Object
show all
Includes:
Encoding
Defined in:
lib/uv-rays/http/request.rb

Constant Summary collapse

'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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/uv-rays/http/request.rb', line 32

def initialize(endpoint, options)
    super(endpoint.thread, endpoint.thread.defer)

    @host = endpoint.host
    @port = endpoint.port
    @http_proxy = endpoint.http_proxy? ? endpoint.proxy : nil
    @encoded_host = endpoint.encoded_host

    path = options[:path]
    if path.is_a?(::URI)
        @path = path.to_s.split(@encoded_host)[1] || '/'
    else
        @path = path
    end

    @method = options[:method]
    @cookiejar = endpoint.cookiejar
    @middleware = endpoint.middleware
    @uri = "#{endpoint.scheme}://#{@encoded_host}#{@path}"
    @path = @uri if @http_proxy
    endpoint = nil

    @options = options
    @ntlm_creds = options[:ntlm]
    @digest_creds = options[:digest]
    @challenge_retries = 0

    # Don't hold references to vars we don't require anymore
    self.finally {
        @host = @port = nil
        @cookiejar = nil
        @middleware = nil
    }
    @error = proc { |reason| reject(reason) }
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



20
21
22
# File 'lib/uv-rays/http/request.rb', line 20

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/uv-rays/http/request.rb', line 20

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/uv-rays/http/request.rb', line 20

def path
  @path
end

Instance Method Details

#cookies_hashObject



23
24
25
# File 'lib/uv-rays/http/request.rb', line 23

def cookies_hash
    @cookiejar.get_hash(@uri)
end

#execute(transport) ⇒ Object



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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/uv-rays/http/request.rb', line 105

def execute(transport)
    # configure ntlm request headers
    if @options[:ntlm]
        @options[:headers] ||= {}
        @options[:headers][:Authorization] ||= ntlm_auth_header
    end

    head, body = build_request, @options[:body]
    @transport = transport

    @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]

    # Set the Content-Length if file is given
    head['content-length'] = File.size(file) if file

    # Set the Content-Length if body is given,
    # or we're doing an empty post or put
    if body
        head['content-length'] = body.bytesize
    elsif method == :post or method == :put
        # wont happen if body is set and we already set content-length above
        head['content-length'] ||= 0
    end

    # Set content-type header if missing and body is a Ruby hash
    if !head['content-type'] and @options[:body].is_a? Hash
        head['content-type'] = 'application/x-www-form-urlencoded'
    end

    request_header = encode_request(method, @path, query)
    if @http_proxy && (@http_proxy[:username] || @http_proxy[:password])
        request_header << encode_auth('Proxy-Authorization', [@http_proxy[:username], @http_proxy[:password]])
    end
    request_header << encode_headers(head)
    request_header << CRLF

    if body
        request_header << body
        transport.write(request_header).catch &@error
    elsif file
        transport.write(request_header).catch &@error

        # Send file
        fileRef = @reactor.file file, File::RDONLY do
            # File is open and available for reading
            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(request_header).catch &@error
    end
end

#notify(*args) ⇒ Object



173
174
175
# File 'lib/uv-rays/http/request.rb', line 173

def notify(*args)
    @defer.notify(*args)
end

#on_headers(&callback) ⇒ Object



181
182
183
# File 'lib/uv-rays/http/request.rb', line 181

def on_headers(&callback)
    @headers_callback = callback
end

#reject(reason) ⇒ Object



101
102
103
# File 'lib/uv-rays/http/request.rb', line 101

def reject(reason)
    @defer.reject(reason)
end

#resolve(response, parser = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/uv-rays/http/request.rb', line 70

def resolve(response, parser = nil)
    if response.status == 401 && @challenge_retries == 0 && response[:"WWW-Authenticate"]
        challenge = Array(response[:"WWW-Authenticate"]).reject { |auth| auth.downcase == 'negotiate' }[0]

        begin
            if @ntlm_creds && challenge[0..3] == 'NTLM'
                @options[:headers] ||= {}
                @options[:headers][:Authorization] = ntlm_auth_header(challenge)
                @challenge_retries += 1

                execute(@transport)
                return false
            elsif @digest_creds && challenge[0..5] == 'Digest'
                @options[:headers] ||= {}
                @options[:headers][:Authorization] = digest_auth_header(challenge)
                @challenge_retries += 1

                execute(@transport)
                return false
            end
        rescue => e
            reject e
            true
        end
    end

    @transport = nil
    @defer.resolve(response)
    true
end


27
28
29
# File 'lib/uv-rays/http/request.rb', line 27

def set_cookie(value)
    @cookiejar.set(@uri, value)
end

#set_headers(head) ⇒ Object



177
178
179
# File 'lib/uv-rays/http/request.rb', line 177

def set_headers(head)
    @headers_callback.call(head) if @headers_callback
end

#streaming?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/uv-rays/http/request.rb', line 185

def streaming?
    @options[:streaming]
end