Skip to content

Commit e61655f

Browse files
Final changes
1 parent 05f1e5f commit e61655f

File tree

6 files changed

+861
-224
lines changed

6 files changed

+861
-224
lines changed

modules/rest_client/rest_cb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ size_t header_func(char *ptr, size_t size, size_t nmemb, void *userdata)
111111

112112
int timer_cb(CURLM *multi_handle, long timeout_ms, void *cbp)
113113
{
114-
LM_DBG("multi_handle timer called %d\n", timeout_ms);
114+
LM_DBG("multi_handle timer called %ld\n", timeout_ms);
115115
long *p = (long*) cbp;
116116

117117
*p = timeout_ms;
@@ -127,5 +127,5 @@ int prereq_callback(void *cbp,
127127
{
128128
enum curl_status *p = (enum curl_status*) cbp;
129129
*p = CURL_REQUEST_SENT;
130-
return CURL_PREREQFUNC_OK;
130+
return 0;
131131
}

modules/rest_client/rest_client.c

Lines changed: 195 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "../tls_mgm/api.h"
4242
#include "rest_client.h"
4343
#include "rest_methods.h"
44+
#include "rest_sockets.h"
4445
#include "../../ssl_init_tweaks.h"
4546
#include "../../pt.h"
4647
#include "../../redact_pii.h"
@@ -52,7 +53,7 @@ long connection_timeout = 20; /* s */
5253
long connect_poll_interval = 20; /* ms */
5354
long connection_timeout_ms;
5455
int max_async_transfers = 100;
55-
int max_host_connection = 10;
56+
int max_connections = 100;
5657
long curl_timeout = 20;
5758
char *ssl_capath;
5859
unsigned int max_transfer_size = 10240; /* KB (10MB) */
@@ -113,6 +114,16 @@ static int w_async_rest_put(struct sip_msg *msg, async_ctx *ctx,
113114
str *url, str *body, str *_ctype, pv_spec_t *body_pv,
114115
pv_spec_t *ctype_pv, pv_spec_t *code_pv);
115116

117+
// Temporary to expose in script
118+
static int w_async_rest_get_v2(struct sip_msg *msg, async_ctx *ctx, str *url,
119+
pv_spec_t *body_pv, pv_spec_t *ctype_pv, pv_spec_t *code_pv);
120+
static int w_async_rest_post_v2(struct sip_msg *msg, async_ctx *ctx,
121+
str *url, str *body, str *_ctype, pv_spec_t *body_pv,
122+
pv_spec_t *ctype_pv, pv_spec_t *code_pv);
123+
static int w_async_rest_put_v2(struct sip_msg *msg, async_ctx *ctx,
124+
str *url, str *body, str *_ctype, pv_spec_t *body_pv,
125+
pv_spec_t *ctype_pv, pv_spec_t *code_pv);
126+
116127
static int w_rest_append_hf(struct sip_msg *msg, str *hfv);
117128
static int w_rest_init_client_tls(struct sip_msg *msg, str *tls_client_dom);
118129
int validate_curl_http_version(const int *http_version);
@@ -148,6 +159,25 @@ static const acmd_export_t acmds[] = {
148159
{CMD_PARAM_VAR,0,0},
149160
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
150161
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}}},
162+
{"rest_get_v2",(acmd_function)w_async_rest_get_v2, {
163+
{CMD_PARAM_STR,0,0},
164+
{CMD_PARAM_VAR,0,0},
165+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
166+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}}},
167+
{"rest_post_v2",(acmd_function)w_async_rest_post_v2, {
168+
{CMD_PARAM_STR,0,0},
169+
{CMD_PARAM_STR,0,0},
170+
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0},
171+
{CMD_PARAM_VAR,0,0},
172+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
173+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}}},
174+
{"rest_put_v2",(acmd_function)w_async_rest_put_v2, {
175+
{CMD_PARAM_STR,0,0},
176+
{CMD_PARAM_STR,0,0},
177+
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0},
178+
{CMD_PARAM_VAR,0,0},
179+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
180+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}}},
151181
{0,0,{{0,0,0}}}
152182
};
153183

