-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathhttp_client.cc
More file actions
168 lines (147 loc) · 4.96 KB
/
Copy pathhttp_client.cc
File metadata and controls
168 lines (147 loc) · 4.96 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
#include "http_client.h"
#include "root_certs.h"
#include <queue>
#include "asserts-cpp/asserts.h"
namespace node {
namespace nsolid {
CurlContext::CurlContext(HttpClient* http_client, curl_socket_t sockfd):
http_client_(http_client),
poll_handle_(new nsuv::ns_poll),
sockfd_(sockfd) {
ASSERT_EQ(0, poll_handle_->init_socket(http_client->loop_, sockfd));
poll_handle_->set_data(this);
}
CurlContext::~CurlContext() {
poll_handle_->close_and_delete();
}
int CurlContext::start(int events, poll_cb cb) {
return poll_handle_->start(events, cb);
}
HttpClient::HttpClient(uv_loop_t* loop): loop_(loop),
timeout_(new nsuv::ns_timer) {
ASSERT_EQ(0, timeout_->init(loop));
timeout_->set_data(this);
ASSERT_EQ(0, curl_global_init(CURL_GLOBAL_ALL));
curl_handle_ = curl_multi_init();
ASSERT_NOT_NULL(curl_handle_);
ASSERT_EQ(0, curl_multi_setopt(curl_handle_,
CURLMOPT_SOCKETFUNCTION,
handle_socket_));
ASSERT_EQ(0, curl_multi_setopt(curl_handle_, CURLMOPT_SOCKETDATA, this));
ASSERT_EQ(0, curl_multi_setopt(curl_handle_,
CURLMOPT_TIMERFUNCTION,
start_timeout_));
ASSERT_EQ(0, curl_multi_setopt(curl_handle_, CURLMOPT_TIMERDATA, this));
for (size_t i = 0; i < GetRootCertsCount(); i++) {
cacert_ += GetRootCerts()[i];
cacert_ += "\n";
}
}
HttpClient::~HttpClient() {
timeout_->close_and_delete();
curl_multi_cleanup(curl_handle_);
}
void HttpClient::check_multi_info() {
CURLMsg* message;
int pending;
std::queue<CURLMsg*> q;
while ((message = curl_multi_info_read(curl_handle_, &pending))) {
switch (message->msg) {
case CURLMSG_DONE:
q.push(message);
break;
default:
break;
}
}
CURL* easy_handle;
while (!q.empty()) {
CURLMsg* message = q.front();
easy_handle = message->easy_handle;
curl_multi_remove_handle(curl_handle_, easy_handle);
struct HttpClientStor* stor;
curl_easy_getinfo(easy_handle, CURLINFO_PRIVATE, &stor);
stor->client->transfer_done(message, stor);
curl_easy_cleanup(easy_handle);
q.pop();
}
}
/*static*/int HttpClient::handle_socket_(CURL* easy,
curl_socket_t s,
int action,
void* userp,
void* socketp) {
CurlContext* context = nullptr;
int events = 0;
HttpClient* client = reinterpret_cast<HttpClient*>(userp);
switch (action) {
case CURL_POLL_IN:
case CURL_POLL_OUT:
case CURL_POLL_INOUT:
context = socketp ? reinterpret_cast<CurlContext*>(socketp)
: new CurlContext(client, s);
curl_multi_assign(client->curl_handle_, s, context);
if (action != CURL_POLL_IN)
events |= UV_WRITABLE;
if (action != CURL_POLL_OUT)
events |= UV_READABLE;
context->start(events, curl_perform_);
break;
case CURL_POLL_REMOVE:
if (socketp) {
context = reinterpret_cast<CurlContext*>(socketp);
delete context;
curl_multi_assign(client->curl_handle_, s, nullptr);
}
break;
default:
abort();
}
return 0;
}
/*static*/void HttpClient::on_timeout_(nsuv::ns_timer* timer) {
int running_handles;
HttpClient* client = timer->get_data<HttpClient>();
curl_multi_socket_action(client->curl_handle_,
CURL_SOCKET_TIMEOUT,
0,
&running_handles);
client->check_multi_info();
}
/*static*/int HttpClient::start_timeout_(CURLM* multi,
long timeout, // NOLINT(runtime/int)
void* userp) {
HttpClient* client = reinterpret_cast<HttpClient*>(userp);
if (timeout < 0) {
return client->timeout_->stop();
} else {
if (timeout == 0) {
timeout = 1; // 0 means directly call socket_action, but we will do it
// in a bit.
}
return client->timeout_->start(on_timeout_, timeout, 0);
}
}
/*static*/void HttpClient::curl_perform_(nsuv::ns_poll* poll,
int status,
int events) {
int running_handles;
int flags = 0;
CurlContext* context = poll->get_data<CurlContext>();
if (events & UV_READABLE)
flags |= CURL_CSELECT_IN;
if (events & UV_WRITABLE)
flags |= CURL_CSELECT_OUT;
HttpClient* client = context->http_client_;
curl_multi_socket_action(client->curl_handle_,
context->sockfd_,
flags,
&running_handles);
client->check_multi_info();
}
/*static*/size_t HttpClient::write_cb_(void*, size_t, size_t nmemb, void*) {
/* Don't write the response anywhere(by default libcurl prints to stdout) */
return nmemb;
}
} // namespace nsolid
} // namespace node