From 0bcdd93bcfa84ce8ab98ffef00905884a2381bb0 Mon Sep 17 00:00:00 2001 From: Stefan Sayer Date: Fri, 9 Jul 2010 16:44:57 +0200 Subject: [PATCH] core: fixes on getHeader and get_header_keyvalue fixes problems when values were at the end of the message/header --- core/AmSipMsg.cpp | 24 ++++++++++++------------ core/AmUtils.cpp | 4 +++- core/tests/test_headers.cpp | 29 +++++++++++++++++++++++++++++ core/tests/test_jsonarg.cpp | 2 +- 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/core/AmSipMsg.cpp b/core/AmSipMsg.cpp index c2dca8ac..a43f656d 100644 --- a/core/AmSipMsg.cpp +++ b/core/AmSipMsg.cpp @@ -31,13 +31,13 @@ string getHeader(const string& hdrs,const string& hdr_name, return getHeader(hdrs, compact_hdr_name, single); return res; } - +#include "log.h" bool findHeader(const string& hdrs,const string& hdr_name, const size_t skip, size_t& pos1, size_t& pos2, size_t& hdr_start) { unsigned int p; - char* hdr = strdup(hdr_name.c_str()); if(skip >= hdrs.length()) return false; + char* hdr = strdup(hdr_name.c_str()); const char* hdrs_c = hdrs.c_str() + skip; char* hdr_c = hdr; const char* hdrs_end = hdrs.c_str() + hdrs.length(); @@ -90,16 +90,16 @@ bool findHeader(const string& hdrs,const string& hdr_name, const size_t skip, p = hdrs_c - hdrs.c_str(); - string::size_type p_end; - if( ((p_end = hdrs.find('\r',p)) != string::npos) - || ((p_end = hdrs.find('\n',p)) != string::npos) ){ - - free(hdr); - // return hdrs.substr(p,p_end-p); - pos1 = p; - pos2 = p_end; - return true; - } + string::size_type p_end = p; + while (p_end < hdrs.size() && + hdrs[p_end] != '\r' && + hdrs[p_end] != '\n') + p_end++; + free(hdr); + // return hdrs.substr(p,p_end-p); + pos1 = p; + pos2 = p_end; + return true; } } diff --git a/core/AmUtils.cpp b/core/AmUtils.cpp index 66a4d5b0..13596f5d 100644 --- a/core/AmUtils.cpp +++ b/core/AmUtils.cpp @@ -905,9 +905,11 @@ string get_header_keyvalue_single(const string& param_hdr, const string& name) { } } - + if (v_begin && v_end) return param_hdr.substr(v_begin, v_end-v_begin+1); + else if (v_begin && (p==param_hdr.length())) + return param_hdr.substr(v_begin); else return ""; } diff --git a/core/tests/test_headers.cpp b/core/tests/test_headers.cpp index 98650f3d..75a7c795 100644 --- a/core/tests/test_headers.cpp +++ b/core/tests/test_headers.cpp @@ -4,16 +4,45 @@ #include "AmSipHeaders.h" #include "AmSipMsg.h" +#include "AmUtils.h" FCTMF_SUITE_BGN(test_headers) { FCT_TEST_BGN(getHeader_simple) { fct_chk( getHeader("P-My-Test: myval" CRLF, "P-My-Test") == "myval"); } FCT_TEST_END(); + FCT_TEST_BGN(getHeader_multi) { fct_chk( getHeader("P-My-Test: myval" CRLF "P-My-Test: myval2" CRLF , "P-My-Test", true) == "myval" ); fct_chk( getHeader("P-My-Test: myval" CRLF "P-My-Test: myval2" CRLF , "P-My-Test", false) == "myval, myval2" ); fct_chk( getHeader("P-My-Test: myval" CRLF "P-My-Otherheader: myval2" CRLF "P-My-Test: myval2" CRLF , "P-My-Test", false) == "myval, myval2" ); } FCT_TEST_END(); + FCT_TEST_BGN(getHeader_atend) { + + fct_chk(getHeader("P-My-Test: mykey=myval;myotherkey=myval" , + "P-My-Test", true) == "mykey=myval;myotherkey=myval"); + fct_chk(getHeader("P-My-Test: mykey=myval;myotherkey=myval\n" , + "P-My-Test", true) == "mykey=myval;myotherkey=myval"); + fct_chk(getHeader("P-My-Test: mykey=myval;myotherkey=myval\r" , + "P-My-Test", true) == "mykey=myval;myotherkey=myval"); + fct_chk(getHeader("P-My-Test: mykey=myval;myotherkey=myval\r\n" , + "P-My-Test", true) == "mykey=myval;myotherkey=myval"); + fct_chk(getHeader("P-My-Test: mykey=myval;myotherkey=myval\r\nP-anotherheader:xy" , + "P-My-Test", true) == "mykey=myval;myotherkey=myval"); + + } FCT_TEST_END(); + + FCT_TEST_BGN(getHeader_keyvalue) { + fct_chk(get_header_keyvalue("mykey=myval;myotherkey=myval", "mykey") == "myval"); + fct_chk(get_header_keyvalue("mykey=myval;myotherkey=myval", "myotherkey") == "myval"); + fct_chk(get_header_keyvalue("mykey=myval;myotherkey=", "myotherkey") == ""); + fct_chk(get_header_keyvalue(getHeader("P-My-Test: mykey=myval;myotherfunkykey=myval" CRLF, "P-My-Test", true), "mykey") == "myval" ); + + fct_chk(get_header_keyvalue(getHeader("P-My-Test: mykey=myval;myotherfunkykey=myval", "P-My-Test", true), "mykey") == "myval" ); + + fct_chk(get_header_keyvalue(getHeader("P-My-Test: mykey=myval;myotherfunkykey=myval2", "P-My-Test", true), "myotherfunkykey") == "myval2" ); + + } FCT_TEST_END(); + } FCTMF_SUITE_END(); diff --git a/core/tests/test_jsonarg.cpp b/core/tests/test_jsonarg.cpp index bf18523f..7de8533c 100644 --- a/core/tests/test_jsonarg.cpp +++ b/core/tests/test_jsonarg.cpp @@ -9,7 +9,7 @@ FCTMF_SUITE_BGN(test_jsonarg) { FCT_TEST_BGN(json_parse_escaped) { string s = "{\"jsonrpc\": \"2.0\", \"id\": \"11\", \"error\": {\"message\": \"(1062, \\\"Duplicate entry '5447' for key 'PRIMARY'\\\")\", \"code\": -32603}},"; - DBG("s='%s'\n",s.c_str()); + // DBG("s='%s'\n",s.c_str()); AmArg rpc_params; fct_chk(json2arg(s.c_str(), rpc_params));