Ignore:
Timestamp:
Jun 27, 2021, 10:17:43 PM (3 years ago)
Author:
coas-nagasima
Message:

バージョンを上げたときに失われたAzure IoT SDKの変更部分を適用

File:
1 edited

Legend:

Unmodified
Added
Removed
  • azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/httpapi_compact.c

    r464 r471  
    77#include <string.h>
    88#include <limits.h>
    9 
    10 #include "azure_macro_utils/macro_utils.h"
    119#include "azure_c_shared_utility/gballoc.h"
    1210#include "azure_c_shared_utility/httpheaders.h"
     
    5149    char*           x509ClientCertificate;
    5250    char*           x509ClientPrivateKey;
     51    const char*     proxy_host;
     52    int             proxy_port;
     53    const char*     proxy_username;
     54    const char*     proxy_password;
    5355    XIO_HANDLE      xio_handle;
    5456    size_t          received_bytes_count;
     
    201203{
    202204    HTTP_HANDLE_DATA* http_instance;
    203     TLSIO_CONFIG tlsio_config;
    204205
    205206    if (hostName == NULL)
     
    223224            LogError("There is no memory to control the http connection");
    224225        }
    225         else if (mallocAndStrcpy_s(&http_instance->hostName, hostName) != 0)
    226         {
    227             LogError("Failed copying hostname");
     226        else if(mallocAndStrcpy_s((char**)&(http_instance->hostName), hostName) != 0)
     227        {
     228            LogError("failure allocate host name");
    228229            free(http_instance);
    229230            http_instance = NULL;
     
    231232        else
    232233        {
    233             tlsio_config.hostname = http_instance->hostName;
    234             tlsio_config.port = 443;
    235             tlsio_config.underlying_io_interface = NULL;
    236             tlsio_config.underlying_io_parameters = NULL;
    237 
    238             http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config);
    239 
    240             /*Codes_SRS_HTTPAPI_COMPACT_21_016: [ If the HTTPAPI_CreateConnection failed to create the connection, it shall return NULL as the handle. ]*/
    241             if (http_instance->xio_handle == NULL)
    242             {
    243                 LogError("Create connection failed");
    244                 free(http_instance->hostName);
    245                 free(http_instance);
    246                 http_instance = NULL;
    247             }
    248             else
    249             {
    250                 http_instance->is_connected = 0;
    251                 http_instance->is_io_error = 0;
    252                 http_instance->received_bytes_count = 0;
    253                 http_instance->received_bytes = NULL;
    254                 http_instance->certificate = NULL;
    255                 http_instance->x509ClientCertificate = NULL;
    256                 http_instance->x509ClientPrivateKey = NULL;
    257             }
     234            http_instance->xio_handle = NULL;
     235            http_instance->is_connected = 0;
     236            http_instance->is_io_error = 0;
     237            http_instance->received_bytes_count = 0;
     238            http_instance->received_bytes = NULL;
     239            http_instance->certificate = NULL;
     240            http_instance->x509ClientCertificate = NULL;
     241            http_instance->x509ClientPrivateKey = NULL;
     242            http_instance->proxy_host = NULL;
     243            http_instance->proxy_port = 0;
     244            http_instance->proxy_username = NULL;
     245            http_instance->proxy_password = NULL;
    258246        }
    259247    }
     
    321309        }
    322310
     311        if (http_instance->hostName != NULL)
     312        {
     313            free((void*)http_instance->hostName);
     314        }
     315
    323316#ifndef DO_NOT_COPY_TRUSTED_CERTS_STRING
    324317        /*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. ]*/
     
    341334        }
    342335
    343         if (http_instance->hostName)
    344         {
    345             free(http_instance->hostName);
     336        if (http_instance->proxy_host != NULL)
     337        {
     338            free((void*)http_instance->proxy_host);
     339        }
     340        if (http_instance->proxy_username != NULL)
     341        {
     342            free((void*)http_instance->proxy_username);
     343        }
     344        if (http_instance->proxy_password != NULL)
     345        {
     346            free((void*)http_instance->proxy_password);
    346347        }
    347348
     
    711712{
    712713    HTTPAPI_RESULT result;
     714    TLSIO_CONFIG tlsio_config;
     715    HTTP_PROXY_IO_CONFIG http_proxy_io_config;
    713716
    714717    if (http_instance->is_connected != 0)
     
    720723    {
    721724        http_instance->is_io_error = 0;
    722 
     725        tlsio_config.hostname = http_instance->hostName;
     726        tlsio_config.port = 443;
     727
     728        if (http_instance->proxy_host != NULL) {
     729            tlsio_config.underlying_io_interface = http_proxy_io_get_interface_description();
     730        }
     731        else {
     732            tlsio_config.underlying_io_interface = NULL;
     733        }
     734
     735        if (tlsio_config.underlying_io_interface != NULL) {
     736            tlsio_config.underlying_io_parameters = (void*)&http_proxy_io_config;
     737
     738            http_proxy_io_config.proxy_hostname = http_instance->proxy_host;
     739            http_proxy_io_config.proxy_port = http_instance->proxy_port;
     740            http_proxy_io_config.username = http_instance->proxy_username;
     741            http_proxy_io_config.password = http_instance->proxy_password;
     742            http_proxy_io_config.hostname = http_instance->hostName;
     743            http_proxy_io_config.port = 443;
     744        }
     745        else {
     746            tlsio_config.underlying_io_parameters = NULL;
     747        }
     748
     749        http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config);
     750
     751        if (http_instance->xio_handle == NULL)
     752        {
     753            LogError("Create connection failed");
     754            free(http_instance);
     755            http_instance = NULL;
     756        }
    723757        /*Codes_SRS_HTTPAPI_COMPACT_21_022: [ If a Certificate was provided, the HTTPAPI_ExecuteRequest shall set this option on the transport layer. ]*/
    724         if ((http_instance->certificate != NULL) &&
     758        else if ((http_instance->certificate != NULL) &&
    725759            (xio_setoption(http_instance->xio_handle, OPTION_TRUSTED_CERT, http_instance->certificate) != 0))
    726760        {
     
    740774            (xio_setoption(http_instance->xio_handle, SU_OPTION_X509_PRIVATE_KEY, http_instance->x509ClientPrivateKey) != 0))
    741775        {
    742 
    743776            /*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. ] */
    744777            result = HTTPAPI_SET_OPTION_FAILED;
     
    764797                {
    765798                    xio_dowork(http_instance->xio_handle);
    766                     LogInfo("Waiting for TLS connection");
     799                    //LogInfo("Waiting for TLS connection");
    767800                    if ((countRetry--) < 0)
    768801                    {
     
    838871const char* get_request_type(HTTPAPI_REQUEST_TYPE requestType)
    839872{
    840     return (const char*)httpapiRequestString[requestType - HTTPAPI_REQUEST_GET];
     873    return (const char*)httpapiRequestString[requestType];
    841874}
    842875
     
    12201253    {
    12211254        result = HTTPAPI_INVALID_ARG;
    1222         LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
     1255        LogError("(result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
    12231256    }
    12241257    /*Codes_SRS_HTTPAPI_COMPACT_21_024: [ The HTTPAPI_ExecuteRequest shall open the transport connection with the host to send the request. ]*/
    12251258    else if ((result = OpenXIOConnection(http_instance)) != HTTPAPI_OK)
    12261259    {
    1227         LogError("Open HTTP connection failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
     1260        LogError("Open HTTP connection failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
    12281261    }
    12291262    /*Codes_SRS_HTTPAPI_COMPACT_21_026: [ If the open process succeed, the HTTPAPI_ExecuteRequest shall send the request message to the host. ]*/
    12301263    else if ((result = SendHeadsToXIO(http_instance, requestType, relativePath, httpHeadersHandle, headersCount)) != HTTPAPI_OK)
    12311264    {
    1232         LogError("Send heads to HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
     1265        LogError("Send heads to HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
    12331266    }
    12341267    /*Codes_SRS_HTTPAPI_COMPACT_21_042: [ The request can contain the a content message, provided in content parameter. ]*/
    12351268    else if ((result = SendContentToXIO(http_instance, content, contentLength)) != HTTPAPI_OK)
    12361269    {
    1237         LogError("Send content to HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
     1270        LogError("Send content to HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
    12381271    }
    12391272    /*Codes_SRS_HTTPAPI_COMPACT_21_030: [ At the end of the transmission, the HTTPAPI_ExecuteRequest shall receive the response from the host. ]*/
     
    12411274    else if ((result = ReceiveHeaderFromXIO(http_instance, statusCode)) != HTTPAPI_OK)
    12421275    {
    1243         LogError("Receive header from HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
     1276        LogError("Receive header from HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
    12441277    }
    12451278    /*Codes_SRS_HTTPAPI_COMPACT_21_074: [ After the header, the message received by the HTTPAPI_ExecuteRequest can contain addition information about the content. ]*/
    12461279    else if ((result = ReceiveContentInfoFromXIO(http_instance, responseHeadersHandle, &bodyLength, &chunked)) != HTTPAPI_OK)
    12471280    {
    1248         LogError("Receive content information from HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
     1281        LogError("Receive content information from HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
    12491282    }
    12501283    /*Codes_SRS_HTTPAPI_COMPACT_42_084: [ The message received by the HTTPAPI_ExecuteRequest should not contain http body. ]*/
     
    12541287        if ((result = ReadHTTPResponseBodyFromXIO(http_instance, bodyLength, chunked, responseContent)) != HTTPAPI_OK)
    12551288        {
    1256             LogError("Read HTTP response body from HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
     1289            LogError("Read HTTP response body from HTTP failed (result = %s)", MU_ENUM_TO_STRING(HTTPAPI_RESULT, result));
    12571290        }
    12581291    }
     
    13111344#endif // DO_NOT_COPY_TRUSTED_CERTS_STRING
    13121345    }
    1313     else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
     1346    else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0)
    13141347    {
    13151348        int len;
     
    13341367        }
    13351368    }
    1336     else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
     1369    else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0)
    13371370    {
    13381371        int len;
     
    13591392    else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0)
    13601393    {
    1361         TLSIO_CONFIG tlsio_config;
    1362         HTTP_PROXY_IO_CONFIG proxy_config;
    1363         HTTP_PROXY_OPTIONS* proxy_options = (HTTP_PROXY_OPTIONS*)value;
    1364 
    1365         if (proxy_options->host_address == NULL)
    1366         {
    1367             LogError("NULL host_address in proxy options");
     1394        HTTP_PROXY_OPTIONS* proxy_data = (HTTP_PROXY_OPTIONS*)value;
     1395        if (proxy_data->host_address == NULL || proxy_data->port <= 0)
     1396        {
     1397            LogError("invalid proxy_data values ( host_address = %p, port = %d)", proxy_data->host_address, proxy_data->port);
    13681398            result = HTTPAPI_ERROR;
    13691399        }
    1370         else if (((proxy_options->username == NULL) || (proxy_options->password == NULL)) &&
    1371                 (proxy_options->username != proxy_options->password))
    1372         {
    1373             LogError("Only one of username and password for proxy settings was NULL");
    1374             result = HTTPAPI_ERROR;
    1375         }
    13761400        else
    13771401        {
    1378 
    1379             /* Workaround: xio interface is already created when HTTPAPI_CreateConnection is call without proxy support
    1380              * need to destroy the interface and create a new one with proxy information
    1381              */
    1382             OPTIONHANDLER_HANDLE xio_options;
    1383             if ((xio_options = xio_retrieveoptions(http_instance->xio_handle)) == NULL)
    1384             {
    1385                 LogError("failed saving underlying I/O transport options");
     1402            if(http_instance->proxy_host != NULL)
     1403            {
     1404                free((void*)http_instance->proxy_host);
     1405                http_instance->proxy_host = NULL;
     1406            }
     1407            if(mallocAndStrcpy_s((char**)&(http_instance->proxy_host), (const char*)proxy_data->host_address) != 0)
     1408            {
     1409                LogError("failure allocate proxy host");
    13861410                result = HTTPAPI_ERROR;
    13871411            }
    13881412            else
    13891413            {
    1390                 xio_destroy(http_instance->xio_handle);
    1391 
    1392                 proxy_config.hostname = http_instance->hostName;
    1393                 proxy_config.proxy_hostname = proxy_options->host_address;
    1394                 proxy_config.password = proxy_options->password;
    1395                 proxy_config.username = proxy_options->username;
    1396                 proxy_config.proxy_port = proxy_options->port;
    1397                 proxy_config.port = 443;
    1398 
    1399                 tlsio_config.hostname = http_instance->hostName;
    1400                 tlsio_config.port = 443;
    1401                 tlsio_config.underlying_io_interface =  http_proxy_io_get_interface_description();
    1402                 tlsio_config.underlying_io_parameters = &proxy_config;
    1403 
    1404                 http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config);
    1405 
    1406                 if (http_instance->xio_handle == NULL)
    1407                 {
    1408                     LogError("Failed to create xio handle with proxy configuration");
    1409                     result = HTTPAPI_ERROR;
     1414                http_instance->proxy_port = proxy_data->port;
     1415
     1416                if (proxy_data->username != NULL && proxy_data->password != NULL)
     1417                {
     1418                    if(http_instance->proxy_username != NULL)
     1419                    {
     1420                        free((void*)http_instance->proxy_username);
     1421                        http_instance->proxy_username = NULL;
     1422                    }
     1423                    if(mallocAndStrcpy_s((char**)&(http_instance->proxy_username), (const char*)proxy_data->username) != 0)
     1424                    {
     1425                        LogError("failure allocate proxy username");
     1426                        free((void*)http_instance->proxy_host);
     1427                        http_instance->proxy_host = NULL;
     1428                        result = HTTPAPI_ERROR;
     1429                    }
     1430                    else
     1431                    {
     1432                        if(http_instance->proxy_password != NULL)
     1433                        {
     1434                          free((void*)http_instance->proxy_password);
     1435                          http_instance->proxy_password = NULL;
     1436                        }
     1437                        if(mallocAndStrcpy_s((char**)&(http_instance->proxy_password), (const char*)proxy_data->password) != 0)
     1438                        {
     1439                            LogError("failure allocate proxy password");
     1440                            free((void*)http_instance->proxy_host);
     1441                            http_instance->proxy_host = NULL;
     1442                            free((void*)http_instance->proxy_username);
     1443                            http_instance->proxy_username = NULL;
     1444                            result = HTTPAPI_ERROR;
     1445                        }
     1446                        else
     1447                        {
     1448                          result = HTTPAPI_OK;
     1449                        }
     1450                    }
    14101451                }
    14111452                else
    14121453                {
    1413                     if (OptionHandler_FeedOptions(xio_options, http_instance->xio_handle) != OPTIONHANDLER_OK)
    1414                     {
    1415                         LogError("Failed feeding existing options to new xio instance.");
    1416                         result = HTTPAPI_ERROR;
    1417                     }
    1418                     else
    1419                     {
    1420                         result = HTTPAPI_OK;
    1421                     }
    1422                 }
    1423 
    1424                 OptionHandler_Destroy(xio_options);
     1454                    result = HTTPAPI_OK;
     1455                }
    14251456            }
    14261457        }
     
    14321463        LogInfo("unknown option %s", optionName);
    14331464    }
    1434 
    14351465    return result;
    14361466}
     
    14771507#endif // DO_NOT_COPY_TRUSTED_CERTS_STRING
    14781508    }
    1479     else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
     1509    else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0)
    14801510    {
    14811511        certLen = strlen((const char*)value);
     
    14941524        }
    14951525    }
    1496     else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
     1526    else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0)
    14971527    {
    14981528        certLen = strlen((const char*)value);
Note: See TracChangeset for help on using the changeset viewer.