Skip to content

Commit

Permalink
utils: option to tream leading and trailing whitespaces
Browse files Browse the repository at this point in the history
- new parameter http_response_trim to control it; default is 0 - don't
  trimi (backward compatible); set to 1 for enabling trimming
- based on the request of PR GH#321
  • Loading branch information
miconda committed Sep 17, 2015
1 parent ef568ca commit de925d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 14 additions & 4 deletions modules/utils/functions.c
Expand Up @@ -36,13 +36,16 @@
#include "../../pvar.h"
#include "../../route_struct.h"
#include "../../ut.h"
#include "../../trim.h"
#include "../../mem/mem.h"
#include "../../parser/msg_parser.h"
#include "../../lvalue.h"

#include "utils.h"


extern int http_response_trim;

/*
* curl write function that saves received data as zero terminated
* to stream. Returns the amount of data taken care of.
Expand Down Expand Up @@ -86,6 +89,7 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post, char* _h
pv_value_t val;
double download_size;
struct curl_slist *chunk = NULL;
str hres;

memset(&stream, 0, sizeof(http_res_stream_t));

Expand Down Expand Up @@ -199,14 +203,20 @@ int http_query(struct sip_msg* _m, char* _url, char* _dst, char* _post, char* _h
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);
}
/* search for line feed */
at = memchr(stream.buf, (char)10, download_size);
at = memchr(hres.s, (char)10, hres.len);
if (at == NULL) {
/* not found: use whole stream */
at = stream.buf + (unsigned int)download_size;
val.rs = hres;
} else {
val.rs.s = hres.s;
val.rs.len = at - hres.s;
}
val.rs.s = stream.buf;
val.rs.len = at - stream.buf;
LM_DBG("http_query result: %.*s\n", val.rs.len, val.rs.s);
val.flags = PV_VAL_STR;
dst = (pv_spec_t *)_dst;
Expand Down
2 changes: 2 additions & 0 deletions modules/utils/utils.c
Expand Up @@ -58,6 +58,7 @@ MODULE_VERSION

/* Module parameter variables */
int http_query_timeout = 4;
int http_response_trim = 0;
static int forward_active = 0;
static int mp_max_id = 0;
static char* mp_switch = "";
Expand Down Expand Up @@ -122,6 +123,7 @@ 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},
{"forward_active", INT_PARAM, &forward_active},
{0, 0, 0}
};
Expand Down

0 comments on commit de925d6

Please sign in to comment.