Skip to content

Commit bd4e518

Browse files
Final changes
1 parent 05f1e5f commit bd4e518

File tree

6 files changed

+860
-224
lines changed

6 files changed

+860
-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: 194 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ long connection_timeout = 20; /* s */
5252
long connect_poll_interval = 20; /* ms */
5353
long connection_timeout_ms;
5454
int max_async_transfers = 100;
55-
int max_host_connection = 10;
55+
int max_connections = 100;
5656
long curl_timeout = 20;
5757
char *ssl_capath;
5858
unsigned int max_transfer_size = 10240; /* KB (10MB) */
@@ -113,6 +113,16 @@ static int w_async_rest_put(struct sip_msg *msg, async_ctx *ctx,
113113
str *url, str *body, str *_ctype, pv_spec_t *body_pv,
114114
pv_spec_t *ctype_pv, pv_spec_t *code_pv);
115115

116+
// Temporary to expose in script
117+
static int w_async_rest_get_v2(struct sip_msg *msg, async_ctx *ctx, str *url,
118+
pv_spec_t *body_pv, pv_spec_t *ctype_pv, pv_spec_t *code_pv);
119+
static int w_async_rest_post_v2(struct sip_msg *msg, async_ctx *ctx,
120+
str *url, str *body, str *_ctype, pv_spec_t *body_pv,
121+
pv_spec_t *ctype_pv, pv_spec_t *code_pv);
122+
static int w_async_rest_put_v2(struct sip_msg *msg, async_ctx *ctx,
123+
str *url, str *body, str *_ctype, pv_spec_t *body_pv,
124+
pv_spec_t *ctype_pv, pv_spec_t *code_pv);
125+
116126
static int w_rest_append_hf(struct sip_msg *msg, str *hfv);
117127
static int w_rest_init_client_tls(struct sip_msg *msg, str *tls_client_dom);
118128
int validate_curl_http_version(const int *http_version);
@@ -148,6 +158,25 @@ static const acmd_export_t acmds[] = {
148158
{CMD_PARAM_VAR,0,0},
149159
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
150160
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}}},
161+
{"rest_get_v2",(acmd_function)w_async_rest_get_v2, {
162+
{CMD_PARAM_STR,0,0},
163+
{CMD_PARAM_VAR,0,0},
164+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
165+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}}},
166+
{"rest_post_v2",(acmd_function)w_async_rest_post_v2, {
167+
{CMD_PARAM_STR,0,0},
168+
{CMD_PARAM_STR,0,0},
169+
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0},
170+
{CMD_PARAM_VAR,0,0},
171+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
172+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}}},
173+
{"rest_put_v2",(acmd_function)w_async_rest_put_v2, {
174+
{CMD_PARAM_STR,0,0},
175+
{CMD_PARAM_STR,0,0},
176+
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0},
177+
{CMD_PARAM_VAR,0,0},
178+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
179+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}}},
151180
{0,0,{{0,0,0}}}
152181
};
153182

@@ -178,6 +207,28 @@ static const cmd_export_t cmds[] = {
178207
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
179208
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}},
180209
ALL_ROUTES},
210+
{"rest_get_v2",(cmd_function)w_rest_get, {
211+
{CMD_PARAM_STR,0,0},
212+
{CMD_PARAM_VAR,0,0},
213+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
214+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}},
215+
ALL_ROUTES},
216+
{"rest_post_v2",(cmd_function)w_rest_post, {
217+
{CMD_PARAM_STR,0,0},
218+
{CMD_PARAM_STR,0,0},
219+
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0},
220+
{CMD_PARAM_VAR,0,0},
221+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
222+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}},
223+
ALL_ROUTES},
224+
{"rest_put_v2",(cmd_function)w_rest_put, {
225+
{CMD_PARAM_STR,0,0},
226+
{CMD_PARAM_STR,0,0},
227+
{CMD_PARAM_STR|CMD_PARAM_OPT,0,0},
228+
{CMD_PARAM_VAR,0,0},
229+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0},
230+
{CMD_PARAM_VAR|CMD_PARAM_OPT,0,0}, {0,0,0}},
231+
ALL_ROUTES},
181232
{"rest_append_hf",(cmd_function)w_rest_append_hf, {
182233
{CMD_PARAM_STR,0,0}, {0,0,0}},
183234
ALL_ROUTES},
@@ -200,7 +251,7 @@ static const trans_export_t trans[] = {
200251
};
201252

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

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)