Microsoft Azure IoTHub client HTTP transport

Dependents:   iothub_client_sample_http simplesample_http temp_sensor_anomaly

This library implements the HTTP transport for Microsoft Azure IoTHub client. The code is replicated from https://github.com/Azure/azure-iot-sdks

Revision:
32:4d4a226b072b
Parent:
31:c889be99d3f7
Child:
33:270c65ccd77d
--- a/iothubtransporthttp.c	Mon May 22 10:34:45 2017 -0700
+++ b/iothubtransporthttp.c	Fri Aug 25 11:22:14 2017 -0700
@@ -25,8 +25,12 @@
 #include "azure_c_shared_utility/agenttime.h"
 
 #define IOTHUB_APP_PREFIX "iothub-app-"
-const char* IOTHUB_MESSAGE_ID = "iothub-messageid";
-const char* IOTHUB_CORRELATION_ID = "iothub-correlationid";
+static const char* IOTHUB_MESSAGE_ID = "iothub-messageid";
+static const char* IOTHUB_CORRELATION_ID = "iothub-correlationid";
+static const char* IOTHUB_CONTENT_TYPE_D2C = "iothub-contenttype";
+static const char* IOTHUB_CONTENT_ENCODING_D2C = "iothub-contentencoding";
+static const char* IOTHUB_CONTENT_TYPE_C2D = "ContentType";
+static const char* IOTHUB_CONTENT_ENCODING_C2D = "ContentEncoding";
 
 #define CONTENT_TYPE "Content-Type"
 #define APPLICATION_OCTET_STREAM "application/octet-stream"
@@ -1514,7 +1518,7 @@
                             size_t count;
                             if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK)
                             {
-                                /*Codes_SRS_TRANSPORTMULTITHTTP_17_078: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
+                                /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
                                 LogError("unable to Map_GetInternals");
                             }
                             else
@@ -1523,6 +1527,8 @@
                                 bool goOn = true;
                                 const char* msgId;
                                 const char* corrId;
+                                const char* userDefinedContentType;
+                                const char* contentEncoding;
 
                                 for (i = 0; (i < count) && goOn; i++)
                                 {
@@ -1588,6 +1594,28 @@
                                     }
                                 }
 
+                                // Codes_SRS_TRANSPORTMULTITHTTP_09_001: [ If the IoTHubMessage being sent contains property `content-type` it shall be added to the HTTP headers as "iothub-contenttype":"value". ]  
+                                userDefinedContentType = IoTHubMessage_GetContentTypeSystemProperty(message->messageHandle);
+                                if (goOn && userDefinedContentType != NULL)
+                                {
+                                    if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, IOTHUB_CONTENT_TYPE_D2C, userDefinedContentType) != HTTP_HEADERS_OK)
+                                    {
+                                        LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair (content-type)");
+                                        goOn = false;
+                                    }
+                                }
+
+                                // Codes_SRS_TRANSPORTMULTITHTTP_09_002: [ If the IoTHubMessage being sent contains property `content-encoding` it shall be added to the HTTP headers as "iothub-contentencoding":"value". ] 
+                                contentEncoding = IoTHubMessage_GetContentEncodingSystemProperty(message->messageHandle);
+                                if (goOn && contentEncoding != NULL)
+                                {
+                                    if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, IOTHUB_CONTENT_ENCODING_D2C, contentEncoding) != HTTP_HEADERS_OK)
+                                    {
+                                        LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair (content-encoding)");
+                                        goOn = false;
+                                    }
+                                }
+
                                 if (!goOn)
                                 {
                                     /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
@@ -2172,6 +2200,37 @@
                                                             }
                                                         }
                                                     }
+                                                    // Codes_SRS_TRANSPORTMULTITHTTP_09_003: [ The HTTP header value of `ContentType` shall be set in the `IoTHubMessage_SetContentTypeSystemProperty`.  ]   
+                                                    else if (strncmp(IOTHUB_CONTENT_TYPE_C2D, completeHeader, strlen(IOTHUB_CONTENT_TYPE_C2D)) == 0)
+                                                    {
+                                                        char* whereIsColon = strchr(completeHeader, ':');
+                                                        if (whereIsColon != NULL)
+                                                        {
+                                                            *whereIsColon = '\0'; /*cut it down*/
+                                                            if (IoTHubMessage_SetContentTypeSystemProperty(receivedMessage, whereIsColon + 2) != IOTHUB_MESSAGE_OK)
+                                                            {
+                                                                LogError("Failed setting IoTHubMessage content-type");
+                                                                free(completeHeader);
+                                                                break;
+                                                            }
+                                                        }
+                                                    }
+                                                    // Codes_SRS_TRANSPORTMULTITHTTP_09_004: [ The HTTP header value of `ContentEncoding` shall be set in the `IoTHub_SetContentEncoding`.  ]   
+                                                    else if (strncmp(IOTHUB_CONTENT_ENCODING_C2D, completeHeader, strlen(IOTHUB_CONTENT_ENCODING_C2D)) == 0)
+                                                    {
+                                                        char* whereIsColon = strchr(completeHeader, ':');
+                                                        if (whereIsColon != NULL)
+                                                        {
+                                                            *whereIsColon = '\0'; /*cut it down*/
+                                                            if (IoTHubMessage_SetContentEncodingSystemProperty(receivedMessage, whereIsColon + 2) != IOTHUB_MESSAGE_OK)
+                                                            {
+                                                                LogError("Failed setting IoTHubMessage content-encoding");
+                                                                free(completeHeader);
+                                                                break;
+                                                            }
+                                                        }
+                                                    }
+
                                                     free(completeHeader);
                                                 }
                                             }