Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/adapters/httpapi_winhttp.c

raw
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 <stdlib.h>
5 #include <string.h>
6 #include "azure_macro_utils/macro_utils.h"
7 #include "azure_c_shared_utility/gballoc.h"
8 #include "azure_c_shared_utility/crt_abstractions.h"
9
10 #include "windows.h"
11 #include "winhttp.h"
12 #include "wincrypt.h"
13 #include "azure_c_shared_utility/httpapi.h"
14 #include "azure_c_shared_utility/httpheaders.h"
15 #include "azure_c_shared_utility/xlogging.h"
16 #include "azure_c_shared_utility/strings.h"
17 #include "azure_c_shared_utility/x509_schannel.h"
18 #include "azure_c_shared_utility/shared_util_options.h"
19 #include "azure_c_shared_utility/safe_math.h"
20
21 MU_DEFINE_ENUM_STRINGS(HTTPAPI_RESULT, HTTPAPI_RESULT_VALUES)
22
23 typedef enum HTTPAPI_STATE_TAG
24 {
25 HTTPAPI_NOT_INITIALIZED,
26 HTTPAPI_INITIALIZED
27 } HTTPAPI_STATE;
28
29 typedef struct HTTP_HANDLE_DATA_TAG
30 {
31 /*working set*/
32 HINTERNET ConnectionHandle;
33 X509_SCHANNEL_HANDLE x509SchannelHandle;
34 /*options*/
35 unsigned int timeout;
36 const char* x509certificate;
37 const char* x509privatekey;
38 const char* proxy_host;
39 const char* proxy_username;
40 const char* proxy_password;
41 // Certificate to check server certificate chains to, overriding built-in Windows certificate store certificates.
42 char* trustedCertificate;
43 // Set when a callback fails.
44 bool handleClosedOnCallbackError;
45 } HTTP_HANDLE_DATA;
46
47 static HTTPAPI_STATE g_HTTPAPIState = HTTPAPI_NOT_INITIALIZED;
48
49 /*There's a global SessionHandle for all the connections*/
50 static HINTERNET g_SessionHandle;
51 static size_t nUsersOfHTTPAPI = 0; /*used for reference counting (a weak one)*/
52
53 static char* ConcatHttpHeaders(HTTP_HEADERS_HANDLE httpHeadersHandle, size_t toAlloc, size_t headersCount)
54 {
55 size_t i;
56 char* result;
57
58 size_t malloc_size = safe_multiply_size_t(toAlloc, sizeof(char));
59 malloc_size = safe_add_size_t(malloc_size, 1);
60 if (malloc_size == SIZE_MAX)
61 {
62 LogError("Invalid malloc size");
63 result = NULL;
64 }
65 else
66 {
67 result = (char*)malloc(malloc_size);
68 }
69
70 if (result == NULL)
71 {
72 LogError("unable to malloc");
73 }
74 else
75 {
76 result[0] = '\0';
77 for (i = 0; i < headersCount; i++)
78 {
79 char* temp;
80 if (HTTPHeaders_GetHeader(httpHeadersHandle, i, &temp) != HTTP_HEADERS_OK)
81 {
82 LogError("unable to HTTPHeaders_GetHeader");
83 break;
84 }
85 else
86 {
87 (void)strcat(result, temp);
88 (void)strcat(result, "\r\n");
89 free(temp);
90 }
91 }
92
93 if (i < headersCount)
94 {
95 free(result);
96 result = NULL;
97 }
98 else
99 {
100 /*all is good*/
101 }
102 }
103
104 return result;
105 }
106
107 /*returns NULL if it failed to construct the headers*/
108 static HTTPAPI_RESULT ConstructHeadersString(HTTP_HEADERS_HANDLE httpHeadersHandle, wchar_t** httpHeaders)
109 {
110 HTTPAPI_RESULT result;
111 size_t headersCount;
112
113 if (HTTPHeaders_GetHeaderCount(httpHeadersHandle, &headersCount) != HTTP_HEADERS_OK)
114 {
115 result = HTTPAPI_ERROR;
116 LogError("HTTPHeaders_GetHeaderCount failed (result = %" PRI_MU_ENUM ").", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
117 }
118 else
119 {
120 size_t i;
121
122 /*the total size of all the headers is given by sumof(lengthof(everyheader)+2)*/
123 size_t toAlloc = 0;
124 for (i = 0; i < headersCount; i++)
125 {
126 char *temp;
127 if (HTTPHeaders_GetHeader(httpHeadersHandle, i, &temp) == HTTP_HEADERS_OK)
128 {
129 toAlloc += strlen(temp);
130 toAlloc += 2;
131 free(temp);
132 }
133 else
134 {
135 LogError("HTTPHeaders_GetHeader failed");
136 break;
137 }
138 }
139
140 if (i < headersCount)
141 {
142 result = HTTPAPI_ERROR;
143 }
144 else
145 {
146 char *httpHeadersA;
147 size_t requiredCharactersForHeaders;
148 size_t malloc_size;
149
150 if ((httpHeadersA = ConcatHttpHeaders(httpHeadersHandle, toAlloc, headersCount)) == NULL)
151 {
152 result = HTTPAPI_ERROR;
153 LogError("Cannot concatenate headers");
154 }
155 else if ((requiredCharactersForHeaders = MultiByteToWideChar(CP_ACP, 0, httpHeadersA, -1, NULL, 0)) == 0)
156 {
157 result = HTTPAPI_STRING_PROCESSING_ERROR;
158 LogError("MultiByteToWideChar failed, GetLastError=0x%08x (result = %" PRI_MU_ENUM ")", GetLastError(), MU_ENUM_VALUE(HTTPAPI_RESULT, result));
159 }
160 else if ((malloc_size = safe_multiply_size_t(safe_add_size_t(requiredCharactersForHeaders, 1), sizeof(wchar_t))) == SIZE_MAX ||
161 (*httpHeaders = (wchar_t*)malloc(malloc_size)) == NULL)
162 {
163 result = HTTPAPI_ALLOC_FAILED;
164 LogError("Cannot allocate memory (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
165 }
166 else if (MultiByteToWideChar(CP_ACP, 0, httpHeadersA, -1, *httpHeaders, (int)requiredCharactersForHeaders) == 0)
167 {
168 result = HTTPAPI_STRING_PROCESSING_ERROR;
169 LogError("MultiByteToWideChar failed, GetLastError=0x%08x (result = %" PRI_MU_ENUM ")", GetLastError(), MU_ENUM_VALUE(HTTPAPI_RESULT, result));
170 free(*httpHeaders);
171 *httpHeaders = NULL;
172 }
173 else
174 {
175 result = HTTPAPI_OK;
176 }
177
178 free(httpHeadersA);
179 }
180 }
181
182 return result;
183 }
184
185 void CALLBACK httpapi_WinhttpStatusCallback(
186 IN HINTERNET hInternet,
187 IN DWORD_PTR dwContext,
188 IN DWORD dwInternetStatus,
189 IN LPVOID lpvStatusInformation,
190 IN DWORD dwStatusInformationLength
191 )
192 {
193 HTTP_HANDLE_DATA* handleData = (HTTP_HANDLE_DATA*)dwContext;
194
195 (void)dwStatusInformationLength;
196 (void)lpvStatusInformation;
197
198 if (dwContext == 0)
199 {
200 LogError("WinhttpStatusCallback called without context set");
201 }
202 else if (dwInternetStatus != WINHTTP_CALLBACK_STATUS_SENDING_REQUEST)
203 {
204 // Silently ignore if there's any statuses we get that we can't handle
205 ;
206 }
207 else if (handleData->trustedCertificate != NULL)
208 {
209 PCERT_CONTEXT pCertContext = NULL;
210 DWORD bufferLength = sizeof(pCertContext);
211 bool certificateTrusted;
212
213 if (! WinHttpQueryOption(hInternet, WINHTTP_OPTION_SERVER_CERT_CONTEXT, (void*)&pCertContext, &bufferLength))
214 {
215 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpQueryOption(WINHTTP_OPTION_SERVER_CERT_CONTEXT) failed");
216 certificateTrusted = false;
217 }
218 else if (x509_verify_certificate_in_chain(handleData->trustedCertificate, pCertContext) != 0)
219 {
220 LogError("Certificate does not chain up correctly");
221 certificateTrusted = false;
222 }
223 else
224 {
225 certificateTrusted = true;
226 }
227
228 if (certificateTrusted == false)
229 {
230 LogError("Server certificate is not trusted. Aborting HTTP request");
231 // To signal to caller that the request is to be terminated, the callback closes the handle.
232 WinHttpCloseHandle(hInternet);
233 // To avoid a double free of this handle (in HTTPAPI_ExecuteRequset cleanup) record we've processed close already.
234 handleData->handleClosedOnCallbackError = true;
235 }
236
237 if (pCertContext != NULL)
238 {
239 CertFreeCertificateContext(pCertContext);
240 }
241 }
242 }
243
244
245 HTTPAPI_RESULT HTTPAPI_Init(void)
246 {
247 HTTPAPI_RESULT result;
248
249 if (nUsersOfHTTPAPI == 0)
250 {
251 if ((g_SessionHandle = WinHttpOpen(
252 NULL,
253 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
254 WINHTTP_NO_PROXY_NAME,
255 WINHTTP_NO_PROXY_BYPASS,
256 0)) == NULL)
257 {
258 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpOpen failed.");
259 result = HTTPAPI_INIT_FAILED;
260 }
261 else
262 {
263 DWORD supportedProtocols = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 | // TLS 1.0 is support for back-compat reasons (https://docs.microsoft.com/azure/iot-fundamentals/iot-security-deployment)
264 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 |
265 WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2;
266
267 if (!WinHttpSetOption(
268 g_SessionHandle,
269 WINHTTP_OPTION_SECURE_PROTOCOLS,
270 &supportedProtocols,
271 sizeof(supportedProtocols)
272 ))
273 {
274 LogErrorWinHTTPWithGetLastErrorAsString("unable to WinHttpSetOption (WINHTTP_OPTION_SECURE_PROTOCOLS)");
275 (void)WinHttpCloseHandle(g_SessionHandle);
276 g_SessionHandle = NULL;
277 result = HTTPAPI_INIT_FAILED;
278 }
279 else if (WinHttpSetStatusCallback(g_SessionHandle, httpapi_WinhttpStatusCallback, WINHTTP_CALLBACK_FLAG_SEND_REQUEST, 0) == WINHTTP_INVALID_STATUS_CALLBACK)
280 {
281 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpSetStatusCallback failed.");
282 (void)WinHttpCloseHandle(g_SessionHandle);
283 g_SessionHandle = NULL;
284 result = HTTPAPI_INIT_FAILED;
285 }
286 else
287 {
288 nUsersOfHTTPAPI++;
289 g_HTTPAPIState = HTTPAPI_INITIALIZED;
290 result = HTTPAPI_OK;
291 }
292 }
293 }
294 else
295 {
296 nUsersOfHTTPAPI++;
297 result = HTTPAPI_OK;
298 }
299
300 return result;
301 }
302
303 void HTTPAPI_Deinit(void)
304 {
305 if (nUsersOfHTTPAPI > 0)
306 {
307 nUsersOfHTTPAPI--;
308 if (nUsersOfHTTPAPI == 0)
309 {
310 if (g_SessionHandle != NULL)
311 {
312 (void)WinHttpCloseHandle(g_SessionHandle);
313 g_SessionHandle = NULL;
314 g_HTTPAPIState = HTTPAPI_NOT_INITIALIZED;
315 }
316 }
317 }
318
319
320 }
321
322 HTTP_HANDLE HTTPAPI_CreateConnection(const char* hostName)
323 {
324 HTTP_HANDLE_DATA* result;
325 if (g_HTTPAPIState != HTTPAPI_INITIALIZED)
326 {
327 LogError("g_HTTPAPIState not HTTPAPI_INITIALIZED");
328 result = NULL;
329 }
330 else
331 {
332 result = (HTTP_HANDLE_DATA*)malloc(sizeof(HTTP_HANDLE_DATA));
333 if (result == NULL)
334 {
335 LogError("malloc returned NULL.");
336 }
337 else
338 {
339 memset(result, 0, sizeof(*result));
340 wchar_t* hostNameTemp;
341 size_t hostNameTemp_size = MultiByteToWideChar(CP_ACP, 0, hostName, -1, NULL, 0);
342 if (hostNameTemp_size == 0)
343 {
344 LogError("MultiByteToWideChar failed");
345 free(result);
346 result = NULL;
347 }
348 else
349 {
350 size_t malloc_size = safe_multiply_size_t(sizeof(wchar_t), hostNameTemp_size);
351 if (malloc_size == SIZE_MAX)
352 {
353 LogError("Invalid malloc size");
354 hostNameTemp = NULL;
355 }
356 else
357 {
358 hostNameTemp = (wchar_t*)malloc(malloc_size);
359 }
360
361 if (hostNameTemp == NULL)
362 {
363 LogError("malloc failed");
364 free(result);
365 result = NULL;
366 }
367 else
368 {
369 if (MultiByteToWideChar(CP_ACP, 0, hostName, -1, hostNameTemp, (int)hostNameTemp_size) == 0)
370 {
371 LogError("MultiByteToWideChar failed");
372 free(result);
373 result = NULL;
374 }
375 else
376 {
377 result->ConnectionHandle = WinHttpConnect(
378 g_SessionHandle,
379 hostNameTemp,
380 INTERNET_DEFAULT_HTTPS_PORT,
381 0);
382
383 if (result->ConnectionHandle == NULL)
384 {
385 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpConnect returned NULL.");
386 free(result);
387 result = NULL;
388 }
389 else
390 {
391 result->timeout = 60000;
392 }
393 }
394 free(hostNameTemp);
395 }
396 }
397 }
398 }
399
400 return (HTTP_HANDLE)result;
401 }
402
403 void HTTPAPI_CloseConnection(HTTP_HANDLE handle)
404 {
405 if (g_HTTPAPIState != HTTPAPI_INITIALIZED)
406 {
407 LogError("g_HTTPAPIState not HTTPAPI_INITIALIZED");
408 }
409 else
410 {
411 HTTP_HANDLE_DATA* handleData = (HTTP_HANDLE_DATA*)handle;
412
413 if (handleData != NULL)
414 {
415 if (handleData->ConnectionHandle != NULL)
416 {
417 (void)WinHttpCloseHandle(handleData->ConnectionHandle);
418 /*no x509 free because the options are owned by httpapiex.*/
419 handleData->ConnectionHandle = NULL;
420 }
421 if (handleData->proxy_host != NULL)
422 {
423 free((void*)handleData->proxy_host);
424 }
425 if (handleData->proxy_username != NULL)
426 {
427 free((void*)handleData->proxy_username);
428 }
429 if (handleData->proxy_password != NULL)
430 {
431 free((void*)handleData->proxy_password);
432 }
433 x509_schannel_destroy(handleData->x509SchannelHandle);
434 free(handleData);
435 }
436 }
437 }
438
439 static const wchar_t* GetHttpRequestString(HTTPAPI_REQUEST_TYPE requestType)
440 {
441 wchar_t* requestTypeString = NULL;
442
443 switch (requestType)
444 {
445 default:
446 break;
447
448 case HTTPAPI_REQUEST_GET:
449 requestTypeString = L"GET";
450 break;
451
452 case HTTPAPI_REQUEST_HEAD:
453 requestTypeString = L"HEAD";
454 break;
455
456 case HTTPAPI_REQUEST_POST:
457 requestTypeString = L"POST";
458 break;
459
460 case HTTPAPI_REQUEST_PUT:
461 requestTypeString = L"PUT";
462 break;
463
464 case HTTPAPI_REQUEST_DELETE:
465 requestTypeString = L"DELETE";
466 break;
467
468 case HTTPAPI_REQUEST_PATCH:
469 requestTypeString = L"PATCH";
470 break;
471 }
472
473 return requestTypeString;
474 }
475
476 static HTTPAPI_RESULT InitiateWinhttpRequest(HTTP_HANDLE_DATA* handleData, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath, HINTERNET *requestHandle)
477 {
478 HTTPAPI_RESULT result;
479 const wchar_t* requestTypeString;
480 size_t requiredCharactersForRelativePath;
481 wchar_t* relativePathTemp = NULL;
482 size_t malloc_size;
483
484 if ((requestTypeString = GetHttpRequestString(requestType)) == NULL)
485 {
486 result = HTTPAPI_INVALID_ARG;
487 LogError("requestTypeString was NULL (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
488 }
489 else if ((requiredCharactersForRelativePath = MultiByteToWideChar(CP_ACP, 0, relativePath, -1, NULL, 0)) == 0)
490 {
491 result = HTTPAPI_STRING_PROCESSING_ERROR;
492 LogError("MultiByteToWideChar failed, GetLastError=0x%08x", GetLastError());
493 }
494 else if ((malloc_size = safe_multiply_size_t(safe_add_size_t(requiredCharactersForRelativePath, 1), sizeof(wchar_t))) == SIZE_MAX)
495 {
496 LogError("malloc invalid size");
497 result = HTTPAPI_ALLOC_FAILED;
498 }
499 else if ((relativePathTemp = (wchar_t*)malloc(malloc_size)) == NULL)
500 {
501 result = HTTPAPI_ALLOC_FAILED;
502 LogError("malloc failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
503 }
504 else if (MultiByteToWideChar(CP_ACP, 0, relativePath, -1, relativePathTemp, (int)requiredCharactersForRelativePath) == 0)
505 {
506 result = HTTPAPI_STRING_PROCESSING_ERROR;
507 LogError("MultiByteToWideChar was 0. (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
508 }
509 else if ((*requestHandle = WinHttpOpenRequest(
510 handleData->ConnectionHandle,
511 requestTypeString,
512 relativePathTemp,
513 NULL,
514 WINHTTP_NO_REFERER,
515 WINHTTP_DEFAULT_ACCEPT_TYPES,
516 WINHTTP_FLAG_SECURE)) == NULL)
517 {
518 result = HTTPAPI_OPEN_REQUEST_FAILED;
519 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpOpenRequest failed (result = %" PRI_MU_ENUM ").", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
520 }
521 else if ((handleData->x509SchannelHandle != NULL) &&
522 !WinHttpSetOption(
523 *requestHandle,
524 WINHTTP_OPTION_CLIENT_CERT_CONTEXT,
525 (void*)x509_schannel_get_certificate_context(handleData->x509SchannelHandle),
526 sizeof(CERT_CONTEXT)
527 ))
528 {
529 result = HTTPAPI_SET_X509_FAILURE;
530 LogErrorWinHTTPWithGetLastErrorAsString("unable to WinHttpSetOption (WINHTTP_OPTION_CLIENT_CERT_CONTEXT)");
531 (void)WinHttpCloseHandle(*requestHandle);
532 *requestHandle = NULL;
533 }
534 else
535 {
536 result = HTTPAPI_OK;
537 }
538
539 free(relativePathTemp);
540 return result;
541 }
542
543 static HTTPAPI_RESULT SendHttpRequest(HTTP_HANDLE_DATA* handleData, HINTERNET requestHandle, const unsigned char* content, size_t contentLength, const wchar_t* httpHeaders)
544 {
545 HTTPAPI_RESULT result;
546
547 if (WinHttpSetTimeouts(requestHandle,
548 0, /*_In_ int dwResolveTimeout - The initial value is zero, meaning no time-out (infinite). */
549 60000, /*_In_ int dwConnectTimeout, - The initial value is 60,000 (60 seconds).*/
550 handleData->timeout, /*_In_ int dwSendTimeout, - The initial value is 30,000 (30 seconds).*/
551 handleData->timeout /* int dwReceiveTimeout The initial value is 30,000 (30 seconds).*/
552 ) == FALSE)
553 {
554 result = HTTPAPI_SET_TIMEOUTS_FAILED;
555 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpOpenRequest failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
556 }
557 else
558 {
559 DWORD dwSecurityFlags;
560
561 if (handleData->trustedCertificate != NULL)
562 {
563 // If caller has specified trusted certificates, then we'll instruct Winhttp to ignore certain classes
564 // of invalid certificate errors (since the trusted cert won't chain into the Windows cert store).
565 // We will validate certificate manually during Winhttp callbacks.
566 dwSecurityFlags = SECURITY_FLAG_IGNORE_UNKNOWN_CA;
567 }
568 else
569 {
570 dwSecurityFlags = 0;
571 }
572
573 if (!WinHttpSetOption(
574 requestHandle,
575 WINHTTP_OPTION_SECURITY_FLAGS,
576 &dwSecurityFlags,
577 sizeof(dwSecurityFlags)))
578 {
579 result = HTTPAPI_SET_OPTION_FAILED;
580 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpSetOption failed (result = %" PRI_MU_ENUM ").", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
581 }
582 else if (!WinHttpSendRequest(
583 requestHandle,
584 httpHeaders,
585 (DWORD)-1L, /*An unsigned long integer value that contains the length, in characters, of the additional headers. If this parameter is -1L ... */
586 (void*)content,
587 (DWORD)contentLength,
588 (DWORD)contentLength,
589 (DWORD_PTR)handleData))
590 {
591 result = HTTPAPI_SEND_REQUEST_FAILED;
592 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpSendRequest: (result = %" PRI_MU_ENUM ").", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
593 }
594 else
595 {
596 result = HTTPAPI_OK;
597 }
598 }
599
600 return result;
601 }
602
603 static HTTPAPI_RESULT SetProxyIfNecessary(HTTP_HANDLE_DATA* handleData, HINTERNET requestHandle)
604 {
605 HTTPAPI_RESULT result;
606
607 // Set proxy host if needed
608 if (handleData->proxy_host != NULL)
609 {
610 WINHTTP_PROXY_INFO winhttp_proxy;
611 wchar_t wproxy[MAX_HOSTNAME_LEN];
612 winhttp_proxy.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
613 if (mbstowcs_s(NULL, wproxy, MAX_HOSTNAME_LEN, handleData->proxy_host, MAX_HOSTNAME_LEN-1) != 0)
614 {
615 LogError("Error during proxy host conversion");
616 result = HTTPAPI_ERROR;
617 }
618 else
619 {
620 winhttp_proxy.lpszProxy = wproxy;
621 winhttp_proxy.lpszProxyBypass = NULL;
622 if (WinHttpSetOption(requestHandle,
623 WINHTTP_OPTION_PROXY,
624 &winhttp_proxy,
625 (DWORD)sizeof(WINHTTP_PROXY_INFO)) != TRUE)
626 {
627 LogError("failure setting proxy address (%i)", GetLastError());
628 result = HTTPAPI_ERROR;
629 }
630 else
631 {
632 //Set username and password if needed
633 if (handleData->proxy_username != NULL && handleData->proxy_password != NULL)
634 {
635 wchar_t wusername[MAX_USERNAME_LEN];
636 if (mbstowcs_s(NULL, wusername, MAX_USERNAME_LEN, handleData->proxy_username, MAX_USERNAME_LEN-1) != 0)
637 {
638 LogError("Error during proxy username conversion");
639 result = HTTPAPI_ERROR;
640 }
641 else
642 {
643 wchar_t wpassword[MAX_PASSWORD_LEN];
644 if (mbstowcs_s(NULL, wpassword, MAX_PASSWORD_LEN, handleData->proxy_password, MAX_PASSWORD_LEN-1) != 0)
645 {
646 LogError("Error during proxy password conversion");
647 result = HTTPAPI_ERROR;
648 }
649 else
650 {
651 if (WinHttpSetCredentials(requestHandle,
652 WINHTTP_AUTH_TARGET_PROXY,
653 WINHTTP_AUTH_SCHEME_BASIC,
654 wusername,
655 wpassword,
656 NULL) != TRUE)
657 {
658 LogErrorWinHTTPWithGetLastErrorAsString("Failure setting proxy credentials");
659 result = HTTPAPI_ERROR;
660 }
661 else
662 {
663 result = HTTPAPI_OK;
664 }
665 }
666 }
667 }
668 else
669 {
670 result = HTTPAPI_OK;
671 }
672 }
673 }
674 }
675 else
676 {
677 result = HTTPAPI_OK;
678 }
679
680 return result;
681 }
682
683 static HTTPAPI_RESULT ReceiveResponseAndStatusCode(HINTERNET requestHandle, unsigned int* statusCode)
684 {
685 HTTPAPI_RESULT result;
686
687 if (!WinHttpReceiveResponse(
688 requestHandle,
689 0))
690 {
691 result = HTTPAPI_RECEIVE_RESPONSE_FAILED;
692 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpReceiveResponse: (result = %" PRI_MU_ENUM ").", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
693 }
694 else if (statusCode != NULL)
695 {
696 DWORD dwStatusCode = 0;
697 DWORD dwBufferLength = sizeof(DWORD);
698
699 if (!WinHttpQueryHeaders(
700 requestHandle,
701 WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
702 WINHTTP_HEADER_NAME_BY_INDEX,
703 &dwStatusCode,
704 &dwBufferLength,
705 WINHTTP_NO_HEADER_INDEX))
706 {
707 result = HTTPAPI_QUERY_HEADERS_FAILED;
708 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpQueryHeaders failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
709 }
710 else
711 {
712 *statusCode = dwStatusCode;
713 result = HTTPAPI_OK;
714 }
715 }
716 else
717 {
718 result = HTTPAPI_OK;
719 }
720
721 return result;
722 }
723
724 static HTTPAPI_RESULT ReceiveResponseContent(HINTERNET requestHandle, BUFFER_HANDLE responseContent)
725 {
726 HTTPAPI_RESULT result;
727 DWORD responseBytesAvailable;
728
729 while (true)
730 {
731 /*from MSDN: If no data is available and the end of the file has not been reached, one of two things happens. If the session is synchronous, the request waits until data becomes available.*/
732 if (!WinHttpQueryDataAvailable(requestHandle, &responseBytesAvailable))
733 {
734 result = HTTPAPI_QUERY_DATA_AVAILABLE_FAILED;
735 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpQueryDataAvailable failed (result = %" PRI_MU_ENUM ").", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
736 break;
737 }
738 else if (responseBytesAvailable == 0)
739 {
740 /*end of the stream, go out*/
741 result = HTTPAPI_OK;
742 break;
743 }
744 else if (BUFFER_enlarge(responseContent, responseBytesAvailable) != 0)
745 {
746 result = HTTPAPI_ALLOC_FAILED;
747 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
748 break;
749 }
750 else
751 {
752 /*Add the read bytes to the response buffer*/
753 size_t bufferSize;
754 const unsigned char* bufferContent;
755
756 if (BUFFER_content(responseContent, &bufferContent) != 0)
757 {
758 result = HTTPAPI_ERROR;
759 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
760 break;
761 }
762 else if (BUFFER_size(responseContent, &bufferSize) != 0)
763 {
764 result = HTTPAPI_ERROR;
765 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
766 break;
767 }
768 else
769 {
770 DWORD bytesReceived;
771 if (!WinHttpReadData(requestHandle, (LPVOID)(bufferContent + bufferSize - responseBytesAvailable), responseBytesAvailable, &bytesReceived))
772 {
773 result = HTTPAPI_READ_DATA_FAILED;
774 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpReadData failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
775 break;
776 }
777 /*if for some reason bytesReceived is zero If you are using WinHttpReadData synchronously, and the return value is TRUE and the number of bytes read is zero, the transfer has been completed and there are no more bytes to read on the handle.*/
778 else if (bytesReceived == 0)
779 {
780 /*end of everything, but this looks like an error still, or a non-conformance between WinHttpQueryDataAvailable and WinHttpReadData*/
781 result = HTTPAPI_READ_DATA_FAILED;
782 LogError("bytesReceived was unexpectedly zero (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
783 break;
784 }
785 else
786 {
787 /*all is fine, keep going*/
788 }
789 }
790 }
791 }
792
793 return result;
794 }
795
796
797 static HTTPAPI_RESULT ReceiveResponseHeaders(HINTERNET requestHandle, HTTP_HEADERS_HANDLE responseHeadersHandle)
798 {
799 HTTPAPI_RESULT result;
800 wchar_t* responseHeadersTemp = NULL;
801 DWORD responseHeadersTempLength = 0;
802
803 // Don't explicictly check return code here - since this should fail (to determine length)
804 // and checking return code means an explicit GetLastError() comparison. Instead
805 // rely on subsequent WinHttpQueryHeaders() to fail.
806 WinHttpQueryHeaders(
807 requestHandle,
808 WINHTTP_QUERY_RAW_HEADERS_CRLF,
809 WINHTTP_HEADER_NAME_BY_INDEX,
810 WINHTTP_NO_OUTPUT_BUFFER,
811 &responseHeadersTempLength,
812 WINHTTP_NO_HEADER_INDEX);
813
814 size_t malloc_size = safe_add_size_t((size_t)responseHeadersTempLength, 2);
815 if (malloc_size == SIZE_MAX)
816 {
817 LogError("Invalid malloc size");
818 responseHeadersTemp = NULL;
819 }
820 else
821 {
822 responseHeadersTemp = (wchar_t*)malloc(malloc_size);
823 }
824
825 if (responseHeadersTemp == NULL)
826 {
827 result = HTTPAPI_ALLOC_FAILED;
828 LogError("malloc failed: (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
829 }
830 else if (! WinHttpQueryHeaders(
831 requestHandle,
832 WINHTTP_QUERY_RAW_HEADERS_CRLF,
833 WINHTTP_HEADER_NAME_BY_INDEX,
834 responseHeadersTemp,
835 &responseHeadersTempLength,
836 WINHTTP_NO_HEADER_INDEX))
837 {
838 result = HTTPAPI_QUERY_HEADERS_FAILED;
839 LogErrorWinHTTPWithGetLastErrorAsString("WinHttpQueryHeaders failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
840 }
841 else
842 {
843 wchar_t *next_token = NULL;
844 wchar_t* token = wcstok_s(responseHeadersTemp, L"\r\n", &next_token);
845 char* tokenTemp = NULL;
846
847 result = HTTPAPI_OK;
848
849 while ((token != NULL) &&
850 (token[0] != L'\0'))
851 {
852 size_t tokenTemp_size;
853
854 tokenTemp_size = WideCharToMultiByte(CP_ACP, 0, token, -1, NULL, 0, NULL, NULL);
855 if (tokenTemp_size == 0)
856 {
857 result = HTTPAPI_STRING_PROCESSING_ERROR;
858 LogError("WideCharToMultiByte failed");
859 break;
860 }
861 else if ((malloc_size = safe_multiply_size_t(sizeof(char), tokenTemp_size)) == SIZE_MAX)
862 {
863 result = HTTPAPI_ALLOC_FAILED;
864 LogError("invalid malloc size");
865 break;
866 }
867 else if ((tokenTemp = (char*)malloc(malloc_size)) == NULL)
868 {
869 result = HTTPAPI_ALLOC_FAILED;
870 LogError("malloc failed");
871 break;
872 }
873 else if (WideCharToMultiByte(CP_ACP, 0, token, -1, tokenTemp, (int)tokenTemp_size, NULL, NULL) == 0)
874 {
875 result = HTTPAPI_STRING_PROCESSING_ERROR;
876 LogError("WideCharToMultiByte failed");
877 break;
878 }
879 else
880 {
881 /*breaking the token in 2 parts: everything before the first ":" and everything after the first ":"*/
882 /*if there is no such character, then skip it*/
883 /*if there is a : then replace is by a '\0' and so it breaks the original string in name and value*/
884 char* whereIsColon = strchr(tokenTemp, ':');
885 if (whereIsColon != NULL)
886 {
887 *whereIsColon = '\0';
888 if (HTTPHeaders_AddHeaderNameValuePair(responseHeadersHandle, tokenTemp, whereIsColon + 1) != HTTP_HEADERS_OK)
889 {
890 LogError("HTTPHeaders_AddHeaderNameValuePair failed");
891 result = HTTPAPI_HTTP_HEADERS_FAILED;
892 break;
893 }
894 }
895 }
896 free(tokenTemp);
897 tokenTemp = NULL;
898
899 token = wcstok_s(NULL, L"\r\n", &next_token);
900 }
901
902 free(tokenTemp);
903 tokenTemp = NULL;
904 }
905
906 free(responseHeadersTemp);
907 return result;
908 }
909
910
911 HTTPAPI_RESULT HTTPAPI_ExecuteRequest(HTTP_HANDLE handle, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath,
912 HTTP_HEADERS_HANDLE httpHeadersHandle, const unsigned char* content,
913 size_t contentLength, unsigned int* statusCode,
914 HTTP_HEADERS_HANDLE responseHeadersHandle, BUFFER_HANDLE responseContent)
915 {
916 HTTPAPI_RESULT result;
917
918 if (g_HTTPAPIState != HTTPAPI_INITIALIZED)
919 {
920 LogError("g_HTTPAPIState not HTTPAPI_INITIALIZED");
921 result = HTTPAPI_NOT_INIT;
922 }
923 else if ((handle == NULL) ||
924 (relativePath == NULL) ||
925 (httpHeadersHandle == NULL))
926 {
927 result = HTTPAPI_INVALID_ARG;
928 LogError("NULL parameter detected (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
929 }
930 else
931 {
932 HTTP_HANDLE_DATA* handleData = (HTTP_HANDLE_DATA*)handle;
933 wchar_t* httpHeaders = NULL;
934 HINTERNET requestHandle = NULL;
935
936 if ((result = InitiateWinhttpRequest(handleData, requestType, relativePath, &requestHandle)) != HTTPAPI_OK)
937 {
938 LogError("Cannot create Winhttp request handle");
939 }
940 else if ((result = SetProxyIfNecessary(handleData, requestHandle)) != HTTPAPI_OK)
941 {
942 LogError("unable to set proxy");
943 }
944 else if ((result = ConstructHeadersString(httpHeadersHandle, &httpHeaders)) != HTTPAPI_OK)
945 {
946 LogError("Cannot construct http headers");
947 }
948 else if ((result = SendHttpRequest(handleData, requestHandle, content, contentLength, httpHeaders)) != HTTPAPI_OK)
949 {
950 LogError("Cannot set options / send http request");
951 }
952 else if ((result = ReceiveResponseAndStatusCode(requestHandle, statusCode)) != HTTPAPI_OK)
953 {
954 LogError("failed receiving response and/or headers");
955 }
956 else if ((responseContent != NULL) && ((result = ReceiveResponseContent(requestHandle, responseContent)) != HTTPAPI_OK))
957 {
958 LogError("failed to receive response content");
959 }
960 else if ((responseHeadersHandle != NULL) && ((result = ReceiveResponseHeaders(requestHandle, responseHeadersHandle)) != HTTPAPI_OK))
961 {
962 LogError("Unable to retrieve http response headers");
963 }
964 else
965 {
966 result = HTTPAPI_OK;
967 }
968
969 if ((requestHandle != NULL) && (handleData->handleClosedOnCallbackError == false))
970 {
971 (void)WinHttpCloseHandle(requestHandle);
972 }
973 handleData->handleClosedOnCallbackError = false;
974
975 free(httpHeaders);
976 }
977
978 return result;
979 }
980
981 HTTPAPI_RESULT HTTPAPI_SetOption(HTTP_HANDLE handle, const char* optionName, const void* value)
982 {
983 HTTPAPI_RESULT result;
984 if (
985 (handle == NULL) ||
986 (optionName == NULL) ||
987 (value == NULL)
988 )
989 {
990 result = HTTPAPI_INVALID_ARG;
991 LogError("invalid parameter (NULL) passed to HTTPAPI_SetOption");
992 }
993 else
994 {
995 HTTP_HANDLE_DATA* httpHandleData = (HTTP_HANDLE_DATA*)handle;
996 if (strcmp(OPTION_HTTP_TIMEOUT, optionName) == 0)
997 {
998 long timeout = (long)(*(unsigned int*)value);
999 httpHandleData->timeout = timeout;
1000 result = HTTPAPI_OK;
1001 }
1002 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
1003 {
1004 httpHandleData->x509certificate = (const char*)value;
1005 if (httpHandleData->x509privatekey != NULL)
1006 {
1007 httpHandleData->x509SchannelHandle = x509_schannel_create(httpHandleData->x509certificate, httpHandleData->x509privatekey);
1008 if (httpHandleData->x509SchannelHandle == NULL)
1009 {
1010 LogError("unable to x509_schannel_create");
1011 result = HTTPAPI_ERROR;
1012 }
1013 else
1014 {
1015 result = HTTPAPI_OK;
1016 }
1017 }
1018 else
1019 {
1020 /*if certificate comes 1st and private key is not set yet, then return OK and wait for the private key to be set*/
1021 result = HTTPAPI_OK;
1022 }
1023 }
1024 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
1025 {
1026 httpHandleData->x509privatekey = (const char*)value;
1027 if (httpHandleData->x509certificate != NULL)
1028 {
1029 httpHandleData->x509SchannelHandle = x509_schannel_create(httpHandleData->x509certificate, httpHandleData->x509privatekey);
1030 if (httpHandleData->x509SchannelHandle == NULL)
1031 {
1032 LogError("unable to x509_schannel_create");
1033 result = HTTPAPI_ERROR;
1034 }
1035 else
1036 {
1037 result = HTTPAPI_OK;
1038 }
1039 }
1040 else
1041 {
1042 /*if privatekey comes 1st and certificate is not set yet, then return OK and wait for the certificate to be set*/
1043 result = HTTPAPI_OK;
1044 }
1045 }
1046 else if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
1047 {
1048 if (value == NULL)
1049 {
1050 LogError("Invalid paramater: OPTION_TRUSTED_CERT value=NULL");
1051 result = MU_FAILURE;
1052 }
1053 else
1054 {
1055 if (httpHandleData->trustedCertificate != NULL)
1056 {
1057 free(httpHandleData->trustedCertificate);
1058 httpHandleData->trustedCertificate = NULL;
1059 }
1060
1061 if (mallocAndStrcpy_s((char**)&httpHandleData->trustedCertificate, (const char*)value) != 0)
1062 {
1063 LogError("unable to mallocAndStrcpy_s option trusted certificate");
1064 result = MU_FAILURE;
1065 }
1066 else
1067 {
1068 result = HTTPAPI_OK;
1069 }
1070 }
1071 }
1072 else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0)
1073 {
1074 char proxyAddressAndPort[MAX_HOSTNAME_LEN];
1075 HTTP_PROXY_OPTIONS* proxy_data = (HTTP_PROXY_OPTIONS*)value;
1076 if (proxy_data->host_address == NULL || proxy_data->port <= 0)
1077 {
1078 LogError("invalid proxy_data values ( host_address = %p, port = %d)", proxy_data->host_address, proxy_data->port);
1079 result = HTTPAPI_ERROR;
1080 }
1081 else
1082 {
1083 if (sprintf_s(proxyAddressAndPort, MAX_HOSTNAME_LEN, "%s:%d", proxy_data->host_address, proxy_data->port) <= 0)
1084 {
1085 LogError("failure constructing proxy address");
1086 result = HTTPAPI_ERROR;
1087 }
1088 else
1089 {
1090 if(httpHandleData->proxy_host != NULL)
1091 {
1092 free((void*)httpHandleData->proxy_host);
1093 httpHandleData->proxy_host = NULL;
1094 }
1095 if(mallocAndStrcpy_s((char**)&(httpHandleData->proxy_host), (const char*)proxyAddressAndPort) != 0)
1096 {
1097 LogError("failure allocate proxy host");
1098 result = HTTPAPI_ERROR;
1099 }
1100 else
1101 {
1102 if (proxy_data->username != NULL && proxy_data->password != NULL)
1103 {
1104 if(httpHandleData->proxy_username != NULL)
1105 {
1106 free((void*)httpHandleData->proxy_username);
1107 httpHandleData->proxy_username = NULL;
1108 }
1109 if(mallocAndStrcpy_s((char**)&(httpHandleData->proxy_username), (const char*)proxy_data->username) != 0)
1110 {
1111 LogError("failure allocate proxy username");
1112 free((void*)httpHandleData->proxy_host);
1113 httpHandleData->proxy_host = NULL;
1114 result = HTTPAPI_ERROR;
1115 }
1116 else
1117 {
1118 if(httpHandleData->proxy_password != NULL)
1119 {
1120 free((void*)httpHandleData->proxy_password);
1121 httpHandleData->proxy_password = NULL;
1122 }
1123 if(mallocAndStrcpy_s((char**)&(httpHandleData->proxy_password), (const char*)proxy_data->password) != 0)
1124 {
1125 LogError("failure allocate proxy password");
1126 free((void*)httpHandleData->proxy_host);
1127 httpHandleData->proxy_host = NULL;
1128 free((void*)httpHandleData->proxy_username);
1129 httpHandleData->proxy_username = NULL;
1130 result = HTTPAPI_ERROR;
1131 }
1132 else
1133 {
1134 result = HTTPAPI_OK;
1135 }
1136 }
1137 }
1138 else
1139 {
1140 result = HTTPAPI_OK;
1141 }
1142 }
1143 }
1144 }
1145 }
1146 else
1147 {
1148 result = HTTPAPI_INVALID_ARG;
1149 LogError("unknown option %s", optionName);
1150 }
1151 }
1152 return result;
1153 }
1154
1155 HTTPAPI_RESULT HTTPAPI_CloneOption(const char* optionName, const void* value, const void** savedValue)
1156 {
1157 HTTPAPI_RESULT result;
1158 if (
1159 (optionName == NULL) ||
1160 (value == NULL) ||
1161 (savedValue == NULL)
1162 )
1163 {
1164 result = HTTPAPI_INVALID_ARG;
1165 LogError("invalid argument(NULL) passed to HTTPAPI_CloneOption");
1166 }
1167 else
1168 {
1169 if (strcmp(OPTION_HTTP_TIMEOUT, optionName) == 0)
1170 {
1171 /*by convention value is pointing to an unsigned int */
1172 unsigned int* temp = (unsigned int*)malloc(sizeof(unsigned int)); /*shall be freed by HTTPAPIEX*/
1173 if (temp == NULL)
1174 {
1175 result = HTTPAPI_ERROR;
1176 LogError("malloc failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
1177 }
1178 else
1179 {
1180 *temp = *(const unsigned int*)value;
1181 *savedValue = temp;
1182 result = HTTPAPI_OK;
1183 }
1184 }
1185 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
1186 {
1187 /*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*/
1188 if (mallocAndStrcpy_s((char**)savedValue, (const char*)value) != 0)
1189 {
1190 LogError("unable to clone the x509 certificate content");
1191 result = HTTPAPI_ERROR;
1192 }
1193 else
1194 {
1195 /*return OK when the certificate has been cloned successfully*/
1196 result = HTTPAPI_OK;
1197 }
1198 }
1199 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
1200 {
1201 /*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*/
1202 if (mallocAndStrcpy_s((char**)savedValue, (const char*)value) != 0)
1203 {
1204 LogError("unable to clone the x509 private key content");
1205 result = HTTPAPI_ERROR;
1206 }
1207 else
1208 {
1209 /*return OK when the private key has been cloned successfully*/
1210 result = HTTPAPI_OK;
1211 }
1212 }
1213 else if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
1214 {
1215 /*this is getting the trusted certificate */
1216 if (mallocAndStrcpy_s((char**)savedValue, (const char*)value) != 0)
1217 {
1218 LogError("unable to clone the trusted certificate content");
1219 result = HTTPAPI_ERROR;
1220 }
1221 else
1222 {
1223 /*return OK when the certificate has been cloned successfully*/
1224 result = HTTPAPI_OK;
1225 }
1226 }
1227 else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0)
1228 {
1229 *savedValue = malloc(sizeof(HTTP_PROXY_OPTIONS));
1230 if (*savedValue == NULL)
1231 {
1232 LogError("failure in malloc");
1233 result = HTTPAPI_ERROR;
1234 }
1235 else
1236 {
1237 HTTP_PROXY_OPTIONS *savedOption = (HTTP_PROXY_OPTIONS*) *savedValue;
1238 HTTP_PROXY_OPTIONS *option = (HTTP_PROXY_OPTIONS*) value;
1239 memset((void*)*savedValue, 0, sizeof(HTTP_PROXY_OPTIONS));
1240 if (mallocAndStrcpy_s((char**)&(savedOption->host_address),
1241 (const char*)option->host_address) != 0)
1242 {
1243 LogError("unable to clone the proxy host adress content");
1244 free((void*)*savedValue);
1245 *savedValue = NULL;
1246 result = HTTPAPI_ERROR;
1247 }
1248 else
1249 {
1250 savedOption->port = option->port;
1251 if (option->username != NULL && mallocAndStrcpy_s((char**)&(savedOption->username),
1252 (const char*)option->username) != 0)
1253 {
1254 LogError("unable to clone the proxy username content");
1255 free((void*)savedOption->host_address);
1256 savedOption->host_address = NULL;
1257 free((void*)*savedValue);
1258 *savedValue = NULL;
1259 result = HTTPAPI_ERROR;
1260 }
1261 else
1262 {
1263 if (option->password != NULL && mallocAndStrcpy_s((char**)&(savedOption->password),
1264 (const char*)option->password) != 0)
1265 {
1266 LogError("unable to clone the proxy password content");
1267 free((void*)savedOption->host_address);
1268 savedOption->host_address = NULL;
1269 free((void*)savedOption->username);
1270 savedOption->username = NULL;
1271 free((void*)*savedValue);
1272 *savedValue = NULL;
1273 result = HTTPAPI_ERROR;
1274 }
1275 else
1276 {
1277 result = HTTPAPI_OK;
1278 }
1279 }
1280 }
1281 }
1282 }
1283 else
1284 {
1285 result = HTTPAPI_INVALID_ARG;
1286 LogError("unknown option %s", optionName);
1287 }
1288 }
1289 return result;
1290 }