forked from alibaba/PhotonLibOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
340 lines (306 loc) · 12.2 KB
/
Copy pathclient.cpp
File metadata and controls
340 lines (306 loc) · 12.2 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
Copyright 2022 The Photon Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "client.h"
#include <bitset>
#include <algorithm>
#include <random>
#include <photon/common/alog-stdstring.h>
#include <photon/common/iovector.h>
#include <photon/common/string_view.h>
#include <photon/net/socket.h>
#include <photon/net/security-context/tls-stream.h>
#include <photon/net/utils.h>
#include <photon/photon.h>
namespace photon {
namespace net {
namespace http {
static const uint64_t kDNSCacheLife = 3600UL * 1000 * 1000;
static constexpr char USERAGENT[] = "PhotonLibOS_HTTP";
class PooledDialer {
public:
net::TLSContext* tls_ctx = nullptr;
std::unique_ptr<ISocketClient> tcpsock;
std::unique_ptr<ISocketClient> tlssock;
std::unique_ptr<ISocketClient> udssock;
std::unique_ptr<Resolver> resolver;
photon::mutex init_mtx;
bool initialized = false;
bool tls_ctx_ownership = false;
// If there is a photon thread switch during construction, the constructor might be called
// multiple times, even for a thread_local instance. Therefore, ensure that there is no photon
// thread switch inside the constructor. Place the initialization work in init() and ensure it
// is initialized only once.
PooledDialer() {
photon::fini_hook({this, &PooledDialer::at_photon_fini});
}
int init(TLSContext *_tls_ctx, std::vector<IPAddr> &src_ips) {
if (initialized)
return 0;
SCOPED_LOCK(init_mtx);
if (initialized)
return 0;
tls_ctx = _tls_ctx;
if (!tls_ctx) {
tls_ctx_ownership = true;
tls_ctx = new_tls_context(nullptr, nullptr, nullptr);
}
auto tcp_cli = new_tcp_socket_client(src_ips.data(), src_ips.size());
auto tls_cli = new_tls_client(tls_ctx, new_tcp_socket_client(src_ips.data(), src_ips.size()), true);
tcpsock.reset(new_tcp_socket_pool(tcp_cli, -1, true));
tlssock.reset(new_tcp_socket_pool(tls_cli, -1, true));
udssock.reset(new_uds_client());
resolver.reset(new_default_resolver(kDNSCacheLife));
initialized = true;
return 0;
}
void at_photon_fini() {
resolver.reset();
udssock.reset();
tlssock.reset();
tcpsock.reset();
if (tls_ctx_ownership)
delete tls_ctx;
initialized = false;
tls_ctx_ownership = false;
}
ISocketStream* dial(std::string_view host, uint16_t port, bool secure,
uint64_t timeout = -1UL);
template <typename T>
ISocketStream* dial(const T& x, uint64_t timeout = -1UL) {
return dial(x.host_no_port(), x.port(), x.secure(), timeout);
}
ISocketStream* dial(std::string_view uds_path, uint64_t timeout = -1UL);
};
ISocketStream* PooledDialer::dial(std::string_view host, uint16_t port, bool secure, uint64_t timeout) {
LOG_DEBUG("Dialing to `:`", host, port);
auto ipaddr = resolver->resolve(host);
if (ipaddr.undefined()) {
LOG_ERROR_RETURN(ENOENT, nullptr, "DNS resolve failed, name = `", host)
}
EndPoint ep(ipaddr, port);
LOG_DEBUG("Connecting ` ssl: `", ep, secure);
ISocketStream *sock = nullptr;
if (secure) {
tlssock->timeout(timeout);
sock = tlssock->connect(ep);
tls_stream_set_hostname(sock, estring_view(host).extract_c_str());
} else {
tcpsock->timeout(timeout);
sock = tcpsock->connect(ep);
}
if (sock) {
LOG_DEBUG("Connected ` ", ep, VALUE(host), VALUE(secure));
return sock;
}
LOG_ERROR("connection failed, ssl : ` ep : ` host : `", secure, ep, host);
// When failed, remove resolved result from dns cache so that following retries can try
// different ips.
resolver->discard_cache(host, ipaddr);
return nullptr;
}
ISocketStream* PooledDialer::dial(std::string_view uds_path, uint64_t timeout) {
udssock->timeout(timeout);
auto stream = udssock->connect(uds_path.data());
if (!stream)
LOG_ERRNO_RETURN(0, nullptr, "failed to dial to unix socket `", uds_path);
return stream;
}
constexpr uint64_t code3xx() { return 0; }
template<typename...Ts>
constexpr uint64_t code3xx(uint64_t x, Ts...xs)
{
return (1 << (x-300)) | code3xx(xs...);
}
constexpr static std::bitset<10>
code_redirect_verb(code3xx(300, 301, 302, 307, 308));
static constexpr size_t kMinimalHeadersSize = 8 * 1024 - 1;
enum RoundtripStatus {
ROUNDTRIP_SUCCESS,
ROUNDTRIP_FAILED,
ROUNDTRIP_REDIRECT,
ROUNDTRIP_NEED_RETRY,
ROUNDTRIP_FORCE_RETRY,
ROUNDTRIP_FAST_RETRY,
};
class ClientImpl : public Client {
public:
CommonHeaders<> m_common_headers;
TLSContext *m_tls_ctx;
ICookieJar *m_cookie_jar;
ClientImpl(ICookieJar *cookie_jar, TLSContext *tls_ctx) :
m_tls_ctx(tls_ctx),
m_cookie_jar(cookie_jar) {
}
PooledDialer& get_dialer() {
thread_local PooledDialer dialer;
dialer.init(m_tls_ctx, m_bind_ips);
return dialer;
}
using SocketStream_ptr = std::unique_ptr<ISocketStream>;
int redirect(Operation* op) {
if (op->resp.body_size() > 0) {
op->resp.skip_remain();
}
auto location = op->resp.headers["Location"];
if (location.empty()) {
LOG_ERROR_RETURN(EINVAL, ROUNDTRIP_FAILED,
"redirect but has no field location");
}
LOG_DEBUG("Redirect to ", location);
Verb v;
auto sc = op->status_code - 300;
if (sc == 3) { // 303
v = Verb::GET;
} else if (sc < 10 && code_redirect_verb[sc]) {
v = op->req.verb();
} else {
LOG_ERROR_RETURN(EINVAL, ROUNDTRIP_FAILED,
"invalid 3xx status code: ", op->status_code);
}
if (op->req.redirect(v, location, op->enable_proxy) < 0) {
LOG_ERRNO_RETURN(0, ROUNDTRIP_FAILED, "redirect failed");
}
return ROUNDTRIP_REDIRECT;
}
int do_roundtrip(Operation* op, Timeout tmo) {
op->status_code = -1;
if (tmo.timeout() == 0)
LOG_ERROR_RETURN(ETIMEDOUT, ROUNDTRIP_FAILED, "connection timedout");
auto &req = op->req;
ISocketStream* s;
if (m_proxy && !m_proxy_url.empty())
s = get_dialer().dial(m_proxy_url, tmo.timeout());
else if (!op->uds_path.empty())
s = get_dialer().dial(op->uds_path, tmo.timeout());
else
s = get_dialer().dial(req, tmo.timeout());
if (!s) {
if (errno == ECONNREFUSED || errno == ENOENT) {
LOG_ERROR_RETURN(0, ROUNDTRIP_FAST_RETRY, "connection refused")
}
LOG_ERROR_RETURN(0, ROUNDTRIP_NEED_RETRY, "connection failed");
}
SocketStream_ptr sock(s);
LOG_DEBUG("Sending request ` `", req.verb(), req.target());
if (req.send_header(sock.get()) < 0) {
sock->close();
req.reset_status();
LOG_ERROR_RETURN(0, ROUNDTRIP_NEED_RETRY, "send header failed, retry");
}
sock->timeout(tmo.timeout());
if (op->body_buffer_size > 0) {
// send body_buffer
if (req.write(op->body_buffer, op->body_buffer_size) < 0) {
sock->close();
req.reset_status();
LOG_ERROR_RETURN(0, ROUNDTRIP_NEED_RETRY, "send body buffer failed, retry");
}
} else if (op->body_stream) {
// send body_stream
if (req.write_stream(op->body_stream) < 0) {
sock->close();
req.reset_status();
LOG_ERROR_RETURN(0, ROUNDTRIP_NEED_RETRY, "send body stream failed, retry");
}
} else {
// call body_writer
if (op->body_writer(&req) < 0) {
sock->close();
req.reset_status();
LOG_ERROR_RETURN(0, ROUNDTRIP_NEED_RETRY, "failed to call body writer, retry");
}
}
if (req.send() < 0) {
sock->close();
req.reset_status();
LOG_ERROR_RETURN(0, ROUNDTRIP_NEED_RETRY, "failed to ensure send");
}
LOG_DEBUG("Request sent, wait for response ` `", req.verb(), req.target());
auto space = req.get_remain_space();
auto &resp = op->resp;
if (space.second > kMinimalHeadersSize) {
resp.reset(space.first, space.second, false, sock.release(), true, req.verb());
} else {
auto buf = malloc(kMinimalHeadersSize);
resp.reset((char *)buf, kMinimalHeadersSize, true, sock.release(), true, req.verb());
}
if (op->resp.receive_header(tmo.timeout()) != 0) {
sock->close();
req.reset_status();
LOG_ERROR_RETURN(0, ROUNDTRIP_NEED_RETRY, "read response header failed");
}
op->status_code = resp.status_code();
LOG_DEBUG("Got response ` ` code=` || content_length=`", req.verb(),
req.target(), resp.status_code(), resp.headers.content_length());
if (m_cookie_jar) m_cookie_jar->get_cookies_from_headers(req.host(), &resp);
if (resp.status_code() < 400 && resp.status_code() >= 300 && op->follow)
return redirect(op);
return ROUNDTRIP_SUCCESS;
}
int call(Operation* /*IN, OUT*/ op) override {
auto content_length = op->req.headers.content_length();
auto encoding = op->req.headers["Transfer-Encoding"];
if ((content_length != 0) && (encoding == "chunked")) {
op->status_code = -1;
LOG_ERROR_RETURN(EINVAL, ROUNDTRIP_FAILED,
"Content-Length and Transfer-Encoding conflicted");
}
op->req.headers.merge(m_common_headers);
op->req.headers.insert("User-Agent", m_user_agent.empty() ? std::string_view(USERAGENT)
: std::string_view(m_user_agent));
op->req.headers.insert("Connection", "keep-alive");
if (m_cookie_jar && m_cookie_jar->set_cookies_to_headers(&op->req) != 0)
LOG_ERROR_RETURN(0, -1, "set_cookies_to_headers failed");
Timeout tmo(std::min(op->timeout.timeout(), m_timeout));
int retry = 0, followed = 0, ret = 0;
uint64_t sleep_interval = 0;
while (followed <= op->follow && retry <= op->retry && tmo.timeout() != 0) {
ret = do_roundtrip(op, tmo);
if (ret == ROUNDTRIP_SUCCESS || ret == ROUNDTRIP_FAILED) break;
switch (ret) {
case ROUNDTRIP_NEED_RETRY:
photon::thread_usleep(sleep_interval * 1000UL);
sleep_interval = (sleep_interval + 500) * 2;
++retry;
break;
case ROUNDTRIP_FAST_RETRY:
++retry;
break;
case ROUNDTRIP_REDIRECT:
retry = 0;
++followed;
break;
default:
break;
}
if (tmo.timeout() == 0)
LOG_ERROR_RETURN(ETIMEDOUT, -1, "connection timedout");
if (followed > op->follow || retry > op->retry)
LOG_ERRNO_RETURN(0, -1, "connection failed");
}
if (ret != ROUNDTRIP_SUCCESS) LOG_ERROR_RETURN(0, -1,"too many retry, roundtrip failed");
return 0;
}
ISocketStream* native_connect(std::string_view host, uint16_t port, bool secure, uint64_t timeout) override {
return get_dialer().dial(host, port, secure, timeout);
}
CommonHeaders<>* common_headers() override {
return &m_common_headers;
}
};
Client* new_http_client(ICookieJar *cookie_jar, TLSContext *tls_ctx) {
return new ClientImpl(cookie_jar, tls_ctx);
}
} // namespace http
} // namespace net
} // namespace photon