@@ -178,6 +208,28 @@ static const cmd_export_t cmds[] = {
178208
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
179209
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}},
180210
ALL_ROUTES},
211+
{"rest_get_v2",(cmd_function)w_rest_get, {
212+
{CMD_PARAM_STR,0,0},
213+
{CMD_PARAM_VAR,0,0},
214+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
215+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}},
216+
ALL_ROUTES},
217+
{"rest_post_v2",(cmd_function)w_rest_post, {
218+
{CMD_PARAM_STR,0,0},
219+
{CMD_PARAM_STR,0,0},
220+
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0},
221+
{CMD_PARAM_VAR,0,0},
222+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
223+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}},
224+
ALL_ROUTES},
225+
{"rest_put_v2",(cmd_function)w_rest_put, {
226+
{CMD_PARAM_STR,0,0},
227+
{CMD_PARAM_STR,0,0},
228+
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0},
229+
{CMD_PARAM_VAR,0,0},
230+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
231+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}},
232+
ALL_ROUTES},
181233
{"rest_append_hf",(cmd_function)w_rest_append_hf, {
182234
{CMD_PARAM_STR,0,0}, {0,0,0}},
183235
ALL_ROUTES},
@@ -200,7 +252,7 @@ static const trans_export_t trans[] = {
200252
};
201253

