Browse code

ruxc: added ruxc_http_delete(...) to do HTTP DELETE request

Daniel-Constantin Mierla authored on 26/08/2021 09:03:09
Showing 1 changed files
... ...
@@ -62,6 +62,9 @@ static int fixup_free_ruxc_http_post(void **param, int param_no);
62 62
 static int w_ruxc_http_post(struct sip_msg *_msg, char *_url,
63 63
 		char *_body, char *_hdrs, char *_result);
64 64
 
65
+static int w_ruxc_http_delete(struct sip_msg *_msg, char *_url,
66
+		char *_body, char *_hdrs, char *_result);
67
+
65 68
 typedef struct ruxc_data {
66 69
 	str value;
67 70
 	int ret;
... ...
@@ -73,6 +76,8 @@ static cmd_export_t cmds[]={
73 76
 		fixup_free_ruxc_http_get, ANY_ROUTE},
74 77
 	{"ruxc_http_post", (cmd_function)w_ruxc_http_post, 4, fixup_ruxc_http_post,
75 78
 		fixup_free_ruxc_http_post, ANY_ROUTE},
79
+	{"ruxc_http_delete", (cmd_function)w_ruxc_http_delete, 4, fixup_ruxc_http_post,
80
+		fixup_free_ruxc_http_post, ANY_ROUTE},
76 81
 
77 82
 	{0, 0, 0, 0, 0, 0}
78 83
 };
... ...
@@ -353,6 +358,113 @@ static int w_ruxc_http_post(struct sip_msg *_msg, char *_url,
353 358
 	return ki_ruxc_http_post_helper(_msg, &url, &body, &hdrs, dst);
354 359
 }
355 360
 
361
+/**
362
+ *
363
+ */
364
+static int ki_ruxc_http_delete_helper(sip_msg_t *_msg, str *url, str *body, str *hdrs,
365
+		pv_spec_t *dst)
366
+{
367
+	RuxcHTTPRequest v_http_request = {0};
368
+	RuxcHTTPResponse v_http_response = {0};
369
+	pv_value_t val = {0};
370
+	int ret;
371
+
372
+	ruxc_request_params_init(&v_http_request);
373
+
374
+	v_http_request.url = url->s;
375
+	v_http_request.url_len = url->len;
376
+
377
+	if(body!=NULL && body->s!=NULL && body->len>0) {
378
+		v_http_request.data = body->s;
379
+		v_http_request.data_len = body->len;
380
+	}
381
+
382
+	if(hdrs!=NULL && hdrs->s!=NULL && hdrs->len>0) {
383
+		v_http_request.headers = hdrs->s;
384
+		v_http_request.headers_len = hdrs->len;
385
+	}
386
+
387
+	ruxc_http_delete(&v_http_request, &v_http_response);
388
+
389
+	if(v_http_response.retcode < 0) {
390
+		LM_ERR("failed to perform http get - retcode: %d\n", v_http_response.retcode);
391
+		ret = v_http_response.retcode;
392
+	} else {
393
+		if(v_http_response.resdata != NULL &&  v_http_response.resdata_len>0) {
394
+			LM_DBG("response code: %d - data len: %d - data: [%.*s]\n",
395
+					v_http_response.rescode, v_http_response.resdata_len,
396
+					v_http_response.resdata_len, v_http_response.resdata);
397
+			val.rs.s = v_http_response.resdata;
398
+			val.rs.len = v_http_response.resdata_len;
399
+			val.flags = PV_VAL_STR;
400
+			if(dst->setf) {
401
+				dst->setf(_msg, &dst->pvp, (int)EQ_T, &val);
402
+			} else {
403
+				LM_WARN("target pv is not writable\n");
404
+			}
405
+		}
406
+		ret = v_http_response.rescode;
407
+	}
408
+	ruxc_http_response_release(&v_http_response);
409
+
410
+	return (ret!=0)?ret:-2;
411
+}
412
+
413
+/**
414
+ *
415
+ */
416
+static int ki_ruxc_http_delete(sip_msg_t *_msg, str *url, str *body, str *hdrs, str *dpv)
417
+{
418
+	pv_spec_t *dst;
419
+
420
+	dst = pv_cache_get(dpv);
421
+	if(dst==NULL) {
422
+		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
423
+		return -1;
424
+	}
425
+	if(dst->setf==NULL) {
426
+		LM_ERR("target pv is not writable: %.*s\n", dpv->len, dpv->s);
427
+		return -1;
428
+	}
429
+	return ki_ruxc_http_delete_helper(_msg, url, body, hdrs, dst);
430
+}
431
+
432
+/**
433
+ *
434
+ */
435
+static int w_ruxc_http_delete(struct sip_msg *_msg, char *_url,
436
+		char *_body, char *_hdrs, char *_result)
437
+{
438
+	str url = {NULL, 0};
439
+	str body = {NULL, 0};
440
+	str hdrs = {NULL, 0};
441
+	pv_spec_t *dst;
442
+
443
+	if(get_str_fparam(&url, _msg, (gparam_p)_url) != 0 || url.len <= 0) {
444
+		LM_ERR("URL has no value\n");
445
+		return -1;
446
+	}
447
+	if(_body && get_str_fparam(&body, _msg, (gparam_p)_body) != 0) {
448
+		LM_ERR("DATA body has no value\n");
449
+		return -1;
450
+	} else {
451
+		if(body.len == 0) {
452
+			body.s = NULL;
453
+		}
454
+	}
455
+	if(_hdrs && get_str_fparam(&hdrs, _msg, (gparam_p)_hdrs) != 0) {
456
+		LM_ERR("HDRS has no value\n");
457
+		return -1;
458
+	} else {
459
+		if(hdrs.len == 0) {
460
+			hdrs.s = NULL;
461
+		}
462
+	}
463
+	dst = (pv_spec_t *)_result;
464
+
465
+	return ki_ruxc_http_delete_helper(_msg, &url, &body, &hdrs, dst);
466
+}
467
+
356 468
 /**
357 469
  *
358 470
  */
