From: Veeraj Khokale Date: Mon, 10 Oct 2016 08:34:54 +0000 (+0530) Subject: Fix for Jira [IOT-1424] X-Git-Tag: 1.3.0~440 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=344a35de6818d2a5420ca722949423bcfa0b1d54;p=platform%2Fupstream%2Fiotivity.git Fix for Jira [IOT-1424] 1. Modified CHPGetOCContentType() and CHPGetOptionID() to internally duplicate the string parameter passed and work on this copy, thereby maintaining const correctness. 2. Also modified unit test to pass "application/cbor" as http content type. Change-Id: Ic0ea5704ddff20dc67086296e54775ef8d677d69 Signed-off-by: Veeraj Khokale Reviewed-on: https://gerrit.iotivity.org/gerrit/13013 Tested-by: jenkins-iotivity Reviewed-by: Ashok Babu Channa (cherry picked from commit cac65bc77127b61056783bdd1178263518682912) Reviewed-on: https://gerrit.iotivity.org/gerrit/15275 Tested-by: jenkins-iotivity Reviewed-by: Phil Coval Reviewed-by: Kevin Kane Reviewed-by: Uze Choi --- diff --git a/service/coap-http-proxy/src/CoapHttpMap.c b/service/coap-http-proxy/src/CoapHttpMap.c index 2712a81..aafe0b7 100644 --- a/service/coap-http-proxy/src/CoapHttpMap.c +++ b/service/coap-http-proxy/src/CoapHttpMap.c @@ -29,38 +29,47 @@ #define TAG "CHPMap" -int CHPGetOptionID(const char *httpOptionName) +int CHPGetOptionID(const char *httpOptionNameStr) { - if (!httpOptionName) + if (!httpOptionNameStr) { OIC_LOG(ERROR, TAG, "HTTP option name is NULL"); return 0; } - OICStringToLower((char *)httpOptionName); + char *httpOptionName = OICStrdup(httpOptionNameStr); + if (NULL == httpOptionName) + { + OIC_LOG(ERROR, TAG, "Could not duplicate HTTP option name"); + return 0; + } + + OICStringToLower(httpOptionName); + int ret = 0; if (0 == strcmp(httpOptionName, HTTP_OPTION_CACHE_CONTROL) || 0 == strcmp(httpOptionName, HTTP_OPTION_EXPIRES)) { - return COAP_OPTION_MAXAGE; + ret = COAP_OPTION_MAXAGE; } else if (0 == strcmp(httpOptionName, HTTP_OPTION_IF_MATCH)) { - return COAP_OPTION_IF_MATCH; + ret = COAP_OPTION_IF_MATCH; } else if (0 == strcmp(httpOptionName, HTTP_OPTION_IF_NONE_MATCH)) { - return COAP_OPTION_IF_NONE_MATCH; + ret = COAP_OPTION_IF_NONE_MATCH; } else if (0 == strcmp(httpOptionName, HTTP_OPTION_ETAG)) { - return COAP_OPTION_ETAG; + ret = COAP_OPTION_ETAG; } else { OIC_LOG_V(ERROR, TAG, "No Mapping found for %s", httpOptionName); } - return 0; + OICFree(httpOptionName); + return ret; } OCStackResult CHPGetOCCode(const HttpResponseResult_t httpCode, const OCMethod method, @@ -156,22 +165,31 @@ OCStackResult CHPGetOCOption(const HttpHeaderOption_t *httpOption, OCHeaderOptio return OC_STACK_OK; } -OCPayloadFormat CHPGetOCContentType(const char *httpContentType) +OCPayloadFormat CHPGetOCContentType(const char *httpContentTypeStr) { OIC_LOG_V(DEBUG, TAG, "%s IN", __func__); - OICStringToLower((char *)httpContentType); + char *httpContentType = OICStrdup(httpContentTypeStr); + if (NULL == httpContentType) + { + OIC_LOG(ERROR, TAG, "Could not duplicate HTTP content type"); + return OC_FORMAT_UNSUPPORTED; + } + + OICStringToLower(httpContentType); + OCPayloadFormat ret = OC_FORMAT_UNSUPPORTED; if (strstr(httpContentType, CBOR_CONTENT_TYPE)) { - return OC_FORMAT_CBOR; + ret = OC_FORMAT_CBOR; } else if (strstr(httpContentType, JSON_CONTENT_TYPE)) { - return OC_FORMAT_JSON; + ret = OC_FORMAT_JSON; } + OICFree(httpContentType); OIC_LOG_V(DEBUG, TAG, "%s OUT", __func__); - return OC_FORMAT_UNSUPPORTED; + return ret; } OCStackResult CHPGetHttpMethod(const OCMethod method, HttpMethod_t *httpMethod) diff --git a/service/coap-http-proxy/unittests/CoAPHttpUnitTest.cpp b/service/coap-http-proxy/unittests/CoAPHttpUnitTest.cpp index d59d7ce..a99e2e3 100644 --- a/service/coap-http-proxy/unittests/CoAPHttpUnitTest.cpp +++ b/service/coap-http-proxy/unittests/CoAPHttpUnitTest.cpp @@ -102,8 +102,7 @@ TEST_F(CoApHttpTest, CHPGetOCOption) TEST_F(CoApHttpTest, CHPGetOCContentType) { HttpResponse_t httpResponse; - //char *httpContentType = "OC_FORMAT_CBOR"; - char *httpContentType = "CBOR_CONTENT_TYPE"; + const char *httpContentType = CBOR_CONTENT_TYPE; EXPECT_EQ(OC_FORMAT_CBOR, (CHPGetOCContentType(httpContentType))); }