202254
static int warm_pool_urls(modparam_t type, void *val) {
203-
int num_conns;
255+
unsigned int num_conns;
204256
char *mod_param, *delim, *host;
205257
size_t delim_index, string_end;
206258
preconnect_urls *tmp;
@@ -272,7 +324,6 @@ static const param_export_t params[] = {
272324
{ "connection_timeout", INT_PARAM, &connection_timeout },
273325
{ "connect_poll_interval", INT_PARAM, &connect_poll_interval },
274326
{ "max_async_transfers", INT_PARAM, &max_async_transfers },
275-
{ "max_host_connection", INT_PARAM, &max_host_connection},
276327
{ "max_transfer_size", INT_PARAM, &max_transfer_size },
277328
{ "curl_timeout", INT_PARAM, &curl_timeout },
278329
{ "ssl_capath", STR_PARAM, &ssl_capath },
@@ -282,6 +333,8 @@ static const param_export_t params[] = {
282333
{ "enable_expect_100", INT_PARAM, &enable_expect_100 },
283334
{ "no_concurrent_connects", INT_PARAM, &no_concurrent_connects },
284335
{ "curl_conn_lifetime", INT_PARAM, &curl_conn_lifetime },
336+
{ "use_multi_socket_api", INT_PARAM, &use_multi_socket_api },
337+
{ "max_connections", INT_PARAM, &max_connections },
285338
{ "warm_pool_urls", STR_PARAM|USE_FUNC_PARAM,
286339
(void*)&warm_pool_urls },
287340
{ 0, 0, 0 }
@@ -895,3 +948,142 @@ static int w_rest_init_client_tls(struct sip_msg *msg, str *tls_client_dom)
895948
{
896949
return rest_init_client_tls(msg, tls_client_dom);
897950
}
951+
952+
// Temporary duplication for feature toggle in script
953+
int async_rest_method_v2(enum rest_client_method method, struct sip_msg *msg,
954+
char *url, str *body, str *ctype, async_ctx *ctx,
955+
pv_spec_p body_pv, pv_spec_p ctype_pv, pv_spec_p code_pv)
956+
{
957+
rest_async_param *param;
958+
pv_value_t val;
959+
char *host;
960+
int read_fd, rc, lrc = RCL_OK;
961+
962+
param = pkg_malloc(sizeof *param);
963+
if (!param) {
964+
LM_ERR("no more shm\n");
965+
return RCL_INTERNAL_ERR;
966+
}
967+
memset(param, '\0', sizeof *param);
968+
969+
if (no_concurrent_connects && (lrc=rcl_acquire_url(url, &host)) < RCL_OK)
970+
return lrc;
971+
972+
param->timeout_s = (ctx->timeout_s && ctx->timeout_s < curl_timeout) ?
973+
ctx->timeout_s : curl_timeout;
974+
975+
rc = start_async_http_req_v2(msg, method, url, body, ctype,
976+
param, &param->body, ctype_pv ? &param->ctype : NULL, &read_fd);
977+
978+
/* error occurred; no transfer done */
979+
if (read_fd == ASYNC_NO_IO) {
980+
ctx->resume_param = NULL;
981+
ctx->resume_f = NULL;
982+
if (code_pv) {
983+
val.flags = PV_VAL_INT|PV_TYPE_INT;
984+
val.ri = 0;
985+
if (pv_set_value(msg, (pv_spec_p)code_pv, 0, &val) != 0)
986+
LM_ERR("failed to set output code pv\n");
987+
}
988+
989+
/* keep default async status of NO_IO */
990+
pkg_free(param);
991+
goto done;
992+
}
993+
994+
/* the TCP connection is established, async started with success */
995+
996+
if (lrc == RCL_OK_LOCKED)
997+
rcl_release_url(host, rc == RCL_OK);
998+
999+
ctx->resume_f = resume_async_http_req_v2;
1000+
ctx->timeout_f = time_out_async_http_req_v2;
1001+
ctx->timeout_s = param->timeout_s;
1002+
1003+
param->method = method;
1004+
param->body_pv = (pv_spec_p)body_pv;
1005+
param->ctype_pv = (pv_spec_p)ctype_pv;
1006+
param->code_pv = (pv_spec_p)code_pv;
1007+
ctx->resume_param = param;
1008+
1009+
async_status = read_fd;
1010+
return 1;
1011+
1012+
done:
1013+
if (lrc == RCL_OK_LOCKED)
1014+
rcl_release_url(host, rc == RCL_OK);
1015+
return rc;
1016+
}
1017+
1018+
static int w_async_rest_get_v2(struct sip_msg *msg, async_ctx *ctx, str *url,
1019+
pv_spec_t *body_pv, pv_spec_t *ctype_pv, pv_spec_t *code_pv)
1020+
{
1021+
str url_nt;
1022+
int rc;
1023+
1024+
if (pkg_nt_str_dup(&url_nt, url) < 0) {
1025+
LM_ERR("No more pkg memory\n");
1026+
return RCL_INTERNAL_ERR;
1027+
}
1028+
1029+
LM_DBG("async rest get %.*s %p %p %p\n", url->len, url->s,
1030+
body_pv, ctype_pv, code_pv);
1031+
1032+
rc = async_rest_method_v2(REST_CLIENT_GET, msg, url_nt.s, NULL, NULL, ctx,
1033+
body_pv, ctype_pv, code_pv);
1034+
1035+
pkg_free(url_nt.s);
1036+
return rc;
1037+
}
1038+
1039+
static int w_async_rest_post_v2(struct sip_msg *msg, async_ctx *ctx,
1040+
str *url, str *body, str *_ctype, pv_spec_t *body_pv,
1041+
pv_spec_t *ctype_pv, pv_spec_t *code_pv)
1042+
{
1043+
str ctype = { NULL, 0 };
1044+
str url_nt;
1045+
int rc;
1046+
1047+
if (pkg_nt_str_dup(&url_nt, url) < 0) {
1048+
LM_ERR("No more pkg memory\n");
1049+
return RCL_INTERNAL_ERR;
1050+
}
1051+
1052+
if (_ctype)
1053+
ctype = *_ctype;
1054+
1055+
LM_DBG("async rest post '%.*s' %p %p %p\n", url->len, url->s,
1056+
body_pv, ctype_pv, code_pv);
1057+
1058+
rc = async_rest_method_v2(REST_CLIENT_POST, msg, url_nt.s, body, &ctype, ctx,
1059+
body_pv, ctype_pv, code_pv);
1060+
1061+
pkg_free(url_nt.s);
1062+
return rc;
1063+
}
1064+
1065+
static int w_async_rest_put_v2(struct sip_msg *msg, async_ctx *ctx,
1066+
str *url, str *body, str *_ctype, pv_spec_t *body_pv,
1067+
pv_spec_t *ctype_pv, pv_spec_t *code_pv)
1068+
{
1069+
str ctype = { NULL, 0 };
1070+
str url_nt;
1071+
int rc;
1072+
1073+
if (pkg_nt_str_dup(&url_nt, url) < 0) {
1074+
LM_ERR("No more pkg memory\n");
1075+
return RCL_INTERNAL_ERR;
1076+
}
1077+
1078+
if (_ctype)
1079+
ctype = *_ctype;
1080+
1081+
LM_DBG("async rest put '%.*s' %p %p %p\n",
1082+
url->len, url->s, body_pv, ctype_pv, code_pv);
1083+
1084+
rc = async_rest_method_v2(REST_CLIENT_PUT, msg, url_nt.s, body, &ctype, ctx,
1085+
body_pv, ctype_pv, code_pv);
1086+
1087+
pkg_free(url_nt.s);
1088+
return rc;
1089+
}

modules/rest_client/rest_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ enum tr_rest_subtype {
3232

3333
typedef struct _preconnect_urls {
3434
char *url;
35-
long connections;
35+
unsigned int connections;
3636
struct _preconnect_urls *next;
3737
} preconnect_urls;
3838

0 commit comments

Comments
 (0)