forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest_methods.c
641 lines (515 loc) · 15.8 KB
/
rest_methods.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
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
* Copyright (C) 2013 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* -------
* 2013-02-28: Created (Liviu)
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
#include "../../mem/shm_mem.h"
#include "../../async.h"
#include "rest_methods.h"
#include "rest_cb.h"
static char print_buff[MAX_CONTENT_TYPE_LEN];
CURLM *multi_handle;
/* additional HTTP headers for the next request */
static struct curl_slist *header_list = NULL;
/* simultaneous ongoing transfers within this process */
static int transfers;
static int read_fds[FD_SETSIZE];
/* libcurl's reported running handles */
static int running_handles;
static long sleep_on_bad_timeout = 500; /* ms */
#define clean_header_list \
do { \
if (header_list) { \
curl_slist_free_all(header_list); \
header_list = NULL; \
} \
} while (0)
#define w_curl_easy_setopt(h, opt, value) \
do { \
rc = curl_easy_setopt(h, opt, value); \
if (rc != CURLE_OK) { \
LM_ERR("curl_easy_setopt(%d): (%s)\n", opt, curl_easy_strerror(rc)); \
clean_header_list; \
goto cleanup; \
} \
} while (0)
static inline char is_new_transfer(int fd)
{
int it;
for (it = 0; it < transfers; it++) {
if (fd == read_fds[it])
return 0;
}
return 1;
}
static inline void add_transfer(int fd)
{
read_fds[transfers++] = fd;
}
static inline char del_transfer(int fd)
{
int it;
LM_DBG("del fd %d\n", fd);
for (it = 0; it < transfers; it++) {
if (fd == read_fds[it]) {
transfers--;
for (; it < transfers; it++)
read_fds[it] = read_fds[it + 1];
return 0;
}
}
return -1;
}
/**
* start_async_http_req - performs an HTTP request, stores results in pvars
* - TCP connect phase is synchronous, due to libcurl limitations
* - TCP read phase is asynchronous, thanks to the libcurl multi interface
*
* @msg: sip message struct
* @method: HTTP verb
* @url: HTTP URL to be queried
* @req_body: Body of the request (NULL if not needed)
* @req_ctype: Value for the "Content-Type: " header of the request (same as ^)
* @out_handle: CURL easy handle used to perform the transfer
* @body: reply body; gradually reallocated as data arrives
* @ctype: will eventually hold the last "Content-Type" header of the reply
*/
int start_async_http_req(struct sip_msg *msg, enum rest_client_method method,
char *url, char *req_body, char *req_ctype,
CURL **out_handle, str *body, str *ctype)
{
CURL *handle;
CURLcode rc;
CURLMcode mrc;
fd_set rset, wset, eset;
int max_fd, fd, i;
long busy_wait, timeout;
long retry_time, check_time = 5; /* 5ms looping time */
int msgs_in_queue;
CURLMsg *cmsg;
if (transfers == FD_SETSIZE) {
LM_ERR("too many ongoing tranfers: %d\n", FD_SETSIZE);
clean_header_list;
return ASYNC_NO_IO;
}
handle = curl_easy_init();
if (!handle) {
LM_ERR("Init curl handle failed!\n");
clean_header_list;
return ASYNC_NO_IO;
}
w_curl_easy_setopt(handle, CURLOPT_URL, url);
switch (method) {
case REST_CLIENT_POST:
w_curl_easy_setopt(handle, CURLOPT_POST, 1);
w_curl_easy_setopt(handle, CURLOPT_POSTFIELDS, req_body);
if (req_ctype) {
sprintf(print_buff, "Content-Type: %s", req_ctype);
header_list = curl_slist_append(header_list, print_buff);
w_curl_easy_setopt(handle, CURLOPT_HTTPHEADER, header_list);
}
break;
case REST_CLIENT_GET:
break;
default:
LM_ERR("Unsupported rest_client_method: %d, defaulting to GET\n", method);
}
if (header_list)
w_curl_easy_setopt(handle, CURLOPT_HTTPHEADER, header_list);
w_curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, connection_timeout);
w_curl_easy_setopt(handle, CURLOPT_TIMEOUT, curl_timeout);
w_curl_easy_setopt(handle, CURLOPT_VERBOSE, 1);
w_curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1);
w_curl_easy_setopt(handle, CURLOPT_STDERR, stdout);
w_curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_func);
w_curl_easy_setopt(handle, CURLOPT_WRITEDATA, body);
if (ctype) {
w_curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, header_func);
w_curl_easy_setopt(handle, CURLOPT_HEADERDATA, ctype);
}
if (ssl_capath)
w_curl_easy_setopt(handle, CURLOPT_CAPATH, ssl_capath);
if (!ssl_verifypeer)
w_curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
if (!ssl_verifyhost)
w_curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
curl_multi_add_handle(multi_handle, handle);
timeout = connection_timeout_ms;
/* obtain a read fd in "connection_timeout" seconds at worst */
for (timeout = connection_timeout_ms; timeout > 0; timeout -= busy_wait) {
mrc = curl_multi_perform(multi_handle, &running_handles);
if (mrc != CURLM_OK) {
LM_ERR("curl_multi_perform: %s\n", curl_multi_strerror(mrc));
goto error;
}
mrc = curl_multi_timeout(multi_handle, &retry_time);
if (mrc != CURLM_OK) {
LM_ERR("curl_multi_timeout: %s\n", curl_multi_strerror(mrc));
goto error;
}
if (retry_time == -1) {
LM_INFO("curl_multi_timeout() returned -1, pausing %ldms...\n",
sleep_on_bad_timeout);
busy_wait = sleep_on_bad_timeout;
usleep(1000UL * busy_wait);
continue;
}
if (retry_time > connection_timeout_ms)
LM_INFO("initial TCP connect: we must wait at least %ldms! Please "
"consider increasing 'connection_timeout'!\n", retry_time);
busy_wait = retry_time < timeout ? retry_time : timeout;
/**
* libcurl is currently stuck in internal operations (connect)
* we have to wait a bit until we receive a read fd
*/
for (i = 0; i < busy_wait; i += check_time) {
/* transfer may have already been completed!! */
while ((cmsg = curl_multi_info_read(multi_handle, &msgs_in_queue))) {
if (cmsg->easy_handle == handle && cmsg->msg == CURLMSG_DONE) {
LM_DBG("done, no need for async!\n");
clean_header_list;
*out_handle = handle;
return ASYNC_SYNC;
}
}
FD_ZERO(&rset);
mrc = curl_multi_fdset(multi_handle, &rset, &wset, &eset, &max_fd);
if (mrc != CURLM_OK) {
LM_ERR("curl_multi_fdset: %s\n", curl_multi_strerror(mrc));
goto error;
}
if (max_fd != -1) {
for (fd = 0; fd <= max_fd; fd++) {
if (FD_ISSET(fd, &rset)) {
LM_DBG(" >>>>>>>>>> fd %d ISSET(read)\n", fd);
if (is_new_transfer(fd)) {
LM_DBG("add fd to read list: %d\n", fd);
add_transfer(fd);
goto success;
}
}
}
}
usleep(1000UL * check_time);
}
}
LM_ERR("timeout while connecting to '%s' (%ld sec)\n", url, connection_timeout);
goto error;
success:
clean_header_list;
*out_handle = handle;
return fd;
error:
mrc = curl_multi_remove_handle(multi_handle, handle);
if (mrc != CURLM_OK)
LM_ERR("curl_multi_remove_handle: %s\n", curl_multi_strerror(mrc));
cleanup:
clean_header_list;
curl_easy_cleanup(handle);
return ASYNC_NO_IO;
}
enum async_ret_code resume_async_http_req(int fd, struct sip_msg *msg, void *_param)
{
CURLcode rc;
CURLMcode mrc;
rest_async_param *param = (rest_async_param *)_param;
int running, max_fd;
long http_rc;
fd_set rset, wset, eset;
pv_value_t val;
mrc = curl_multi_perform(multi_handle, &running);
if (mrc != CURLM_OK) {
LM_ERR("curl_multi_perform: %s\n", curl_multi_strerror(mrc));
return -1;
}
LM_DBG("running handles: %d\n", running);
if (running == running_handles) {
async_status = ASYNC_CONTINUE;
return 1;
}
if (running > running_handles) {
LM_BUG("incremented handles!!");
/* default async status is DONE */
return -1;
}
running_handles = running;
FD_ZERO(&rset);
mrc = curl_multi_fdset(multi_handle, &rset, &wset, &eset, &max_fd);
if (mrc != CURLM_OK) {
LM_ERR("curl_multi_fdset: %s\n", curl_multi_strerror(mrc));
/* default async status is DONE */
return -1;
}
if (max_fd == -1) {
if (running_handles != 0) {
LM_BUG("running_handles == %d", running_handles);
abort();
/* default async status is DONE */
return -1;
}
if (FD_ISSET(fd, &rset)) {
LM_BUG("fd %d is still in rset!", fd);
abort();
/* default async status is DONE */
return -1;
}
} else if (FD_ISSET(fd, &rset)) {
LM_DBG("fd %d still transferring...\n", fd);
async_status = ASYNC_CONTINUE;
return 1;
}
if (del_transfer(fd) != 0) {
LM_BUG("failed to delete fd %d", fd);
abort();
/* default async status is DONE */
return -1;
}
mrc = curl_multi_remove_handle(multi_handle, param->handle);
if (mrc != CURLM_OK) {
LM_ERR("curl_multi_remove_handle: %s\n", curl_multi_strerror(mrc));
/* default async status is DONE */
return -1;
}
val.flags = PV_VAL_STR;
val.rs = param->body;
if (pv_set_value(msg, param->body_pv, 0, &val) != 0)
LM_ERR("failed to set output body pv\n");
if (param->ctype_pv) {
val.rs = param->ctype;
if (pv_set_value(msg, param->ctype_pv, 0, &val) != 0)
LM_ERR("failed to set output ctype pv\n");
}
if (param->code_pv) {
rc = curl_easy_getinfo(param->handle, CURLINFO_RESPONSE_CODE, &http_rc);
if (rc != CURLE_OK) {
LM_ERR("curl_easy_getinfo: %s\n", curl_easy_strerror(rc));
http_rc = 0;
}
LM_DBG("Last response code: %ld\n", http_rc);
val.flags = PV_VAL_INT|PV_TYPE_INT;
val.ri = (int)http_rc;
if (pv_set_value(msg, param->code_pv, 0, &val) != 0)
LM_ERR("failed to set output code pv\n");
}
pkg_free(param->body.s);
if (param->ctype_pv && param->ctype.s)
pkg_free(param->ctype.s);
curl_easy_cleanup(param->handle);
pkg_free(param);
/* default async status is DONE */
return 1;
}
/**
* rest_get_method - performs an HTTP GET request, stores results in pvars
* @msg: sip message struct
* @url: HTTP URL to be queried
* @body_pv: pseudo var which will hold the result body
* @ctype_pv: pvar which will hold the body encoding method
* @code_pv: pvar to hold the HTTP return code
*/
int rest_get_method(struct sip_msg *msg, char *url,
pv_spec_p body_pv, pv_spec_p ctype_pv, pv_spec_p code_pv)
{
CURLcode rc;
CURL *handle = NULL;
long http_rc;
pv_value_t pv_val;
str st = { 0, 0 };
str body = { NULL, 0 }, tbody;
handle = curl_easy_init();
if (!handle) {
LM_ERR("Init curl handle failed!\n");
clean_header_list;
return -1;
}
if (header_list)
w_curl_easy_setopt(handle, CURLOPT_HTTPHEADER, header_list);
w_curl_easy_setopt(handle, CURLOPT_URL, url);
w_curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, connection_timeout);
w_curl_easy_setopt(handle, CURLOPT_TIMEOUT, curl_timeout);
w_curl_easy_setopt(handle, CURLOPT_VERBOSE, 1);
w_curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1);
w_curl_easy_setopt(handle, CURLOPT_STDERR, stdout);
w_curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_func);
w_curl_easy_setopt(handle, CURLOPT_WRITEDATA, &body);
w_curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, header_func);
w_curl_easy_setopt(handle, CURLOPT_HEADERDATA, &st);
if (ssl_capath)
w_curl_easy_setopt(handle, CURLOPT_CAPATH, ssl_capath);
if (!ssl_verifypeer)
w_curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
if (!ssl_verifyhost)
w_curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
rc = curl_easy_perform(handle);
clean_header_list;
if (code_pv) {
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &http_rc);
LM_DBG("Last response code: %ld\n", http_rc);
pv_val.flags = PV_VAL_INT|PV_TYPE_INT;
pv_val.ri = (int)http_rc;
if (pv_set_value(msg, code_pv, 0, &pv_val) != 0) {
LM_ERR("Set code pv value failed!\n");
goto cleanup;
}
}
if (rc != CURLE_OK) {
LM_ERR("curl_easy_perform: %s\n", curl_easy_strerror(rc));
goto cleanup;
}
tbody = body;
trim(&tbody);
pv_val.flags = PV_VAL_STR;
pv_val.rs = tbody;
if (pv_set_value(msg, body_pv, 0, &pv_val) != 0) {
LM_ERR("Set body pv value failed!\n");
goto cleanup;
}
if (body.s) {
pkg_free(body.s);
}
if (ctype_pv) {
pv_val.rs = st;
if (pv_set_value(msg, ctype_pv, 0, &pv_val) != 0) {
LM_ERR("Set content type pv value failed!\n");
goto cleanup;
}
if (st.s)
pkg_free(st.s);
}
curl_easy_cleanup(handle);
return 1;
cleanup:
curl_easy_cleanup(handle);
return -1;
}
/**
* rest_post_method - performs an HTTP POST request, stores results in pvars
* @msg: sip message struct
* @url: HTTP URL to be queried
* @ctype: Value for the "Content-Type: " header of the request
* @body: Body of the request
* @body_pv: pseudo var which will hold the result body
* @ctype_pv: pvar which will hold the result content type
* @code_pv: pvar to hold the HTTP return code
*/
int rest_post_method(struct sip_msg *msg, char *url, char *body, char *ctype,
pv_spec_p body_pv, pv_spec_p ctype_pv, pv_spec_p code_pv)
{
CURLcode rc;
CURL *handle = NULL;
long http_rc;
str st = { 0, 0 };
str res_body = { NULL, 0 }, tbody;
pv_value_t pv_val;
handle = curl_easy_init();
if (!handle) {
LM_ERR("Init curl handle failed!\n");
clean_header_list;
return -1;
}
if (ctype) {
sprintf(print_buff, "Content-Type: %s", ctype);
header_list = curl_slist_append(header_list, print_buff);
}
if (header_list)
w_curl_easy_setopt(handle, CURLOPT_HTTPHEADER, header_list);
w_curl_easy_setopt(handle, CURLOPT_URL, url);
w_curl_easy_setopt(handle, CURLOPT_POST, 1);
w_curl_easy_setopt(handle, CURLOPT_POSTFIELDS, body);
w_curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, connection_timeout);
w_curl_easy_setopt(handle, CURLOPT_TIMEOUT, curl_timeout);
w_curl_easy_setopt(handle, CURLOPT_VERBOSE, 1);
w_curl_easy_setopt(handle, CURLOPT_STDERR, stdout);
w_curl_easy_setopt(handle, CURLOPT_FAILONERROR, 1);
w_curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_func);
w_curl_easy_setopt(handle, CURLOPT_WRITEDATA, &res_body);
w_curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, header_func);
w_curl_easy_setopt(handle, CURLOPT_HEADERDATA, &st);
if (ssl_capath)
w_curl_easy_setopt(handle, CURLOPT_CAPATH, ssl_capath);
if (!ssl_verifypeer)
w_curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
if (!ssl_verifyhost)
w_curl_easy_setopt(handle, CURLOPT_SSL_VERIFYHOST, 0L);
rc = curl_easy_perform(handle);
clean_header_list;
if (code_pv) {
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &http_rc);
LM_DBG("Last response code: %ld\n", http_rc);
pv_val.flags = PV_VAL_INT|PV_TYPE_INT;
pv_val.ri = (int)http_rc;
if (pv_set_value(msg, code_pv, 0, &pv_val) != 0) {
LM_ERR("Set code pv value failed!\n");
goto cleanup;
}
}
if (rc != CURLE_OK) {
LM_ERR("curl_easy_perform: %s\n", curl_easy_strerror(rc));
goto cleanup;
}
tbody = res_body;
trim(&tbody);
pv_val.flags = PV_VAL_STR;
pv_val.rs = tbody;
if (pv_set_value(msg, body_pv, 0, &pv_val) != 0) {
LM_ERR("Set body pv value failed!\n");
goto cleanup;
}
if (res_body.s) {
pkg_free(res_body.s);
}
if (ctype_pv) {
pv_val.rs = st;
if (pv_set_value(msg, ctype_pv, 0, &pv_val) != 0) {
LM_ERR("Set content type pv value failed!\n");
goto cleanup;
}
if (st.s)
pkg_free(st.s);
}
curl_easy_cleanup(handle);
return 1;
cleanup:
curl_easy_cleanup(handle);
return -1;
}
/**
* rest_append_hf - add a custom HTTP header before a rest call
* @msg: sip message struct
* @hfv: HTTP header field and value
*/
int rest_append_hf_method(struct sip_msg *msg, str *hfv)
{
char buf[MAX_HEADER_FIELD_LEN];
if (hfv->len > MAX_HEADER_FIELD_LEN) {
LM_ERR("header field buffer too small\n");
return -1;
}
/* TODO: header validation */
/* append the header to the global list */
strncpy(buf, hfv->s, hfv->len);
header_list = curl_slist_append(header_list, buf);
return 1;
}