Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Files at this revision

API Documentation at this revision

Comitter:
AzureIoTClient
Date:
Thu Oct 04 09:17:16 2018 -0700
Parent:
48:81866008bba4
Commit message:
1.2.10

Changed in this revision

azure_c_shared_utility/singlylinkedlist.h Show annotated file Show diff for this revision Revisions of this file
azure_c_shared_utility/uuid.h Show annotated file Show diff for this revision Revisions of this file
httpapi_compact.c Show annotated file Show diff for this revision Revisions of this file
singlylinkedlist.c Show annotated file Show diff for this revision Revisions of this file
urlencode.c Show annotated file Show diff for this revision Revisions of this file
uuid.c Show annotated file Show diff for this revision Revisions of this file
diff -r 81866008bba4 -r 6bb8b9a66642 azure_c_shared_utility/singlylinkedlist.h
--- a/azure_c_shared_utility/singlylinkedlist.h	Tue Sep 11 11:15:08 2018 -0700
+++ b/azure_c_shared_utility/singlylinkedlist.h	Thu Oct 04 09:17:16 2018 -0700
@@ -41,6 +41,7 @@
 MOCKABLE_FUNCTION(, SINGLYLINKEDLIST_HANDLE, singlylinkedlist_create);
 MOCKABLE_FUNCTION(, void, singlylinkedlist_destroy, SINGLYLINKEDLIST_HANDLE, list);
 MOCKABLE_FUNCTION(, LIST_ITEM_HANDLE, singlylinkedlist_add, SINGLYLINKEDLIST_HANDLE, list, const void*, item);
+MOCKABLE_FUNCTION(, LIST_ITEM_HANDLE, singlylinkedlist_add_head, SINGLYLINKEDLIST_HANDLE, list, const void*, item);
 MOCKABLE_FUNCTION(, int, singlylinkedlist_remove, SINGLYLINKEDLIST_HANDLE, list, LIST_ITEM_HANDLE, item_handle);
 MOCKABLE_FUNCTION(, LIST_ITEM_HANDLE, singlylinkedlist_get_head_item, SINGLYLINKEDLIST_HANDLE, list);
 MOCKABLE_FUNCTION(, LIST_ITEM_HANDLE, singlylinkedlist_get_next_item, LIST_ITEM_HANDLE, item_handle);
diff -r 81866008bba4 -r 6bb8b9a66642 azure_c_shared_utility/uuid.h
--- a/azure_c_shared_utility/uuid.h	Tue Sep 11 11:15:08 2018 -0700
+++ b/azure_c_shared_utility/uuid.h	Thu Oct 04 09:17:16 2018 -0700
@@ -43,7 +43,7 @@
 *  @param uuid          Sequence of bytes representing an UUID.
 *  @returns             A null-terminated string representation of the UUID value provided (e.g., "7f907d75-5e13-44cf-a1a3-19a01a2b4528").
 */
-MOCKABLE_FUNCTION(, char*, UUID_to_string, UUID_T*, uuid);
+MOCKABLE_FUNCTION(, char*, UUID_to_string, const UUID_T*, uuid);
 
 #ifdef __cplusplus
 }
diff -r 81866008bba4 -r 6bb8b9a66642 httpapi_compact.c
--- a/httpapi_compact.c	Tue Sep 11 11:15:08 2018 -0700
+++ b/httpapi_compact.c	Thu Oct 04 09:17:16 2018 -0700
@@ -309,11 +309,13 @@
             xio_destroy(http_instance->xio_handle);
         }
 
+#ifndef DO_NOT_COPY_TRUSTED_CERTS_STRING
         /*Codes_SRS_HTTPAPI_COMPACT_21_018: [ If there is a certificate associated to this connection, the HTTPAPI_CloseConnection shall free all allocated memory for the certificate. ]*/
         if (http_instance->certificate)
         {
             free(http_instance->certificate);
         }
+#endif
 
         /*Codes_SRS_HTTPAPI_COMPACT_06_001: [ If there is a x509 client certificate associated to this connection, the HTTAPI_CloseConnection shall free all allocated memory for the certificate. ]*/
         if (http_instance->x509ClientCertificate)
@@ -703,7 +705,7 @@
 
         /*Codes_SRS_HTTPAPI_COMPACT_21_022: [ If a Certificate was provided, the HTTPAPI_ExecuteRequest shall set this option on the transport layer. ]*/
         if ((http_instance->certificate != NULL) &&
-            (xio_setoption(http_instance->xio_handle, "TrustedCerts", http_instance->certificate) != 0))
+            (xio_setoption(http_instance->xio_handle, OPTION_TRUSTED_CERT, http_instance->certificate) != 0))
         {
             /*Codes_SRS_HTTPAPI_COMPACT_21_023: [ If the transport failed setting the Certificate, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ]*/
             result = HTTPAPI_SET_OPTION_FAILED;
@@ -814,8 +816,8 @@
     return result;
 }
 
-/*Codes_SRS_HTTPAPI_COMPACT_21_035: [ The HTTPAPI_ExecuteRequest shall execute resquest for types `GET`, `POST`, `PUT`, `DELETE`, `PATCH`. ]*/
-const char httpapiRequestString[5][7] = { "GET", "POST", "PUT", "DELETE", "PATCH" };
+/*Codes_SRS_HTTPAPI_COMPACT_21_035: [ The HTTPAPI_ExecuteRequest shall execute resquest for types `GET`, `POST`, `PUT`, `DELETE`, `PATCH`, `HEAD`. ]*/
+const char httpapiRequestString[6][7] = { "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD" };
 const char* get_request_type(HTTPAPI_REQUEST_TYPE requestType)
 {
     return (const char*)httpapiRequestString[requestType];
@@ -1158,7 +1160,8 @@
         (requestType == HTTPAPI_REQUEST_POST) ||
         (requestType == HTTPAPI_REQUEST_PUT) ||
         (requestType == HTTPAPI_REQUEST_DELETE) ||
-        (requestType == HTTPAPI_REQUEST_PATCH))
+        (requestType == HTTPAPI_REQUEST_PATCH) ||
+        (requestType == HTTPAPI_REQUEST_HEAD))
     {
         result = true;
     }
@@ -1227,15 +1230,18 @@
     {
         LogError("Receive content information from HTTP failed (result = %s)", ENUM_TO_STRING(HTTPAPI_RESULT, result));
     }
-    /*Codes_SRS_HTTPAPI_COMPACT_21_075: [ The message received by the HTTPAPI_ExecuteRequest can contain a body with the message content. ]*/
-    else if ((result = ReadHTTPResponseBodyFromXIO(http_instance, bodyLength, chunked, responseContent)) != HTTPAPI_OK)
+    /*Codes_SRS_HTTPAPI_COMPACT_42_084: [ The message received by the HTTPAPI_ExecuteRequest should not contain http body. ]*/
+    else if (requestType != HTTPAPI_REQUEST_HEAD)
     {
-        LogError("Read HTTP response body from HTTP failed (result = %s)", ENUM_TO_STRING(HTTPAPI_RESULT, result));
+        /*Codes_SRS_HTTPAPI_COMPACT_21_075: [ The message received by the HTTPAPI_ExecuteRequest can contain a body with the message content. ]*/
+        if ((result = ReadHTTPResponseBodyFromXIO(http_instance, bodyLength, chunked, responseContent)) != HTTPAPI_OK)
+        {
+            LogError("Read HTTP response body from HTTP failed (result = %s)", ENUM_TO_STRING(HTTPAPI_RESULT, result));
+        }
     }
 
     conn_receive_discard_buffer(http_instance);
 
-
     return result;
 }
 
@@ -1258,8 +1264,12 @@
         /*Codes_SRS_HTTPAPI_COMPACT_21_061: [ If the value is NULL, the HTTPAPI_SetOption shall return HTTPAPI_INVALID_ARG. ]*/
         result = HTTPAPI_INVALID_ARG;
     }
