Index: azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/httpapi_compact.c
===================================================================
--- azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/httpapi_compact.c	(revision 464)
+++ azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/httpapi_compact.c	(revision 471)
@@ -7,6 +7,4 @@
 #include <string.h>
 #include <limits.h>
-
-#include "azure_macro_utils/macro_utils.h"
 #include "azure_c_shared_utility/gballoc.h"
 #include "azure_c_shared_utility/httpheaders.h"
@@ -51,4 +49,8 @@
     char*           x509ClientCertificate;
     char*           x509ClientPrivateKey;
+    const char*     proxy_host;
+    int             proxy_port;
+    const char*     proxy_username;
+    const char*     proxy_password;
     XIO_HANDLE      xio_handle;
     size_t          received_bytes_count;
@@ -201,5 +203,4 @@
 {
     HTTP_HANDLE_DATA* http_instance;
-    TLSIO_CONFIG tlsio_config;
 
     if (hostName == NULL)
@@ -223,7 +224,7 @@
             LogError("There is no memory to control the http connection");
         }
-        else if (mallocAndStrcpy_s(&http_instance->hostName, hostName) != 0)
-        {
-            LogError("Failed copying hostname");
+        else if(mallocAndStrcpy_s((char**)&(http_instance->hostName), hostName) != 0)
+        {
+            LogError("failure allocate host name");
             free(http_instance);
             http_instance = NULL;
@@ -231,29 +232,16 @@
         else
         {
-            tlsio_config.hostname = http_instance->hostName;
-            tlsio_config.port = 443;
-            tlsio_config.underlying_io_interface = NULL;
-            tlsio_config.underlying_io_parameters = NULL;
-
-            http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config);
-
-            /*Codes_SRS_HTTPAPI_COMPACT_21_016: [ If the HTTPAPI_CreateConnection failed to create the connection, it shall return NULL as the handle. ]*/
-            if (http_instance->xio_handle == NULL)
-            {
-                LogError("Create connection failed");
-                free(http_instance->hostName);
-                free(http_instance);
-                http_instance = NULL;
-            }
-            else
-            {
-                http_instance->is_connected = 0;
-                http_instance->is_io_error = 0;
-                http_instance->received_bytes_count = 0;
-                http_instance->received_bytes = NULL;
-                http_instance->certificate = NULL;
-                http_instance->x509ClientCertificate = NULL;
-                http_instance->x509ClientPrivateKey = NULL;
-            }
+            http_instance->xio_handle = NULL;
+            http_instance->is_connected = 0;
+            http_instance->is_io_error = 0;
+            http_instance->received_bytes_count = 0;
+            http_instance->received_bytes = NULL;
+            http_instance->certificate = NULL;
+            http_instance->x509ClientCertificate = NULL;
+            http_instance->x509ClientPrivateKey = NULL;
+            http_instance->proxy_host = NULL;
+            http_instance->proxy_port = 0;
+            http_instance->proxy_username = NULL;
+            http_instance->proxy_password = NULL;
         }
     }
@@ -321,4 +309,9 @@
         }
 
+        if (http_instance->hostName != NULL)
+        {
+            free((void*)http_instance->hostName);
+        }
+
 #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. ]*/
@@ -341,7 +334,15 @@
         }
 
