forked from nsqio/libnsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.c
193 lines (162 loc) · 5.18 KB
/
http.c
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
#include <curl/curl.h>
#include "nsq.h"
struct HttpClient {
CURLM *multi;
struct ev_loop *loop;
struct ev_timer timer_event;
int still_running;
};
struct HttpRequest {
CURL *easy;
char *url;
struct HttpClient *httpc;
char error[CURL_ERROR_SIZE];
struct Buffer *data;
};
struct HttpSocket {
curl_socket_t sockfd;
CURL *easy;
int action;
long timeout;
struct ev_io ev;
int evset;
struct HttpClient *httpc;
};
static void timer_cb(EV_P_ struct ev_timer *w, int revents);
static int multi_timer_cb(CURLM *multi, long timeout_ms, void *arg)
{
struct HttpClient *httpc = (struct HttpClient *)arg;
ev_timer_stop(httpc->loop, &httpc->timer_event);
if (timeout_ms > 0) {
double t = timeout_ms / 1000;
ev_timer_init(&httpc->timer_event, timer_cb, t, 0.);
ev_timer_start(httpc->loop, &httpc->timer_event);
} else {
timer_cb(httpc->loop, &httpc->timer_event, 0);
}
return 0;
}
static void check_multi_info(struct HttpClient *httpc)
{
char *eff_url;
CURLMsg *msg;
int msgs_left;
struct HttpRequest *req;
CURL *easy;
CURLcode res;
while ((msg = curl_multi_info_read(httpc->multi, &msgs_left))) {
if (msg->msg == CURLMSG_DONE) {
easy = msg->easy_handle;
res = msg->data.result;
curl_easy_getinfo(easy, CURLINFO_PRIVATE, &req);
curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url);
curl_multi_remove_handle(httpc->multi, easy);
curl_easy_cleanup(easy);
free_buffer(req->data);
free(req->url);
free(req);
}
}
}
static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *arg)
{
struct HttpRequest *req = (struct HttpRequest *)arg;
size_t realsize = size * nmemb;
buffer_add(req->data, ptr, realsize);
return realsize;
}
static void event_cb(EV_P_ struct ev_io *w, int revents)
{
struct HttpClient *httpc = (struct HttpClient *)w->data;
CURLMcode rc;
int action = (revents & EV_READ ? CURL_POLL_IN : 0) | (revents & EV_WRITE ? CURL_POLL_OUT : 0);
rc = curl_multi_socket_action(httpc->multi, w->fd, action, &httpc->still_running);
// TODO: handle rc
check_multi_info(httpc);
}
static void timer_cb(EV_P_ struct ev_timer *w, int revents)
{
struct HttpClient *httpc = (struct HttpClient *)w->data;
CURLMcode rc;
rc = curl_multi_socket_action(httpc->multi, CURL_SOCKET_TIMEOUT, 0, &httpc->still_running);
// TODO: handle rc
check_multi_info(httpc);
}
static int sock_cb(CURL *e, curl_socket_t s, int what, void *arg, void *sock_arg)
{
struct HttpClient *httpc = (struct HttpClient *)arg;
struct HttpSocket *sock = (struct HttpSocket *)sock_arg;
int kind = (what & CURL_POLL_IN ? EV_READ : 0) | (what & CURL_POLL_OUT ? EV_WRITE : 0);
if (what == CURL_POLL_REMOVE) {
if (sock) {
if (sock->evset) {
ev_io_stop(httpc->loop, &sock->ev);
}
free(sock);
}
} else {
int new = 0;
if (!sock) {
sock = calloc(1, sizeof(struct HttpSocket));
new = 1;
}
sock->httpc = httpc;
sock->sockfd = s;
sock->action = what;
sock->easy = e;
if (sock->evset) {
ev_io_stop(httpc->loop, &sock->ev);
}
ev_io_init(&sock->ev, event_cb, sock->sockfd, kind);
sock->ev.data = httpc;
sock->evset = 1;
ev_io_start(httpc->loop, &sock->ev);
if (new) {
curl_multi_assign(httpc->multi, s, sock);
}
}
return 0;
}
struct HttpClient *new_http_client(void)
{
struct HttpClient *httpc;
httpc = malloc(sizeof(struct HttpClient));
httpc->multi = curl_multi_init();
ev_timer_init(&httpc->timer_event, timer_cb, 0., 0.);
httpc->timer_event.data = httpc;
curl_multi_setopt(httpc->multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
curl_multi_setopt(httpc->multi, CURLMOPT_SOCKETDATA, httpc);
curl_multi_setopt(httpc->multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb);
curl_multi_setopt(httpc->multi, CURLMOPT_TIMERDATA, httpc);
return httpc;
}
void free_http_client(struct HttpClient *httpc)
{
if (httpc) {
curl_multi_cleanup(httpc->multi);
ev_timer_stop(httpc->loop, &httpc->timer_event);
free(httpc);
}
}
int http_client_get(struct HttpClient *httpc, const char *url)
{
struct HttpRequest *req;
CURLMcode rc;
req = calloc(1, sizeof(struct HttpRequest));
req->data = new_buffer(4096, 0);
req->easy = curl_easy_init();
if (!req->easy) {
return 0;
}
req->httpc = httpc;
req->url = strdup(url);
curl_easy_setopt(req->easy, CURLOPT_URL, req->url);
curl_easy_setopt(req->easy, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(req->easy, CURLOPT_WRITEDATA, req);
curl_easy_setopt(req->easy, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(req->easy, CURLOPT_ERRORBUFFER, req->error);
curl_easy_setopt(req->easy, CURLOPT_PRIVATE, req);
rc = curl_multi_add_handle(httpc->multi, req->easy);
// TODO: handle rc
return 1;
}