... ...
@@ -452,6 +564,11 @@ static sr_kemi_t sr_kemi_ruxc_exports[] = {
452 564
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
453 565
 			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
454 566
 	},
567
+	{ str_init("ruxc"), str_init("http_delete"),
568
+		SR_KEMIP_INT, ki_ruxc_http_delete,
569
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
570
+			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
571
+	},
455 572
 
456 573
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
457 574
 };
Browse code

ruxc: added parameters for connection, read and write timeouts

Daniel-Constantin Mierla authored on 24/08/2021 07:45:45
Showing 1 changed files
... ...
@@ -39,6 +39,9 @@
39 39
 MODULE_VERSION
40 40
 
41 41
 static int _ruxc_http_timeout = 5000;
42
+static int _ruxc_http_timeout_connect = 5000;
43
+static int _ruxc_http_timeout_read = 5000;
44
+static int _ruxc_http_timeout_write = 5000;
42 45
 static int _ruxc_http_tlsmode = 0;
43 46
 static int _ruxc_http_reuse = 0;
44 47
 static int _ruxc_http_retry = 0;
... ...
@@ -75,12 +78,15 @@ static cmd_export_t cmds[]={
75 78
 };
76 79
 
77 80
 static param_export_t params[]={
78
-	{"http_timeout",       PARAM_INT,   &_ruxc_http_timeout},
79
-	{"http_tlsmode",       PARAM_INT,   &_ruxc_http_tlsmode},
80
-	{"http_reuse",         PARAM_INT,   &_ruxc_http_reuse},
81
-	{"http_retry",         PARAM_INT,   &_ruxc_http_retry},
82
-	{"http_logtype",       PARAM_INT,   &_ruxc_http_logtype},
83
-	{"http_debug",         PARAM_INT,   &_ruxc_http_debug},
81
+	{"http_timeout",         PARAM_INT,   &_ruxc_http_timeout},
82
+	{"http_timeout_connect", PARAM_INT,   &_ruxc_http_timeout_connect},
83
+	{"http_timeout_read",    PARAM_INT,   &_ruxc_http_timeout_read},
84
+	{"http_timeout_write",   PARAM_INT,   &_ruxc_http_timeout_write},
85
+	{"http_tlsmode",         PARAM_INT,   &_ruxc_http_tlsmode},
86
+	{"http_reuse",           PARAM_INT,   &_ruxc_http_reuse},
87
+	{"http_retry",           PARAM_INT,   &_ruxc_http_retry},
88
+	{"http_logtype",         PARAM_INT,   &_ruxc_http_logtype},
89
+	{"http_debug",           PARAM_INT,   &_ruxc_http_debug},
84 90
 
85 91
 	{0, 0, 0}
86 92
 };
... ...
@@ -136,10 +142,10 @@ static void mod_destroy(void)
136 142
  */
137 143
 static void ruxc_request_params_init(RuxcHTTPRequest *v_http_request)
138 144
 {
139
-    v_http_request->timeout = _ruxc_http_timeout;
140
-    v_http_request->timeout_connect = _ruxc_http_timeout;
141
-    v_http_request->timeout_read = _ruxc_http_timeout;
142
-    v_http_request->timeout_write = _ruxc_http_timeout;
145
+	v_http_request->timeout = _ruxc_http_timeout;
146
+	v_http_request->timeout_connect = _ruxc_http_timeout_connect;
147
+	v_http_request->timeout_read = _ruxc_http_timeout_read;
148
+	v_http_request->timeout_write = _ruxc_http_timeout_write;
143 149
 	v_http_request->tlsmode = _ruxc_http_tlsmode;
144 150
 	v_http_request->reuse = _ruxc_http_reuse;
145 151
 	v_http_request->retry = _ruxc_http_retry;
Browse code

ruxc: groupped setting request params in a function

Daniel-Constantin Mierla authored on 24/08/2021 07:36:06
Showing 1 changed files
... ...
@@ -131,6 +131,22 @@ static void mod_destroy(void)
131 131
 	return;
132 132
 }
