/
adapter.rb
192 lines (155 loc) · 4.52 KB
/
adapter.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
182
183
184
185
186
187
188
189
190
191
192
# frozen_string_literal: true
# Released under the MIT License.
# Copyright, 2018-2024, by Samuel Williams.
# Copyright, 2018, by Andreas Garnaes.
# Copyright, 2019, by Denis Talakevich.
# Copyright, 2019-2020, by Igor Sidorov.
# Copyright, 2023, by Genki Takiuchi.
# Copyright, 2023, by Flavio Fernandes.
require 'faraday'
require 'faraday/adapter'
require 'kernel/sync'
require 'async/http/client'
require 'async/http/proxy'
module Async
module HTTP
module Faraday
class BodyReadWrapper < ::Protocol::HTTP::Body::Readable
def initialize(body, block_size: 4096)
@body = body
@block_size = block_size
end
def close(error = nil)
@body.close if @body.respond_to?(:close)
ensure
super
end
def read
@body.read(@block_size)
end
end
class Adapter < ::Faraday::Adapter
CONNECTION_EXCEPTIONS = [
Errno::EADDRNOTAVAIL,
Errno::ECONNABORTED,
Errno::ECONNREFUSED,
Errno::ECONNRESET,
Errno::EHOSTUNREACH,
Errno::EINVAL,
Errno::ENETUNREACH,
Errno::EPIPE,
IOError,
SocketError
].freeze
def initialize(*arguments, timeout: nil, **options, &block)
super(*arguments, **options)
@timeout = timeout
@clients = {}
@options = options
end
def make_client(endpoint)
Client.new(endpoint, **@connection_options)
end
def host_key(endpoint)
url = endpoint.url.dup
url.path = ""
url.fragment = nil
url.query = nil
return url
end
def client_for(endpoint)
key = host_key(endpoint)
@clients.fetch(key) do
@clients[key] = make_client(endpoint)
end
end
def proxy_client_for(proxy_endpoint, endpoint)
key = [host_key(proxy_endpoint), host_key(endpoint)]
@clients.fetch(key) do
client = client_for(proxy_endpoint)
@clients[key] = client.proxied_client(endpoint)
end
end
def close
# The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
clients = @clients.values
@clients.clear
clients.each(&:close)
end
def call(env)
super
# for compatibility with the default adapter
env.url.path = '/' if env.url.path.empty?
Sync do
endpoint = Endpoint.new(env.url)
if proxy = env.request.proxy
proxy_endpoint = Endpoint.new(proxy.uri)
client = self.proxy_client_for(proxy_endpoint, endpoint)
else
client = self.client_for(endpoint)
end
if body = env.body
# We need to wrap the body in a Readable object so that it can be read in chunks:
# Faraday's body only responds to `#read`.
if body.respond_to?(:read)
body = BodyReadWrapper.new(body)
else
body = ::Protocol::HTTP::Body::Buffered.wrap(body)
end
end
if headers = env.request_headers
headers = ::Protocol::HTTP::Headers[headers]
end
method = env.method.to_s.upcase
request = ::Protocol::HTTP::Request.new(endpoint.scheme, endpoint.authority, method, endpoint.path, nil, headers, body)
with_timeout do
response = client.call(request)
save_response(env, response.status, encoded_body(response), response.headers)
end
end
return @app.call(env)
rescue Errno::ETIMEDOUT, Async::TimeoutError => e
raise ::Faraday::TimeoutError, e
rescue OpenSSL::SSL::SSLError => e
raise ::Faraday::SSLError, e
rescue *CONNECTION_EXCEPTIONS => e
raise ::Faraday::ConnectionFailed, e
end
private
def with_timeout(task: Async::Task.current)
if @timeout
task.with_timeout(@timeout, ::Faraday::TimeoutError) do
yield
end
else
yield
end
end
def encoded_body(response)
body = response.read
return body if body.nil?
content_type = response.headers['content-type']
return body unless content_type
params = extract_type_parameters(content_type)
if charset = params['charset']
body = body.dup if body.frozen?
body.force_encoding(charset)
end
body
rescue ArgumentError
nil
end
def extract_type_parameters(content_type)
result = {}
list = content_type.split(';')
list.shift
list.each do |param|
key, value = *param.split('=', 2)
result[key.strip] = value.strip
end
result
end
end
end
end
end