forked from httprb/http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.rb
179 lines (145 loc) · 5.14 KB
/
client.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
# frozen_string_literal: true
require "forwardable"
require "http/form_data"
require "http/options"
require "http/feature"
require "http/headers"
require "http/connection"
require "http/redirector"
require "http/uri"
module HTTP
# Clients make requests and receive responses
class Client
extend Forwardable
include Chainable
HTTP_OR_HTTPS_RE = %r{^https?://}i.freeze
def initialize(default_options = {})
@default_options = HTTP::Options.new(default_options)
@connection = nil
@state = :clean
end
# Make an HTTP request
def request(verb, uri, opts = {}) # rubocop:disable Style/OptionHash
opts = @default_options.merge(opts)
req = build_request(verb, uri, opts)
res = perform(req, opts)
return res unless opts.follow
Redirector.new(opts.follow).perform(req, res) do |request|
perform(request, opts)
end
end
# Prepare an HTTP request
def build_request(verb, uri, opts = {}) # rubocop:disable Style/OptionHash
opts = @default_options.merge(opts)
uri = make_request_uri(uri, opts)
headers = make_request_headers(opts)
body = make_request_body(opts, headers)
req = HTTP::Request.new(
:verb => verb,
:uri => uri,
:uri_normalizer => opts.feature(:normalize_uri)&.normalizer,
:proxy => opts.proxy,
:headers => headers,
:body => body
)
opts.features.inject(req) do |request, (_name, feature)|
feature.wrap_request(request)
end
end
# @!method persistent?
# @see Options#persistent?
# @return [Boolean] whenever client is persistent
def_delegator :default_options, :persistent?
# Perform a single (no follow) HTTP request
def perform(req, options)
verify_connection!(req.uri)
@state = :dirty
@connection ||= HTTP::Connection.new(req, options)
unless @connection.failed_proxy_connect?
@connection.send_request(req)
@connection.read_headers!
end
res = Response.new(
:status => @connection.status_code,
:version => @connection.http_version,
:headers => @connection.headers,
:proxy_headers => @connection.proxy_response_headers,
:connection => @connection,
:encoding => options.encoding,
:uri => req.uri,
:request => req
)
res = options.features.inject(res) do |response, (_name, feature)|
feature.wrap_response(response)
end
@connection.finish_response if req.verb == :head
@state = :clean
res
rescue
close
raise
end
def close
@connection.close if @connection
@connection = nil
@state = :clean
end
private
# Verify our request isn't going to be made against another URI
def verify_connection!(uri)
if default_options.persistent? && uri.origin != default_options.persistent
raise StateError, "Persistence is enabled for #{default_options.persistent}, but we got #{uri.origin}"
# We re-create the connection object because we want to let prior requests
# lazily load the body as long as possible, and this mimics prior functionality.
elsif @connection && (!@connection.keep_alive? || @connection.expired?)
close
# If we get into a bad state (eg, Timeout.timeout ensure being killed)
# close the connection to prevent potential for mixed responses.
elsif @state == :dirty
close
end
end
# Merges query params if needed
#
# @param [#to_s] uri
# @return [URI]
def make_request_uri(uri, opts)
uri = uri.to_s
uri = "#{default_options.persistent}#{uri}" if default_options.persistent? && uri !~ HTTP_OR_HTTPS_RE
uri = HTTP::URI.parse uri
uri.query_values = uri.query_values(Array).to_a.concat(opts.params.to_a) if opts.params && !opts.params.empty?
# Some proxies (seen on WEBRick) fail if URL has
# empty path (e.g. `http://example.com`) while it's RFC-complaint:
# http://tools.ietf.org/html/rfc1738#section-3.1
uri.path = "/" if uri.path.empty?
uri
end
# Creates request headers with cookies (if any) merged in
def make_request_headers(opts)
headers = opts.headers
# Tell the server to keep the conn open
headers[Headers::CONNECTION] = default_options.persistent? ? Connection::KEEP_ALIVE : Connection::CLOSE
cookies = opts.cookies.values
unless cookies.empty?
cookies = opts.headers.get(Headers::COOKIE).concat(cookies).join("; ")
headers[Headers::COOKIE] = cookies
end
headers
end
# Create the request body object to send
def make_request_body(opts, headers)
case
when opts.body
opts.body
when opts.form
form = HTTP::FormData.create opts.form
headers[Headers::CONTENT_TYPE] ||= form.content_type
form
when opts.json
body = MimeType[:json].encode opts.json
headers[Headers::CONTENT_TYPE] ||= "application/json; charset=#{body.encoding.name}"
body
end
end
end
end