Modified query parsing to handle all filters.
authorMandeep Shetty <mandeep.shetty@intel.com>
Wed, 29 Apr 2015 18:58:03 +0000 (11:58 -0700)
committerErich Keane <erich.keane@intel.com>
Fri, 1 May 2015 16:43:13 +0000 (16:43 +0000)
Earlier the parsing only passed if "rt" and "if" were in the query
which did not work for other filters for non virtual resources.
Function now simply splits the uri on the delimiter '?'.

Change-Id: Iaafd2c5c55140f7c0b9207cf069f1a8e11979b25
Signed-off-by: Mandeep Shetty <mandeep.shetty@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/869
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
Reviewed-by: Sakthivel Samidurai <sakthivel.samidurai@intel.com>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/stack/src/ocstack.c

index 2600747..ef84d11 100644 (file)
@@ -1373,7 +1373,7 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t*
     OC_LOG_V(INFO, TAG, PCF("Endpoint URI : %s\n"), (char*)endPoint->resourceUri);
 
     char * newUri = NULL;
-    char * query = NULL;
+    char * query  = NULL;
 
     requestResult = getQueryFromUri(endPoint->resourceUri, &query, &newUri);
 
@@ -3699,46 +3699,63 @@ OCStackResult getResourceType(const char * query, char** resourceType)
     return result;
 }
 
-OCStackResult getQueryFromUri(const char * uri, char** query, char ** newURI)
+/*
+ * This function splits the uri using the '?' delimiter.
+ * "uriWithoutQuery" is the block of characters between the beginning
+ * till the delimiter or '\0' which ever comes first.
+ * "query" is whatever is to the right of the delimiter if present.
+ * No delimiter sets the query to NULL.
+ * If either are present, they will be malloc'ed into the params 2, 3.
+ * The first param, *uri is left untouched.
+
+ * NOTE: This function does not account for whitespace at the end of the uri NOR
+ *       malformed uri's with '??'. Whitespace at the end will be assumed to be
+ *       part of the query.
+ */
+OCStackResult getQueryFromUri(const char * uri, char** query, char ** uriWithoutQuery)
 {
     if(!uri)
     {
         return OC_STACK_INVALID_URI;
     }
-    if(!query || !newURI)
+    if(!query || !uriWithoutQuery)
     {
         return OC_STACK_INVALID_PARAM;
     }
-    char* strTokPtr = NULL;
-    char * leftToken = NULL;
-    char * tempURI = (char *) OCMalloc(strlen(uri) + 1);
-    if(!tempURI)
+
+    *query           = NULL;
+    *uriWithoutQuery = NULL;
+
+    size_t uriWithoutQueryLen = 0;
+    size_t queryLen = 0;
+    size_t uriLen = strlen(uri);
+
+    char *pointerToDelimiter = strstr(uri, "?");
+
+    uriWithoutQueryLen = pointerToDelimiter == NULL ? uriLen : pointerToDelimiter - uri;
+    queryLen = pointerToDelimiter == NULL ? 0 : uriLen - uriWithoutQueryLen - 1;
+
+    if (uriWithoutQueryLen)
     {
-        goto exit;
+        *uriWithoutQuery =  (char *) OCCalloc(uriWithoutQueryLen + 1, 1);
+        if (!*uriWithoutQuery)
+        {
+            goto exit;
+        }
+        strncpy(*uriWithoutQuery, uri, uriWithoutQueryLen);
     }
-    strcpy(tempURI, uri);
-    strTokPtr = NULL;
-    leftToken = strtok_r(tempURI, "?", &strTokPtr);
-
-    //TODO-CA: This could be simplified. Clean up required.
-    while(leftToken != NULL)
+    if (queryLen)
     {
-        if(strncmp(leftToken, "rt=", 3) == 0 || strncmp(leftToken, "if=", 3) == 0)
+        *query = (char *) OCCalloc(queryLen + 1, 1);
+        if (!*query)
         {
-            *query = (char *) OCMalloc(strlen(leftToken) + 1);
-            if(!*query)
-            {
-                OCFree(tempURI);
-                goto exit;
-            }
-            strcpy(*query, leftToken);
-            break;
+            OCFree(*uriWithoutQuery);
+            *uriWithoutQuery = NULL;
+            goto exit;
         }
-        leftToken = strtok_r(NULL, "?", &strTokPtr);
+        strncpy(*query, pointerToDelimiter + 1, queryLen);
     }
 
-    *newURI = tempURI;
-
     return OC_STACK_OK;
 
     exit: