From 4ad19d01dce96c846a18f7f51619420a78e22bc7 Mon Sep 17 00:00:00 2001 From: "Olle E. Johansson" Date: Sat, 14 Jan 2017 21:02:05 +0100 Subject: [PATCH] utils Remove http_query function that now is replaced by the http_client module --- src/modules/utils/Makefile | 14 -- src/modules/utils/doc/utils_admin.xml | 147 +--------------- src/modules/utils/functions.c | 232 -------------------------- src/modules/utils/functions.h | 46 ----- src/modules/utils/utils.c | 191 --------------------- 5 files changed, 1 insertion(+), 629 deletions(-) delete mode 100644 src/modules/utils/functions.c delete mode 100644 src/modules/utils/functions.h diff --git a/src/modules/utils/Makefile b/src/modules/utils/Makefile index d8b1fceba31..56b131d1857 100644 --- a/src/modules/utils/Makefile +++ b/src/modules/utils/Makefile @@ -10,12 +10,6 @@ NAME=utils.so ifeq ($(CROSS_COMPILE),) XML2CFG=$(shell which xml2-config) -CURL_BUILDER=$(shell \ - if pkg-config --exists libcurl; then \ - echo 'pkg-config libcurl'; \ - else \ - which curl-config; \ - fi) endif ifneq ($(XML2CFG),) @@ -27,14 +21,6 @@ else LIBS+=-L$(LOCALBASE)/lib -lxml2 endif -ifneq ($(CURL_BUILDER),) - DEFS += $(shell $(CURL_BUILDER) --cflags ) - LIBS += $(shell $(CURL_BUILDER) --libs) -else - DEFS+=-I$(LOCALBASE)/include - LIBS+=-L$(LOCALBASE)/lib -lcurl -endif - DEFS+=-DKAMAILIO_MOD_INTERFACE SERLIBPATH=../../lib diff --git a/src/modules/utils/doc/utils_admin.xml b/src/modules/utils/doc/utils_admin.xml index 8972df9de00..342030e0c71 100644 --- a/src/modules/utils/doc/utils_admin.xml +++ b/src/modules/utils/doc/utils_admin.xml @@ -20,10 +20,6 @@ related. - Function http_query allows &kamailio; to issue an HTTP GET - request and get access to parts of the reply. - - The forward functionality allows &kamailio; to configure forwarding at runtime with FIFO commands. The forwarding is executed in the pre script call back and therefore handled before the routing script is @@ -62,7 +58,7 @@ - libcurl. + none. @@ -72,67 +68,6 @@
Parameters -
- <varname>http_query_timeout</varname> (int) - - Defines in seconds how long &kamailio; waits for response - from HTTP server. - - - - Default value is zero, i.e., - that the http_query_timeout function is disabled. - - - - Set <varname>http_query_timeout</varname> parameter - -... -modparam("utils", "http_query_timeout", 2) -... - - -
-
- <varname>http_response_mode</varname> (int) - - Control what part of the HTTP reponse is returned: only - first line (for value 0), or all response (for value 1). - - - - Default value is 0 (return only the first line). - - - - Set <varname>http_response_mode</varname> parameter - -... -modparam("utils", "http_response_mode", 1) -... - - -
-
- <varname>http_response_trim</varname> (int) - - Control if white space, tab and end of line characters should be - trimmed from leading and trailing parts of HTTP response. - - - - Default value is 0 (don't trim). - - - - Set <varname>http_response_trim</varname> parameter - -... -modparam("utils", "http_response_trim", 1) -... - - -
<varname>forward_active</varname> (int) @@ -196,75 +131,6 @@ modparam("utils", "xcap_table", "pres_xcap")
Functions -
- - <function moreinfo="none">http_query(url, [post-data], [header-data], result)</function> - - - Sends HTTP GET or POST request according to URL given in - url parameter, which is a string that may - contain pseudo variables. - - - If you want to make a POST-Request, you have to define - the post-data, that should be submitted - in that request as the second parameter. If this - parameter is empty, it is not set. - - - If you want to add additional headers to the request, - you have to define the header-data, - that should be submitted in that request as the third - parameter. If this parameter is empty, it is not set. - - - If HTTP server returns a class 2xx, 3xx or 4xx reply, - the first line of the reply's body (if any) is - stored in result parameter, - which must be a writable pseudo variable. - - - Function returns reply code of HTTP reply or -1 - if something went wrong. - - - This function can be used from REQUEST_ROUTE, - ONREPLY_ROUTE, FAILURE_ROUTE, and BRANCH_ROUTE. - - - <function>http_query()</function> usage - -... -# GET-Request -http_query("http://tutpro.com/index.php?r_uri=$(ru{s.escape.param})&f_uri=$(fu{s.escape.param})", - "$var(result)"); -switch ($retcode) { - ... -} -... - - -... -# POST-Request -http_query("http://tutpro.com/index.php", "r_uri=$(ru{s.escape.param})&f_uri=$(fu{s.escape.param})", - "$var(result)"); -switch ($retcode) { - ... -} -... - - -... -# GET-Request with additional headers and Post-Data. -http_query("http://tutpro.com/index.php", "{ "alert": "Alert text goes here" }", "Content-Type: application/json" - "$var(result)"); -switch ($retcode) { - ... -} -... - - -
<function moreinfo="none">xcap_auth_status(watcher_uri, presentity_uri)</function> @@ -415,15 +281,4 @@ id switch filter proxy <listitem>digit ::= ? 0-9 ?</listitem> </itemizedlist> </section> - <section id="utils.s.remarks"> - <title>Remarks - - Note: libcurl leak in CentOS 6 - this module uses libcurl library - and in case if you are using CentOS 6, be aware that standard - libcurl-7.19.7-52 has a memory leak. To fix this memory, install - libcurl from city-fan repository. More details at: - - https://www.digitalocean.com/community/questions/how-to-upgrade-curl-in-centos6 - -
diff --git a/src/modules/utils/functions.c b/src/modules/utils/functions.c deleted file mode 100644 index 67800dbff3a..00000000000 --- a/src/modules/utils/functions.c +++ /dev/null @@ -1,232 +0,0 @@ -/* - * script functions of utils module - * - * Copyright (C) 2008 Juha Heinanen - * Copyright (C) 2013-2015 Carsten Bock, ng-voice GmbH - * - * This file is part of Kamailio, a free SIP server. - * - * Kamailio is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version - * - * Kamailio is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -/*! - * \file - * \brief Kamailio utils :: - * \ingroup utils - * Module: \ref utils - */ - - -#include - -#include "../../core/mod_fix.h" -#include "../../core/pvar.h" -#include "../../core/route_struct.h" -#include "../../core/ut.h" -#include "../../core/trim.h" -#include "../../core/mem/mem.h" -#include "../../core/parser/msg_parser.h" -#include "../../core/lvalue.h" - -#include "utils.h" - - -extern int http_response_trim; -extern int http_response_mode; - -/* - * curl write function that saves received data as zero terminated - * to stream. Returns the amount of data taken care of. - * - * This function may be called multiple times for larger responses, - * so it reallocs + concatenates the buffer as needed. - */ -size_t write_function( void *ptr, size_t size, size_t nmemb, void *stream_ptr) -{ - http_res_stream_t *stream = (http_res_stream_t *) stream_ptr; - - char *tmp = (char *) pkg_realloc(stream->buf, stream->curr_size + - (size * nmemb)); - - if (tmp == NULL) { - LM_ERR("cannot allocate memory for stream\n"); - return CURLE_WRITE_ERROR; - } - stream->buf = tmp; - - memcpy(&stream->buf[stream->pos], (char *) ptr, (size * nmemb)); - - stream->curr_size += (size * nmemb); - stream->pos += (size * nmemb); - - return size * nmemb; -} - -/* - * Performs http_query and saves possible result (first body line of reply) - * to pvar. - */ -int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post, char* _hdr) -{ - CURL *curl; - CURLcode res; - str value, post_value, hdr_value; - char *url, *at, *post = NULL, *hdr = NULL; - http_res_stream_t stream; - long stat; - pv_spec_t *dst; - pv_value_t val; - double download_size; - struct curl_slist *chunk = NULL; - str hres; - - memset(&stream, 0, sizeof(http_res_stream_t)); - - if (fixup_get_svalue(_m, (gparam_p)_url, &value) != 0) { - LM_ERR("cannot get page value\n"); - return -1; - } - - curl = curl_easy_init(); - if (curl == NULL) { - LM_ERR("failed to initialize curl\n"); - return -1; - } - - url = pkg_malloc(value.len + 1); - if (url == NULL) { - curl_easy_cleanup(curl); - LM_ERR("cannot allocate pkg memory for url\n"); - return -1; - } - memcpy(url, value.s, value.len); - *(url + value.len) = (char)0; - curl_easy_setopt(curl, CURLOPT_URL, url); - - if (_hdr) { - if (fixup_get_svalue(_m, (gparam_p)_hdr, &hdr_value) != 0) { - LM_ERR("cannot get Header value\n"); - curl_easy_cleanup(curl); - pkg_free(url); - return -1; - } - if (hdr_value.len > 0) { - hdr = pkg_malloc(hdr_value.len + 1); - if (hdr == NULL) { - curl_easy_cleanup(curl); - pkg_free(url); - LM_ERR("cannot allocate pkg memory for header\n"); - return -1; - } - memcpy(hdr, hdr_value.s, hdr_value.len); - *(hdr + hdr_value.len) = (char)0; - - chunk = curl_slist_append(chunk, hdr); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); - } - } - - if (_post) { - if (fixup_get_svalue(_m, (gparam_p)_post, &post_value) != 0) { - LM_ERR("cannot get post value\n"); - curl_easy_cleanup(curl); - pkg_free(url); - if (hdr) pkg_free(hdr); - return -1; - } - if (post_value.len > 0) { - /* Now specify we want to POST data */ - curl_easy_setopt(curl, CURLOPT_POST, 1L); - - post = pkg_malloc(post_value.len + 1); - if (post == NULL) { - curl_easy_cleanup(curl); - pkg_free(url); - if (hdr) pkg_free(hdr); - LM_ERR("cannot allocate pkg memory for post\n"); - return -1; - } - memcpy(post, post_value.s, post_value.len); - *(post + post_value.len) = (char)0; - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); - } - } - - - curl_easy_setopt(curl, CURLOPT_NOSIGNAL, (long)1); - curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)http_query_timeout); - - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_function); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream); - - res = curl_easy_perform(curl); - pkg_free(url); - if (_post) { - pkg_free(post); - } - if (_hdr) { - pkg_free(hdr); - } - if (chunk) { - curl_slist_free_all(chunk); - } - - if (res != CURLE_OK) { - /* http://curl.haxx.se/libcurl/c/libcurl-errors.html */ - if (res == CURLE_COULDNT_CONNECT) { - LM_WARN("failed to connect() to host\n"); - } else if ( res == CURLE_COULDNT_RESOLVE_HOST ) { - LM_WARN("couldn't resolve host\n"); - } else { - LM_ERR("failed to perform curl (%d)\n", res); - } - - curl_easy_cleanup(curl); - if(stream.buf) - pkg_free(stream.buf); - return -1; - } - - curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &stat); - if ((stat >= 200) && (stat < 500)) { - curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &download_size); - LM_DBG("http_query download size: %u\n", (unsigned int)download_size); - - hres.s = stream.buf; - hres.len = download_size; - if(http_response_trim) { - trim(&hres); - } - val.rs = hres; - if(http_response_mode==0) { - /* only first line - search for line feed */ - at = memchr(hres.s, (char)10, hres.len); - if (at != NULL) { - /* found: use first line */ - val.rs.s = hres.s; - val.rs.len = at - hres.s; - } - } - LM_DBG("http_query result: %.*s\n", val.rs.len, val.rs.s); - val.flags = PV_VAL_STR; - dst = (pv_spec_t *)_dst; - dst->setf(_m, &dst->pvp, (int)EQ_T, &val); - } - - curl_easy_cleanup(curl); - pkg_free(stream.buf); - return (stat!=0)?stat:-1; -} diff --git a/src/modules/utils/functions.h b/src/modules/utils/functions.h deleted file mode 100644 index b2c4491cec6..00000000000 --- a/src/modules/utils/functions.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * headers of script functions of utils module - * - * Copyright (C) 2008 Juha Heinanen - * Copyright (C) 2013 Carsten Bock, ng-voice GmbH - * - * This file is part of Kamailio, a free SIP server. - * - * Kamailio is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version - * - * Kamailio is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - * - */ - -/*! - * \file - * \brief Kamailio utils :: - * \ingroup utils - * Module: \ref utils - */ - - -#ifndef UTILS_FUNCTIONS_H -#define UTILS_FUNCTIONS_H - -#include "../../core/parser/msg_parser.h" - - -/* - * Performs http_query and saves possible result (first body line of reply) - * to pvar. - */ -int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post, char* _hdr); - - -#endif /* UTILS_FUNCTIONS_H */ diff --git a/src/modules/utils/utils.c b/src/modules/utils/utils.c index 49231309d41..dd38f042958 100644 --- a/src/modules/utils/utils.c +++ b/src/modules/utils/utils.c @@ -47,7 +47,6 @@ #include "../../core/mem/shm_mem.h" #include "../../lib/srdb1/db.h" -#include "functions.h" #include "conf.h" #include "xcap_auth.h" @@ -57,9 +56,6 @@ MODULE_VERSION #define XCAP_TABLE_VERSION 4 /* Module parameter variables */ -int http_query_timeout = 4; -int http_response_trim = 0; -int http_response_mode = 0; static int forward_active = 0; static int mp_max_id = 0; static char* mp_switch = ""; @@ -88,33 +84,11 @@ static int mod_init(void); static int child_init(int); static void destroy(void); -/* Fixup functions to be defined later */ -static int fixup_http_query_get(void** param, int param_no); -static int fixup_free_http_query_get(void** param, int param_no); -static int fixup_http_query_post(void** param, int param_no); -static int fixup_http_query_post_hdr(void** param, int param_no); -static int fixup_free_http_query_post(void** param, int param_no); -static int fixup_free_http_query_post_hdr(void** param, int param_no); - -/* Wrappers for http_query to be defined later */ -static int w_http_query(struct sip_msg* _m, char* _url, char* _result); -static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post, char* _result); -static int w_http_query_post_hdr(struct sip_msg* _m, char* _url, char* _post, char* _hdr, char* _result); - /* forward function */ int utils_forward(struct sip_msg *msg, int id, int proto); /* Exported functions */ static cmd_export_t cmds[] = { - {"http_query", (cmd_function)w_http_query, 2, fixup_http_query_get, - fixup_free_http_query_get, - REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, - {"http_query", (cmd_function)w_http_query_post, 3, fixup_http_query_post, - fixup_free_http_query_post, - REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, - {"http_query", (cmd_function)w_http_query_post_hdr, 4, fixup_http_query_post_hdr, - fixup_free_http_query_post_hdr, - REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE}, {"xcap_auth_status", (cmd_function)xcap_auth_status, 2, fixup_pvar_pvar, fixup_free_pvar_pvar, REQUEST_ROUTE}, {0, 0, 0, 0, 0, 0} @@ -125,9 +99,6 @@ static cmd_export_t cmds[] = { static param_export_t params[] = { {"pres_db_url", PARAM_STR, &pres_db_url}, {"xcap_table", PARAM_STR, &xcap_table}, - {"http_query_timeout", INT_PARAM, &http_query_timeout}, - {"http_response_trim", INT_PARAM, &http_response_trim}, - {"http_response_mode", INT_PARAM, &http_response_mode}, {"forward_active", INT_PARAM, &forward_active}, {0, 0, 0} }; @@ -247,13 +218,6 @@ static int pres_db_open(void) { /* Module initialization function */ static int mod_init(void) { - /* Initialize curl */ - if (curl_global_init(CURL_GLOBAL_ALL)) { - LM_ERR("curl_global_init failed\n"); - return -1; - } - - if (init_shmlock() != 0) { LM_CRIT("cannot initialize shmlock.\n"); return -1; @@ -315,8 +279,6 @@ static int child_init(int rank) static void destroy(void) { - /* Cleanup curl */ - curl_global_cleanup(); /* Cleanup forward */ conf_destroy(); destroy_shmlock(); @@ -325,159 +287,6 @@ static void destroy(void) } -/* Fixup functions */ - -/* - * Fix http_query params: url (string that may contain pvars) and - * result (writable pvar). - */ -static int fixup_http_query_get(void** param, int param_no) -{ - if (param_no == 1) { - return fixup_spve_null(param, 1); - } - - if (param_no == 2) { - if (fixup_pvar_null(param, 1) != 0) { - LM_ERR("failed to fixup result pvar\n"); - return -1; - } - if (((pv_spec_t *)(*param))->setf == NULL) { - LM_ERR("result pvar is not writeble\n"); - return -1; - } - return 0; - } - - LM_ERR("invalid parameter number <%d>\n", param_no); - return -1; -} - -/* - * Free http_query params. - */ -static int fixup_free_http_query_get(void** param, int param_no) -{ - if (param_no == 1) { - return fixup_free_spve_null(param, 1); - } - - if (param_no == 2) { - return fixup_free_pvar_null(param, 1); - } - - LM_ERR("invalid parameter number <%d>\n", param_no); - return -1; -} - - -/* - * Fix http_query params: url (string that may contain pvars) and - * result (writable pvar). - */ -static int fixup_http_query_post(void** param, int param_no) -{ - if ((param_no == 1) || (param_no == 2)) { - return fixup_spve_null(param, 1); - } - - if (param_no == 3) { - if (fixup_pvar_null(param, 1) != 0) { - LM_ERR("failed to fixup result pvar\n"); - return -1; - } - if (((pv_spec_t *)(*param))->setf == NULL) { - LM_ERR("result pvar is not writeble\n"); - return -1; - } - return 0; - } - - LM_ERR("invalid parameter number <%d>\n", param_no); - return -1; -} - -/* - * Fix http_query params: url (string that may contain pvars) and - * result (writable pvar). - */ -static int fixup_http_query_post_hdr(void** param, int param_no) -{ - if ((param_no >= 1) && (param_no <= 3)) { - return fixup_spve_null(param, 1); - } - - if (param_no == 4) { - if (fixup_pvar_null(param, 1) != 0) { - LM_ERR("failed to fixup result pvar\n"); - return -1; - } - if (((pv_spec_t *)(*param))->setf == NULL) { - LM_ERR("result pvar is not writeble\n"); - return -1; - } - return 0; - } - - LM_ERR("invalid parameter number <%d>\n", param_no); - return -1; -} - -/* - * Free http_query params. - */ -static int fixup_free_http_query_post(void** param, int param_no) -{ - if ((param_no == 1) || (param_no == 2)) { - return fixup_free_spve_null(param, 1); - } - - if (param_no == 3) { - return fixup_free_pvar_null(param, 1); - } - - LM_ERR("invalid parameter number <%d>\n", param_no); - return -1; -} - -/* - * Free http_query params. - */ -static int fixup_free_http_query_post_hdr(void** param, int param_no) -{ - if ((param_no >= 1) && (param_no <= 3)) { - return fixup_free_spve_null(param, 1); - } - - if (param_no == 4) { - return fixup_free_pvar_null(param, 1); - } - - LM_ERR("invalid parameter number <%d>\n", param_no); - return -1; -} - -/* - * Wrapper for HTTP-Query (GET) - */ -static int w_http_query(struct sip_msg* _m, char* _url, char* _result) { - return http_query(_m, _url, _result, NULL, NULL); -} - -/* - * Wrapper for HTTP-Query (POST-Variant) - */ -static int w_http_query_post(struct sip_msg* _m, char* _url, char* _post, char* _result) { - return http_query(_m, _url, _result, _post, NULL); -} - -/* - * Wrapper for HTTP-Query (POST-Variant) - */ -static int w_http_query_post_hdr(struct sip_msg* _m, char* _url, char* _post, char* _hdr, char* _result) { - return http_query(_m, _url, _result, _post, _hdr); -} - /*! * \brief checks precondition, switch, filter and forwards msg if necessary * \param msg the message to be forwarded