Fix for Jira [IOT-1424]
authorVeeraj Khokale <veeraj.sk@samsung.com>
Mon, 10 Oct 2016 08:34:54 +0000 (14:04 +0530)
committerUze Choi <uzchoi@samsung.com>
Thu, 23 Mar 2017 09:39:19 +0000 (09:39 +0000)
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 <veeraj.sk@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/13013
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Ashok Babu Channa <ashok.channa@samsung.com>
(cherry picked from commit cac65bc77127b61056783bdd1178263518682912)
Reviewed-on: https://gerrit.iotivity.org/gerrit/15275
Tested-by: jenkins-iotivity <jenkins@iotivity.org>
Reviewed-by: Phil Coval <philippe.coval@osg.samsung.com>
Reviewed-by: Kevin Kane <kkane@microsoft.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/coap-http-proxy/src/CoapHttpMap.c
service/coap-http-proxy/unittests/CoAPHttpUnitTest.cpp

index 2712a81..aafe0b7 100644 (file)
 
 #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)
index d59d7ce..a99e2e3 100644 (file)
@@ -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)));
 }