| 1 |
// Copyright (c) Microsoft. All rights reserved. |
| 2 |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <string.h> |
| 7 |
#include <stddef.h> |
| 8 |
#include <ctype.h> |
| 9 |
|
| 10 |
#include "azure_macro_utils/macro_utils.h" |
| 11 |
#include "azure_c_shared_utility/strings.h" |
| 12 |
#include "azure_c_shared_utility/httpapi.h" |
| 13 |
#include "azure_c_shared_utility/httpheaders.h" |
| 14 |
#include "azure_c_shared_utility/crt_abstractions.h" |
| 15 |
#include "curl/curl.h" |
| 16 |
#include "azure_c_shared_utility/xlogging.h" |
| 17 |
#ifdef USE_OPENSSL |
| 18 |
#include "azure_c_shared_utility/x509_openssl.h" |
| 19 |
#include "openssl/engine.h" |
| 20 |
#elif USE_WOLFSSL |
| 21 |
#define WOLFSSL_OPTIONS_IGNORE_SYS |
| 22 |
#include "wolfssl/options.h" |
| 23 |
#include "wolfssl/ssl.h" |
| 24 |
#include "wolfssl/error-ssl.h" |
| 25 |
#elif USE_MBEDTLS |
| 26 |
#include "mbedtls/x509_crt.h" |
| 27 |
#include "mbedtls/ssl.h" |
| 28 |
#endif |
| 29 |
#include "azure_c_shared_utility/shared_util_options.h" |
| 30 |
#include "azure_c_shared_utility/safe_math.h" |
| 31 |
|
| 32 |
#define TEMP_BUFFER_SIZE 1024 |
| 33 |
|
| 34 |
MU_DEFINE_ENUM_STRINGS(HTTPAPI_RESULT, HTTPAPI_RESULT_VALUES); |
| 35 |
|
| 36 |
typedef struct HTTP_HANDLE_DATA_TAG |
| 37 |
{ |
| 38 |
CURL* curl; |
| 39 |
char* hostURL; |
| 40 |
long timeout; |
| 41 |
long lowSpeedLimit; |
| 42 |
long lowSpeedTime; |
| 43 |
long forbidReuse; |
| 44 |
long freshConnect; |
| 45 |
long verbose; |
| 46 |
const char* x509privatekey; |
| 47 |
const char* x509certificate; |
| 48 |
const char* certificates; /*a list of CA certificates*/ |
| 49 |
#if USE_OPENSSL |
| 50 |
OPTION_OPENSSL_KEY_TYPE x509privatekeytype; |
| 51 |
#ifndef OPENSSL_NO_ENGINE |
| 52 |
char* engineId; |
| 53 |
ENGINE* engine; |
| 54 |
#endif // OPENSSL_NO_ENGINE |
| 55 |
#elif USE_MBEDTLS |
| 56 |
mbedtls_x509_crt cert; |
| 57 |
mbedtls_pk_context key; |
| 58 |
mbedtls_x509_crt trusted_certificates; |
| 59 |
#endif |
| 60 |
} HTTP_HANDLE_DATA; |
| 61 |
|
| 62 |
typedef struct HTTP_RESPONSE_CONTENT_BUFFER_TAG |
| 63 |
{ |
| 64 |
unsigned char* buffer; |
| 65 |
size_t bufferSize; |
| 66 |
unsigned char error; |
| 67 |
} HTTP_RESPONSE_CONTENT_BUFFER; |
| 68 |
|
| 69 |
static size_t nUsersOfHTTPAPI = 0; /*used for reference counting (a weak one)*/ |
| 70 |
|
| 71 |
HTTPAPI_RESULT HTTPAPI_Init(void) |
| 72 |
{ |
| 73 |
HTTPAPI_RESULT result; |
| 74 |
if (nUsersOfHTTPAPI == 0) |
| 75 |
{ |
| 76 |
if (curl_global_init(CURL_GLOBAL_NOTHING) != 0) |
| 77 |
{ |
| 78 |
result = HTTPAPI_INIT_FAILED; |
| 79 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
nUsersOfHTTPAPI++; |
| 84 |
result = HTTPAPI_OK; |
| 85 |
} |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
nUsersOfHTTPAPI++; |
| 90 |
result = HTTPAPI_OK; |
| 91 |
} |
| 92 |
|
| 93 |
return result; |
| 94 |
} |
| 95 |
|
| 96 |
void HTTPAPI_Deinit(void) |
| 97 |
{ |
| 98 |
if (nUsersOfHTTPAPI > 0) |
| 99 |
{ |
| 100 |
nUsersOfHTTPAPI--; |
| 101 |
if (nUsersOfHTTPAPI == 0) |
| 102 |
{ |
| 103 |
curl_global_cleanup(); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
HTTP_HANDLE HTTPAPI_CreateConnection(const char* hostName) |
| 109 |
{ |
| 110 |
HTTP_HANDLE_DATA* httpHandleData; |
| 111 |
|
| 112 |
if (hostName == NULL) |
| 113 |
{ |
| 114 |
LogError("invalid arg const char* hostName = %p", hostName); |
| 115 |
httpHandleData = NULL; |
| 116 |
} |
| 117 |
else |
| 118 |
{ |
| 119 |
httpHandleData = (HTTP_HANDLE_DATA*)malloc(sizeof(HTTP_HANDLE_DATA)); |
| 120 |
if (httpHandleData != NULL) |
| 121 |
{ |
| 122 |
size_t hostURL_size = safe_add_size_t(strlen("https://"), strlen(hostName)); |
| 123 |
hostURL_size = safe_add_size_t(hostURL_size, 1); |
| 124 |
|
| 125 |
if (hostURL_size == SIZE_MAX) |
| 126 |
{ |
| 127 |
LogError("invalid malloc size"); |
| 128 |
httpHandleData->hostURL = NULL; |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
httpHandleData->hostURL = malloc(hostURL_size); |
| 133 |
} |
| 134 |
|
| 135 |
if (httpHandleData->hostURL == NULL) |
| 136 |
{ |
| 137 |
LogError("unable to malloc"); |
| 138 |
free(httpHandleData); |
| 139 |
httpHandleData = NULL; |
| 140 |
} |
| 141 |
else if ((strcpy_s(httpHandleData->hostURL, hostURL_size, "https://") != 0) || |
| 142 |
(strcat_s(httpHandleData->hostURL, hostURL_size, hostName) != 0)) |
| 143 |
{ |
| 144 |
LogError("unable to set hostURL"); |
| 145 |
free(httpHandleData->hostURL); |
| 146 |
free(httpHandleData); |
| 147 |
httpHandleData = NULL; |
| 148 |
} |
| 149 |
else if ((httpHandleData->curl = curl_easy_init()) == NULL) |
| 150 |
{ |
| 151 |
LogError("unable to init cURL structure"); |
| 152 |
free(httpHandleData->hostURL); |
| 153 |
free(httpHandleData); |
| 154 |
httpHandleData = NULL; |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
#ifdef USE_BEARSSL |
| 159 |
// Gate testing currently supports version of cURL prior to cURL's BearSSL or SecureTransport support. |
| 160 |
// To pass Gates, cannot directly reference CURLSSLBACKEND_BEARSSL or CURLSSLBACKEND_SECURETRANSPORT. |
| 161 |
// Skipping validation of cURL's ssl backend. |
| 162 |
LogInfo("If using BearSSL with the C SDK, please confirm cURL is also configured to use BearSSL."); |
| 163 |
#elif defined USE_OPENSSL || defined USE_WOLFSSL || defined USE_MBEDTLS |
| 164 |
|
| 165 |
// Check C SDK TLS platform matches cURL's |
| 166 |
const struct curl_tlssessioninfo* info = NULL; |
| 167 |
CURLcode result = curl_easy_getinfo(httpHandleData->curl, CURLINFO_TLS_SSL_PTR, &info); |
| 168 |
|
| 169 |
if (result != CURLE_OK || info == NULL) |
| 170 |
{ |
| 171 |
LogError("unable to get cURL backend SSL info"); |
| 172 |
} |
| 173 |
else |
| 174 |
{ |
| 175 |
#ifdef USE_OPENSSL |
| 176 |
if (CURLSSLBACKEND_OPENSSL != (int32_t)info->backend) |
| 177 |
{ |
| 178 |
char* SDKSSLName = "OpenSSL"; |
| 179 |
#elif USE_WOLFSSL |
| 180 |
if (CURLSSLBACKEND_WOLFSSL != (int32_t)info->backend) |
| 181 |
{ |
| 182 |
char* SDKSSLName = "wolfSSL"; |
| 183 |
#elif USE_MBEDTLS |
| 184 |
if (CURLSSLBACKEND_MBEDTLS != (int32_t)info->backend) |
| 185 |
{ |
| 186 |
char* SDKSSLName = "mbedTLS"; |
| 187 |
#else |
| 188 |
// Should not get here. |
| 189 |
#endif |
| 190 |
LogError("curl_sslbackend (%d) currently used by cURL does not match TLS platform (%s) " |
| 191 |
"used by C SDK on Linux or OSX. Please configure and compile cURL to use %s.", |
| 192 |
info->backend, SDKSSLName, SDKSSLName); |
| 193 |
} |
| 194 |
} |
| 195 |
#else // Other, possibly SecureTransport. |
| 196 |
// Gate testing currently supports version of cURL prior to cURL's BearSSL or SecureTransport support. |
| 197 |
// To pass Gates, cannot directly reference CURLSSLBACKEND_BEARSSL or CURLSSLBACKEND_SECURETRANSPORT. |
| 198 |
// Skipping validation of cURL's ssl backend. |
| 199 |
LogInfo("If using SecureTransport with the C SDK, please confirm cURL is also configured to use SecureTransport."); |
| 200 |
#endif |
| 201 |
httpHandleData->timeout = 242 * 1000; /*242 seconds seems like a nice enough time. Reasone for 242: |
| 202 |
1. http://curl.haxx.se/libcurl/c/CURLOPT_TIMEOUT.html says Normally, name lookups can take a considerable time and limiting operations to less than a few minutes risk aborting perfectly normal operations. |
| 203 |
2. 256KB of data... at 9600 bps transfers in about 218 seconds. Add to that a buffer of 10%... round it up to 242 :)*/ |
| 204 |
httpHandleData->lowSpeedTime = 0; |
| 205 |
httpHandleData->lowSpeedLimit = 0; |
| 206 |
httpHandleData->forbidReuse = 0; |
| 207 |
httpHandleData->freshConnect = 0; |
| 208 |
httpHandleData->verbose = 0; |
| 209 |
httpHandleData->x509certificate = NULL; |
| 210 |
httpHandleData->x509privatekey = NULL; |
| 211 |
httpHandleData->certificates = NULL; |
| 212 |
#ifdef USE_OPENSSL |
| 213 |
httpHandleData->x509privatekeytype = KEY_TYPE_DEFAULT; |
| 214 |
#ifndef OPENSSL_NO_ENGINE |
| 215 |
httpHandleData->engineId = NULL; |
| 216 |
httpHandleData->engine = NULL; |
| 217 |
#endif // OPENSSL_NO_ENGINE |
| 218 |
#elif USE_MBEDTLS |
| 219 |
mbedtls_x509_crt_init(&httpHandleData->cert); |
| 220 |
mbedtls_pk_init(&httpHandleData->key); |
| 221 |
mbedtls_x509_crt_init(&httpHandleData->trusted_certificates); |
| 222 |
#endif |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
return (HTTP_HANDLE)httpHandleData; |
| 228 |
} |
| 229 |
|
| 230 |
void HTTPAPI_CloseConnection(HTTP_HANDLE handle) |
| 231 |
{ |
| 232 |
HTTP_HANDLE_DATA* httpHandleData = (HTTP_HANDLE_DATA*)handle; |
| 233 |
if (httpHandleData != NULL) |
| 234 |
{ |
| 235 |
free(httpHandleData->hostURL); |
| 236 |
httpHandleData->hostURL = NULL; |
| 237 |
curl_easy_cleanup(httpHandleData->curl); |
| 238 |
#ifdef USE_OPENSSL |
| 239 |
#ifndef OPENSSL_NO_ENGINE |
| 240 |
if (httpHandleData->engine != NULL) |
| 241 |
{ |
| 242 |
ENGINE_free(httpHandleData->engine); |
| 243 |
httpHandleData->engine = NULL; |
| 244 |
} |
| 245 |
|
| 246 |
if (httpHandleData->engineId != NULL) |
| 247 |
{ |
| 248 |
free(httpHandleData->engineId); |
| 249 |
httpHandleData->engineId = NULL; |
| 250 |
} |
| 251 |
#endif // OPENSSL_NO_ENGINE |
| 252 |
#elif USE_MBEDTLS |
| 253 |
mbedtls_x509_crt_free(&httpHandleData->cert); |
| 254 |
mbedtls_pk_free(&httpHandleData->key); |
| 255 |
mbedtls_x509_crt_free(&httpHandleData->trusted_certificates); |
| 256 |
#endif |
| 257 |
free(httpHandleData); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
static size_t HeadersWriteFunction(void *ptr, size_t size, size_t nmemb, void *userdata) |
| 262 |
{ |
| 263 |
HTTP_HEADERS_HANDLE responseHeadersHandle = (HTTP_HEADERS_HANDLE)userdata; |
| 264 |
char* headerLine = (char*)ptr; |
| 265 |
|
| 266 |
if (headerLine != NULL) |
| 267 |
{ |
| 268 |
char* token = strtok(headerLine, "\r\n"); |
| 269 |
while ((token != NULL) && |
| 270 |
(token[0] != '\0')) |
| 271 |
{ |
| 272 |
char* whereIsColon = strchr(token, ':'); |
| 273 |
if(whereIsColon!=NULL) |
| 274 |
{ |
| 275 |
*whereIsColon='\0'; |
| 276 |
HTTPHeaders_AddHeaderNameValuePair(responseHeadersHandle, token, whereIsColon+1); |
| 277 |
*whereIsColon=':'; |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
/*not a header, maybe a status-line*/ |
| 282 |
} |
| 283 |
token = strtok(NULL, "\r\n"); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
return size * nmemb; |
| 288 |
} |
| 289 |
|
| 290 |
static size_t ContentWriteFunction(void *ptr, size_t size, size_t nmemb, void *userdata) |
| 291 |
{ |
| 292 |
HTTP_RESPONSE_CONTENT_BUFFER* responseContentBuffer = (HTTP_RESPONSE_CONTENT_BUFFER*)userdata; |
| 293 |
if ((userdata != NULL) && |
| 294 |
(ptr != NULL) && |
| 295 |
(size * nmemb > 0)) |
| 296 |
{ |
| 297 |
size_t malloc_size = safe_multiply_size_t(size, nmemb); |
| 298 |
malloc_size = safe_add_size_t(malloc_size, responseContentBuffer->bufferSize); |
| 299 |
|
| 300 |
void* newBuffer; |
| 301 |
if (malloc_size == SIZE_MAX) |
| 302 |
{ |
| 303 |
LogError("Invalid buffer size"); |
| 304 |
newBuffer = NULL; |
| 305 |
} |
| 306 |
else |
| 307 |
{ |
| 308 |
newBuffer = realloc(responseContentBuffer->buffer, malloc_size); |
| 309 |
} |
| 310 |
|
| 311 |
if (newBuffer != NULL) |
| 312 |
{ |
| 313 |
responseContentBuffer->buffer = newBuffer; |
| 314 |
memcpy(responseContentBuffer->buffer + responseContentBuffer->bufferSize, ptr, size * nmemb); |
| 315 |
responseContentBuffer->bufferSize += size * nmemb; |
| 316 |
} |
| 317 |
else |
| 318 |
{ |
| 319 |
LogError("Could not allocate buffer of size %lu", (unsigned long)(malloc_size)); |
| 320 |
responseContentBuffer->error = 1; |
| 321 |
if (responseContentBuffer->buffer != NULL) |
| 322 |
{ |
| 323 |
free(responseContentBuffer->buffer); |
| 324 |
responseContentBuffer->buffer = NULL; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
return size * nmemb; |
| 330 |
} |
| 331 |
|
| 332 |
static CURLcode ssl_ctx_callback(CURL *curl, void *ssl_ctx, void *userptr) |
| 333 |
{ |
| 334 |
CURLcode result; |
| 335 |
|
| 336 |
if ( |
| 337 |
(curl == NULL) || |
| 338 |
(ssl_ctx == NULL) || |
| 339 |
(userptr == NULL) |
| 340 |
) |
| 341 |
{ |
| 342 |
LogError("unexpected parameter CURL *curl=%p, void *ssl_ctx=%p, void *userptr=%p", curl, ssl_ctx, userptr); |
| 343 |
result = CURLE_SSL_CERTPROBLEM; |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
HTTP_HANDLE_DATA *httpHandleData = (HTTP_HANDLE_DATA *)userptr; |
| 348 |
#ifdef USE_OPENSSL |
| 349 |
/*trying to set the x509 per device certificate*/ |
| 350 |
#ifndef OPENSSL_NO_ENGINE |
| 351 |
if (httpHandleData->x509privatekeytype == KEY_TYPE_ENGINE) { |
| 352 |
ENGINE_load_builtin_engines(); |
| 353 |
httpHandleData->engine = ENGINE_by_id(httpHandleData->engineId); |
| 354 |
} |
| 355 |
if (httpHandleData->x509privatekeytype == KEY_TYPE_ENGINE && httpHandleData->engine == NULL) |
| 356 |
{ |
| 357 |
LogError("unable to load engine by ID: %s", httpHandleData->engineId); |
| 358 |
result = CURLE_SSL_CERTPROBLEM; |
| 359 |
} |
| 360 |
else if ( |
| 361 |
(httpHandleData->x509certificate != NULL) && (httpHandleData->x509privatekey != NULL) && |
| 362 |
(x509_openssl_add_credentials(ssl_ctx, httpHandleData->x509certificate, httpHandleData->x509privatekey, httpHandleData->x509privatekeytype, httpHandleData->engine) != 0) |
| 363 |
) |
| 364 |
#else // OPENSSL_NO_ENGINE |
| 365 |
if ( |
| 366 |
(httpHandleData->x509certificate != NULL) && (httpHandleData->x509privatekey != NULL) && |
| 367 |
(x509_openssl_add_credentials(ssl_ctx, httpHandleData->x509certificate, httpHandleData->x509privatekey, httpHandleData->x509privatekeytype) != 0) |
| 368 |
) |
| 369 |
#endif // OPENSSL_NO_ENGINE |
| 370 |
{ |
| 371 |
LogError("unable to x509_openssl_add_credentials"); |
| 372 |
result = CURLE_SSL_CERTPROBLEM; |
| 373 |
#ifndef OPENSSL_NO_ENGINE |
| 374 |
ENGINE_free(httpHandleData->engine); |
| 375 |
httpHandleData->engine = NULL; |
| 376 |
#endif // OPENSSL_NO_ENGINE |
| 377 |
} |
| 378 |
/*trying to set CA certificates*/ |
| 379 |
else if ( |
| 380 |
(httpHandleData->certificates != NULL) && |
| 381 |
(x509_openssl_add_certificates(ssl_ctx, httpHandleData->certificates) != 0) |
| 382 |
) |
| 383 |
{ |
| 384 |
LogError("failure in x509_openssl_add_certificates"); |
| 385 |
result = CURLE_SSL_CERTPROBLEM; |
| 386 |
#ifndef OPENSSL_NO_ENGINE |
| 387 |
ENGINE_free(httpHandleData->engine); |
| 388 |
httpHandleData->engine = NULL; |
| 389 |
#endif // OPENSSL_NO_ENGINE |
| 390 |
} |
| 391 |
#elif USE_WOLFSSL |
| 392 |
if ( |
| 393 |
(httpHandleData->x509certificate != NULL) && |
| 394 |
(httpHandleData->x509privatekey != NULL) && |
| 395 |
( |
| 396 |
((wolfSSL_CTX_use_certificate_chain_buffer(ssl_ctx, (unsigned char*)httpHandleData->x509certificate, strlen(httpHandleData->x509certificate)) != SSL_SUCCESS)) || |
| 397 |
((wolfSSL_CTX_use_PrivateKey_buffer(ssl_ctx, (unsigned char*)httpHandleData->x509privatekey, strlen(httpHandleData->x509privatekey), SSL_FILETYPE_PEM) != SSL_SUCCESS)) |
| 398 |
) |
| 399 |
) |
| 400 |
{ |
| 401 |
LogError("unable to add x509 certs to wolfssl"); |
| 402 |
result = CURLE_SSL_CERTPROBLEM; |
| 403 |
} |
| 404 |
else if ( |
| 405 |
(httpHandleData->certificates != NULL) && |
| 406 |
(wolfSSL_CTX_load_verify_buffer(ssl_ctx, (const unsigned char*)httpHandleData->certificates, strlen(httpHandleData->certificates), SSL_FILETYPE_PEM) != SSL_SUCCESS) |
| 407 |
) |
| 408 |
{ |
| 409 |
LogError("failure in adding trusted certificate to client"); |
| 410 |
result = CURLE_SSL_CERTPROBLEM; |
| 411 |
} |
| 412 |
#elif USE_MBEDTLS |
| 413 |
// set device cert and key |
| 414 |
if ( |
| 415 |
(httpHandleData->x509certificate != NULL) && (httpHandleData->x509privatekey != NULL) && |
| 416 |
!( |
| 417 |
(mbedtls_x509_crt_parse(&httpHandleData->cert, (const unsigned char *)httpHandleData->x509certificate, (int)(strlen(httpHandleData->x509certificate) + 1)) == 0) && |
| 418 |
(mbedtls_pk_parse_key(&httpHandleData->key, (const unsigned char *)httpHandleData->x509privatekey, (int)(strlen(httpHandleData->x509privatekey) + 1), NULL, 0) == 0) && |
| 419 |
(mbedtls_ssl_conf_own_cert(ssl_ctx, &httpHandleData->cert, &httpHandleData->key) == 0) |
| 420 |
) |
| 421 |
) |
| 422 |
{ |
| 423 |
LogError("unable to set x509 credentials"); |
| 424 |
result = CURLE_SSL_CERTPROBLEM; |
| 425 |
} |
| 426 |
// set CA |
| 427 |
else if (httpHandleData->certificates != NULL) |
| 428 |
{ |
| 429 |
if (mbedtls_x509_crt_parse(&httpHandleData->trusted_certificates, (const unsigned char *)httpHandleData->certificates, (int)(strlen(httpHandleData->certificates) + 1)) != 0) |
| 430 |
{ |
| 431 |
LogError("unable to set trusted certificate"); |
| 432 |
result = CURLE_SSL_CERTPROBLEM; |
| 433 |
} |
| 434 |
else |
| 435 |
{ |
| 436 |
mbedtls_ssl_conf_ca_chain(ssl_ctx, &httpHandleData->trusted_certificates, NULL); |
| 437 |
result = CURLE_OK; |
| 438 |
} |
| 439 |
} |
| 440 |
#else |
| 441 |
if (httpHandleData->x509certificate != NULL || httpHandleData->x509privatekey != NULL) |
| 442 |
{ |
| 443 |
LogError("Failure no platform is enabled to handle certificates"); |
| 444 |
result = CURLE_SSL_CERTPROBLEM; |
| 445 |
} |
| 446 |
#endif |
| 447 |
else |
| 448 |
{ |
| 449 |
result = CURLE_OK; |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
return result; |
| 454 |
} |
| 455 |
|
| 456 |
HTTPAPI_RESULT HTTPAPI_ExecuteRequest(HTTP_HANDLE handle, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath, |
| 457 |
HTTP_HEADERS_HANDLE httpHeadersHandle, const unsigned char* content, |
| 458 |
size_t contentLength, unsigned int* statusCode, |
| 459 |
HTTP_HEADERS_HANDLE responseHeadersHandle, BUFFER_HANDLE responseContent) |
| 460 |
{ |
| 461 |
HTTPAPI_RESULT result; |
| 462 |
HTTP_HANDLE_DATA* httpHandleData = (HTTP_HANDLE_DATA*)handle; |
| 463 |
size_t headersCount; |
| 464 |
HTTP_RESPONSE_CONTENT_BUFFER responseContentBuffer; |
| 465 |
|
| 466 |
if ((httpHandleData == NULL) || |
| 467 |
(relativePath == NULL) || |
| 468 |
(httpHeadersHandle == NULL) || |
| 469 |
((content == NULL) && (contentLength > 0)) |
| 470 |
) |
| 471 |
{ |
| 472 |
result = HTTPAPI_INVALID_ARG; |
| 473 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 474 |
} |
| 475 |
else if (HTTPHeaders_GetHeaderCount(httpHeadersHandle, &headersCount) != HTTP_HEADERS_OK) |
| 476 |
{ |
| 477 |
result = HTTPAPI_INVALID_ARG; |
| 478 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 479 |
} |
| 480 |
else |
| 481 |
{ |
| 482 |
char* tempHostURL; |
| 483 |
size_t tempHostURL_size = safe_add_size_t(strlen(httpHandleData->hostURL), strlen(relativePath)); |
| 484 |
tempHostURL_size = safe_add_size_t(tempHostURL_size, 1); |
| 485 |
if (tempHostURL_size == SIZE_MAX) |
| 486 |
{ |
| 487 |
LogError("Invalid malloc size"); |
| 488 |
tempHostURL = NULL; |
| 489 |
} |
| 490 |
else |
| 491 |
{ |
| 492 |
tempHostURL = malloc(tempHostURL_size); |
| 493 |
} |
| 494 |
|
| 495 |
if (tempHostURL == NULL) |
| 496 |
{ |
| 497 |
result = HTTPAPI_ERROR; |
| 498 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 499 |
} |
| 500 |
else |
| 501 |
{ |
| 502 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_VERBOSE, httpHandleData->verbose) != CURLE_OK) |
| 503 |
{ |
| 504 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 505 |
LogError("failed to set CURLOPT_VERBOSE (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 506 |
} |
| 507 |
else if ((strcpy_s(tempHostURL, tempHostURL_size, httpHandleData->hostURL) != 0) || |
| 508 |
(strcat_s(tempHostURL, tempHostURL_size, relativePath) != 0)) |
| 509 |
{ |
| 510 |
result = HTTPAPI_STRING_PROCESSING_ERROR; |
| 511 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 512 |
} |
| 513 |
/* set the URL */ |
| 514 |
else if (curl_easy_setopt(httpHandleData->curl, CURLOPT_URL, tempHostURL) != CURLE_OK) |
| 515 |
{ |
| 516 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 517 |
LogError("failed to set CURLOPT_URL (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 518 |
} |
| 519 |
else if (curl_easy_setopt(httpHandleData->curl, CURLOPT_TIMEOUT_MS, httpHandleData->timeout) != CURLE_OK) |
| 520 |
{ |
| 521 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 522 |
LogError("failed to set CURLOPT_TIMEOUT_MS (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 523 |
} |
| 524 |
else if (curl_easy_setopt(httpHandleData->curl, CURLOPT_LOW_SPEED_LIMIT, httpHandleData->lowSpeedLimit) != CURLE_OK) |
| 525 |
{ |
| 526 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 527 |
LogError("failed to set CURLOPT_LOW_SPEED_LIMIT (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 528 |
} |
| 529 |
else if (curl_easy_setopt(httpHandleData->curl, CURLOPT_LOW_SPEED_TIME, httpHandleData->lowSpeedTime) != CURLE_OK) |
| 530 |
{ |
| 531 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 532 |
LogError("failed to set CURLOPT_LOW_SPEED_TIME (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 533 |
} |
| 534 |
else if (curl_easy_setopt(httpHandleData->curl, CURLOPT_FRESH_CONNECT, httpHandleData->freshConnect) != CURLE_OK) |
| 535 |
{ |
| 536 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 537 |
LogError("failed to set CURLOPT_FRESH_CONNECT (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 538 |
} |
| 539 |
else if (curl_easy_setopt(httpHandleData->curl, CURLOPT_FORBID_REUSE, httpHandleData->forbidReuse) != CURLE_OK) |
| 540 |
{ |
| 541 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 542 |
LogError("failed to set CURLOPT_FORBID_REUSE (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 543 |
} |
| 544 |
else if (curl_easy_setopt(httpHandleData->curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1) != CURLE_OK) |
| 545 |
{ |
| 546 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 547 |
LogError("failed to set CURLOPT_HTTP_VERSION (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 548 |
} |
| 549 |
else |
| 550 |
{ |
| 551 |
result = HTTPAPI_OK; |
| 552 |
|
| 553 |
switch (requestType) |
| 554 |
{ |
| 555 |
default: |
| 556 |
result = HTTPAPI_INVALID_ARG; |
| 557 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 558 |
break; |
| 559 |
|
| 560 |
case HTTPAPI_REQUEST_GET: |
| 561 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_HTTPGET, 1L) != CURLE_OK) |
| 562 |
{ |
| 563 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 564 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 565 |
} |
| 566 |
else |
| 567 |
{ |
| 568 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_CUSTOMREQUEST, NULL) != CURLE_OK) |
| 569 |
{ |
| 570 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 571 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
break; |
| 576 |
|
| 577 |
case HTTPAPI_REQUEST_HEAD: |
| 578 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_HTTPGET, 1L) != CURLE_OK) |
| 579 |
{ |
| 580 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 581 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 582 |
} |
| 583 |
else if (curl_easy_setopt(httpHandleData->curl, CURLOPT_NOBODY, 1L) != CURLE_OK) |
| 584 |
{ |
| 585 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 586 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 587 |
} |
| 588 |
else if (curl_easy_setopt(httpHandleData->curl, CURLOPT_CUSTOMREQUEST, NULL) != CURLE_OK) |
| 589 |
{ |
| 590 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 591 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 592 |
} |
| 593 |
|
| 594 |
break; |
| 595 |
|
| 596 |
case HTTPAPI_REQUEST_POST: |
| 597 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_POST, 1L) != CURLE_OK) |
| 598 |
{ |
| 599 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 600 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 601 |
} |
| 602 |
else |
| 603 |
{ |
| 604 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_CUSTOMREQUEST, NULL) != CURLE_OK) |
| 605 |
{ |
| 606 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 607 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
break; |
| 612 |
|
| 613 |
case HTTPAPI_REQUEST_PUT: |
| 614 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_POST, 1L)) |
| 615 |
{ |
| 616 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 617 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 618 |
} |
| 619 |
else |
| 620 |
{ |
| 621 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_CUSTOMREQUEST, "PUT") != CURLE_OK) |
| 622 |
{ |
| 623 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 624 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 625 |
} |
| 626 |
} |
| 627 |
break; |
| 628 |
|
| 629 |
case HTTPAPI_REQUEST_DELETE: |
| 630 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_POST, 1L) != CURLE_OK) |
| 631 |
{ |
| 632 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 633 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 634 |
} |
| 635 |
else |
| 636 |
{ |
| 637 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_CUSTOMREQUEST, "DELETE") != CURLE_OK) |
| 638 |
{ |
| 639 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 640 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 641 |
} |
| 642 |
} |
| 643 |
break; |
| 644 |
|
| 645 |
case HTTPAPI_REQUEST_PATCH: |
| 646 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_POST, 1L) != CURLE_OK) |
| 647 |
{ |
| 648 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 649 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 650 |
} |
| 651 |
else |
| 652 |
{ |
| 653 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_CUSTOMREQUEST, "PATCH") != CURLE_OK) |
| 654 |
{ |
| 655 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 656 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
break; |
| 661 |
} |
| 662 |
|
| 663 |
if (result == HTTPAPI_OK) |
| 664 |
{ |
| 665 |
/* add headers */ |
| 666 |
struct curl_slist* headers = NULL; |
| 667 |
size_t i; |
| 668 |
|
| 669 |
for (i = 0; i < headersCount; i++) |
| 670 |
{ |
| 671 |
char *tempBuffer; |
| 672 |
if (HTTPHeaders_GetHeader(httpHeadersHandle, i, &tempBuffer) != HTTP_HEADERS_OK) |
| 673 |
{ |
| 674 |
/* error */ |
| 675 |
result = HTTPAPI_HTTP_HEADERS_FAILED; |
| 676 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 677 |
break; |
| 678 |
} |
| 679 |
else |
| 680 |
{ |
| 681 |
struct curl_slist* newHeaders = curl_slist_append(headers, tempBuffer); |
| 682 |
if (newHeaders == NULL) |
| 683 |
{ |
| 684 |
result = HTTPAPI_ALLOC_FAILED; |
| 685 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 686 |
free(tempBuffer); |
| 687 |
break; |
| 688 |
} |
| 689 |
else |
| 690 |
{ |
| 691 |
free(tempBuffer); |
| 692 |
headers = newHeaders; |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
if (result == HTTPAPI_OK) |
| 698 |
{ |
| 699 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_HTTPHEADER, headers) != CURLE_OK) |
| 700 |
{ |
| 701 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 702 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 703 |
} |
| 704 |
else |
| 705 |
{ |
| 706 |
/* add content */ |
| 707 |
if ((content != NULL) && |
| 708 |
(contentLength > 0)) |
| 709 |
{ |
| 710 |
if ((curl_easy_setopt(httpHandleData->curl, CURLOPT_POSTFIELDS, (void*)content) != CURLE_OK) || |
| 711 |
(curl_easy_setopt(httpHandleData->curl, CURLOPT_POSTFIELDSIZE, contentLength) != CURLE_OK)) |
| 712 |
{ |
| 713 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 714 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 715 |
} |
| 716 |
} |
| 717 |
else |
| 718 |
{ |
| 719 |
if (requestType != HTTPAPI_REQUEST_GET) |
| 720 |
{ |
| 721 |
if ((curl_easy_setopt(httpHandleData->curl, CURLOPT_POSTFIELDS, (void*)NULL) != CURLE_OK) || |
| 722 |
(curl_easy_setopt(httpHandleData->curl, CURLOPT_POSTFIELDSIZE, 0) != CURLE_OK)) |
| 723 |
{ |
| 724 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 725 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 726 |
} |
| 727 |
} |
| 728 |
else |
| 729 |
{ |
| 730 |
/*GET request cannot POST, so "do nothing*/ |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
if (result == HTTPAPI_OK) |
| 735 |
{ |
| 736 |
if ((curl_easy_setopt(httpHandleData->curl, CURLOPT_WRITEHEADER, NULL) != CURLE_OK) || |
| 737 |
(curl_easy_setopt(httpHandleData->curl, CURLOPT_HEADERFUNCTION, NULL) != CURLE_OK) || |
| 738 |
(curl_easy_setopt(httpHandleData->curl, CURLOPT_WRITEFUNCTION, ContentWriteFunction) != CURLE_OK)) |
| 739 |
{ |
| 740 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 741 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 742 |
} |
| 743 |
else |
| 744 |
{ |
| 745 |
if (responseHeadersHandle != NULL) |
| 746 |
{ |
| 747 |
/* setup the code to get the response headers */ |
| 748 |
if ((curl_easy_setopt(httpHandleData->curl, CURLOPT_WRITEHEADER, responseHeadersHandle) != CURLE_OK) || |
| 749 |
(curl_easy_setopt(httpHandleData->curl, CURLOPT_HEADERFUNCTION, HeadersWriteFunction) != CURLE_OK)) |
| 750 |
{ |
| 751 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 752 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
if (result == HTTPAPI_OK) |
| 757 |
{ |
| 758 |
responseContentBuffer.buffer = NULL; |
| 759 |
responseContentBuffer.bufferSize = 0; |
| 760 |
responseContentBuffer.error = 0; |
| 761 |
|
| 762 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_WRITEDATA, &responseContentBuffer) != CURLE_OK) |
| 763 |
{ |
| 764 |
result = HTTPAPI_SET_OPTION_FAILED; |
| 765 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 766 |
} |
| 767 |
|
| 768 |
if (result == HTTPAPI_OK) |
| 769 |
{ |
| 770 |
/* Execute request */ |
| 771 |
CURLcode curlRes = curl_easy_perform(httpHandleData->curl); |
| 772 |
if (curlRes != CURLE_OK) |
| 773 |
{ |
| 774 |
LogError("curl_easy_perform() failed: %s\n", curl_easy_strerror(curlRes)); |
| 775 |
result = HTTPAPI_OPEN_REQUEST_FAILED; |
| 776 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 777 |
} |
| 778 |
else |
| 779 |
{ |
| 780 |
long httpCode; |
| 781 |
|
| 782 |
/* get the status code */ |
| 783 |
if (curl_easy_getinfo(httpHandleData->curl, CURLINFO_RESPONSE_CODE, &httpCode) != CURLE_OK) |
| 784 |
{ |
| 785 |
result = HTTPAPI_QUERY_HEADERS_FAILED; |
| 786 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 787 |
} |
| 788 |
else if (responseContentBuffer.error) |
| 789 |
{ |
| 790 |
result = HTTPAPI_READ_DATA_FAILED; |
| 791 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 792 |
} |
| 793 |
else |
| 794 |
{ |
| 795 |
if (statusCode != NULL) |
| 796 |
{ |
| 797 |
*statusCode = (unsigned int)httpCode; |
| 798 |
} |
| 799 |
|
| 800 |
/* fill response content length */ |
| 801 |
if (responseContent != NULL) |
| 802 |
{ |
| 803 |
if ((responseContentBuffer.bufferSize > 0) && (BUFFER_build(responseContent, responseContentBuffer.buffer, responseContentBuffer.bufferSize) != 0)) |
| 804 |
{ |
| 805 |
result = HTTPAPI_INSUFFICIENT_RESPONSE_BUFFER; |
| 806 |
LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 807 |
} |
| 808 |
else |
| 809 |
{ |
| 810 |
/*all nice*/ |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
if (httpCode >= 300) |
| 815 |
{ |
| 816 |
LogError("Failure in HTTP communication: server reply code is %ld", httpCode); |
| 817 |
LogInfo("HTTP Response:%*.*s", (int)responseContentBuffer.bufferSize, |
| 818 |
(int)responseContentBuffer.bufferSize, responseContentBuffer.buffer); |
| 819 |
} |
| 820 |
else |
| 821 |
{ |
| 822 |
result = HTTPAPI_OK; |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
if (responseContentBuffer.buffer != NULL) |
| 829 |
{ |
| 830 |
free(responseContentBuffer.buffer); |
| 831 |
responseContentBuffer.buffer = NULL; |
| 832 |
} |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
} |
| 838 |
curl_slist_free_all(headers); |
| 839 |
} |
| 840 |
} |
| 841 |
free(tempHostURL); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
return result; |
| 846 |
} |
| 847 |
|
| 848 |
HTTPAPI_RESULT HTTPAPI_SetOption(HTTP_HANDLE handle, const char* optionName, const void* value) |
| 849 |
{ |
| 850 |
HTTPAPI_RESULT result; |
| 851 |
if ( |
| 852 |
(handle == NULL) || |
| 853 |
(optionName == NULL) || |
| 854 |
(value == NULL) |
| 855 |
) |
| 856 |
{ |
| 857 |
result = HTTPAPI_INVALID_ARG; |
| 858 |
LogError("invalid parameter (NULL) passed to HTTPAPI_SetOption"); |
| 859 |
} |
| 860 |
else |
| 861 |
{ |
| 862 |
HTTP_HANDLE_DATA* httpHandleData = (HTTP_HANDLE_DATA*)handle; |
| 863 |
if (strcmp(OPTION_HTTP_TIMEOUT, optionName) == 0) |
| 864 |
{ |
| 865 |
long timeout = (long)(*(unsigned int*)value); |
| 866 |
httpHandleData->timeout = timeout; |
| 867 |
result = HTTPAPI_OK; |
| 868 |
} |
| 869 |
else if (strcmp(OPTION_CURL_LOW_SPEED_LIMIT, optionName) == 0) |
| 870 |
{ |
| 871 |
httpHandleData->lowSpeedLimit = *(const long*)value; |
| 872 |
result = HTTPAPI_OK; |
| 873 |
} |
| 874 |
else if (strcmp(OPTION_CURL_LOW_SPEED_TIME, optionName) == 0) |
| 875 |
{ |
| 876 |
httpHandleData->lowSpeedTime = *(const long*)value; |
| 877 |
result = HTTPAPI_OK; |
| 878 |
} |
| 879 |
else if (strcmp(OPTION_CURL_FRESH_CONNECT, optionName) == 0) |
| 880 |
{ |
| 881 |
httpHandleData->freshConnect = *(const long*)value; |
| 882 |
result = HTTPAPI_OK; |
| 883 |
} |
| 884 |
else if (strcmp(OPTION_CURL_FORBID_REUSE, optionName) == 0) |
| 885 |
{ |
| 886 |
httpHandleData->forbidReuse = *(const long*)value; |
| 887 |
result = HTTPAPI_OK; |
| 888 |
} |
| 889 |
else if (strcmp(OPTION_CURL_VERBOSE, optionName) == 0) |
| 890 |
{ |
| 891 |
httpHandleData->verbose = *(const long*)value; |
| 892 |
result = HTTPAPI_OK; |
| 893 |
} |
| 894 |
#ifdef USE_OPENSSL |
| 895 |
else if (strcmp(OPTION_OPENSSL_PRIVATE_KEY_TYPE, optionName) == 0) |
| 896 |
{ |
| 897 |
const OPTION_OPENSSL_KEY_TYPE type = *(const OPTION_OPENSSL_KEY_TYPE*)value; |
| 898 |
if (type == KEY_TYPE_DEFAULT || type == KEY_TYPE_ENGINE) |
| 899 |
{ |
| 900 |
httpHandleData->x509privatekeytype = type; |
| 901 |
result = HTTPAPI_OK; |
| 902 |
} |
| 903 |
else |
| 904 |
{ |
| 905 |
LogError("Unknown x509PrivatekeyType: %i", type); |
| 906 |
result = HTTPAPI_ERROR; |
| 907 |
} |
| 908 |
} |
| 909 |
#ifndef OPENSSL_NO_ENGINE |
| 910 |
else if (strcmp(OPTION_OPENSSL_ENGINE, optionName) == 0) |
| 911 |
{ |
| 912 |
if (mallocAndStrcpy_s((char**)&httpHandleData->engineId, value) != 0) |
| 913 |
{ |
| 914 |
LogError("unable to mallocAndStrcpy_s x509PrivatekeyType"); |
| 915 |
result = HTTPAPI_ERROR; |
| 916 |
} |
| 917 |
else |
| 918 |
{ |
| 919 |
result = HTTPAPI_OK; |
| 920 |
} |
| 921 |
} |
| 922 |
#endif // OPENSSL_NO_ENGINE |
| 923 |
#endif |
| 924 |
else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0) |
| 925 |
{ |
| 926 |
httpHandleData->x509privatekey = value; |
| 927 |
if (httpHandleData->x509certificate != NULL) |
| 928 |
{ |
| 929 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_SSL_CTX_FUNCTION, ssl_ctx_callback) != CURLE_OK) |
| 930 |
{ |
| 931 |
LogError("unable to curl_easy_setopt"); |
| 932 |
result = HTTPAPI_ERROR; |
| 933 |
} |
| 934 |
else |
| 935 |
{ |
| 936 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_SSL_CTX_DATA, httpHandleData) != CURLE_OK) |
| 937 |
{ |
| 938 |
LogError("unable to curl_easy_setopt"); |
| 939 |
result = HTTPAPI_ERROR; |
| 940 |
} |
| 941 |
else |
| 942 |
{ |
| 943 |
result = HTTPAPI_OK; |
| 944 |
} |
| 945 |
} |
| 946 |
} |
| 947 |
else |
| 948 |
{ |
| 949 |
/*if privatekey comes 1st and certificate is not set yet, then return OK and wait for the certificate to be set*/ |
| 950 |
result = HTTPAPI_OK; |
| 951 |
} |
| 952 |
} |
| 953 |
else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0) |
| 954 |
{ |
| 955 |
httpHandleData->x509certificate = value; |
| 956 |
if (httpHandleData->x509privatekey != NULL) |
| 957 |
{ |
| 958 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_SSL_CTX_FUNCTION, ssl_ctx_callback) != CURLE_OK) |
| 959 |
{ |
| 960 |
LogError("unable to curl_easy_setopt"); |
| 961 |
result = HTTPAPI_ERROR; |
| 962 |
} |
| 963 |
else |
| 964 |
{ |
| 965 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_SSL_CTX_DATA, httpHandleData) != CURLE_OK) |
| 966 |
{ |
| 967 |
LogError("unable to curl_easy_setopt"); |
| 968 |
result = HTTPAPI_ERROR; |
| 969 |
} |
| 970 |
else |
| 971 |
{ |
| 972 |
result = HTTPAPI_OK; |
| 973 |
} |
| 974 |
} |
| 975 |
} |
| 976 |
else |
| 977 |
{ |
| 978 |
/*if certificate comes 1st and private key is not set yet, then return OK and wait for the private key to be set*/ |
| 979 |
result = HTTPAPI_OK; |
| 980 |
} |
| 981 |
} |
| 982 |
else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0) |
| 983 |
{ |
| 984 |
char proxy[MAX_HOSTNAME_LEN]; |
| 985 |
char* proxy_auth; |
| 986 |
|
| 987 |
HTTP_PROXY_OPTIONS* proxy_data = (HTTP_PROXY_OPTIONS*)value; |
| 988 |
|
| 989 |
if (sprintf_s(proxy, MAX_HOSTNAME_LEN, "%s:%d", proxy_data->host_address, proxy_data->port) <= 0) |
| 990 |
{ |
| 991 |
LogError("failure constructing proxy address"); |
| 992 |
result = HTTPAPI_ERROR; |
| 993 |
} |
| 994 |
else |
| 995 |
{ |
| 996 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_PROXY, proxy) != CURLE_OK) |
| 997 |
{ |
| 998 |
LogError("failure setting curl proxy address"); |
| 999 |
result = HTTPAPI_ERROR; |
| 1000 |
} |
| 1001 |
else |
| 1002 |
{ |
| 1003 |
if (proxy_data->username != NULL && proxy_data->password != NULL) |
| 1004 |
{ |
| 1005 |
size_t authLen = safe_add_size_t(strlen(proxy_data->username), strlen(proxy_data->password)); |
| 1006 |
authLen = safe_add_size_t(authLen, 2); |
| 1007 |
if (authLen == SIZE_MAX) |
| 1008 |
{ |
| 1009 |
LogError("Invalid malloc size"); |
| 1010 |
proxy_auth = NULL; |
| 1011 |
} |
| 1012 |
else |
| 1013 |
{ |
| 1014 |
proxy_auth = malloc(authLen); |
| 1015 |
} |
| 1016 |
|
| 1017 |
if (proxy_auth == NULL) |
| 1018 |
{ |
| 1019 |
LogError("failure allocating proxy authentication"); |
| 1020 |
result = HTTPAPI_ERROR; |
| 1021 |
} |
| 1022 |
else |
| 1023 |
{ |
| 1024 |
// From curl website 'Pass a char * as parameter, which should be [user name]:[password]' |
| 1025 |
if (sprintf_s(proxy_auth, MAX_HOSTNAME_LEN, "%s:%s", proxy_data->username, proxy_data->password) <= 0) |
| 1026 |
{ |
| 1027 |
LogError("failure constructing proxy authentication"); |
| 1028 |
result = HTTPAPI_ERROR; |
| 1029 |
} |
| 1030 |
else |
| 1031 |
{ |
| 1032 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_PROXYUSERPWD, proxy_auth) != CURLE_OK) |
| 1033 |
{ |
| 1034 |
LogError("failure setting curl proxy authentication"); |
| 1035 |
result = HTTPAPI_ERROR; |
| 1036 |
} |
| 1037 |
else |
| 1038 |
{ |
| 1039 |
result = HTTPAPI_OK; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
free(proxy_auth); |
| 1043 |
proxy_auth = NULL; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
else |
| 1047 |
{ |
| 1048 |
result = HTTPAPI_OK; |
| 1049 |
} |
| 1050 |
} |
| 1051 |
} |
| 1052 |
} |
| 1053 |
else if (strcmp("TrustedCerts", optionName) == 0) |
| 1054 |
{ |
| 1055 |
/*TrustedCerts needs to trigger the CURLOPT_SSL_CTX_FUNCTION in curl so we can pass the CAs*/ |
| 1056 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_SSL_CTX_FUNCTION, ssl_ctx_callback) != CURLE_OK) |
| 1057 |
{ |
| 1058 |
LogError("failure in curl_easy_setopt - CURLOPT_SSL_CTX_FUNCTION"); |
| 1059 |
result = HTTPAPI_ERROR; |
| 1060 |
} |
| 1061 |
else |
| 1062 |
{ |
| 1063 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_SSL_CTX_DATA, httpHandleData) != CURLE_OK) |
| 1064 |
{ |
| 1065 |
LogError("failure in curl_easy_setopt - CURLOPT_SSL_CTX_DATA"); |
| 1066 |
result = HTTPAPI_ERROR; |
| 1067 |
} |
| 1068 |
else |
| 1069 |
{ |
| 1070 |
httpHandleData->certificates = (const char*)value; |
| 1071 |
result = HTTPAPI_OK; |
| 1072 |
} |
| 1073 |
} |
| 1074 |
} |
| 1075 |
else if (strcmp(OPTION_CURL_INTERFACE, optionName) == 0) |
| 1076 |
{ |
| 1077 |
const char *interfaceName = (const char*)value; |
| 1078 |
if (interfaceName != NULL && interfaceName[0] != '\0') |
| 1079 |
{ |
| 1080 |
if (curl_easy_setopt(httpHandleData->curl, CURLOPT_INTERFACE, interfaceName) != CURLE_OK) |
| 1081 |
{ |
| 1082 |
LogError("unable to curl_easy_setopt for CURLOPT_INTERFACE"); |
| 1083 |
result = HTTPAPI_ERROR; |
| 1084 |
} |
| 1085 |
else |
| 1086 |
{ |
| 1087 |
result = HTTPAPI_OK; |
| 1088 |
} |
| 1089 |
} |
| 1090 |
else |
| 1091 |
{ |
| 1092 |
LogError("unable to curl_easy_setopt for CURLOPT_INTERFACE option as option-value is invalid/empty"); |
| 1093 |
result = HTTPAPI_ERROR; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
else |
| 1097 |
{ |
| 1098 |
result = HTTPAPI_INVALID_ARG; |
| 1099 |
LogError("unknown option %s", optionName); |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
return result; |
| 1104 |
} |
| 1105 |
|
| 1106 |
HTTPAPI_RESULT HTTPAPI_CloneOption(const char* optionName, const void* value, const void** savedValue) |
| 1107 |
{ |
| 1108 |
HTTPAPI_RESULT result; |
| 1109 |
if ( |
| 1110 |
(optionName == NULL) || |
| 1111 |
(value == NULL) || |
| 1112 |
(savedValue == NULL) |
| 1113 |
) |
| 1114 |
{ |
| 1115 |
result = HTTPAPI_INVALID_ARG; |
| 1116 |
LogError("invalid argument(NULL) passed to HTTPAPI_CloneOption"); |
| 1117 |
} |
| 1118 |
else |
| 1119 |
{ |
| 1120 |
if (strcmp(OPTION_HTTP_TIMEOUT, optionName) == 0) |
| 1121 |
{ |
| 1122 |
/*by convention value is pointing to an unsigned int */ |
| 1123 |
unsigned int* temp = malloc(sizeof(unsigned int)); /*shall be freed by HTTPAPIEX*/ |
| 1124 |
if (temp == NULL) |
| 1125 |
{ |
| 1126 |
result = HTTPAPI_ERROR; |
| 1127 |
LogError("malloc failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 1128 |
} |
| 1129 |
else |
| 1130 |
{ |
| 1131 |
*temp = *(const unsigned int*)value; |
| 1132 |
*savedValue = temp; |
| 1133 |
result = HTTPAPI_OK; |
| 1134 |
} |
| 1135 |
} |
| 1136 |
else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0) |
| 1137 |
{ |
| 1138 |
/*this is getting the x509 certificate. In this case, value is a pointer to a const char* that contains the certificate as a null terminated string*/ |
| 1139 |
if (mallocAndStrcpy_s((char**)savedValue, value) != 0) |
| 1140 |
{ |
| 1141 |
LogError("unable to clone the x509 certificate content"); |
| 1142 |
result = HTTPAPI_ERROR; |
| 1143 |
} |
| 1144 |
else |
| 1145 |
{ |
| 1146 |
/*return OK when the certificate has been cloned successfully*/ |
| 1147 |
result = HTTPAPI_OK; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
#ifdef USE_OPENSSL |
| 1151 |
else if (strcmp(OPTION_OPENSSL_PRIVATE_KEY_TYPE, optionName) == 0) |
| 1152 |
{ |
| 1153 |
const OPTION_OPENSSL_KEY_TYPE type = *(const OPTION_OPENSSL_KEY_TYPE*)value; |
| 1154 |
if (type == KEY_TYPE_DEFAULT || type == KEY_TYPE_ENGINE) |
| 1155 |
{ |
| 1156 |
OPTION_OPENSSL_KEY_TYPE* temp = malloc(sizeof(OPTION_OPENSSL_KEY_TYPE)); |
| 1157 |
if (temp == NULL) |
| 1158 |
{ |
| 1159 |
LogError("unable to clone x509PrivatekeyType"); |
| 1160 |
result = HTTPAPI_ERROR; |
| 1161 |
} |
| 1162 |
else |
| 1163 |
{ |
| 1164 |
*temp = type; |
| 1165 |
*savedValue = temp; |
| 1166 |
result = HTTPAPI_OK; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
else |
| 1170 |
{ |
| 1171 |
LogError("Unknown x509PrivatekeyType: %i", type); |
| 1172 |
result = HTTPAPI_ERROR; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
else if (strcmp(OPTION_OPENSSL_ENGINE, optionName) == 0) |
| 1176 |
{ |
| 1177 |
/*this is getting the engine. In this case, value is a pointer to a const char* that contains the engine as a null terminated string*/ |
| 1178 |
if (mallocAndStrcpy_s((char**)savedValue, value) != 0) |
| 1179 |
{ |
| 1180 |
LogError("unable to clone %s", optionName); |
| 1181 |
result = HTTPAPI_ERROR; |
| 1182 |
} |
| 1183 |
else |
| 1184 |
{ |
| 1185 |
/*return OK when the engine has been cloned successfully*/ |
| 1186 |
result = HTTPAPI_OK; |
| 1187 |
} |
| 1188 |
} |
| 1189 |
#endif |
| 1190 |
else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0) |
| 1191 |
{ |
| 1192 |
/*this is getting the x509 private key. In this case, value is a pointer to a const char* that contains the private key as a null terminated string*/ |
| 1193 |
if (mallocAndStrcpy_s((char**)savedValue, value) != 0) |
| 1194 |
{ |
| 1195 |
LogError("unable to clone the x509 private key content"); |
| 1196 |
result = HTTPAPI_ERROR; |
| 1197 |
} |
| 1198 |
else |
| 1199 |
{ |
| 1200 |
/*return OK when the private key has been cloned successfully*/ |
| 1201 |
result = HTTPAPI_OK; |
| 1202 |
} |
| 1203 |
} |
| 1204 |
else if (strcmp("TrustedCerts", optionName) == 0) |
| 1205 |
{ |
| 1206 |
if (mallocAndStrcpy_s((char**)savedValue, value) != 0) |
| 1207 |
{ |
| 1208 |
LogError("unable to clone TrustedCerts"); |
| 1209 |
result = HTTPAPI_ERROR; |
| 1210 |
} |
| 1211 |
else |
| 1212 |
{ |
| 1213 |
/*return OK when the certificates have been cloned successfully*/ |
| 1214 |
result = HTTPAPI_OK; |
| 1215 |
} |
| 1216 |
} |
| 1217 |
else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0) |
| 1218 |
{ |
| 1219 |
HTTP_PROXY_OPTIONS* proxy_data = (HTTP_PROXY_OPTIONS*)value; |
| 1220 |
|
| 1221 |
HTTP_PROXY_OPTIONS* new_proxy_info = malloc(sizeof(HTTP_PROXY_OPTIONS)); |
| 1222 |
if (new_proxy_info == NULL) |
| 1223 |
{ |
| 1224 |
LogError("unable to allocate proxy option information"); |
| 1225 |
result = HTTPAPI_ERROR; |
| 1226 |
} |
| 1227 |
else |
| 1228 |
{ |
| 1229 |
new_proxy_info->host_address = proxy_data->host_address; |
| 1230 |
new_proxy_info->port = proxy_data->port; |
| 1231 |
new_proxy_info->password = proxy_data->password; |
| 1232 |
new_proxy_info->username = proxy_data->username; |
| 1233 |
*savedValue = new_proxy_info; |
| 1234 |
result = HTTPAPI_OK; |
| 1235 |
} |
| 1236 |
} |
| 1237 |
/*all "long" options are cloned in the same way*/ |
| 1238 |
else if ( |
| 1239 |
(strcmp(OPTION_CURL_LOW_SPEED_LIMIT, optionName) == 0) || |
| 1240 |
(strcmp(OPTION_CURL_LOW_SPEED_TIME, optionName) == 0) || |
| 1241 |
(strcmp(OPTION_CURL_FRESH_CONNECT, optionName) == 0) || |
| 1242 |
(strcmp(OPTION_CURL_FORBID_REUSE, optionName) == 0) || |
| 1243 |
(strcmp(OPTION_CURL_VERBOSE, optionName) == 0) |
| 1244 |
) |
| 1245 |
{ |
| 1246 |
/*by convention value is pointing to an long */ |
| 1247 |
long* temp = malloc(sizeof(long)); /*shall be freed by HTTPAPIEX*/ |
| 1248 |
if (temp == NULL) |
| 1249 |
{ |
| 1250 |
result = HTTPAPI_ERROR; |
| 1251 |
LogError("malloc failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result)); |
| 1252 |
} |
| 1253 |
else |
| 1254 |
{ |
| 1255 |
*temp = *(const long*)value; |
| 1256 |
*savedValue = temp; |
| 1257 |
result = HTTPAPI_OK; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
else if (strcmp(OPTION_CURL_INTERFACE, optionName) == 0) |
| 1261 |
{ |
| 1262 |
if (mallocAndStrcpy_s((char**)savedValue, value) != 0) |
| 1263 |
{ |
| 1264 |
LogError("unable to clone the interface name"); |
| 1265 |
result = HTTPAPI_ERROR; |
| 1266 |
} |
| 1267 |
else |
| 1268 |
{ |
| 1269 |
/*return OK when the interface name has been cloned successfully*/ |
| 1270 |
result = HTTPAPI_OK; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
else |
| 1274 |
{ |
| 1275 |
result = HTTPAPI_INVALID_ARG; |
| 1276 |
LogError("unknown option %s", optionName); |
| 1277 |
} |
| 1278 |
} |
| 1279 |
return result; |
| 1280 |
} |