-        if (http_instance->hostName)
-        {
-            free(http_instance->hostName);
+        if (http_instance->proxy_host != NULL)
+        {
+            free((void*)http_instance->proxy_host);
+        }
+        if (http_instance->proxy_username != NULL)
+        {
+            free((void*)http_instance->proxy_username);
+        }
+        if (http_instance->proxy_password != NULL)
+        {
+            free((void*)http_instance->proxy_password);
         }
 
@@ -711,4 +712,6 @@
 {
     HTTPAPI_RESULT result;
+    TLSIO_CONFIG tlsio_config;
+    HTTP_PROXY_IO_CONFIG http_proxy_io_config;
 
     if (http_instance->is_connected != 0)
@@ -720,7 +723,38 @@
     {
         http_instance->is_io_error = 0;
-
+        tlsio_config.hostname = http_instance->hostName;
+        tlsio_config.port = 443;
+
+        if (http_instance->proxy_host != NULL) {
+            tlsio_config.underlying_io_interface = http_proxy_io_get_interface_description();
+        }
+        else {
+            tlsio_config.underlying_io_interface = NULL;
+        }
+
+        if (tlsio_config.underlying_io_interface != NULL) {
+            tlsio_config.underlying_io_parameters = (void*)&http_proxy_io_config;
+
+            http_proxy_io_config.proxy_hostname = http_instance->proxy_host;
+            http_proxy_io_config.proxy_port = http_instance->proxy_port;
+            http_proxy_io_config.username = http_instance->proxy_username;
+            http_proxy_io_config.password = http_instance->proxy_password;
+            http_proxy_io_config.hostname = http_instance->hostName;
+            http_proxy_io_config.port = 443;
+        }
+        else {
+            tlsio_config.underlying_io_parameters = NULL;
+        }
+
+        http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config);
+
+        if (http_instance->xio_handle == NULL)
+        {
+            LogError("Create connection failed");
+            free(http_instance);
+            http_instance = NULL;
+        }
         /*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) &&
+        else if ((http_instance->certificate != NULL) &&
             (xio_setoption(http_instance->xio_handle, OPTION_TRUSTED_CERT, http_instance->certificate) != 0))
         {
@@ -740,5 +774,4 @@
             (xio_setoption(http_instance->xio_handle, SU_OPTION_X509_PRIVATE_KEY, http_instance->x509ClientPrivateKey) != 0))
         {
-
             /*Codes_SRS_HTTPAPI_COMPACT_06_006: [ If the transport failed setting the client certificate private key, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ] */
             result = HTTPAPI_SET_OPTION_FAILED;
@@ -764,5 +797,5 @@
                 {
                     xio_dowork(http_instance->xio_handle);
-                    LogInfo("Waiting for TLS connection");
+                    //LogInfo("Waiting for TLS connection");
                     if ((countRetry--) < 0)
                     {
@@ -838,5 +871,5 @@
 const char* get_request_type(HTTPAPI_REQUEST_TYPE requestType)
 {
-    return (const char*)httpapiRequestString[requestType - HTTPAPI_REQUEST_GET];
+    return (const char*)httpapiRequestString[requestType];
 }
 
@@ -1220,20 +1253,20 @@
     {
         result = HTTPAPI_INVALID_ARG;
-        LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
+        LogError("(result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
     }
     /*Codes_SRS_HTTPAPI_COMPACT_21_024: [ The HTTPAPI_ExecuteRequest shall open the transport connection with the host to send the request. ]*/
     else if ((result = OpenXIOConnection(http_instance)) != HTTPAPI_OK)
     {
-        LogError("Open HTTP connection failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
+        LogError("Open HTTP connection failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
     }
     /*Codes_SRS_HTTPAPI_COMPACT_21_026: [ If the open process succeed, the HTTPAPI_ExecuteRequest shall send the request message to the host. ]*/
     else if ((result = SendHeadsToXIO(http_instance, requestType, relativePath, httpHeadersHandle, headersCount)) != HTTPAPI_OK)
     {
-        LogError("Send heads to HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
+        LogError("Send heads to HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
     }
     /*Codes_SRS_HTTPAPI_COMPACT_21_042: [ The request can contain the a content message, provided in content parameter. ]*/
     else if ((result = SendContentToXIO(http_instance, content, contentLength)) != HTTPAPI_OK)
     {
-        LogError("Send content to HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
+        LogError("Send content to HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
     }
     /*Codes_SRS_HTTPAPI_COMPACT_21_030: [ At the end of the transmission, the HTTPAPI_ExecuteRequest shall receive the response from the host. ]*/
@@ -1241,10 +1274,10 @@
     else if ((result = ReceiveHeaderFromXIO(http_instance, statusCode)) != HTTPAPI_OK)
     {
-        LogError("Receive header from HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
+        LogError("Receive header from HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
     }
     /*Codes_SRS_HTTPAPI_COMPACT_21_074: [ After the header, the message received by the HTTPAPI_ExecuteRequest can contain addition information about the content. ]*/
     else if ((result = ReceiveContentInfoFromXIO(http_instance, responseHeadersHandle, &bodyLength, &chunked)) != HTTPAPI_OK)
     {
-        LogError("Receive content information from HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
+        LogError("Receive content information from HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
     }
     /*Codes_SRS_HTTPAPI_COMPACT_42_084: [ The message received by the HTTPAPI_ExecuteRequest should not contain http body. ]*/
@@ -1254,5 +1287,5 @@
         if ((result = ReadHTTPResponseBodyFromXIO(http_instance, bodyLength, chunked, responseContent)) != HTTPAPI_OK)
         {
-            LogError("Read HTTP response body from HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
+            LogError("Read HTTP response body from HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
         }
     }
@@ -1311,5 +1344,5 @@
 #endif // DO_NOT_COPY_TRUSTED_CERTS_STRING
     }
-    else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
+    else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0)
     {
         int len;
@@ -1334,5 +1367,5 @@
         }
     }
-    else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
+    else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0)
     {
         int len;
@@ -1359,68 +1392,66 @@
     else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0)
     {
-        TLSIO_CONFIG tlsio_config;
-        HTTP_PROXY_IO_CONFIG proxy_config;
-        HTTP_PROXY_OPTIONS* proxy_options = (HTTP_PROXY_OPTIONS*)value;
-
-        if (proxy_options->host_address == NULL)
-        {
-            LogError("NULL host_address in proxy options");
+        HTTP_PROXY_OPTIONS* proxy_data = (HTTP_PROXY_OPTIONS*)value;
+        if (proxy_data->host_address == NULL || proxy_data->port <= 0)
+        {
+            LogError("invalid proxy_data values ( host_address = %p, port = %d)", proxy_data->host_address, proxy_data->port);
             result = HTTPAPI_ERROR;
         }
-        else if (((proxy_options->username == NULL) || (proxy_options->password == NULL)) &&
-                (proxy_options->username != proxy_options->password))
-        {
-            LogError("Only one of username and password for proxy settings was NULL");
-            result = HTTPAPI_ERROR;
-        }
         else
         {
-
-            /* Workaround: xio interface is already created when HTTPAPI_CreateConnection is call without proxy support
-             * need to destroy the interface and create a new one with proxy information
-             */
-            OPTIONHANDLER_HANDLE xio_options;
-            if ((xio_options = xio_retrieveoptions(http_instance->xio_handle)) == NULL)
-            {
-                LogError("failed saving underlying I/O transport options");
+            if(http_instance->proxy_host != NULL)
+            {
+                free((void*)http_instance->proxy_host);
+                http_instance->proxy_host = NULL;
+            }
+            if(mallocAndStrcpy_s((char**)&(http_instance->proxy_host), (const char*)proxy_data->host_address) != 0)
+            {
+                LogError("failure allocate proxy host");
                 result = HTTPAPI_ERROR;
             }
             else
             {
-                xio_destroy(http_instance->xio_handle);
-
-                proxy_config.hostname = http_instance->hostName;
-                proxy_config.proxy_hostname = proxy_options->host_address;
-                proxy_config.password = proxy_options->password;
-                proxy_config.username = proxy_options->username;
-                proxy_config.proxy_port = proxy_options->port;
-                proxy_config.port = 443;
-
-                tlsio_config.hostname = http_instance->hostName;
-                tlsio_config.port = 443;
-                tlsio_config.underlying_io_interface =  http_proxy_io_get_interface_description();
-                tlsio_config.underlying_io_parameters = &proxy_config;
-
-                http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config);
-
-                if (http_instance->xio_handle == NULL)
-                {
-                    LogError("Failed to create xio handle with proxy configuration");
-                    result = HTTPAPI_ERROR;
+                http_instance->proxy_port = proxy_data->port;
+
+                if (proxy_data->username != NULL && proxy_data->password != NULL)
+                {
+                    if(http_instance->proxy_username != NULL)
+                    {
+                        free((void*)http_instance->proxy_username);
+                        http_instance->proxy_username = NULL;
+                    }
+                    if(mallocAndStrcpy_s((char**)&(http_instance->proxy_username), (const char*)proxy_data->username) != 0)
+                    {
+                        LogError("failure allocate proxy username");
+                        free((void*)http_instance->proxy_host);
+                        http_instance->proxy_host = NULL;
+                        result = HTTPAPI_ERROR;
+                    }
+                    else
+                    {
+                        if(http_instance->proxy_password != NULL)
+                        {
+                          free((void*)http_instance->proxy_password);
+                          http_instance->proxy_password = NULL;
+                        }
+                        if(mallocAndStrcpy_s((char**)&(http_instance->proxy_password), (const char*)proxy_data->password) != 0)
+                        {
+                            LogError("failure allocate proxy password");
+                            free((void*)http_instance->proxy_host);
+                            http_instance->proxy_host = NULL;
+                            free((void*)http_instance->proxy_username);
+                            http_instance->proxy_username = NULL;
+                            result = HTTPAPI_ERROR;
+                        }
+                        else
+                        {
+                          result = HTTPAPI_OK;
+                        }
+                    }
                 }
                 else
                 {
-                    if (OptionHandler_FeedOptions(xio_options, http_instance->xio_handle) != OPTIONHANDLER_OK)
-                    {
-                        LogError("Failed feeding existing options to new xio instance.");
-                        result = HTTPAPI_ERROR;
-                    }
-                    else
-                    {
-                        result = HTTPAPI_OK;
-                    }
-                }
-
-                OptionHandler_Destroy(xio_options);
+                    result = HTTPAPI_OK;
+                }
             }
         }
@@ -1432,5 +1463,4 @@
         LogInfo("unknown option %s", optionName);
     }
-
     return result;
 }
@@ -1477,5 +1507,5 @@
 #endif // DO_NOT_COPY_TRUSTED_CERTS_STRING
     }
-    else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
+    else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0)
     {
         certLen = strlen((const char*)value);
@@ -1494,5 +1524,5 @@
         }
     }
-    else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
+    else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0)
     {
         certLen = strlen((const char*)value);
