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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 <assert.h>
#include <curl/curl.h>
#include <apr_lib.h>
#include <apr_strings.h>
#include <apr_buckets.h>
#include "md_http.h"
#include "md_log.h"
#include "md_util.h"
#include "md_curl.h"
/**************************************************************************************************/
/* md_http curl implementation */
static apr_status_t curl_status(unsigned int curl_code)
{
switch (curl_code) {
case CURLE_OK: return APR_SUCCESS;
case CURLE_UNSUPPORTED_PROTOCOL: return APR_ENOTIMPL;
case CURLE_NOT_BUILT_IN: return APR_ENOTIMPL;
case CURLE_URL_MALFORMAT: return APR_EINVAL;
case CURLE_COULDNT_RESOLVE_PROXY:return APR_ECONNREFUSED;
case CURLE_COULDNT_RESOLVE_HOST: return APR_ECONNREFUSED;
case CURLE_COULDNT_CONNECT: return APR_ECONNREFUSED;
case CURLE_REMOTE_ACCESS_DENIED: return APR_EACCES;
case CURLE_OUT_OF_MEMORY: return APR_ENOMEM;
case CURLE_OPERATION_TIMEDOUT: return APR_TIMEUP;
case CURLE_SSL_CONNECT_ERROR: return APR_ECONNABORTED;
case CURLE_AGAIN: return APR_EAGAIN;
default: return APR_EGENERAL;
}
}
typedef struct {
CURL *curl;
CURLM *curlm;
struct curl_slist *req_hdrs;
md_http_response_t *response;
apr_status_t rv;
int status_fired;
} md_curl_internals_t;
static size_t req_data_cb(void *data, size_t len, size_t nmemb, void *baton)
{
apr_bucket_brigade *body = baton;
size_t blen, read_len = 0, max_len = len * nmemb;
const char *bdata;
apr_bucket *b;
apr_status_t rv;
while (body && !APR_BRIGADE_EMPTY(body) && max_len > 0) {
b = APR_BRIGADE_FIRST(body);
if (APR_BUCKET_IS_METADATA(b)) {
if (APR_BUCKET_IS_EOS(b)) {
body = NULL;
}
}
else {
rv = apr_bucket_read(b, &bdata, &blen, APR_BLOCK_READ);
if (rv == APR_SUCCESS) {
if (blen > max_len) {
apr_bucket_split(b, max_len);
blen = max_len;
}
memcpy(data, bdata, blen);
read_len += blen;
max_len -= blen;
}
else {
body = NULL;
if (!APR_STATUS_IS_EOF(rv)) {
/* everything beside EOF is an error */
read_len = CURL_READFUNC_ABORT;
}
}
}
apr_bucket_delete(b);
}
return read_len;
}
static size_t resp_data_cb(void *data, size_t len, size_t nmemb, void *baton)
{
md_curl_internals_t *internals = baton;
md_http_response_t *res = internals->response;
size_t blen = len * nmemb;
apr_status_t rv;
if (res->body) {
if (res->req->resp_limit) {
apr_off_t body_len = 0;
apr_brigade_length(res->body, 0, &body_len);
if (body_len + (apr_off_t)blen > res->req->resp_limit) {
return 0; /* signal curl failure */
}
}
rv = apr_brigade_write(res->body, NULL, NULL, (const char *)data, blen);
if (rv != APR_SUCCESS) {
/* returning anything != blen will make CURL fail this */
return 0;
}
}
return blen;
}
static size_t header_cb(void *buffer, size_t elen, size_t nmemb, void *baton)
{
md_curl_internals_t *internals = baton;
md_http_response_t *res = internals->response;
size_t len, clen = elen * nmemb;
const char *name = NULL, *value = "", *b = buffer;
apr_size_t i;
len = (clen && b[clen-1] == '\n')? clen-1 : clen;
len = (len && b[len-1] == '\r')? len-1 : len;
for (i = 0; i < len; ++i) {
if (b[i] == ':') {
name = apr_pstrndup(res->req->pool, b, i);
++i;
while (i < len && b[i] == ' ') {
++i;
}
if (i < len) {
value = apr_pstrndup(res->req->pool, b+i, len - i);
}
break;
}
}
if (name != NULL) {
apr_table_add(res->headers, name, value);
}
return clen;
}
typedef struct {
md_http_request_t *req;
struct curl_slist *hdrs;
apr_status_t rv;
} curlify_hdrs_ctx;
static int curlify_headers(void *baton, const char *key, const char *value)
{
curlify_hdrs_ctx *ctx = baton;
const char *s;
if (strchr(key, '\r') || strchr(key, '\n')
|| strchr(value, '\r') || strchr(value, '\n')) {
ctx->rv = APR_EINVAL;
return 0;
}
s = apr_psprintf(ctx->req->pool, "%s: %s", key, value);
ctx->hdrs = curl_slist_append(ctx->hdrs, s);
return 1;
}
/* Convert timeout values for curl. Since curl uses 0 to disable
* timeout, return at least 1 if the apr_time_t value is non-zero. */
static long timeout_msec(apr_time_t timeout)
{
long ms = (long)apr_time_as_msec(timeout);
return ms? ms : (timeout? 1 : 0);
}
static long timeout_sec(apr_time_t timeout)
{
long s = (long)apr_time_sec(timeout);
return s? s : (timeout? 1 : 0);
}
static int curl_debug_log(CURL *curl, curl_infotype type, char *data, size_t size, void *baton)
{
md_http_request_t *req = baton;
(void)curl;
switch (type) {
case CURLINFO_TEXT:
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE4, 0, req->pool,
"req[%d]: info %s", req->id, apr_pstrndup(req->pool, data, size));
break;
case CURLINFO_HEADER_OUT:
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE4, 0, req->pool,
"req[%d]: header --> %s", req->id, apr_pstrndup(req->pool, data, size));
break;
case CURLINFO_HEADER_IN:
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE4, 0, req->pool,
"req[%d]: header <-- %s", req->id, apr_pstrndup(req->pool, data, size));
break;
case CURLINFO_DATA_OUT:
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE4, 0, req->pool,
"req[%d]: data --> %ld bytes", req->id, (long)size);
if (md_log_is_level(req->pool, MD_LOG_TRACE5)) {
md_data_t d;
const char *s;
md_data_init(&d, data, size);
md_data_to_hex(&s, 0, req->pool, &d);
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE5, 0, req->pool,
"req[%d]: data(hex) --> %s", req->id, s);
}
break;
case CURLINFO_DATA_IN:
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE4, 0, req->pool,
"req[%d]: data <-- %ld bytes", req->id, (long)size);
if (md_log_is_level(req->pool, MD_LOG_TRACE5)) {
md_data_t d;
const char *s;
md_data_init(&d, data, size);
md_data_to_hex(&s, 0, req->pool, &d);
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE5, 0, req->pool,
"req[%d]: data(hex) <-- %s", req->id, s);
}
break;
default:
break;
}
return 0;
}
static apr_status_t internals_setup(md_http_request_t *req)
{
md_curl_internals_t *internals;
CURL *curl;
apr_status_t rv = APR_SUCCESS;
curl = md_http_get_impl_data(req->http);
if (!curl) {
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, 0, req->pool, "creating curl instance");
curl = curl_easy_init();
if (!curl) {
rv = APR_EGENERAL;
goto leave;
}
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_cb);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, NULL);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, req_data_cb);
curl_easy_setopt(curl, CURLOPT_READDATA, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, resp_data_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
}
else {
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, 0, req->pool, "reusing curl instance from http");
}
internals = apr_pcalloc(req->pool, sizeof(*internals));
internals->curl = curl;
internals->response = apr_pcalloc(req->pool, sizeof(md_http_response_t));
internals->response->req = req;
internals->response->status = 400;
internals->response->headers = apr_table_make(req->pool, 5);
internals->response->body = apr_brigade_create(req->pool, req->bucket_alloc);
curl_easy_setopt(curl, CURLOPT_URL, req->url);
if (!apr_strnatcasecmp("GET", req->method)) {
/* nop */
}
else if (!apr_strnatcasecmp("HEAD", req->method)) {
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
}
else if (!apr_strnatcasecmp("POST", req->method)) {
curl_easy_setopt(curl, CURLOPT_POST, 1L);
}
else {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, req->method);
}
curl_easy_setopt(curl, CURLOPT_HEADERDATA, internals);
curl_easy_setopt(curl, CURLOPT_READDATA, req->body);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, internals);
if (req->timeout.overall > 0) {
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, timeout_msec(req->timeout.overall));
}
if (req->timeout.connect > 0) {
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, timeout_msec(req->timeout.connect));
}
if (req->timeout.stalled > 0) {
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, req->timeout.stall_bytes_per_sec);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, timeout_sec(req->timeout.stalled));
}
if (req->ca_file) {
curl_easy_setopt(curl, CURLOPT_CAINFO, req->ca_file);
}
if (req->body_len >= 0) {
/* set the Content-Length */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)req->body_len);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)req->body_len);
}
if (req->user_agent) {
curl_easy_setopt(curl, CURLOPT_USERAGENT, req->user_agent);
}
if (req->proxy_url) {
curl_easy_setopt(curl, CURLOPT_PROXY, req->proxy_url);
}
if (!apr_is_empty_table(req->headers)) {
curlify_hdrs_ctx ctx;
ctx.req = req;
ctx.hdrs = NULL;
ctx.rv = APR_SUCCESS;
apr_table_do(curlify_headers, &ctx, req->headers, NULL);
internals->req_hdrs = ctx.hdrs;
if (ctx.rv == APR_SUCCESS) {
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, internals->req_hdrs);
}
}
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, 0, req->pool,
"req[%d]: %s %s", req->id, req->method, req->url);
if (md_log_is_level(req->pool, MD_LOG_TRACE4)) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curl_debug_log);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, req);
}
leave:
req->internals = (APR_SUCCESS == rv)? internals : NULL;
return rv;
}
static apr_status_t update_status(md_http_request_t *req)
{
md_curl_internals_t *internals = req->internals;
long l;
apr_status_t rv = APR_SUCCESS;
if (internals) {
rv = curl_status(curl_easy_getinfo(internals->curl, CURLINFO_RESPONSE_CODE, &l));
if (APR_SUCCESS == rv) {
internals->response->status = (int)l;
}
}
return rv;
}
static void fire_status(md_http_request_t *req, apr_status_t rv)
{
md_curl_internals_t *internals = req->internals;
if (internals && !internals->status_fired) {
internals->status_fired = 1;
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, rv, req->pool,
"req[%d] fire callbacks", req->id);
if ((APR_SUCCESS == rv) && req->cb.on_response) {
rv = req->cb.on_response(internals->response, req->cb.on_response_data);
}
internals->rv = rv;
if (req->cb.on_status) {
req->cb.on_status(req, rv, req->cb.on_status_data);
}
}
}
static apr_status_t md_curl_perform(md_http_request_t *req)
{
apr_status_t rv = APR_SUCCESS;
CURLcode curle;
md_curl_internals_t *internals;
long l;
if (APR_SUCCESS != (rv = internals_setup(req))) goto leave;
internals = req->internals;
curle = curl_easy_perform(internals->curl);
rv = curl_status(curle);
if (APR_SUCCESS != rv) {
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, req->pool,
"request failed(%d): %s", curle, curl_easy_strerror(curle));
goto leave;
}
rv = curl_status(curl_easy_getinfo(internals->curl, CURLINFO_RESPONSE_CODE, &l));
if (APR_SUCCESS == rv) {
internals->response->status = (int)l;
}
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE1, rv, req->pool, "request <-- %d",
internals->response->status);
if (req->cb.on_response) {
rv = req->cb.on_response(internals->response, req->cb.on_response_data);
req->cb.on_response = NULL;
}
leave:
fire_status(req, rv);
md_http_req_destroy(req);
return rv;
}
static md_http_request_t *find_curl_request(apr_array_header_t *requests, CURL *curl)
{
md_http_request_t *req;
md_curl_internals_t *internals;
int i;
for (i = 0; i < requests->nelts; ++i) {
req = APR_ARRAY_IDX(requests, i, md_http_request_t*);
internals = req->internals;
if (internals && internals->curl == curl) {
return req;
}
}
return NULL;
}
static void add_to_curlm(md_http_request_t *req, CURLM *curlm)
{
md_curl_internals_t *internals = req->internals;
if (curlm && internals && internals->curlm == NULL) {
curl_multi_add_handle(curlm, internals->curl);
internals->curlm = curlm;
}
}
static void remove_from_curlm(md_http_request_t *req, CURLM *curlm)
{
md_curl_internals_t *internals = req->internals;
if (curlm && internals && internals->curlm == curlm) {
curl_multi_remove_handle(curlm, internals->curl);
internals->curlm = NULL;
}
}
static apr_status_t md_curl_multi_perform(md_http_t *http, apr_pool_t *p,
md_http_next_req *nextreq, void *baton)
{
md_http_request_t *req;
CURLM *curlm = NULL;
CURLMcode mc;
struct CURLMsg *curlmsg;
apr_array_header_t *requests;
int i, running, numfds, slowdown, msgcount;
apr_status_t rv;
requests = apr_array_make(p, 10, sizeof(md_http_request_t*));
curlm = curl_multi_init();
if (!curlm) {
rv = APR_ENOMEM;
goto leave;
}
running = 1;
slowdown = 0;
while(1) {
while (1) {
/* fetch as many requests as nextreq gives us */
rv = nextreq(&req, baton, http, requests->nelts);
if (APR_SUCCESS == rv) {
if (APR_SUCCESS != (rv = internals_setup(req))) {
if (req->cb.on_status) req->cb.on_status(req, rv, req->cb.on_status_data);
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, rv, p,
"multi_perform[%d reqs]: setup failed", requests->nelts);
}
else {
APR_ARRAY_PUSH(requests, md_http_request_t*) = req;
add_to_curlm(req, curlm);
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, rv, p,
"multi_perform[%d reqs]: added request", requests->nelts);
}
continue;
}
else if (APR_STATUS_IS_ENOENT(rv)) {
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, 0, p,
"multi_perform[%d reqs]: no more requests", requests->nelts);
if (!requests->nelts) {
goto leave;
}
break;
}
else {
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, rv, p,
"multi_perform[%d reqs]: nextreq() failed", requests->nelts);
goto leave;
}
}
mc = curl_multi_perform(curlm, &running);
if (CURLM_OK == mc) {
mc = curl_multi_wait(curlm, NULL, 0, 1000, &numfds);
if (numfds) slowdown = 0;
}
if (CURLM_OK != mc) {
rv = APR_ECONNABORTED;
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, rv, p,
"multi_perform[%d reqs] failed(%d): %s",
requests->nelts, mc, curl_multi_strerror(mc));
goto leave;
}
if (!numfds) {
/* no activity on any connection, timeout */
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, 0, p,
"multi_perform[%d reqs]: slowdown %d", requests->nelts, slowdown);
if (slowdown) apr_sleep(apr_time_from_msec(100));
++slowdown;
}
/* process status messages, e.g. that a request is done */
while (running < requests->nelts) {
curlmsg = curl_multi_info_read(curlm, &msgcount);
if (!curlmsg) break;
if (curlmsg->msg == CURLMSG_DONE) {
req = find_curl_request(requests, curlmsg->easy_handle);
if (req) {
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE2, 0, p,
"multi_perform[%d reqs]: req[%d] done",
requests->nelts, req->id);
update_status(req);
fire_status(req, curl_status(curlmsg->data.result));
remove_from_curlm(req, curlm);
md_array_remove(requests, req);
md_http_req_destroy(req);
}
else {
md_log_perror(MD_LOG_MARK, MD_LOG_DEBUG, 0, p,
"multi_perform[%d reqs]: req done, but not found by handle",
requests->nelts);
}
}
}
};
leave:
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, rv, p,
"multi_perform[%d reqs]: leaving", requests->nelts);
for (i = 0; i < requests->nelts; ++i) {
req = APR_ARRAY_IDX(requests, i, md_http_request_t*);
fire_status(req, APR_SUCCESS);
remove_from_curlm(req, curlm);
md_http_req_destroy(req);
}
if (curlm) curl_multi_cleanup(curlm);
return rv;
}
static int initialized;
static apr_status_t md_curl_init(void) {
if (!initialized) {
initialized = 1;
curl_global_init(CURL_GLOBAL_DEFAULT);
}
return APR_SUCCESS;
}
static void md_curl_req_cleanup(md_http_request_t *req)
{
md_curl_internals_t *internals = req->internals;
if (internals) {
if (internals->curl) {
CURL *curl = md_http_get_impl_data(req->http);
if (curl == internals->curl) {
/* NOP: we have this curl at the md_http_t already */
}
else if (!curl) {
/* no curl at the md_http_t yet, install this one */
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, 0, req->pool, "register curl instance at http");
md_http_set_impl_data(req->http, internals->curl);
}
else {
/* There already is a curl at the md_http_t and it's not this one. */
curl_easy_cleanup(internals->curl);
}
}
if (internals->req_hdrs) curl_slist_free_all(internals->req_hdrs);
req->internals = NULL;
}
}
static void md_curl_cleanup(md_http_t *http, apr_pool_t *pool)
{
CURL *curl;
curl = md_http_get_impl_data(http);
if (curl) {
md_log_perror(MD_LOG_MARK, MD_LOG_TRACE3, 0, pool, "cleanup curl instance");
md_http_set_impl_data(http, NULL);
curl_easy_cleanup(curl);
}
}
static md_http_impl_t impl = {
md_curl_init,
md_curl_req_cleanup,
md_curl_perform,
md_curl_multi_perform,
md_curl_cleanup,
};
md_http_impl_t * md_curl_get_impl(apr_pool_t *p)
{
/* trigger early global curl init, before we are down a rabbit hole */
(void)p;
md_curl_init();
return &impl;
}
|