Browse code

http_client: new function http_client_get()

- do explicit GET request with headers (and body)
- exported KSR.http_client.get_hdrs()

Daniel-Constantin Mierla authored on 21/12/2020 09:15:53
Showing 1 changed files
... ...
@@ -147,6 +147,8 @@ static int w_http_query_post(
147 147
 		struct sip_msg *_m, char *_url, char *_post, char *_result);
148 148
 static int w_http_query_post_hdr(struct sip_msg *_m, char *_url, char *_post,
149 149
 		char *_hdrs, char *_result);
150
+static int w_http_query_get_hdr(struct sip_msg *_m, char *_url, char *_body,
151
+		char *_hdrs, char *_result);
150 152
 static int w_curl_connect(
151 153
 		struct sip_msg *_m, char *_con, char *_url, char *_result);
152 154
 
... ...
@@ -168,6 +170,9 @@ static cmd_export_t cmds[] = {
168 170
 	{"http_client_query", (cmd_function)w_http_query_post_hdr, 4, fixup_http_query_post_hdr,
169 171
 		fixup_free_http_query_post_hdr,
170 172
 		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
173
+	{"http_client_get", (cmd_function)w_http_query_get_hdr, 4, fixup_http_query_post_hdr,
174
+		fixup_free_http_query_post_hdr,
175
+		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
171 176
 	{"http_connect", (cmd_function)w_curl_connect, 3, fixup_curl_connect,
172 177
 		fixup_free_curl_connect,
173 178
 		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
... ...
@@ -1002,6 +1007,107 @@ static int w_http_query_post_hdr(
1002 1007
 	return w_http_query_script(_m, _url, _post, _hdrs, _result);
1003 1008
 }
1004 1009
 
1010
+/*!
1011
+ * helper for HTTP-Query function
1012
+ */
1013
+static int ki_http_request_helper(sip_msg_t *_m, str *met, str *url, str *body,
1014
+		str *hdrs, pv_spec_t *dst)
1015
+{
1016
+	int ret = 0;
1017
+	str result = {NULL, 0};
1018
+	pv_value_t val;
1019
+
1020
+	if(url==NULL || url->s==NULL) {
1021
+		LM_ERR("invalid url parameter\n");
1022
+		return -1;
1023
+	}
1024
+	ret = http_client_request(_m, url->s, &result,
1025
+			(body && body->s && body->len>0)?body->s:NULL,
1026
+			(hdrs && hdrs->s && hdrs->len>0)?hdrs->s:NULL,
1027
+			(met && met->s && met->len>0)?met->s:NULL);
1028
+
1029
+	val.rs = result;
1030
+	val.flags = PV_VAL_STR;
1031
+	if(dst->setf) {
1032
+		dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
1033
+	} else {
1034
+		LM_WARN("target pv is not writable\n");
1035
+	}
1036
+
1037
+	if(result.s != NULL)
1038
+		pkg_free(result.s);
1039
+
1040
+	return (ret == 0) ? -1 : ret;
1041
+}
1042
+
1043
+/*!
1044
+ * KEMI function to perform GET with headers and body
1045
+ */
1046
+static int ki_http_get_hdrs(sip_msg_t *_m, str *url, str *body,
1047
+		str *hdrs, str *dpv)
1048
+{
1049
+	str met = str_init("GET");
1050
+	pv_spec_t *dst;
1051
+
1052
+	dst = pv_cache_get(dpv);
1053
+	if(dst==NULL) {
1054
+		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
1055
+		return -1;
1056
+	}
1057
+	if(dst->setf==NULL) {
1058
+		LM_ERR("target pv is not writable: %.*s\n", dpv->len, dpv->s);
1059
+		return -1;
1060
+	}
1061
+
1062
+	return ki_http_request_helper(_m, &met, url, body, hdrs, dst);
1063
+}
1064
+
1065
+/*!
1066
+ * Wrapper for HTTP-Query function for cfg script
1067
+ */
1068
+static int w_http_get_script(sip_msg_t *_m, char *_url, char *_body,
1069
+		char *_hdrs, char *_result)
1070
+{
1071
+	str met = str_init("GET");
1072
+	str url = {NULL, 0};
1073
+	str body = {NULL, 0};
1074
+	str hdrs = {NULL, 0};
1075
+	pv_spec_t *dst;
1076
+
1077
+	if(get_str_fparam(&url, _m, (gparam_p)_url) != 0 || url.len <= 0) {
1078
+		LM_ERR("URL has no value\n");
1079
+		return -1;
1080
+	}
1081
+	if(_body && get_str_fparam(&body, _m, (gparam_p)_body) != 0) {
1082
+		LM_ERR("DATA has no value\n");
1083
+		return -1;
1084
+	} else {
1085
+		if(body.len == 0) {
1086
+			body.s = NULL;
1087
+		}
1088
+	}
1089
+	if(_hdrs && get_str_fparam(&hdrs, _m, (gparam_p)_hdrs) != 0) {
1090
+		LM_ERR("HDRS has no value\n");
1091
+		return -1;
1092
+	} else {
1093
+		if(hdrs.len == 0) {
1094
+			hdrs.s = NULL;
1095
+		}
1096
+	}
1097
+	dst = (pv_spec_t *)_result;
1098
+
1099
+	return ki_http_request_helper(_m, &met, &url, &body, &hdrs, dst);
1100
+}
1101
+
1102
+/*!
1103
+ * Wrapper for HTTP-GET (HDRS-Variant)
1104
+ */
1105
+static int w_http_query_get_hdr(
1106
+		sip_msg_t *_m, char *_url, char *_body, char *_hdrs, char *_result)
1107
+{
1108
+	return w_http_get_script(_m, _url, _body, _hdrs, _result);
1109
+}
1110
+
1005 1111
 /*!
1006 1112
  * Parse arguments to  pv $curlerror
1007 1113
  */
... ...
@@ -1149,6 +1255,11 @@ static sr_kemi_t sr_kemi_http_client_exports[] = {
1149 1255
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
1150 1256
 			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
1151 1257
 	},
1258
+	{ str_init("http_client"), str_init("get_hdrs"),
1259
+		SR_KEMIP_INT, ki_http_get_hdrs,
1260
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
1261
+			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
1262
+	},
1152 1263
 	{ str_init("http_client"), str_init("curl_connect"),
1153 1264
 		SR_KEMIP_INT, ki_curl_connect,
1154 1265
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
Browse code

http_client: do not set empty headers and body

Daniel-Constantin Mierla authored on 16/12/2020 19:54:57
Showing 1 changed files
... ...
@@ -894,8 +894,9 @@ static int ki_http_query_helper(sip_msg_t *_m, str *url, str *post, str *hdrs,
894 894
 		LM_ERR("invalid url parameter\n");
895 895
 		return -1;
896 896
 	}
897
-	ret = http_client_query(_m, url->s, &result, (post && post->s)?post->s:NULL,
898
-			(hdrs && hdrs->s)?hdrs->s:NULL);
897
+	ret = http_client_query(_m, url->s, &result,
898
+			(post && post->s && post->len>0)?post->s:NULL,
899
+			(hdrs && hdrs->s && hdrs->len>0)?hdrs->s:NULL);
899 900
 
900 901
 	val.rs = result;
901 902
 	val.flags = PV_VAL_STR;
Browse code

http_client: typo in variable name for network interface

Daniel-Constantin Mierla authored on 21/01/2020 20:21:46
Showing 1 changed files
... ...
@@ -100,7 +100,7 @@ unsigned int default_authmethod =
100 100
 		CURLAUTH_BASIC
101 101
 		| CURLAUTH_DIGEST; /*!< authentication method - Basic, Digest or both */
102 102
 
103
-char *default_netintrface = 0; /*!< local network interface */
103
+char *default_netinterface = 0; /*!< local network interface */
104 104
 
105 105
 /*!< Default http query result mode
106 106
  * - 0: return full result
Browse code

http_client: new param netinterface to set curl network interface

Daniel-Constantin Mierla authored on 17/01/2020 11:39:11
Showing 1 changed files
... ...
@@ -100,6 +100,8 @@ unsigned int default_authmethod =
100 100
 		CURLAUTH_BASIC
101 101
 		| CURLAUTH_DIGEST; /*!< authentication method - Basic, Digest or both */
102 102
 
103
+char *default_netintrface = 0; /*!< local network interface */
104
+
103 105
 /*!< Default http query result mode
104 106
  * - 0: return full result
105 107
  * - 1: return first line only */
... ...
@@ -204,6 +206,7 @@ static param_export_t params[] = {
204 206
 	{"keep_connections", PARAM_INT, &default_keep_connections },
205 207
 	{"query_result", PARAM_INT, &default_query_result },
206 208
 	{"query_maxdatasize", PARAM_INT, &default_query_maxdatasize },
209
+	{"netinterface", PARAM_STRING,  &default_netinterface },
207 210
 	{0, 0, 0}
208 211
 };
209 212
 
Browse code

http_client: new param query_maxdatasize

- set the maximum size to be downloaded for http_client_query() result
- default: 0 - disabled (unlimited size)

Daniel-Constantin Mierla authored on 13/08/2019 07:20:08
Showing 1 changed files
... ...
@@ -104,6 +104,8 @@ unsigned int default_authmethod =
104 104
  * - 0: return full result
105 105
  * - 1: return first line only */
106 106
 unsigned int default_query_result = 1;
107
+/*!< Default download size for result of query function. 0=disabled (no limit) */
108
+unsigned int default_query_maxdatasize = 0;
107 109
 
108 110
 str http_client_config_file = STR_NULL;
109 111
 
... ...
@@ -201,6 +203,7 @@ static param_export_t params[] = {
201 203
 	{"authmethod", PARAM_INT, &default_authmethod },
202 204
 	{"keep_connections", PARAM_INT, &default_keep_connections },
203 205
 	{"query_result", PARAM_INT, &default_query_result },
206
+	{"query_maxdatasize", PARAM_INT, &default_query_maxdatasize },
204 207
 	{0, 0, 0}
205 208
 };
206 209
 
Browse code

http_client: new param query_result

- control if the http_client_query() returns only the first line or the
entire result
- 0: return the entire result
- 1: return the first line (default, backward compatible)

Daniel-Constantin Mierla authored on 13/08/2019 07:07:34
Showing 1 changed files
... ...
@@ -100,6 +100,11 @@ unsigned int default_authmethod =
100 100
 		CURLAUTH_BASIC
101 101
 		| CURLAUTH_DIGEST; /*!< authentication method - Basic, Digest or both */
102 102
 
103
+/*!< Default http query result mode
104
+ * - 0: return full result
105
+ * - 1: return first line only */
106
+unsigned int default_query_result = 1;
107
+
103 108
 str http_client_config_file = STR_NULL;
104 109
 
105 110
 static curl_version_info_data *curl_info;
... ...
@@ -195,6 +200,7 @@ static param_export_t params[] = {
195 200
 	{"httpcon",  PARAM_STRING|USE_FUNC_PARAM, (void*)curl_con_param},
196 201
 	{"authmethod", PARAM_INT, &default_authmethod },
197 202
 	{"keep_connections", PARAM_INT, &default_keep_connections },
203
+	{"query_result", PARAM_INT, &default_query_result },
198 204
 	{0, 0, 0}
199 205
 };
200 206
 
Browse code

http_client: added http_connect_raw(...) function

- similar to http_connect() for http/post, however the data parameter
is not PV parsed, in this way json and any other content can be used
without any escaping

Julien Chavanton authored on 12/12/2018 08:54:16 • Daniel-Constantin Mierla committed on 12/12/2018 08:54:16
Showing 1 changed files
... ...
@@ -120,9 +120,13 @@ static int fixup_free_http_query_post_hdr(void **param, int param_no);
120 120
 static int fixup_curl_connect(void **param, int param_no);
121 121
 static int fixup_free_curl_connect(void **param, int param_no);
122 122
 static int fixup_curl_connect_post(void **param, int param_no);
123
+static int fixup_curl_connect_post_raw(void **param, int param_no);
123 124
 static int fixup_free_curl_connect_post(void **param, int param_no);
125
+static int fixup_free_curl_connect_post_raw(void **param, int param_no);
124 126
 static int w_curl_connect_post(struct sip_msg *_m, char *_con, char *_url,
125 127
 		char *_result, char *_ctype, char *_data);
128
+static int w_curl_connect_post_raw(struct sip_msg *_m, char *_con, char *_url,
129
+		char *_result, char *_ctype, char *_data);
126 130
 
127 131
 static int fixup_curl_get_redirect(void **param, int param_no);
128 132
 static int fixup_free_curl_get_redirect(void **param, int param_no);
... ...
@@ -161,6 +165,9 @@ static cmd_export_t cmds[] = {
161 165
 	{"http_connect", (cmd_function)w_curl_connect_post, 5, fixup_curl_connect_post,
162 166
 		fixup_free_curl_connect_post,
163 167
 		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
168
+	{"http_connect_raw", (cmd_function)w_curl_connect_post_raw, 5, fixup_curl_connect_post_raw,
169
+		fixup_free_curl_connect_post_raw,
170
+		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
164 171
 	{"http_get_redirect", (cmd_function)w_curl_get_redirect, 2, fixup_curl_get_redirect,
165 172
 		fixup_free_curl_get_redirect,
166 173
 		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE},
... ...
@@ -482,6 +489,37 @@ static int fixup_curl_connect_post(void **param, int param_no)
482 489
 	return -1;
483 490
 }
484 491
 
492
+/*
493
+ * Fix curl_connect params when posting (5 parameters): 
494
+ *	connection (string/pvar), url (string with pvars), content-type, 
495
+ *      data (string(with no pvar parsing), pvar)
496
+ */
497
+static int fixup_curl_connect_post_raw(void **param, int param_no)
498
+{
499
+
500
+	if(param_no == 1 || param_no == 3 || param_no == 4) {
501
+		/* We want char * strings */
502
+		return 0;
503
+	}
504
+	/* URL and data may contain pvar */
505
+	if(param_no == 2) {
506
+		return fixup_spve_null(param, 1);
507
+	}
508
+	if(param_no == 5) {
509
+		if(fixup_pvar_null(param, 1) != 0) {
510
+			LM_ERR("failed to fixup result pseudo variable\n");
511
+			return -1;
512
+		}
513
+		if(((pv_spec_t *)(*param))->setf == NULL) {
514
+			LM_ERR("result pvar is not writeable\n");
515
+			return -1;
516
+		}
517
+		return 0;
518
+	}
519
+
520
+	LM_ERR("invalid parameter number <%d>\n", param_no);
521
+	return -1;
522
+}
485 523
 
486 524
 /*
487 525
  * Free curl_connect params.
... ...
@@ -504,6 +542,27 @@ static int fixup_free_curl_connect_post(void **param, int param_no)
504 542
 	return -1;
505 543
 }
506 544
 
545
+/*
546
+ * Free curl_connect params.
547
+ */
548
+static int fixup_free_curl_connect_post_raw(void **param, int param_no)
549
+{
550
+	if(param_no == 1 || param_no == 3 || param_no == 4) {
551
+		/* Char strings don't need freeing */
552
+		return 0;
553
+	}
554
+	if(param_no == 2) {
555
+		return fixup_free_spve_null(param, 1);
556
+	}
557
+
558
+	if(param_no == 5) {
559
+		return fixup_free_pvar_null(param, 1);
560
+	}
561
+
562
+	LM_ERR("invalid parameter number <%d>\n", param_no);
563
+	return -1;
564
+}
565
+
507 566
 /*
508 567
  * Free curl_connect params.
509 568
  */
... ...
@@ -645,6 +704,44 @@ static int ki_curl_connect_post(sip_msg_t *_m, str *con, str *url,
645 704
 	return ki_curl_connect_post_helper(_m, con, url, ctype, data, dst);
646 705
 }
647 706
 
707
+/*
708
+ * Wrapper for Curl_connect (POST) raw data (no pvar parsing inside the data)
709
+ */
710
+static int w_curl_connect_post_raw(struct sip_msg *_m, char *_con, char *_url,
711
+		char *_ctype, char *_data, char *_result)
712
+{
713
+	str con = {NULL, 0};
714
+	str url = {NULL, 0};
715
+	str ctype = {NULL, 0};
716
+	str data = {NULL, 0};
717
+	pv_spec_t *dst;
718
+
719
+	if(_con == NULL || _url == NULL || _ctype==NULL || _data == NULL
720
+			|| _result == NULL) {
721
+		LM_ERR("http_connect: Invalid parameters\n");
722
+		return -1;
723
+	}
724
+	con.s = _con;
725
+	con.len = strlen(con.s);
726
+
727
+	if(get_str_fparam(&url, _m, (gparam_p)_url) != 0) {
728
+		LM_ERR("http_connect: URL has no value\n");
729
+		return -1;
730
+	}
731
+
732
+	ctype.s = _ctype;
733
+	ctype.len = strlen(ctype.s);
734
+
735
+	data.s = _data;
736
+	data.len = strlen(data.s);
737
+
738
+	LM_DBG("**** HTTP_CONNECT: Connection %s URL %s Result var %s\n", _con,
739
+			_url, _result);
740
+	dst = (pv_spec_t *)_result;
741
+
742
+	return ki_curl_connect_post_helper(_m, &con, &url, &ctype, &data, dst);
743
+}
744
+
648 745
 /*
649 746
  * Wrapper for Curl_connect (POST)
650 747
  */
... ...
@@ -1058,4 +1155,4 @@ int mod_register(char *path, int *dlflags, void *p1, void *p2)
1058 1155
 {
1059 1156
 	sr_kemi_modules_add(sr_kemi_http_client_exports);
1060 1157
 	return 0;
1061
-}
1062 1158
\ No newline at end of file
1159
+}
Browse code

http_client: updated to new module interface format

Charles Chance authored on 27/09/2018 16:17:24 • Victor Seva committed on 28/09/2018 11:03:26
Showing 1 changed files
... ...
@@ -204,18 +204,16 @@ static pv_export_t mod_pvs[] = {
204 204
 
205 205
 /* Module interface */
206 206
 struct module_exports exports = {
207
-	"http_client",
208
-	DEFAULT_DLFLAGS, /* dlopen flags */
209
-	cmds,      /* Exported functions */
210
-	params,    /* Exported parameters */
211
-	0,         /* exported statistics */
212
-	0,         /* exported MI functions */
213
-	mod_pvs,         /* exported pseudo-variables */
214
-	0,         /* extra processes */
215
-	mod_init,  /* module initialization function */
216
-	0,         /* response function*/
217
-	destroy,   /* destroy function */
218
-	child_init /* per-child init function */
207
+	"http_client",		/* module name */
208
+	DEFAULT_DLFLAGS,	/* dlopen flags */
209
+	cmds,				/* exported functions */
210
+	params,				/* exported parameters */
211
+	0,					/* RPC method exports */
212
+	mod_pvs,					/* exported pseudo-variables */
213
+	0,					/* response handling function */
214
+	mod_init,			/* module initialization function */
215
+	child_init,			/* per-child init function */
216
+	destroy				/* module destroy function */
219 217
 };
220 218
 /* clang-format on */
221 219
 
Browse code

Fix Typeo: authmetod -> authmethod

Exported parameter for authmethod was mis-spelled as authmetod.

Barry flanagan authored on 25/09/2018 08:56:11 • Daniel-Constantin Mierla committed on 26/09/2018 11:25:23
Showing 1 changed files
... ...
@@ -186,7 +186,7 @@ static param_export_t params[] = {
186 186
 	{"maxdatasize", PARAM_INT,  &default_maxdatasize },
187 187
 	{"config_file", PARAM_STR,  &http_client_config_file },
188 188
 	{"httpcon",  PARAM_STRING|USE_FUNC_PARAM, (void*)curl_con_param},
189
-	{"authmetod", PARAM_INT, &default_authmethod },
189
+	{"authmethod", PARAM_INT, &default_authmethod },
190 190
 	{"keep_connections", PARAM_INT, &default_keep_connections },
191 191
 	{0, 0, 0}
192 192
 };
Browse code

http_client: fixed safety check looking up var names

Daniel-Constantin Mierla authored on 08/12/2017 08:07:40
Showing 1 changed files
... ...
@@ -561,7 +561,7 @@ static int ki_curl_connect(sip_msg_t *_m, str *con, str *url, str *dpv)
561 561
 	pv_spec_t *dst;
562 562
 
563 563
 	dst = pv_cache_get(dpv);
564
-	if(dpv==NULL) {
564
+	if(dst==NULL) {
565 565
 		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
566 566
 		return -1;
567 567
 	}
... ...
@@ -635,7 +635,7 @@ static int ki_curl_connect_post(sip_msg_t *_m, str *con, str *url,
635 635
 	pv_spec_t *dst;
636 636
 
637 637
 	dst = pv_cache_get(dpv);
638
-	if(dpv==NULL) {
638
+	if(dst==NULL) {
639 639
 		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
640 640
 		return -1;
641 641
 	}
... ...
@@ -810,7 +810,7 @@ static int ki_http_query_post_hdrs(sip_msg_t *_m, str *url, str *post, str *hdrs
810 810
 	pv_spec_t *dst;
811 811
 
812 812
 	dst = pv_cache_get(dpv);
813
-	if(dpv==NULL) {
813
+	if(dst==NULL) {
814 814
 		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
815 815
 		return -1;
816 816
 	}
Browse code

http_client: exported functions to kemi framework

Daniel-Constantin Mierla authored on 05/12/2017 15:45:27
Showing 1 changed files
... ...
@@ -61,6 +61,7 @@
61 61
 #include "../../core/config.h"
62 62
 #include "../../core/lvalue.h"
63 63
 #include "../../core/pt.h" /* Process table */
64
+#include "../../core/kemi.h"
64 65
 
65 66
 #include "functions.h"
66 67
 #include "curlcon.h"
... ...
@@ -529,16 +530,57 @@ static int fixup_free_curl_connect(void **param, int param_no)
529 530
 /*
530 531
  * Wrapper for Curl_connect (GET)
531 532
  */
532
-static int w_curl_connect(
533
-		struct sip_msg *_m, char *_con, char *_url, char *_result)
533
+static int ki_curl_connect_helper(sip_msg_t *_m, str *con, str *url,
534
+		pv_spec_t *dst)
534 535
 {
536
+	str result = {NULL, 0};
537
+	pv_value_t val;
538
+	int ret = 0;
539
+
540
+	ret = curl_con_query_url(_m, con, url, &result, NULL, NULL);
541
+
542
+	val.rs = result;
543
+	val.flags = PV_VAL_STR;
544
+	if(dst->setf) {
545
+		dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
546
+	} else {
547
+		LM_WARN("target pv is not writable\n");
548
+	}
535 549
 
550
+	if(result.s != NULL)
551
+		pkg_free(result.s);
552
+
553
+	return (ret == 0) ? -1 : ret;
554
+}
555
+
556
+/*
557
+ * Kemi wrapper for Curl_connect (GET)
558
+ */
559
+static int ki_curl_connect(sip_msg_t *_m, str *con, str *url, str *dpv)
560
+{
561
+	pv_spec_t *dst;
562
+
563
+	dst = pv_cache_get(dpv);
564
+	if(dpv==NULL) {
565
+		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
566
+		return -1;
567
+	}
568
+	if(dst->setf==NULL) {
569
+		LM_ERR("target pv is not writable: %.*s\n", dpv->len, dpv->s);
570
+		return -1;
571
+	}
572
+
573
+	return ki_curl_connect_helper(_m, con, url, dst);
574
+}
575
+
576
+/*
577
+ * Cfg wrapper for Curl_connect (GET)
578
+ */
579
+static int w_curl_connect(sip_msg_t *_m, char *_con, char *_url, char *_result)
580
+{
536 581
 	str con = {NULL, 0};
537 582
 	str url = {NULL, 0};
538
-	str result = {NULL, 0};
539 583
 	pv_spec_t *dst;
540
-	pv_value_t val;
541
-	int ret = 0;
542 584
 
543 585
 	if(_con == NULL || _url == NULL || _result == NULL) {
544 586
 		LM_ERR("http_connect: Invalid parameter\n");
... ...
@@ -546,7 +588,6 @@ static int w_curl_connect(
546 588
 	}
547 589
 	con.s = _con;
548 590
 	con.len = strlen(con.s);
549
-
550 591
 	if(get_str_fparam(&url, _m, (gparam_p)_url) != 0) {
551 592
 		LM_ERR("http_connect: url has no value\n");
552 593
 		return -1;
... ...
@@ -554,13 +595,30 @@ static int w_curl_connect(
554 595
 
555 596
 	LM_DBG("**** HTTP_CONNECT Connection %s URL %s Result var %s\n", _con, _url,
556 597
 			_result);
598
+	dst = (pv_spec_t *)_result;
557 599
 
558
-	ret = curl_con_query_url(_m, &con, &url, &result, NULL, NULL);
600
+	return ki_curl_connect_helper(_m, &con, &url, dst);
601
+}
602
+
603
+/*
604
+ * Wrapper for Curl_connect (POST)
605
+ */
606
+static int ki_curl_connect_post_helper(sip_msg_t *_m, str *con, str *url,
607
+		str *ctype, str *data, pv_spec_t *dst)
608
+{
609
+	str result = {NULL, 0};
610
+	pv_value_t val;
611
+	int ret = 0;
612
+
613
+	ret = curl_con_query_url(_m, con, url, &result, ctype->s, data);
559 614
 
560 615
 	val.rs = result;
561 616
 	val.flags = PV_VAL_STR;
562
-	dst = (pv_spec_t *)_result;
563
-	dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
617
+	if(dst->setf) {
618
+		dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
619
+	} else {
620
+		LM_WARN("target pv is not writtable\n");
621
+	}
564 622
 
565 623
 	if(result.s != NULL)
566 624
 		pkg_free(result.s);
... ...
@@ -568,6 +626,27 @@ static int w_curl_connect(
568 626
 	return (ret == 0) ? -1 : ret;
569 627
 }
570 628
 
629
+/*
630
+ * Kemi wrapper for Curl_connect (POST)
631
+ */
632
+static int ki_curl_connect_post(sip_msg_t *_m, str *con, str *url,
633
+		str *ctype, str *data, str *dpv)
634
+{
635
+	pv_spec_t *dst;
636
+
637
+	dst = pv_cache_get(dpv);
638
+	if(dpv==NULL) {
639
+		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
640
+		return -1;
641
+	}
642
+	if(dst->setf==NULL) {
643
+		LM_ERR("target pv is not writable: %.*s\n", dpv->len, dpv->s);
644
+		return -1;
645
+	}
646
+
647
+	return ki_curl_connect_post_helper(_m, con, url, ctype, data, dst);
648
+}
649
+
571 650
 /*
572 651
  * Wrapper for Curl_connect (POST)
573 652
  */
... ...
@@ -576,13 +655,12 @@ static int w_curl_connect_post(struct sip_msg *_m, char *_con, char *_url,
576 655
 {
577 656
 	str con = {NULL, 0};
578 657
 	str url = {NULL, 0};
658
+	str ctype = {NULL, 0};
579 659
 	str data = {NULL, 0};
580
-	str result = {NULL, 0};
581 660
 	pv_spec_t *dst;
582
-	pv_value_t val;
583
-	int ret = 0;
584 661
 
585
-	if(_con == NULL || _url == NULL || _data == NULL || _result == NULL) {
662
+	if(_con == NULL || _url == NULL || _ctype==NULL || _data == NULL
663
+			|| _result == NULL) {
586 664
 		LM_ERR("http_connect: Invalid parameters\n");
587 665
 		return -1;
588 666
 	}
... ...
@@ -593,6 +671,10 @@ static int w_curl_connect_post(struct sip_msg *_m, char *_con, char *_url,
593 671
 		LM_ERR("http_connect: URL has no value\n");
594 672
 		return -1;
595 673
 	}
674
+
675
+	ctype.s = _ctype;
676
+	ctype.len = strlen(ctype.s);
677
+
596 678
 	if(get_str_fparam(&data, _m, (gparam_p)_data) != 0) {
597 679
 		LM_ERR("http_connect: No post data given\n");
598 680
 		return -1;
... ...
@@ -600,21 +682,11 @@ static int w_curl_connect_post(struct sip_msg *_m, char *_con, char *_url,
600 682
 
601 683
 	LM_DBG("**** HTTP_CONNECT: Connection %s URL %s Result var %s\n", _con,
602 684
 			_url, _result);
603
-
604
-	ret = curl_con_query_url(_m, &con, &url, &result, _ctype, &data);
605
-
606
-	val.rs = result;
607
-	val.flags = PV_VAL_STR;
608 685
 	dst = (pv_spec_t *)_result;
609
-	dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
610 686
 
611
-	if(result.s != NULL)
612
-		pkg_free(result.s);
613
-
614
-	return (ret == 0) ? -1 : ret;
687
+	return ki_curl_connect_post_helper(_m, &con, &url, &ctype, &data, dst);
615 688
 }
616 689
 
617
-
618 690
 /*!
619 691
  * Fix http_query params: url (string that may contain pvars) and
620 692
  * result (writable pvar).
... ...
@@ -702,18 +774,74 @@ static int fixup_free_http_query_post_hdr(void **param, int param_no)
702 774
 }
703 775
 
704 776
 /*!
705
- * Wrapper for HTTP-Query function for cfg script
777
+ * helper for HTTP-Query function
706 778
  */
707
-static int w_http_query_script(
708
-		sip_msg_t *_m, char *_url, char *_post, char *_hdrs, char *_result)
779
+static int ki_http_query_helper(sip_msg_t *_m, str *url, str *post, str *hdrs,
780
+		pv_spec_t *dst)
709 781
 {
710 782
 	int ret = 0;
783
+	str result = {NULL, 0};
784
+	pv_value_t val;
785
+
786
+	if(url==NULL || url->s==NULL) {
787
+		LM_ERR("invalid url parameter\n");
788
+		return -1;
789
+	}
790
+	ret = http_client_query(_m, url->s, &result, (post && post->s)?post->s:NULL,
791
+			(hdrs && hdrs->s)?hdrs->s:NULL);
792
+
793
+	val.rs = result;
794
+	val.flags = PV_VAL_STR;
795
+	if(dst->setf) {
796
+		dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
797
+	} else {
798
+		LM_WARN("target pv is not writable\n");
799
+	}
800
+
801
+	if(result.s != NULL)
802
+		pkg_free(result.s);
803
+
804
+	return (ret == 0) ? -1 : ret;
805
+}
806
+
807
+static int ki_http_query_post_hdrs(sip_msg_t *_m, str *url, str *post, str *hdrs,
808
+		str *dpv)
809
+{
810
+	pv_spec_t *dst;
811
+
812
+	dst = pv_cache_get(dpv);
813
+	if(dpv==NULL) {
814
+		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
815
+		return -1;
816
+	}
817
+	if(dst->setf==NULL) {
818
+		LM_ERR("target pv is not writable: %.*s\n", dpv->len, dpv->s);
819
+		return -1;
820
+	}
821
+
822
+	return ki_http_query_helper(_m, url, post, hdrs, dst);
823
+}
824
+
825
+static int ki_http_query_post(sip_msg_t *_m, str *url, str *post, str *dpv)
826
+{
827
+	return ki_http_query_post_hdrs(_m, url, post, NULL, dpv);
828
+}
829
+
830
+static int ki_http_query(sip_msg_t *_m, str *url, str *dpv)
831
+{
832
+	return ki_http_query_post_hdrs(_m, url, NULL, NULL, dpv);
833
+}
834
+
835
+/*!
836
+ * Wrapper for HTTP-Query function for cfg script
837
+ */
838
+static int w_http_query_script(sip_msg_t *_m, char *_url, char *_post,
839
+		char *_hdrs, char *_result)
840
+{
711 841
 	str url = {NULL, 0};
712 842
 	str post = {NULL, 0};
713 843
 	str hdrs = {NULL, 0};
714
-	str result = {NULL, 0};
715 844
 	pv_spec_t *dst;
716
-	pv_value_t val;
717 845
 
718 846
 	if(get_str_fparam(&url, _m, (gparam_p)_url) != 0 || url.len <= 0) {
719 847
 		LM_ERR("URL has no value\n");
... ...
@@ -735,18 +863,9 @@ static int w_http_query_script(
735 863
 			hdrs.s = NULL;
736 864
 		}
737 865
 	}
738
-
739
-	ret = http_client_query(_m, url.s, &result, post.s, hdrs.s);
740
-
741
-	val.rs = result;
742
-	val.flags = PV_VAL_STR;
743 866
 	dst = (pv_spec_t *)_result;
744
-	dst->setf(_m, &dst->pvp, (int)EQ_T, &val);
745
-
746
-	if(result.s != NULL)
747
-		pkg_free(result.s);
748 867
 
749
-	return (ret == 0) ? -1 : ret;
868
+	return ki_http_query_helper(_m, &url, &post, &hdrs, dst);
750 869
 }
751 870
 
752 871
 /*!
... ...
@@ -900,4 +1019,45 @@ static int w_curl_get_redirect(struct sip_msg *_m, char *_con, char *_result)
900 1019
 		pkg_free(result.s);
901 1020
 
902 1021
 	return ret;
1022
+}
1023
+
1024
+/**
1025
+ *
1026
+ */
1027
+/* clang-format off */
1028
+static sr_kemi_t sr_kemi_http_client_exports[] = {
1029
+	{ str_init("http_client"), str_init("query"),
1030
+		SR_KEMIP_INT, ki_http_query,
1031
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
1032
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1033
+	},
1034
+	{ str_init("http_client"), str_init("query_post"),
1035
+		SR_KEMIP_INT, ki_http_query_post,
1036
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
1037
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1038
+	},
1039
+	{ str_init("http_client"), str_init("query_post_hdrs"),
1040
+		SR_KEMIP_INT, ki_http_query_post_hdrs,
1041
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
1042
+			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
1043
+	},
1044
+	{ str_init("http_client"), str_init("curl_connect"),
1045
+		SR_KEMIP_INT, ki_curl_connect,
1046
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
1047
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1048
+	},
1049
+	{ str_init("http_client"), str_init("curl_connect_post"),
1050
+		SR_KEMIP_INT, ki_curl_connect_post,
1051
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
1052
+			SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE }
1053
+	},
1054
+
1055
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
1056
+};
1057
+/* clang-format on */
1058
+
1059
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
1060
+{
1061
+	sr_kemi_modules_add(sr_kemi_http_client_exports);
1062
+	return 0;
903 1063
 }
904 1064
\ No newline at end of file
Browse code

http_client: clang format the code

Daniel-Constantin Mierla authored on 05/12/2017 14:20:55
Showing 1 changed files
... ...
@@ -60,7 +60,7 @@
60 60
 #include "../../core/rpc_lookup.h"
61 61
 #include "../../core/config.h"
62 62
 #include "../../core/lvalue.h"
63
-#include "../../core/pt.h"		/* Process table */
63
+#include "../../core/pt.h" /* Process table */
64 64
 
65 65
 #include "functions.h"
66 66
 #include "curlcon.h"
... ...
@@ -69,27 +69,37 @@
69 69
 
70 70
 MODULE_VERSION
71 71
 
72
-#define CURL_USER_AGENT  NAME  " (" VERSION " (" ARCH "/" OS_QUOTED "))"
73
-#define CURL_USER_AGENT_LEN (sizeof(CURL_USER_AGENT)-1)
72
+#define CURL_USER_AGENT NAME " (" VERSION " (" ARCH "/" OS_QUOTED "))"
73
+#define CURL_USER_AGENT_LEN (sizeof(CURL_USER_AGENT) - 1)
74 74
 
75 75
 /* Module parameter variables */
76
-unsigned int	default_connection_timeout = 4;
77
-char		*default_tls_cacert = NULL;		/*!< File name: Default CA cert to use for curl TLS connection */
78
-str		default_tls_clientcert = STR_NULL;	/*!< File name: Default client certificate to use for curl TLS connection */
79
-str		default_tls_clientkey = STR_NULL;	/*!< File name: Key in PEM format that belongs to client cert */
80
-str		default_cipher_suite_list = STR_NULL;	/*!< List of allowed cipher suites */
81
-unsigned int	default_tls_version = 0;		/*!< 0 = Use libcurl default */
82
-unsigned int	default_tls_verify_peer = 1;		/*!< 0 = Do not verify TLS server cert. 1 = Verify TLS cert (default) */
83
-unsigned int	default_tls_verify_host = 2;		/*!< 0 = Do not verify TLS server CN/SAN  2 = Verify TLS server CN/SAN (default) */
84
-str 		default_http_proxy = STR_NULL;		/*!< Default HTTP proxy to use */
85
-unsigned int	default_http_proxy_port = 0;		/*!< Default HTTP proxy port to use */
86
-unsigned int	default_http_follow_redirect = 0;	/*!< Follow HTTP redirects CURLOPT_FOLLOWLOCATION */
87
-unsigned int	default_keep_connections = 0;		/*!< Keep http connections open for reuse */
88
-str 		default_useragent = { CURL_USER_AGENT, CURL_USER_AGENT_LEN };	/*!< Default CURL useragent. Default "Kamailio Curl " */
89
-unsigned int	default_maxdatasize = 0;		/*!< Default download size. 0=disabled */
90
-unsigned int 	default_authmethod = CURLAUTH_BASIC | CURLAUTH_DIGEST;		/*!< authentication method - Basic, Digest or both */
91
-
92
-str		http_client_config_file = STR_NULL;
76
+unsigned int default_connection_timeout = 4;
77
+char *default_tls_cacert =
78
+		NULL; /*!< File name: Default CA cert to use for curl TLS connection */
79
+str default_tls_clientcert =
80
+		STR_NULL; /*!< File name: Default client certificate to use for curl TLS connection */
81
+str default_tls_clientkey =
82
+		STR_NULL; /*!< File name: Key in PEM format that belongs to client cert */
83
+str default_cipher_suite_list = STR_NULL; /*!< List of allowed cipher suites */
84
+unsigned int default_tls_version = 0;	 /*!< 0 = Use libcurl default */
85
+unsigned int default_tls_verify_peer =
86
+		1; /*!< 0 = Do not verify TLS server cert. 1 = Verify TLS cert (default) */
87
+unsigned int default_tls_verify_host =
88
+		2;								  /*!< 0 = Do not verify TLS server CN/SAN  2 = Verify TLS server CN/SAN (default) */
89
+str default_http_proxy = STR_NULL;		  /*!< Default HTTP proxy to use */
90
+unsigned int default_http_proxy_port = 0; /*!< Default HTTP proxy port to use */
91
+unsigned int default_http_follow_redirect =
92
+		0; /*!< Follow HTTP redirects CURLOPT_FOLLOWLOCATION */
93
+unsigned int default_keep_connections =
94
+		0; /*!< Keep http connections open for reuse */
95
+str default_useragent = {CURL_USER_AGENT,
96
+		CURL_USER_AGENT_LEN}; /*!< Default CURL useragent. Default "Kamailio Curl " */
97
+unsigned int default_maxdatasize = 0; /*!< Default download size. 0=disabled */
98
+unsigned int default_authmethod =
99
+		CURLAUTH_BASIC
100
+		| CURLAUTH_DIGEST; /*!< authentication method - Basic, Digest or both */
101
+
102
+str http_client_config_file = STR_NULL;
93 103
 
94 104
 static curl_version_info_data *curl_info;
95 105
 
... ...
@@ -99,37 +109,40 @@ static int child_init(int);
99 109
 static void destroy(void);
100 110
 
101 111
 /* Fixup functions to be defined later */
102
-static int fixup_http_query_get(void** param, int param_no);
103
-static int fixup_free_http_query_get(void** param, int param_no);
104
-static int fixup_http_query_post(void** param, int param_no);
105
-static int fixup_free_http_query_post(void** param, int param_no);
106
-static int fixup_http_query_post_hdr(void** param, int param_no);
107
-static int fixup_free_http_query_post_hdr(void** param, int param_no);
108
-
109
-static int fixup_curl_connect(void** param, int param_no);
110
-static int fixup_free_curl_connect(void** param, int param_no);
111
-static int fixup_curl_connect_post(void** param, int param_no);
112
-static int fixup_free_curl_connect_post(void** param, int param_no);
113
-static int w_curl_connect_post(struct sip_msg* _m, char* _con, char * _url,
114
-		char* _result, char* _ctype, char* _data);
115
-
116
-static int fixup_curl_get_redirect(void** param, int param_no);
117
-static int fixup_free_curl_get_redirect(void** param, int param_no);
118
-static int w_curl_get_redirect(struct sip_msg* _m, char* _con, char* _result);
112
+static int fixup_http_query_get(void **param, int param_no);
113
+static int fixup_free_http_query_get(void **param, int param_no);
114
+static int fixup_http_query_post(void **param, int param_no);
115
+static int fixup_free_http_query_post(void **param, int param_no);
116
+static int fixup_http_query_post_hdr(void **param, int param_no);
117
+static int fixup_free_http_query_post_hdr(void **param, int param_no);
118
+
119
+static int fixup_curl_connect(void **param, int param_no);
120
+static int fixup_free_curl_connect(void **param, int param_no);
121
+static int fixup_curl_connect_post(void **param, int param_no);
122
+static int fixup_free_curl_connect_post(void **param, int param_no);
123
+static int w_curl_connect_post(struct sip_msg *_m, char *_con, char *_url,
124
+		char *_result, char *_ctype, char *_data);
125
+
126
+static int fixup_curl_get_redirect(void **param, int param_no);
127
+static int fixup_free_curl_get_redirect(void **param, int param_no);
128
+static int w_curl_get_redirect(struct sip_msg *_m, char *_con, char *_result);
119 129
 
120 130
 /* Wrappers for http_query to be defined later */
121
-static int w_http_query(struct sip_msg* _m, char* _url, char* _result);
122
-static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post,
123
-		char* _result);
124
-static int w_http_query_post_hdr(struct sip_msg* _m, char* _url, char* _post,
125
-		char* _hdrs, char* _result);
126
-static int w_curl_connect(struct sip_msg* _m, char* _con, char * _url, char* _result);
131
+static int w_http_query(struct sip_msg *_m, char *_url, char *_result);
132
+static int w_http_query_post(
133
+		struct sip_msg *_m, char *_url, char *_post, char *_result);
134
+static int w_http_query_post_hdr(struct sip_msg *_m, char *_url, char *_post,
135
+		char *_hdrs, char *_result);
136
+static int w_curl_connect(
137
+		struct sip_msg *_m, char *_con, char *_url, char *_result);
127 138
 
128 139
 /* forward function */
129
-static int curl_con_param(modparam_t type, void* val);
140
+static int curl_con_param(modparam_t type, void *val);
130 141
 static int pv_parse_curlerror(pv_spec_p sp, str *in);
131
-static int pv_get_curlerror(struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
142
+static int pv_get_curlerror(
143
+		struct sip_msg *msg, pv_param_t *param, pv_value_t *res);
132 144
 
145
+/* clang-format off */
133 146
 /* Exported functions */
134 147
 static cmd_export_t cmds[] = {
135 148
 	{"http_client_query", (cmd_function)w_http_query, 2, fixup_http_query_get,
... ...
@@ -183,7 +196,7 @@ static param_export_t params[] = {
183 196
  */
184 197
 static pv_export_t mod_pvs[] = {
185 198
 	{{"curlerror", (sizeof("curlerror")-1)}, /* Curl error codes */
186
-	PVT_OTHER, pv_get_curlerror, 0, pv_parse_curlerror, 0, 0, 0},
199
+		PVT_OTHER, pv_get_curlerror, 0, pv_parse_curlerror, 0, 0, 0},
187 200
 
188 201
 	{{0, 0}, 0, 0, 0, 0, 0, 0, 0}