-    else if (strcmp("TrustedCerts", optionName) == 0)
+    else if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
     {
+#ifdef DO_NOT_COPY_TRUSTED_CERTS_STRING
+        result = HTTPAPI_OK;
+        http_instance->certificate = (char*)value;
+#else
         int len;
 
         if (http_instance->certificate)
@@ -1281,6 +1291,7 @@
             (void)strcpy(http_instance->certificate, (const char*)value);
             result = HTTPAPI_OK;
         }
+#endif // DO_NOT_COPY_TRUSTED_CERTS_STRING
     }
     else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0)
     {
@@ -1356,8 +1367,12 @@
         /*Codes_SRS_HTTPAPI_COMPACT_21_069: [ If the savedValue is NULL, the HTTPAPI_CloneOption shall return HTTPAPI_INVALID_ARG. ]*/
         result = HTTPAPI_INVALID_ARG;
     }
-    else if (strcmp("TrustedCerts", optionName) == 0)
+    else if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
     {
+#ifdef DO_NOT_COPY_TRUSTED_CERTS_STRING
+        *savedValue = (const void*)value;
+        result = HTTPAPI_OK;
+#else
         certLen = strlen((const char*)value);
         tempCert = (char*)malloc((certLen + 1) * sizeof(char));
         if (tempCert == NULL)
@@ -1372,6 +1387,7 @@
             *savedValue = tempCert;
             result = HTTPAPI_OK;
         }
+#endif // DO_NOT_COPY_TRUSTED_CERTS_STRING
     }
     else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0)
     {
diff -r 81866008bba4 -r 6bb8b9a66642 singlylinkedlist.c
--- a/singlylinkedlist.c	Tue Sep 11 11:15:08 2018 -0700
+++ b/singlylinkedlist.c	Thu Oct 04 09:17:16 2018 -0700
@@ -73,7 +73,7 @@
         if (result == NULL)
         {
             /* Codes_SRS_LIST_01_007: [If allocating the new list node fails, singlylinkedlist_add shall return NULL.] */
-            result = NULL;
+            /*return as is*/
         }
         else
         {
@@ -365,3 +365,45 @@
 
     return result;
 }
+
+LIST_ITEM_HANDLE singlylinkedlist_add_head(SINGLYLINKEDLIST_HANDLE list, const void* item)
+{
+    LIST_ITEM_HANDLE result;
+
+    /* Codes_SRS_LIST_02_001: [ If list is NULL then singlylinkedlist_add_head shall fail and return NULL. ]*/
+    if (list == NULL)
+    {
+        LogError("Invalid argument SINGLYLINKEDLIST_HANDLE list=%p", list);
+        result = NULL;
+    }
+    else
+    {
+        result = malloc(sizeof(LIST_ITEM_INSTANCE));
+
+        if (result == NULL)
+        {
+            /*Codes_SRS_LIST_02_003: [ If there are any failures then singlylinkedlist_add_head shall fail and return NULL. ]*/
+            LogError("failure in malloc");
+            /*return as is*/
+        }
+        else
+        {
+            /*Codes_SRS_LIST_02_002: [ singlylinkedlist_add_head shall insert item at head, succeed and return a non-NULL value. ]*/
+            result->item = item;
+            if (list->head == NULL)
+            {
+                result->next = NULL;
+                list->head = result;
+                list->tail = result;
+                
+            }
+            else
+            {
+                result->next = list->head;
+                list->head = result;
+            }
+        }
+    }
+
+    return result;
+}
diff -r 81866008bba4 -r 6bb8b9a66642 urlencode.c
--- a/urlencode.c	Tue Sep 11 11:15:08 2018 -0700
+++ b/urlencode.c	Thu Oct 04 09:17:16 2018 -0700
@@ -218,6 +218,47 @@
     return size;
 }
 
