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

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



26
27
28
29
30
31
32
33
34
35
# File 'lib/uv-rays/http/request.rb', line 26

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

    @options = options
    @endpoint = endpoint

    @path = options[:path]
    @method = options[:method]
    @uri = "#{endpoint.scheme}#{encode_host(endpoint.host, endpoint.port)}#{@path}"
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



14
15
16
# File 'lib/uv-rays/http/request.rb', line 14

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



13
14
15
# File 'lib/uv-rays/http/request.rb', line 13

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/uv-rays/http/request.rb', line 12

def path
  @path
end

Instance Method Details

#cookies_hashObject



17
18
19
# File 'lib/uv-rays/http/request.rb', line 17

def cookies_hash
    @endpoint.cookiejar.get_hash(@uri)
end

#execute(transport, error) ⇒ Object



48
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/uv-rays/http/request.rb', line 48

def execute(transport, error)
    head, body = build_request, @options[:body]

    @endpoint.middleware.each do |m|
        head, body = m.request(self, head, body) if m.respond_to?(:request)
    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)
    request_header << encode_headers(head)
    request_header << CRLF

    transport.write(request_header).catch error

    if body
        transport.write(body).catch error
    elsif file
        # Send file
        fileRef = @endpoint.loop.file file, File::RDONLY
        fileRef.progress do
            # File is open and available for reading
            pSend = fileRef.send_file(transport, :raw)
            pSend.catch error
            pSend.finally do
                fileRef.close
            end
        end
        fileRef.catch error
    end
end

#notify(*args) ⇒ Object



99
100
101
# File 'lib/uv-rays/http/request.rb', line 99

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

#on_headers(callback, &blk) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/uv-rays/http/request.rb', line 110

def on_headers(callback, &blk)
    callback ||= blk
    if @headers.nil?
        @headers_callback = callback
    else
        callback.call(@headers)
    end
end

#reject(reason) ⇒ Object



44
45
46
# File 'lib/uv-rays/http/request.rb', line 44

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

#resolve(response) ⇒ Object



37
38
39
40
41
42
# File 'lib/uv-rays/http/request.rb', line 37

def resolve(response)
    @defer.resolve({
        headers: @headers,
        body: response
    })
end


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

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

#set_headers(head) ⇒ Object



103
104
105
106
107
108
# File 'lib/uv-rays/http/request.rb', line 103

def set_headers(head)
    @headers = head
    if not @headers_callback.nil?
        @headers_callback.call(@headers)
    end
end