Răsfoiți Sursa

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

Daniel-Constantin Mierla 4 ani în urmă
părinte
comite
ec9eb48dfd
2 a modificat fișierele cu 148 adăugiri și 0 ștergeri
  1. 31 0
      src/modules/ruxc/doc/ruxc_admin.xml
  2. 117 0
      src/modules/ruxc/ruxc_mod.c

+ 31 - 0
src/modules/ruxc/doc/ruxc_admin.xml

@@ -322,6 +322,37 @@ ruxc_http_post("http://api.com/index.php?r_uri=$(ru{s.escape.param})&f_uri=$
 switch ($rc) {
     ...
 }
+...
+				</programlisting>
+			</example>
+		</section>
+		<section id="ruxc.f.ruxc_http_delete">
+			<title>
+				<function moreinfo="none">ruxc_http_delete(url, body, hdrs, respv)</function>
+			</title>
+			<para>
+				Perform a HTTP DELETE request to "url", storing the response body
+				in the "respv" variable. The "body" and "hdrs" can be empty strings
+				to skip setting them. The first three parameters can contain
+				variables that are evaluated at runtime. The "respv" has to be
+				the name of a writable variable.
+			</para>
+			<para>
+			The function returns response code of HTTP reply or negative value
+			if something went wrong.
+			</para>
+			<para>
+			This function can be used from ANY_ROUTE.
+			</para>
+			<example>
+				<title><function>ruxc_http_delete()</function> usage</title>
+				<programlisting format="linespecific">
+...
+ruxc_http_delete("http://api.com/index.php?r_uri=$(ru{s.escape.param})&amp;f_uri=$(fu{s.escape.param})",
+	   "", "X-Token: abc", "$var(result)");
+switch ($rc) {
+    ...
+}
 ...
 				</programlisting>
 			</example>

+ 117 - 0
src/modules/ruxc/ruxc_mod.c

@@ -62,6 +62,9 @@ static int fixup_free_ruxc_http_post(void **param, int param_no);
 static int w_ruxc_http_post(struct sip_msg *_msg, char *_url,
 		char *_body, char *_hdrs, char *_result);
 
+static int w_ruxc_http_delete(struct sip_msg *_msg, char *_url,
+		char *_body, char *_hdrs, char *_result);
+
 typedef struct ruxc_data {
 	str value;
 	int ret;
@@ -73,6 +76,8 @@ static cmd_export_t cmds[]={
 		fixup_free_ruxc_http_get, ANY_ROUTE},
 	{"ruxc_http_post", (cmd_function)w_ruxc_http_post, 4, fixup_ruxc_http_post,
 		fixup_free_ruxc_http_post, ANY_ROUTE},
+	{"ruxc_http_delete", (cmd_function)w_ruxc_http_delete, 4, fixup_ruxc_http_post,
+		fixup_free_ruxc_http_post, ANY_ROUTE},
 
 	{0, 0, 0, 0, 0, 0}
 };
@@ -353,6 +358,113 @@ static int w_ruxc_http_post(struct sip_msg *_msg, char *_url,
 	return ki_ruxc_http_post_helper(_msg, &url, &body, &hdrs, dst);
 }
 
+/**
+ *
+ */
+static int ki_ruxc_http_delete_helper(sip_msg_t *_msg, str *url, str *body, str *hdrs,
+		pv_spec_t *dst)
+{
+	RuxcHTTPRequest v_http_request = {0};
+	RuxcHTTPResponse v_http_response = {0};
+	pv_value_t val = {0};
+	int ret;
+
+	ruxc_request_params_init(&v_http_request);
+
+	v_http_request.url = url->s;
+	v_http_request.url_len = url->len;
+
+	if(body!=NULL && body->s!=NULL && body->len>0) {
+		v_http_request.data = body->s;
+		v_http_request.data_len = body->len;
+	}
+
+	if(hdrs!=NULL && hdrs->s!=NULL && hdrs->len>0) {
+		v_http_request.headers = hdrs->s;
+		v_http_request.headers_len = hdrs->len;
+	}
+
+	ruxc_http_delete(&v_http_request, &v_http_response);
+
+	if(v_http_response.retcode < 0) {
+		LM_ERR("failed to perform http get - retcode: %d\n", v_http_response.retcode);
+		ret = v_http_response.retcode;
+	} else {
+		if(v_http_response.resdata != NULL &&  v_http_response.resdata_len>0) {
+			LM_DBG("response code: %d - data len: %d - data: [%.*s]\n",
+					v_http_response.rescode, v_http_response.resdata_len,
+					v_http_response.resdata_len, v_http_response.resdata);
+			val.rs.s = v_http_response.resdata;
+			val.rs.len = v_http_response.resdata_len;
+			val.flags = PV_VAL_STR;
+			if(dst->setf) {
+				dst->setf(_msg, &dst->pvp, (int)EQ_T, &val);
+			} else {
+				LM_WARN("target pv is not writable\n");
+			}
+		}
+		ret = v_http_response.rescode;
+	}
+	ruxc_http_response_release(&v_http_response);
+
+	return (ret!=0)?ret:-2;
+}
+
+/**
+ *
+ */
+static int ki_ruxc_http_delete(sip_msg_t *_msg, str *url, str *body, str *hdrs, str *dpv)
+{
+	pv_spec_t *dst;
+
+	dst = pv_cache_get(dpv);
+	if(dst==NULL) {
+		LM_ERR("failed to get pv spec for: %.*s\n", dpv->len, dpv->s);
+		return -1;
+	}
+	if(dst->setf==NULL) {
+		LM_ERR("target pv is not writable: %.*s\n", dpv->len, dpv->s);
+		return -1;
+	}
+	return ki_ruxc_http_delete_helper(_msg, url, body, hdrs, dst);
+}
+
+/**
+ *
+ */
+static int w_ruxc_http_delete(struct sip_msg *_msg, char *_url,
+		char *_body, char *_hdrs, char *_result)
+{
+	str url = {NULL, 0};
+	str body = {NULL, 0};
+	str hdrs = {NULL, 0};
+	pv_spec_t *dst;
+
+	if(get_str_fparam(&url, _msg, (gparam_p)_url) != 0 || url.len <= 0) {
+		LM_ERR("URL has no value\n");
+		return -1;
+	}
+	if(_body && get_str_fparam(&body, _msg, (gparam_p)_body) != 0) {
+		LM_ERR("DATA body has no value\n");
+		return -1;
+	} else {
+		if(body.len == 0) {
+			body.s = NULL;
+		}
+	}
+	if(_hdrs && get_str_fparam(&hdrs, _msg, (gparam_p)_hdrs) != 0) {
+		LM_ERR("HDRS has no value\n");
+		return -1;
+	} else {
+		if(hdrs.len == 0) {
+			hdrs.s = NULL;
+		}
+	}
+	dst = (pv_spec_t *)_result;
+
+	return ki_ruxc_http_delete_helper(_msg, &url, &body, &hdrs, dst);
+}
+
 /**
  *
  */
@@ -452,6 +564,11 @@ static sr_kemi_t sr_kemi_ruxc_exports[] = {
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
 			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
 	},
+	{ str_init("ruxc"), str_init("http_delete"),
+		SR_KEMIP_INT, ki_ruxc_http_delete,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
+			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
 
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
 };