+static STRING_HANDLE encode_url_data(const char* text)
+{
+    STRING_HANDLE result;
+    size_t lengthOfResult = 0;
+    char* encodedURL;
+    unsigned char currentUnsignedChar;
+    const char* iterator = text;
+
+    /*Codes_SRS_URL_ENCODE_06_003: [If input is a zero length string then URL_Encode will return a zero length string.]*/
+    do
+    {
+        currentUnsignedChar = (unsigned char)(*iterator++);
+        lengthOfResult += URL_PrintableCharSize(currentUnsignedChar);
+    } while (currentUnsignedChar != 0);
+
+    if ((encodedURL = (char*)malloc(lengthOfResult)) == NULL)
+    {
+        /*Codes_SRS_URL_ENCODE_06_002: [If an error occurs during the encoding of input then URL_Encode will return NULL.]*/
+        result = NULL;
+        LogError("URL_Encode:: MALLOC failure on encode.");
+    }
+    else
+    {
+        size_t currentEncodePosition = 0;
+        iterator = text;;
+        do
+        {
+            currentUnsignedChar = (unsigned char)(*iterator++);
+            currentEncodePosition += URL_PrintableChar(currentUnsignedChar, &encodedURL[currentEncodePosition]);
+        } while (currentUnsignedChar != 0);
+
+        result = STRING_new_with_memory(encodedURL);
+        if (result == NULL)
+        {
+            LogError("URL_Encode:: MALLOC failure on encode.");
+            free(encodedURL);
+        }
+    }
+    return result;
+}
+
 STRING_HANDLE URL_EncodeString(const char* textEncode)
 {
     STRING_HANDLE result;
@@ -227,16 +268,7 @@
     }
     else
     {
-        STRING_HANDLE tempString = STRING_construct(textEncode);
-        if (tempString == NULL)
-        {
-            result = NULL;
-        }
-        else
-        {
-            result = URL_Encode(tempString);
-            STRING_delete(tempString);
-        }
+        result = encode_url_data(textEncode);
     }
     return result;
 }
@@ -252,40 +284,7 @@
     }
     else
     {
-        size_t lengthOfResult = 0;
-        char* encodedURL;
-        const char* currentInput;
-        unsigned char currentUnsignedChar;
-        currentInput = STRING_c_str(input);
-        /*Codes_SRS_URL_ENCODE_06_003: [If input is a zero length string then URL_Encode will return a zero length string.]*/
-        do
-        {
-            currentUnsignedChar = (unsigned char)(*currentInput++);
-            lengthOfResult += URL_PrintableCharSize(currentUnsignedChar);
-        } while (currentUnsignedChar != 0);
-        if ((encodedURL = (char*)malloc(lengthOfResult)) == NULL)
-        {
-            /*Codes_SRS_URL_ENCODE_06_002: [If an error occurs during the encoding of input then URL_Encode will return NULL.]*/
-            result = NULL;
-            LogError("URL_Encode:: MALLOC failure on encode.");
-        }
-        else
-        {
-            size_t currentEncodePosition = 0;
-            currentInput = STRING_c_str(input);
-            do
-            {
-                currentUnsignedChar = (unsigned char)(*currentInput++);
-                currentEncodePosition += URL_PrintableChar(currentUnsignedChar, &encodedURL[currentEncodePosition]);
-            } while (currentUnsignedChar != 0);
-
-            result = STRING_new_with_memory(encodedURL);
-            if (result == NULL)
-            {
-                LogError("URL_Encode:: MALLOC failure on encode.");
-                free(encodedURL);
-            }
-        }
+        result = encode_url_data(STRING_c_str(input));
     }
     return result;
 }
diff -r 81866008bba4 -r 6bb8b9a66642 uuid.c
--- a/uuid.c	Tue Sep 11 11:15:08 2018 -0700
+++ b/uuid.c	Thu Oct 04 09:17:16 2018 -0700
@@ -75,7 +75,7 @@
     return result;
 }
 
-char* UUID_to_string(UUID_T* uuid)
+char* UUID_to_string(const UUID_T* uuid)
 {
     char* result;