133 133
 
134
+/**
135
+ *
136
+ */
137
+static void ruxc_request_params_init(RuxcHTTPRequest *v_http_request)
138
+{
139
+    v_http_request->timeout = _ruxc_http_timeout;
140
+    v_http_request->timeout_connect = _ruxc_http_timeout;
141
+    v_http_request->timeout_read = _ruxc_http_timeout;
142
+    v_http_request->timeout_write = _ruxc_http_timeout;
143
+	v_http_request->tlsmode = _ruxc_http_tlsmode;
144
+	v_http_request->reuse = _ruxc_http_reuse;
145
+	v_http_request->retry = _ruxc_http_retry;
146
+	v_http_request->logtype = _ruxc_http_logtype;
147
+	v_http_request->debug = _ruxc_http_debug;
148
+}
149
+
134 150
 /**
135 151
  *
136 152
  */
... ...
@@ -142,15 +158,7 @@ static int ki_ruxc_http_get_helper(sip_msg_t *_msg, str *url, str *hdrs,
142 158
 	pv_value_t val = {0};
143 159
 	int ret;
144 160
 
145
-    v_http_request.timeout = _ruxc_http_timeout;
146
-    v_http_request.timeout_connect = _ruxc_http_timeout;
147
-    v_http_request.timeout_read = _ruxc_http_timeout;
148
-    v_http_request.timeout_write = _ruxc_http_timeout;
149
-	v_http_request.tlsmode = _ruxc_http_tlsmode;
150
-	v_http_request.reuse = _ruxc_http_reuse;
151
-	v_http_request.retry = _ruxc_http_retry;
152
-	v_http_request.logtype = _ruxc_http_logtype;
153
-	v_http_request.debug = _ruxc_http_debug;
161
+	ruxc_request_params_init(&v_http_request);
154 162
 
155 163
 	v_http_request.url = url->s;
156 164
 	v_http_request.url_len = url->len;
... ...
@@ -243,15 +251,7 @@ static int ki_ruxc_http_post_helper(sip_msg_t *_msg, str *url, str *body, str *h
243 251
 	pv_value_t val = {0};
244 252
 	int ret;
245 253
 
246
-    v_http_request.timeout = _ruxc_http_timeout;
247
-    v_http_request.timeout_connect = _ruxc_http_timeout;
248
-    v_http_request.timeout_read = _ruxc_http_timeout;
249
-    v_http_request.timeout_write = _ruxc_http_timeout;
250
-	v_http_request.tlsmode = _ruxc_http_tlsmode;
251
-	v_http_request.reuse = _ruxc_http_reuse;
252
-	v_http_request.retry = _ruxc_http_retry;
253
-	v_http_request.logtype = _ruxc_http_logtype;
254
-	v_http_request.debug = _ruxc_http_debug;
254
+	ruxc_request_params_init(&v_http_request);
255 255
 
256 256
 	v_http_request.url = url->s;
257 257
 	v_http_request.url_len = url->len;
Browse code

ruxc: added modparams to set libruxc logtype and debug

Daniel-Constantin Mierla authored on 20/08/2021 06:01:25
Showing 1 changed files
... ...
@@ -42,6 +42,8 @@ static int _ruxc_http_timeout = 5000;
42 42
 static int _ruxc_http_tlsmode = 0;
43 43
 static int _ruxc_http_reuse = 0;
44 44
 static int _ruxc_http_retry = 0;
45
+static int _ruxc_http_logtype = 0;
46
+static int _ruxc_http_debug = 0;
45 47
 
46 48
 static int mod_init(void);
47 49
 static int child_init(int);
... ...
@@ -77,6 +79,8 @@ static param_export_t params[]={
77 79
 	{"http_tlsmode",       PARAM_INT,   &_ruxc_http_tlsmode},
78 80
 	{"http_reuse",         PARAM_INT,   &_ruxc_http_reuse},
79 81
 	{"http_retry",         PARAM_INT,   &_ruxc_http_retry},
82
+	{"http_logtype",       PARAM_INT,   &_ruxc_http_logtype},
83
+	{"http_debug",         PARAM_INT,   &_ruxc_http_debug},
80 84
 
81 85
 	{0, 0, 0}
82 86
 };
... ...
@@ -101,6 +105,13 @@ struct module_exports exports = {
101 105
  */
102 106
 static int mod_init(void)
103 107
 {
108
+	if(_ruxc_http_logtype==1 && log_stderr==1) {
109
+		LM_INFO("setting http logtype to 0\n");
110
+		_ruxc_http_logtype = 0;
111
+	} else if(_ruxc_http_logtype==0 && log_stderr==0) {
112
+		LM_INFO("setting http logtype to 1\n");
113
+		_ruxc_http_logtype = 1;
114
+	}
104 115
 	return 0;
105 116
 }
106 117
 
... ...
@@ -138,6 +149,8 @@ static int ki_ruxc_http_get_helper(sip_msg_t *_msg, str *url, str *hdrs,
138 149
 	v_http_request.tlsmode = _ruxc_http_tlsmode;
139 150
 	v_http_request.reuse = _ruxc_http_reuse;
140 151
 	v_http_request.retry = _ruxc_http_retry;
152
+	v_http_request.logtype = _ruxc_http_logtype;
153
+	v_http_request.debug = _ruxc_http_debug;
141 154
 
142 155
 	v_http_request.url = url->s;
143 156
 	v_http_request.url_len = url->len;
... ...
@@ -237,6 +250,8 @@ static int ki_ruxc_http_post_helper(sip_msg_t *_msg, str *url, str *body, str *h
237 250
 	v_http_request.tlsmode = _ruxc_http_tlsmode;
238 251
 	v_http_request.reuse = _ruxc_http_reuse;
239 252
 	v_http_request.retry = _ruxc_http_retry;
253
+	v_http_request.logtype = _ruxc_http_logtype;
254
+	v_http_request.debug = _ruxc_http_debug;
240 255
 
241 256
 	v_http_request.url = url->s;
242 257
 	v_http_request.url_len = url->len;
Browse code

ruxc: added http_retry modparam

Daniel-Constantin Mierla authored on 17/08/2021 19:27:36
Showing 1 changed files
... ...
@@ -41,6 +41,7 @@ MODULE_VERSION
41 41
 static int _ruxc_http_timeout = 5000;
42 42
 static int _ruxc_http_tlsmode = 0;
43 43
 static int _ruxc_http_reuse = 0;
44
+static int _ruxc_http_retry = 0;
44 45
 
45 46
 static int mod_init(void);
46 47
 static int child_init(int);
... ...
@@ -75,6 +76,7 @@ static param_export_t params[]={
75 76
 	{"http_timeout",       PARAM_INT,   &_ruxc_http_timeout},
76 77
 	{"http_tlsmode",       PARAM_INT,   &_ruxc_http_tlsmode},
77 78
 	{"http_reuse",         PARAM_INT,   &_ruxc_http_reuse},
79
+	{"http_retry",         PARAM_INT,   &_ruxc_http_retry},
78 80
 
79 81
 	{0, 0, 0}
80 82
 };
... ...
@@ -135,6 +137,7 @@ static int ki_ruxc_http_get_helper(sip_msg_t *_msg, str *url, str *hdrs,
135 137
     v_http_request.timeout_write = _ruxc_http_timeout;
136 138
 	v_http_request.tlsmode = _ruxc_http_tlsmode;
137 139
 	v_http_request.reuse = _ruxc_http_reuse;
140
+	v_http_request.retry = _ruxc_http_retry;
138 141
 
139 142
 	v_http_request.url = url->s;
140 143
 	v_http_request.url_len = url->len;
... ...
@@ -233,6 +236,7 @@ static int ki_ruxc_http_post_helper(sip_msg_t *_msg, str *url, str *body, str *h
233 236
     v_http_request.timeout_write = _ruxc_http_timeout;
234 237
 	v_http_request.tlsmode = _ruxc_http_tlsmode;
235 238
 	v_http_request.reuse = _ruxc_http_reuse;
239
+	v_http_request.retry = _ruxc_http_retry;
236 240
 
237 241
 	v_http_request.url = url->s;
238 242
 	v_http_request.url_len = url->len;
Browse code

ruxc: added http_reuse modparam

- enable connection reuse

Daniel-Constantin Mierla authored on 24/07/2021 06:16:49
Showing 1 changed files
... ...
@@ -40,6 +40,7 @@ MODULE_VERSION
40 40
 
41 41
 static int _ruxc_http_timeout = 5000;
42 42
 static int _ruxc_http_tlsmode = 0;
43
+static int _ruxc_http_reuse = 0;
43 44
 
44 45
 static int mod_init(void);
45 46
 static int child_init(int);
... ...
@@ -73,6 +74,7 @@ static cmd_export_t cmds[]={
73 74
 static param_export_t params[]={
74 75
 	{"http_timeout",       PARAM_INT,   &_ruxc_http_timeout},
75 76
 	{"http_tlsmode",       PARAM_INT,   &_ruxc_http_tlsmode},
77
+	{"http_reuse",         PARAM_INT,   &_ruxc_http_reuse},
76 78
 
77 79
 	{0, 0, 0}
78 80
 };
... ...
@@ -132,6 +134,7 @@ static int ki_ruxc_http_get_helper(sip_msg_t *_msg, str *url, str *hdrs,
132 134
     v_http_request.timeout_read = _ruxc_http_timeout;
133 135
     v_http_request.timeout_write = _ruxc_http_timeout;
134 136
 	v_http_request.tlsmode = _ruxc_http_tlsmode;
137
+	v_http_request.reuse = _ruxc_http_reuse;
135 138
 
136 139
 	v_http_request.url = url->s;
137 140
 	v_http_request.url_len = url->len;
... ...
@@ -229,6 +232,7 @@ static int ki_ruxc_http_post_helper(sip_msg_t *_msg, str *url, str *body, str *h
229 232
     v_http_request.timeout_read = _ruxc_http_timeout;
230 233
     v_http_request.timeout_write = _ruxc_http_timeout;
231 234
 	v_http_request.tlsmode = _ruxc_http_tlsmode;
235
+	v_http_request.reuse = _ruxc_http_reuse;
232 236
 
233 237
 	v_http_request.url = url->s;
234 238
 	v_http_request.url_len = url->len;
Browse code

ruxc: added http_tlsmode parameter

- if set to 1, it connects only to trusted https websites
- default 0 - accept all certificates

Daniel-Constantin Mierla authored on 21/07/2021 14:22:42
Showing 1 changed files
... ...
@@ -39,6 +39,7 @@
39 39
 MODULE_VERSION
40 40
 
41 41
 static int _ruxc_http_timeout = 5000;
42
+static int _ruxc_http_tlsmode = 0;
42 43
 
43 44
 static int mod_init(void);
44 45
 static int child_init(int);
... ...
@@ -71,6 +72,7 @@ static cmd_export_t cmds[]={
71 72
 
72 73
 static param_export_t params[]={
73 74
 	{"http_timeout",       PARAM_INT,   &_ruxc_http_timeout},
75
+	{"http_tlsmode",       PARAM_INT,   &_ruxc_http_tlsmode},
74 76
 
75 77
 	{0, 0, 0}
76 78
 };
... ...
@@ -129,6 +131,7 @@ static int ki_ruxc_http_get_helper(sip_msg_t *_msg, str *url, str *hdrs,
129 131
     v_http_request.timeout_connect = _ruxc_http_timeout;
130 132
     v_http_request.timeout_read = _ruxc_http_timeout;
131 133
     v_http_request.timeout_write = _ruxc_http_timeout;
134
+	v_http_request.tlsmode = _ruxc_http_tlsmode;
132 135
 
133 136
 	v_http_request.url = url->s;
134 137
 	v_http_request.url_len = url->len;
... ...
@@ -225,6 +228,7 @@ static int ki_ruxc_http_post_helper(sip_msg_t *_msg, str *url, str *body, str *h
225 228
     v_http_request.timeout_connect = _ruxc_http_timeout;
226 229
     v_http_request.timeout_read = _ruxc_http_timeout;
227 230
     v_http_request.timeout_write = _ruxc_http_timeout;
231
+	v_http_request.tlsmode = _ruxc_http_tlsmode;
228 232
 
229 233
 	v_http_request.url = url->s;
230 234
 	v_http_request.url_len = url->len;
Browse code

ruxc: set response variable

Daniel-Constantin Mierla authored on 19/07/2021 08:19:14
Showing 1 changed files
... ...
@@ -122,6 +122,7 @@ static int ki_ruxc_http_get_helper(sip_msg_t *_msg, str *url, str *hdrs,
122 122
 {
123 123
 	RuxcHTTPRequest v_http_request = {0};
124 124
 	RuxcHTTPResponse v_http_response = {0};
125
+	pv_value_t val = {0};
125 126
 	int ret;
126 127
 
127 128
     v_http_request.timeout = _ruxc_http_timeout;
... ...
@@ -147,6 +148,14 @@ static int ki_ruxc_http_get_helper(sip_msg_t *_msg, str *url, str *hdrs,
147 148
 			LM_DBG("response code: %d - data len: %d - data: [%.*s]\n",
148 149
 					v_http_response.rescode, v_http_response.resdata_len,
149 150
 					v_http_response.resdata_len, v_http_response.resdata);
151
+			val.rs.s = v_http_response.resdata;
152
+			val.rs.len = v_http_response.resdata_len;
153
+			val.flags = PV_VAL_STR;
154
+			if(dst->setf) {
155
+				dst->setf(_msg, &dst->pvp, (int)EQ_T, &val);
156
+			} else {
157
+				LM_WARN("target pv is not writable\n");
158
+			}
150 159
 		}
151 160
 		ret = v_http_response.rescode;
152 161
 	}
... ...
@@ -209,6 +218,7 @@ static int ki_ruxc_http_post_helper(sip_msg_t *_msg, str *url, str *body, str *h
209 218
 {
210 219
 	RuxcHTTPRequest v_http_request = {0};
211 220
 	RuxcHTTPResponse v_http_response = {0};
221
+	pv_value_t val = {0};
212 222
 	int ret;
213 223
 
214 224
     v_http_request.timeout = _ruxc_http_timeout;
... ...
@@ -239,6 +249,14 @@ static int ki_ruxc_http_post_helper(sip_msg_t *_msg, str *url, str *body, str *h
239 249
 			LM_DBG("response code: %d - data len: %d - data: [%.*s]\n",
240 250
 					v_http_response.rescode, v_http_response.resdata_len,
241 251
 					v_http_response.resdata_len, v_http_response.resdata);
252
+			val.rs.s = v_http_response.resdata;
253
+			val.rs.len = v_http_response.resdata_len;
254
+			val.flags = PV_VAL_STR;
255
+			if(dst->setf) {
256
+				dst->setf(_msg, &dst->pvp, (int)EQ_T, &val);
257
+			} else {
258
+				LM_WARN("target pv is not writable\n");
259
+			}
242 260
 		}
243 261
 		ret = v_http_response.rescode;
244 262
 	}
Browse code

ruxc: new module with utility functions from libruxc

- first target is to provide http get/post functions that do not depend
on libcurl+libssl

Daniel-Constantin Mierla authored on 19/07/2021 06:32:48
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,416 @@
1
+/**
2
+ * Copyright (C) 2021 Daniel-Constantin Mierla (asipto.com)
3
+ *
4
+ * This file is part of Kamailio, a free SIP server.
5
+ *
6
+ * This file is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version
10
+ *
11
+ *
12
+ * This file is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with this program; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
+ *
21
+ */
22
+
23
+#include <stdio.h>
24
+#include <unistd.h>
25
+#include <stdlib.h>
26
+#include <string.h>
27
+
28
+#include <ruxc.h>
29
+
30
+#include "../../core/sr_module.h"
31
+#include "../../core/dprint.h"
32
+#include "../../core/mod_fix.h"
33
+#include "../../core/data_lump.h"
34
+#include "../../core/str_list.h"
35
+#include "../../core/lvalue.h"
36
+#include "../../core/kemi.h"
37
+
38
+
39
+MODULE_VERSION
40
+
41
+static int _ruxc_http_timeout = 5000;
42
+
43
+static int mod_init(void);
44
+static int child_init(int);
45
+static void mod_destroy(void);
46
+
47
+static int fixup_ruxc_http_get(void **param, int param_no);
48
+static int fixup_free_ruxc_http_get(void **param, int param_no);
49
+static int w_ruxc_http_get(struct sip_msg *_msg, char *_url,
50
+		char *_hdrs, char *_result);
51
+
52
+static int fixup_ruxc_http_post(void **param, int param_no);
53
+static int fixup_free_ruxc_http_post(void **param, int param_no);
54
+static int w_ruxc_http_post(struct sip_msg *_msg, char *_url,
55
+		char *_body, char *_hdrs, char *_result);
56
+
57
+typedef struct ruxc_data {
58
+	str value;
59
+	int ret;
60
+} ruxc_data_t;
61
+
62
+/* clang-format off */
63
+static cmd_export_t cmds[]={
64
+	{"ruxc_http_get", (cmd_function)w_ruxc_http_get, 3, fixup_ruxc_http_get,
65
+		fixup_free_ruxc_http_get, ANY_ROUTE},
66
+	{"ruxc_http_post", (cmd_function)w_ruxc_http_post, 4, fixup_ruxc_http_post,
67
+		fixup_free_ruxc_http_post, ANY_ROUTE},
68
+
69
+	{0, 0, 0, 0, 0, 0}
70
+};
71
+
72
+static param_export_t params[]={
73
+	{"http_timeout",       PARAM_INT,   &_ruxc_http_timeout},
74
+
75
+	{0, 0, 0}
76
+};
77
+
78
+struct module_exports exports = {
79
+	"ruxc",
80
+	DEFAULT_DLFLAGS, /* dlopen flags */
81
+	cmds,
82
+	params,
83
+	0,              /* exported RPC methods */
84
+	0,              /* exported pseudo-variables */
85
+	0,              /* response function */
86
+	mod_init,       /* module initialization function */
87
+	child_init,     /* per child init function */
88
+	mod_destroy    	/* destroy function */
89
+};
90
+/* clang-format on */
91
+
92
+
93
+/**
94
+ * init module function
95
+ */
96
+static int mod_init(void)
97
+{
98
+	return 0;
99
+}
100
+
101
+/**
102
+ * @brief Initialize async module children
103
+ */
104
+static int child_init(int rank)
105
+{
106
+	return 0;
107
+}
108
+
109
+/**
110
+ * destroy module function
111
+ */
112
+static void mod_destroy(void)
113
+{
114
+	return;
115
+}
116
+
117
+/**
118
+ *
119
+ */
120
+static int ki_ruxc_http_get_helper(sip_msg_t *_msg, str *url, str *hdrs,
121
+		pv_spec_t *dst)
122
+{
123
+	RuxcHTTPRequest v_http_request = {0};
124
+	RuxcHTTPResponse v_http_response = {0};
125
+	int ret;
126
+
127
+    v_http_request.timeout = _ruxc_http_timeout;
128
+    v_http_request.timeout_connect = _ruxc_http_timeout;
129
+    v_http_request.timeout_read = _ruxc_http_timeout;
130
+    v_http_request.timeout_write = _ruxc_http_timeout;
131
+
132
+	v_http_request.url = url->s;
133
+	v_http_request.url_len = url->len;
134
+
135
+	if(hdrs!=NULL && hdrs->s!=NULL && hdrs->len>0) {
136
+		v_http_request.headers = hdrs->s;
137
+		v_http_request.headers_len = hdrs->len;
138
+	}
139
+
140
+	ruxc_http_get(&v_http_request, &v_http_response);
141
+
142
+	if(v_http_response.retcode < 0) {
143
+		LM_ERR("failed to perform http get - retcode: %d\n", v_http_response.retcode);
144
+		ret = v_http_response.retcode;
145
+	} else {
146
+		if(v_http_response.resdata != NULL &&  v_http_response.resdata_len>0) {
147
+			LM_DBG("response code: %d - data len: %d - data: [%.*s]\n",
148
+					v_http_response.rescode, v_http_response.resdata_len,
149
+					v_http_response.resdata_len, v_http_response.resdata);
150
+		}
151
+		ret = v_http_response.rescode;
152
+	}
153
+	ruxc_http_response_release(&v_http_response);
154
+
155
+	return (ret!=0)?ret:-2;
156
+}
157
+
158
+/**
159
+ *
160
+ */
161
+static int ki_ruxc_http_get(sip_msg_t *_msg, str *url, str *hdrs, str *dpv)
162
+{
163
+	pv_spec_t *dst;
164
+
165
+	dst = pv_cache_get(dpv);
166
+	if(dst==NULL) {
167
+		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
168
+		return -1;
169
+	}
170
+	if(dst->setf==NULL) {
171
+		LM_ERR("target pv is not writable: %.*s\n", dpv->len, dpv->s);
172
+		return -1;
173
+	}
174
+	return ki_ruxc_http_get_helper(_msg, url, hdrs, dst);
175
+}
176
+
177
+/**
178
+ *
179
+ */
180
+static int w_ruxc_http_get(struct sip_msg *_msg, char *_url,
181
+		char *_hdrs, char *_result)
182
+{
183
+	str url = {NULL, 0};
184
+	str hdrs = {NULL, 0};
185
+	pv_spec_t *dst;
186
+
187
+	if(get_str_fparam(&url, _msg, (gparam_p)_url) != 0 || url.len <= 0) {
188
+		LM_ERR("URL has no value\n");
189
+		return -1;
190
+	}
191
+	if(_hdrs && get_str_fparam(&hdrs, _msg, (gparam_p)_hdrs) != 0) {
192
+		LM_ERR("HDRS has no value\n");
193
+		return -1;
194
+	} else {
195
+		if(hdrs.len == 0) {
196
+			hdrs.s = NULL;
197
+		}
198
+	}
199
+	dst = (pv_spec_t *)_result;
200
+
201
+	return ki_ruxc_http_get_helper(_msg, &url, &hdrs, dst);
202
+}
203
+
204
+/**
205
+ *
206
+ */
207
+static int ki_ruxc_http_post_helper(sip_msg_t *_msg, str *url, str *body, str *hdrs,
208
+		pv_spec_t *dst)
209
+{
210
+	RuxcHTTPRequest v_http_request = {0};
211
+	RuxcHTTPResponse v_http_response = {0};
212
+	int ret;
213
+
214
+    v_http_request.timeout = _ruxc_http_timeout;
215
+    v_http_request.timeout_connect = _ruxc_http_timeout;
216
+    v_http_request.timeout_read = _ruxc_http_timeout;
217
+    v_http_request.timeout_write = _ruxc_http_timeout;
218
+
219
+	v_http_request.url = url->s;
220
+	v_http_request.url_len = url->len;
221
+
222
+	if(body!=NULL && body->s!=NULL && body->len>0) {
223
+		v_http_request.data = body->s;
224
+		v_http_request.data_len = body->len;
225
+	}
226
+
227
+	if(hdrs!=NULL && hdrs->s!=NULL && hdrs->len>0) {
228
+		v_http_request.headers = hdrs->s;
229
+		v_http_request.headers_len = hdrs->len;
230
+	}
231
+
232
+	ruxc_http_post(&v_http_request, &v_http_response);
233
+
234
+	if(v_http_response.retcode < 0) {
235
+		LM_ERR("failed to perform http get - retcode: %d\n", v_http_response.retcode);
236
+		ret = v_http_response.retcode;
237
+	} else {
238
+		if(v_http_response.resdata != NULL &&  v_http_response.resdata_len>0) {
239
+			LM_DBG("response code: %d - data len: %d - data: [%.*s]\n",
240
+					v_http_response.rescode, v_http_response.resdata_len,
241
+					v_http_response.resdata_len, v_http_response.resdata);
242
+		}
243
+		ret = v_http_response.rescode;
244
+	}
245
+	ruxc_http_response_release(&v_http_response);
246
+
247
+	return (ret!=0)?ret:-2;
248
+}
249
+
250
+/**
251
+ *
252
+ */
253
+static int ki_ruxc_http_post(sip_msg_t *_msg, str *url, str *body, str *hdrs, str *dpv)
254
+{
255
+	pv_spec_t *dst;
256
+
257
+	dst = pv_cache_get(dpv);
258
+	if(dst==NULL) {
259
+		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
260
+		return -1;
261
+	}
262
+	if(dst->setf==NULL) {
263
+		LM_ERR("target pv is not writable: %.*s\n", dpv->len, dpv->s);
264
+		return -1;
265
+	}
266
+	return ki_ruxc_http_post_helper(_msg, url, body, hdrs, dst);
267
+}
268
+
269
+/**
270
+ *
271
+ */
272
+static int w_ruxc_http_post(struct sip_msg *_msg, char *_url,
273
+		char *_body, char *_hdrs, char *_result)
274
+{
275
+	str url = {NULL, 0};
276
+	str body = {NULL, 0};
277
+	str hdrs = {NULL, 0};
278
+	pv_spec_t *dst;
279
+
280
+	if(get_str_fparam(&url, _msg, (gparam_p)_url) != 0 || url.len <= 0) {
281
+		LM_ERR("URL has no value\n");
282
+		return -1;
283
+	}
284
+	if(_body && get_str_fparam(&body, _msg, (gparam_p)_body) != 0) {
285
+		LM_ERR("DATA body has no value\n");
286
+		return -1;
287
+	} else {
288
+		if(body.len == 0) {
289
+			body.s = NULL;
290
+		}
291
+	}
292
+	if(_hdrs && get_str_fparam(&hdrs, _msg, (gparam_p)_hdrs) != 0) {
293
+		LM_ERR("HDRS has no value\n");
294
+		return -1;
295
+	} else {
296
+		if(hdrs.len == 0) {
297
+			hdrs.s = NULL;
298
+		}
299
+	}
300
+	dst = (pv_spec_t *)_result;
301
+
302
+	return ki_ruxc_http_post_helper(_msg, &url, &body, &hdrs, dst);
303
+}
304
+
305
+/**
306
+ *
307
+ */
308
+static int fixup_ruxc_http_get(void **param, int param_no)
309
+{
310
+	if((param_no >= 1) && (param_no <= 2)) {
311
+		return fixup_spve_null(param, 1);
312
+	}
313
+
314
+	if(param_no == 3) {
315
+		if(fixup_pvar_null(param, 1) != 0) {
316
+			LM_ERR("failed to fixup result pvar\n");
317
+			return -1;
318
+		}
319
+		if(((pv_spec_t *)(*param))->setf == NULL) {
320
+			LM_ERR("result pvar is not writeble\n");
321
+			return -1;
322
+		}
323
+		return 0;
324
+	}
325
+
326
+	LM_ERR("invalid parameter number <%d>\n", param_no);
327
+	return -1;
328
+}
329
+
330
+/**
331
+ *
332
+ */
333
+static int fixup_free_ruxc_http_get(void **param, int param_no)
334
+{
335
+	if((param_no >= 1) && (param_no <= 2)) {
336
+		return fixup_free_spve_null(param, 1);
337
+	}
338
+
339
+	if(param_no == 3) {
340
+		return fixup_free_pvar_null(param, 1);
341
+	}
342
+
343
+	LM_ERR("invalid parameter number <%d>\n", param_no);
344
+	return -1;
345
+}
346
+
347
+/**
348
+ *
349
+ */
350
+static int fixup_ruxc_http_post(void **param, int param_no)
351
+{
352
+	if((param_no >= 1) && (param_no <= 3)) {
353
+		return fixup_spve_null(param, 1);
354
+	}
355
+
356
+	if(param_no == 4) {
357
+		if(fixup_pvar_null(param, 1) != 0) {
358
+			LM_ERR("failed to fixup result pvar\n");
359
+			return -1;
360
+		}
361
+		if(((pv_spec_t *)(*param))->setf == NULL) {
362
+			LM_ERR("result pvar is not writeble\n");
363
+			return -1;
364
+		}
365
+		return 0;
366
+	}
367
+
368
+	LM_ERR("invalid parameter number <%d>\n", param_no);
369
+	return -1;
370
+}
371
+
372
+/**
373
+ *
374
+ */
375
+static int fixup_free_ruxc_http_post(void **param, int param_no)
376
+{
377
+	if((param_no >= 1) && (param_no <= 3)) {
378
+		return fixup_free_spve_null(param, 1);
379
+	}
380
+
381
+	if(param_no == 4) {
382
+		return fixup_free_pvar_null(param, 1);
383
+	}
384
+
385
+	LM_ERR("invalid parameter number <%d>\n", param_no);
386
+	return -1;
387
+}
388
+
389
+/**
390
+ *
391
+ */
392
+/* clang-format off */
393
+static sr_kemi_t sr_kemi_ruxc_exports[] = {
394
+	{ str_init("ruxc"), str_init("http_get"),
395
+		SR_KEMIP_INT, ki_ruxc_http_get,
396
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
397
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
398
+	},
399
+	{ str_init("ruxc"), str_init("http_post"),
400
+		SR_KEMIP_INT, ki_ruxc_http_post,
401
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
402
+			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
403
+	},
404
+
405
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
406
+};
407
+/* clang-format on */
408
+
409
+/**
410
+ *
411
+ */
412
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
413
+{
414
+	sr_kemi_modules_add(sr_kemi_ruxc_exports);
415
+	return 0;
416
+}