- Timestamp:
- Jun 27, 2021, 10:17:43 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/adapters/httpapi_compact.c
r464 r471 7 7 #include <string.h> 8 8 #include <limits.h> 9 10 #include "azure_macro_utils/macro_utils.h"11 9 #include "azure_c_shared_utility/gballoc.h" 12 10 #include "azure_c_shared_utility/httpheaders.h" … … 51 49 char* x509ClientCertificate; 52 50 char* x509ClientPrivateKey; 51 const char* proxy_host; 52 int proxy_port; 53 const char* proxy_username; 54 const char* proxy_password; 53 55 XIO_HANDLE xio_handle; 54 56 size_t received_bytes_count; … … 201 203 { 202 204 HTTP_HANDLE_DATA* http_instance; 203 TLSIO_CONFIG tlsio_config;204 205 205 206 if (hostName == NULL) … … 223 224 LogError("There is no memory to control the http connection"); 224 225 } 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"); 228 229 free(http_instance); 229 230 http_instance = NULL; … … 231 232 else 232 233 { 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; 258 246 } 259 247 } … … 321 309 } 322 310 311 if (http_instance->hostName != NULL) 312 { 313 free((void*)http_instance->hostName); 314 } 315 323 316 #ifndef DO_NOT_COPY_TRUSTED_CERTS_STRING 324 317 /*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 334 } 342 335 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); 346 347 } 347 348 … … 711 712 { 712 713 HTTPAPI_RESULT result; 714 TLSIO_CONFIG tlsio_config; 715 HTTP_PROXY_IO_CONFIG http_proxy_io_config; 713 716 714 717 if (http_instance->is_connected != 0) … … 720 723 { 721 724 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 } 723 757 /*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) && 725 759 (xio_setoption(http_instance->xio_handle, OPTION_TRUSTED_CERT, http_instance->certificate) != 0)) 726 760 { … … 740 774 (xio_setoption(http_instance->xio_handle, SU_OPTION_X509_PRIVATE_KEY, http_instance->x509ClientPrivateKey) != 0)) 741 775 { 742 743 776 /*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. ] */ 744 777 result = HTTPAPI_SET_OPTION_FAILED; … … 764 797 { 765 798 xio_dowork(http_instance->xio_handle); 766 LogInfo("Waiting for TLS connection");799 //LogInfo("Waiting for TLS connection"); 767 800 if ((countRetry--) < 0) 768 801 { … … 838 871 const char* get_request_type(HTTPAPI_REQUEST_TYPE requestType) 839 872 { 840 return (const char*)httpapiRequestString[requestType - HTTPAPI_REQUEST_GET];873 return (const char*)httpapiRequestString[requestType]; 841 874 } 842 875 … … 1220 1253 { 1221 1254 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)); 1223 1256 } 1224 1257 /*Codes_SRS_HTTPAPI_COMPACT_21_024: [ The HTTPAPI_ExecuteRequest shall open the transport connection with the host to send the request. ]*/ 1225 1258 else if ((result = OpenXIOConnection(http_instance)) != HTTPAPI_OK) 1226 1259 { 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)); 1228 1261 } 1229 1262 /*Codes_SRS_HTTPAPI_COMPACT_21_026: [ If the open process succeed, the HTTPAPI_ExecuteRequest shall send the request message to the host. ]*/ 1230 1263 else if ((result = SendHeadsToXIO(http_instance, requestType, relativePath, httpHeadersHandle, headersCount)) != HTTPAPI_OK) 1231 1264 { 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)); 1233 1266 } 1234 1267 /*Codes_SRS_HTTPAPI_COMPACT_21_042: [ The request can contain the a content message, provided in content parameter. ]*/ 1235 1268 else if ((result = SendContentToXIO(http_instance, content, contentLength)) != HTTPAPI_OK) 1236 1269 { 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)); 1238 1271 } 1239 1272 /*Codes_SRS_HTTPAPI_COMPACT_21_030: [ At the end of the transmission, the HTTPAPI_ExecuteRequest shall receive the response from the host. ]*/ … … 1241 1274 else if ((result = ReceiveHeaderFromXIO(http_instance, statusCode)) != HTTPAPI_OK) 1242 1275 { 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)); 1244 1277 } 1245 1278 /*Codes_SRS_HTTPAPI_COMPACT_21_074: [ After the header, the message received by the HTTPAPI_ExecuteRequest can contain addition information about the content. ]*/ 1246 1279 else if ((result = ReceiveContentInfoFromXIO(http_instance, responseHeadersHandle, &bodyLength, &chunked)) != HTTPAPI_OK) 1247 1280 { 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)); 1249 1282 } 1250 1283 /*Codes_SRS_HTTPAPI_COMPACT_42_084: [ The message received by the HTTPAPI_ExecuteRequest should not contain http body. ]*/ … … 1254 1287 if ((result = ReadHTTPResponseBodyFromXIO(http_instance, bodyLength, chunked, responseContent)) != HTTPAPI_OK) 1255 1288 { 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)); 1257 1290 } 1258 1291 } … … 1311 1344 #endif // DO_NOT_COPY_TRUSTED_CERTS_STRING 1312 1345 } 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) 1314 1347 { 1315 1348 int len; … … 1334 1367 } 1335 1368 } 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) 1337 1370 { 1338 1371 int len; … … 1359 1392 else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0) 1360 1393 { 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); 1368 1398 result = HTTPAPI_ERROR; 1369 1399 } 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 }1376 1400 else 1377 1401 { 1378 1379 /* Workaround: xio interface is already created when HTTPAPI_CreateConnection is call without proxy support1380 * need to destroy the interface and create a new one with proxy information1381 */1382 OPTIONHANDLER_HANDLE xio_options;1383 if ((xio_options = xio_retrieveoptions(http_instance->xio_handle)) == NULL)1384 { 1385 LogError("fail ed 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"); 1386 1410 result = HTTPAPI_ERROR; 1387 1411 } 1388 1412 else 1389 1413 { 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 } 1410 1451 } 1411 1452 else 1412 1453 { 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 } 1425 1456 } 1426 1457 } … … 1432 1463 LogInfo("unknown option %s", optionName); 1433 1464 } 1434 1435 1465 return result; 1436 1466 } … … 1477 1507 #endif // DO_NOT_COPY_TRUSTED_CERTS_STRING 1478 1508 } 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) 1480 1510 { 1481 1511 certLen = strlen((const char*)value); … … 1494 1524 } 1495 1525 } 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) 1497 1527 { 1498 1528 certLen = strlen((const char*)value);
Note:
See TracChangeset
for help on using the changeset viewer.