forked from httprb/http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.rb
181 lines (141 loc) · 5.12 KB
/
request.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
require "forwardable"
require "base64"
require "time"
require "http/errors"
require "http/headers"
require "http/request/caching"
require "http/request/writer"
require "http/version"
require "http/uri"
module HTTP
class Request
extend Forwardable
include HTTP::Headers::Mixin
# The method given was not understood
class UnsupportedMethodError < RequestError; end
# The scheme of given URI was not understood
class UnsupportedSchemeError < RequestError; end
# Default User-Agent header value
USER_AGENT = "RubyHTTPGem/#{HTTP::VERSION}".freeze
# RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1
METHODS = [:options, :get, :head, :post, :put, :delete, :trace, :connect]
# RFC 2518: HTTP Extensions for Distributed Authoring -- WEBDAV
METHODS.concat [:propfind, :proppatch, :mkcol, :copy, :move, :lock, :unlock]
# RFC 3648: WebDAV Ordered Collections Protocol
METHODS.concat [:orderpatch]
# RFC 3744: WebDAV Access Control Protocol
METHODS.concat [:acl]
# draft-dusseault-http-patch: PATCH Method for HTTP
METHODS.concat [:patch]
# draft-reschke-webdav-search: WebDAV Search
METHODS.concat [:search]
# Allowed schemes
SCHEMES = [:http, :https, :ws, :wss]
# Default ports of supported schemes
PORTS = {
:http => 80,
:https => 443,
:ws => 80,
:wss => 443
}
# Method is given as a lowercase symbol e.g. :get, :post
attr_reader :verb
# Scheme is normalized to be a lowercase symbol e.g. :http, :https
attr_reader :scheme
# The following alias may be removed in two minor versions (0.8.0) or one
# major version (1.0.0)
def __method__(*args)
warn "#{Kernel.caller.first}: [DEPRECATION] HTTP::Request#__method__ is deprecated. Use #method instead."
method(*args)
end
# "Request URI" as per RFC 2616
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
attr_reader :uri
attr_reader :proxy, :body, :version
# :nodoc:
def initialize(verb, uri, headers = {}, proxy = {}, body = nil, version = "1.1") # rubocop:disable ParameterLists
@verb = verb.to_s.downcase.to_sym
@uri = HTTP::URI.parse uri
@scheme = @uri.scheme && @uri.scheme.to_s.downcase.to_sym
fail(UnsupportedMethodError, "unknown method: #{verb}") unless METHODS.include?(@verb)
fail(UnsupportedSchemeError, "unknown scheme: #{scheme}") unless SCHEMES.include?(@scheme)
@proxy, @body, @version = proxy, body, version
@headers = HTTP::Headers.coerce(headers || {})
@headers["Host"] ||= default_host_header_value
@headers["User-Agent"] ||= USER_AGENT
end
# Returns new Request with updated uri
def redirect(uri, verb = @verb)
req = self.class.new(verb, @uri.join(uri), headers, proxy, body, version)
req["Host"] = req.uri.host
req
end
# Stream the request to a socket
def stream(socket)
include_proxy_authorization_header if using_authenticated_proxy? && !@uri.https?
Request::Writer.new(socket, body, headers, request_header).stream
end
# Is this request using a proxy?
def using_proxy?
proxy && proxy.keys.size >= 2
end
# Is this request using an authenticated proxy?
def using_authenticated_proxy?
proxy && proxy.keys.size == 4
end
# Compute and add the Proxy-Authorization header
def include_proxy_authorization_header
headers["Proxy-Authorization"] = proxy_authorization_header
end
def proxy_authorization_header
digest = Base64.strict_encode64("#{proxy[:proxy_username]}:#{proxy[:proxy_password]}")
"Basic #{digest}"
end
# Setup tunnel through proxy for SSL request
def connect_using_proxy(socket)
Request::Writer.new(socket, nil, proxy_connect_headers, proxy_connect_header).connect_through_proxy
end
# Compute HTTP request header for direct or proxy request
def request_header
"#{verb.to_s.upcase} #{uri.normalize} HTTP/#{version}"
end
# Compute HTTP request header SSL proxy connection
def proxy_connect_header
"CONNECT #{@uri.host}:#{@uri.port} HTTP/#{version}"
end
# Headers to send with proxy connect request
def proxy_connect_headers
connect_headers = HTTP::Headers.coerce(
"Host" => headers["Host"],
"User-Agent" => headers["User-Agent"]
)
connect_headers["Proxy-Authorization"] = proxy_authorization_header if using_authenticated_proxy?
connect_headers
end
# Host for tcp socket
def socket_host
using_proxy? ? proxy[:proxy_address] : host
end
# Port for tcp socket
def socket_port
using_proxy? ? proxy[:proxy_port] : port
end
# @return [HTTP::Request::Caching]
def caching
Caching.new self
end
private
# @!attribute [r] host
# @return [String]
def_delegator :@uri, :host
# @!attribute [r] port
# @return [Fixnum]
def port
@uri.port || @uri.default_port
end
# @return [String] Default host (with port if needed) header value.
def default_host_header_value
PORTS[@scheme] != port ? "#{host}:#{port}" : host
end
end
end