Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/adapters/httpapi_compact.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 <stdio.h>
6 #include <ctype.h>
7 #include <string.h>
8 #include <limits.h>
9
10 #include "azure_macro_utils/macro_utils.h"
11 #include "azure_c_shared_utility/gballoc.h"
12 #include "azure_c_shared_utility/httpheaders.h"
13 #include "azure_c_shared_utility/crt_abstractions.h"
14 #include "azure_c_shared_utility/xlogging.h"
15 #include "azure_c_shared_utility/xio.h"
16 #include "azure_c_shared_utility/platform.h"
17 #include "azure_c_shared_utility/tlsio.h"
18 #include "azure_c_shared_utility/threadapi.h"
19 #include "azure_c_shared_utility/shared_util_options.h"
20 #include "azure_c_shared_utility/http_proxy_io.h"
21 #include "azure_c_shared_utility/safe_math.h"
22
23 #ifdef _MSC_VER
24 #define snprintf _snprintf
25 #endif
26
27 /*Codes_SRS_HTTPAPI_COMPACT_21_001: [ The httpapi_compact shall implement the methods defined by the httpapi.h. ]*/
28 /*Codes_SRS_HTTPAPI_COMPACT_21_002: [ The httpapi_compact shall support the http requests. ]*/
29 /*Codes_SRS_HTTPAPI_COMPACT_21_003: [ The httpapi_compact shall return error codes defined by HTTPAPI_RESULT. ]*/
30 #include "azure_c_shared_utility/httpapi.h"
31
32 #define MAX_HOSTNAME 64
33 #define TEMP_BUFFER_SIZE 1024
34
35 /*Codes_SRS_HTTPAPI_COMPACT_21_077: [ The HTTPAPI_ExecuteRequest shall wait, at least, 10 seconds for the SSL open process. ]*/
36 #define MAX_OPEN_RETRY 100
37 /*Codes_SRS_HTTPAPI_COMPACT_21_084: [ The HTTPAPI_CloseConnection shall wait, at least, 10 seconds for the SSL close process. ]*/
38 #define MAX_CLOSE_RETRY 100
39 /*Codes_SRS_HTTPAPI_COMPACT_21_079: [ The HTTPAPI_ExecuteRequest shall wait, at least, 20 seconds to send a buffer using the SSL connection. ]*/
40 #define MAX_SEND_RETRY 200
41 /*Codes_SRS_HTTPAPI_COMPACT_21_081: [ The HTTPAPI_ExecuteRequest shall try to read the message with the response up to 20 seconds. ]*/
42 #define MAX_RECEIVE_RETRY 200
43 /*Codes_SRS_HTTPAPI_COMPACT_21_083: [ The HTTPAPI_ExecuteRequest shall wait, at least, 100 milliseconds between retries. ]*/
44 #define RETRY_INTERVAL_IN_MS 100
45
46 MU_DEFINE_ENUM_STRINGS(HTTPAPI_RESULT, HTTPAPI_RESULT_VALUES)
47
48 typedef struct HTTP_HANDLE_DATA_TAG
49 {
50 char* hostName;
51 char* certificate;
52 char* x509ClientCertificate;
53 char* x509ClientPrivateKey;
54 XIO_HANDLE xio_handle;
55 size_t received_bytes_count;
56 unsigned char* received_bytes;
57 unsigned int is_io_error : 1;
58 unsigned int is_connected : 1;
59 unsigned int send_completed : 1;
60 bool tls_renegotiation;
61 } HTTP_HANDLE_DATA;
62
63 /*the following function does the same as sscanf(pos2, "%d", &sec)*/
64 /*this function only exists because some of platforms do not have sscanf. */
65 static int ParseStringToDecimal(const char *src, int* dst)
66 {
67 int result;
68 char* next;
69 long num = strtol(src, &next, 0);
70 if (src == next || num < INT_MIN || num > INT_MAX)
71 {
72 result = EOF;
73 }
74 else
75 {
76 result = 1;
77 }
78 if (num < INT_MIN) num = INT_MIN;
79 if (num > INT_MAX) num = INT_MAX;
80 *dst = (int)num;
81 return result;
82 }
83
84 /*the following function does the same as sscanf(pos2, "%x", &sec)*/
85 /*this function only exists because some of platforms do not have sscanf. This is not a full implementation; it only works with well-defined x numbers. */
86 #define HEXA_DIGIT_VAL(c) (((c>='0') && (c<='9')) ? (c-'0') : ((c>='a') && (c<='f')) ? (c-'a'+10) : ((c>='A') && (c<='F')) ? (c-'A'+10) : -1)
87 static int ParseStringToHexadecimal(const char *src, size_t* dst)
88 {
89 int result;
90 int digitVal;
91 if (src == NULL)
92 {
93 result = EOF;
94 }
95 else if (HEXA_DIGIT_VAL(*src) == -1)
96 {
97 result = EOF;
98 }
99 else
100 {
101 (*dst) = 0;
102 while ((digitVal = HEXA_DIGIT_VAL(*src)) != -1)
103 {
104 (*dst) *= 0x10;
105 (*dst) += (size_t)digitVal;
106 src++;
107 }
108 result = 1;
109 }
110 return result;
111 }
112
113 /*the following function does the same as sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &ret) */
114 /*this function only exists because some of platforms do not have sscanf. This is not a full implementation; it only works with well-defined HTTP response. */
115 static int ParseHttpResponse(const char* src, int* dst)
116 {
117 int result;
118 static const char HTTPPrefix[] = "HTTP/";
119 bool fail;
120 const char* runPrefix;
121
122 if ((src == NULL) || (dst == NULL))
123 {
124 result = EOF;
125 }
126 else
127 {
128 fail = false;
129 runPrefix = HTTPPrefix;
130
131 while((*runPrefix) != '\0')
132 {
133 if ((*runPrefix) != (*src))
134 {
135 fail = true;
136 break;
137 }
138 src++;
139 runPrefix++;
140 }
141
142 if (!fail)
143 {
144 while ((*src) != '.')
145 {
146 if ((*src) == '\0')
147 {
148 fail = true;
149 break;
150 }
151 src++;
152 }
153 }
154
155 if (!fail)
156 {
157 while ((*src) != ' ')
158 {
159 if ((*src) == '\0')
160 {
161 fail = true;
162 break;
163 }
164 src++;
165 }
166 }
167
168 if (fail)
169 {
170 result = EOF;
171 }
172 else
173 {
174 result = ParseStringToDecimal(src, dst);
175 }
176 }
177
178 return result;
179 }
180
181 HTTPAPI_RESULT HTTPAPI_Init(void)
182 {
183 /*Codes_SRS_HTTPAPI_COMPACT_21_004: [ The HTTPAPI_Init shall allocate all memory to control the http protocol. ]*/
184 /*Codes_SRS_HTTPAPI_COMPACT_21_007: [ If there is not enough memory to control the http protocol, the HTTPAPI_Init shall return HTTPAPI_ALLOC_FAILED. ]*/
185 /**
186 * No memory is necessary.
187 */
188
189 /*Codes_SRS_HTTPAPI_COMPACT_21_006: [ If HTTPAPI_Init succeed allocating all the needed memory, it shall return HTTPAPI_OK. ]*/
190 return HTTPAPI_OK;
191 }
192
193 void HTTPAPI_Deinit(void)
194 {
195 /*Codes_SRS_HTTPAPI_COMPACT_21_009: [ The HTTPAPI_Init shall release all memory allocated by the httpapi_compact. ]*/
196 /**
197 * No memory was necessary.
198 */
199 }
200
201 /*Codes_SRS_HTTPAPI_COMPACT_21_011: [ The HTTPAPI_CreateConnection shall create an http connection to the host specified by the hostName parameter. ]*/
202 HTTP_HANDLE HTTPAPI_CreateConnection(const char* hostName)
203 {
204 HTTP_HANDLE_DATA* http_instance;
205 TLSIO_CONFIG tlsio_config;
206
207 if (hostName == NULL)
208 {
209 /*Codes_SRS_HTTPAPI_COMPACT_21_014: [ If the hostName is NULL, the HTTPAPI_CreateConnection shall return NULL as the handle. ]*/
210 LogError("Invalid host name. Null hostName parameter.");
211 http_instance = NULL;
212 }
213 else if (*hostName == '\0')
214 {
215 /*Codes_SRS_HTTPAPI_COMPACT_21_015: [ If the hostName is empty, the HTTPAPI_CreateConnection shall return NULL as the handle. ]*/
216 LogError("Invalid host name. Empty string.");
217 http_instance = NULL;
218 }
219 else
220 {
221 http_instance = (HTTP_HANDLE_DATA*)malloc(sizeof(HTTP_HANDLE_DATA));
222 /*Codes_SRS_HTTPAPI_COMPACT_21_013: [ If there is not enough memory to control the http connection, the HTTPAPI_CreateConnection shall return NULL as the handle. ]*/
223 if (http_instance == NULL)
224 {
225 LogError("There is no memory to control the http connection");
226 }
227 else if (mallocAndStrcpy_s(&http_instance->hostName, hostName) != 0)
228 {
229 LogError("Failed copying hostname");
230 free(http_instance);
231 http_instance = NULL;
232 }
233 else
234 {
235 tlsio_config.hostname = http_instance->hostName;
236 tlsio_config.port = 443;
237 tlsio_config.underlying_io_interface = NULL;
238 tlsio_config.underlying_io_parameters = NULL;
239 tlsio_config.invoke_on_send_complete_callback_for_fragments = true;
240 http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config);
241
242 /*Codes_SRS_HTTPAPI_COMPACT_21_016: [ If the HTTPAPI_CreateConnection failed to create the connection, it shall return NULL as the handle. ]*/
243 if (http_instance->xio_handle == NULL)
244 {
245 LogError("Create connection failed");
246 free(http_instance->hostName);
247 free(http_instance);
248 http_instance = NULL;
249 }
250 else
251 {
252 http_instance->is_connected = 0;
253 http_instance->is_io_error = 0;
254 http_instance->received_bytes_count = 0;
255 http_instance->received_bytes = NULL;
256 http_instance->certificate = NULL;
257 http_instance->x509ClientCertificate = NULL;
258 http_instance->x509ClientPrivateKey = NULL;
259 http_instance->tls_renegotiation = false;
260 }
261 }
262 }
263
264 /*Codes_SRS_HTTPAPI_COMPACT_21_012: [ The HTTPAPI_CreateConnection shall return a non-NULL handle on success. ]*/
265 return (HTTP_HANDLE)http_instance;
266 }
267
268 static void on_io_close_complete(void* context)
269 {
270 HTTP_HANDLE_DATA* http_instance = (HTTP_HANDLE_DATA*)context;
271
272 if (http_instance != NULL)
273 {
274 http_instance->is_connected = 0;
275 }
276 }
277
278 void HTTPAPI_CloseConnection(HTTP_HANDLE handle)
279 {
280 HTTP_HANDLE_DATA* http_instance = (HTTP_HANDLE_DATA*)handle;
281
282 /*Codes_SRS_HTTPAPI_COMPACT_21_020: [ If the connection handle is NULL, the HTTPAPI_CloseConnection shall not do anything. ]*/
283 if (http_instance != NULL)
284 {
285 /*Codes_SRS_HTTPAPI_COMPACT_21_019: [ If there is no previous connection, the HTTPAPI_CloseConnection shall not do anything. ]*/
286 if (http_instance->xio_handle != NULL)
287 {
288 http_instance->is_io_error = 0;
289 /*Codes_SRS_HTTPAPI_COMPACT_21_017: [ The HTTPAPI_CloseConnection shall close the connection previously created in HTTPAPI_ExecuteRequest. ]*/
290 if (xio_close(http_instance->xio_handle, on_io_close_complete, http_instance) != 0)
291 {
292 LogError("The SSL got error closing the connection");
293 /*Codes_SRS_HTTPAPI_COMPACT_21_087: [ If the xio return anything different than 0, the HTTPAPI_CloseConnection shall destroy the connection anyway. ]*/
294 http_instance->is_connected = 0;
295 }
296 else
297 {
298 /*Codes_SRS_HTTPAPI_COMPACT_21_084: [ The HTTPAPI_CloseConnection shall wait, at least, 10 seconds for the SSL close process. ]*/
299 int countRetry = MAX_CLOSE_RETRY;
300 while (http_instance->is_connected == 1)
301 {
302 xio_dowork(http_instance->xio_handle);
303 if ((countRetry--) < 0)
304 {
305 /*Codes_SRS_HTTPAPI_COMPACT_21_085: [ If the HTTPAPI_CloseConnection retries 10 seconds to close the connection without success, it shall destroy the connection anyway. ]*/
306 LogError("Close timeout. The SSL didn't close the connection");
307 http_instance->is_connected = 0;
308 }
309 else if (http_instance->is_io_error == 1)
310 {
311 LogError("The SSL got error closing the connection");
312 http_instance->is_connected = 0;
313 }
314 else if (http_instance->is_connected == 1)
315 {
316 LogInfo("Waiting for TLS close connection");
317 /*Codes_SRS_HTTPAPI_COMPACT_21_086: [ The HTTPAPI_CloseConnection shall wait, at least, 100 milliseconds between retries. ]*/
318 ThreadAPI_Sleep(RETRY_INTERVAL_IN_MS);
319 }
320 }
321 }
322 /*Codes_SRS_HTTPAPI_COMPACT_21_076: [ After close the connection, The HTTPAPI_CloseConnection shall destroy the connection previously created in HTTPAPI_CreateConnection. ]*/
323 xio_destroy(http_instance->xio_handle);
324 }
325
326 #ifndef DO_NOT_COPY_TRUSTED_CERTS_STRING
327 /*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. ]*/
328 if (http_instance->certificate)
329 {
330 free(http_instance->certificate);
331 }
332 #endif
333
334 /*Codes_SRS_HTTPAPI_COMPACT_06_001: [ If there is a x509 client certificate associated to this connection, the HTTAPI_CloseConnection shall free all allocated memory for the certificate. ]*/
335 if (http_instance->x509ClientCertificate)
336 {
337 free(http_instance->x509ClientCertificate);
338 }
339
340 /*Codes_SRS_HTTPAPI_COMPACT_06_002: [ If there is a x509 client private key associated to this connection, then HTTP_CloseConnection shall free all the allocated memory for the private key. ]*/
341 if (http_instance->x509ClientPrivateKey)
342 {
343 free(http_instance->x509ClientPrivateKey);
344 }
345
346 if (http_instance->hostName)
347 {
348 free(http_instance->hostName);
349 }
350
351 free(http_instance);
352 }
353 }
354
355 static void on_io_open_complete(void* context, IO_OPEN_RESULT open_result)
356 {
357 HTTP_HANDLE_DATA* http_instance = (HTTP_HANDLE_DATA*)context;
358
359 if (http_instance != NULL)
360 {
361 if (open_result == IO_OPEN_OK)
362 {
363 http_instance->is_connected = 1;
364 http_instance->is_io_error = 0;
365 }
366 else
367 {
368 http_instance->is_io_error = 1;
369 }
370 }
371 }
372
373 static void on_send_complete(void* context, IO_SEND_RESULT send_result)
374 {
375 HTTP_HANDLE_DATA* http_instance = (HTTP_HANDLE_DATA*)context;
376
377 if (http_instance != NULL)
378 {
379 if (send_result == IO_SEND_OK)
380 {
381 http_instance->send_completed = 1;
382 http_instance->is_io_error = 0;
383 }
384 else
385 {
386 http_instance->is_io_error = 1;
387 }
388 }
389 }
390
391 #define TOLOWER(c) (((c>='A') && (c<='Z'))?c-'A'+'a':c)
392 static int InternStrnicmp(const char* s1, const char* s2, size_t n)
393 {
394 int result;
395
396 if (s1 == NULL) result = -1;
397 else if (s2 == NULL) result = 1;
398 else
399 {
400 result = 0;
401
402 while(n-- && result == 0)
403 {
404 if (*s1 == 0) result = -1;
405 else if (*s2 == 0) result = 1;
406 else
407 {
408
409 result = TOLOWER(*s1) - TOLOWER(*s2);
410 ++s1;
411 ++s2;
412 }
413 }
414 }
415
416 return result;
417 }
418
419 static void on_bytes_received(void* context, const unsigned char* buffer, size_t size)
420 {
421 unsigned char* new_received_bytes;
422 HTTP_HANDLE_DATA* http_instance = (HTTP_HANDLE_DATA*)context;
423
424 if (http_instance != NULL)
425 {
426
427 if (buffer == NULL)
428 {
429 http_instance->is_io_error = 1;
430 LogError("NULL pointer error");
431 }
432 else
433 {
434 /* Here we got some bytes so we'll buffer them so the receive functions can consumer it */
435 size_t malloc_size = http_instance->received_bytes_count + size;
436 if (malloc_size < size)
437 {
438 // check for int overflow
439 new_received_bytes = NULL;
440 LogError("Invalid size parameter");
441 }
442 else
443 {
444 new_received_bytes = (unsigned char*)realloc(http_instance->received_bytes, malloc_size);
445 }
446
447 if (new_received_bytes == NULL)
448 {
449 http_instance->is_io_error = 1;
450 LogError("Error allocating memory for received data");
451 }
452 else
453 {
454 http_instance->received_bytes = new_received_bytes;
455 if (memcpy(http_instance->received_bytes + http_instance->received_bytes_count, buffer, size) == NULL)
456 {
457 http_instance->is_io_error = 1;
458 LogError("Error copping received data to the HTTP bufffer");
459 }
460 else
461 {
462 http_instance->received_bytes_count += size;
463 }
464 }
465 }
466 }
467 }
468
469 static void on_io_error(void* context)
470 {
471 HTTP_HANDLE_DATA* http_instance = (HTTP_HANDLE_DATA*)context;
472 if (http_instance != NULL)
473 {
474 http_instance->is_io_error = 1;
475 LogError("Error signalled by underlying IO");
476 }
477 }
478
479 static int conn_receive(HTTP_HANDLE_DATA* http_instance, char* buffer, int count)
480 {
481 int result;
482
483 if ((http_instance == NULL) || (buffer == NULL) || (count < 0))
484 {
485 LogError("conn_receive: %s", ((http_instance == NULL) ? "Invalid HTTP instance" : "Invalid HTTP buffer"));
486 result = -1;
487 }
488 else
489 {
490 result = 0;
491 while (result < count)
492 {
493 xio_dowork(http_instance->xio_handle);
494
495 /* if any error was detected while receiving then simply break and report it */
496 if (http_instance->is_io_error != 0)
497 {
498 LogError("xio reported error on dowork");
499 result = -1;
500 break;
501 }
502
503 if (http_instance->received_bytes_count >= (size_t)count)
504 {
505 /* Consuming bytes from the receive buffer */
506 (void)memcpy(buffer, http_instance->received_bytes, count);
507 (void)memmove(http_instance->received_bytes, http_instance->received_bytes + count, http_instance->received_bytes_count - count);
508 http_instance->received_bytes_count -= count;
509
510 /* we're not reallocating at each consumption so that we don't trash due to byte by byte consumption */
511 if (http_instance->received_bytes_count == 0)
512 {
513 free(http_instance->received_bytes);
514 http_instance->received_bytes = NULL;
515 }
516
517 result = count;
518 break;
519 }
520
521 /*Codes_SRS_HTTPAPI_COMPACT_21_083: [ The HTTPAPI_ExecuteRequest shall wait, at least, 100 milliseconds between retries. ]*/
522 ThreadAPI_Sleep(RETRY_INTERVAL_IN_MS);
523 }
524 }
525
526 return result;
527 }
528
529 static void conn_receive_discard_buffer(HTTP_HANDLE_DATA* http_instance)
530 {
531 if (http_instance != NULL)
532 {
533 if (http_instance->received_bytes != NULL)
534 {
535 free(http_instance->received_bytes);
536 http_instance->received_bytes = NULL;
537 }
538 http_instance->received_bytes_count = 0;
539 }
540 }
541
542 static int readLine(HTTP_HANDLE_DATA* http_instance, char* buf, const size_t maxBufSize)
543 {
544 int resultLineSize;
545
546 if ((http_instance == NULL) || (buf == NULL) || (maxBufSize == 0))
547 {
548 LogError("%s", ((http_instance == NULL) ? "Invalid HTTP instance" : "Invalid HTTP buffer"));
549 resultLineSize = -1;
550 }
551 else
552 {
553 char* destByte = buf;
554 /*Codes_SRS_HTTPAPI_COMPACT_21_081: [ The HTTPAPI_ExecuteRequest shall try to read the message with the response up to 20 seconds. ]*/
555 int countRetry = MAX_RECEIVE_RETRY;
556 bool endOfSearch = false;
557 resultLineSize = -1;
558 while (!endOfSearch)
559 {
560 xio_dowork(http_instance->xio_handle);
561
562 /* if any error was detected while receiving then simply break and report it */
563 if (http_instance->is_io_error != 0)
564 {
565 LogError("xio reported error on dowork");
566 endOfSearch = true;
567 }
568 else
569 {
570 unsigned char* receivedByte = http_instance->received_bytes;
571 while (receivedByte < (http_instance->received_bytes + http_instance->received_bytes_count))
572 {
573 if ((*receivedByte) != '\r')
574 {
575 (*destByte) = (*receivedByte);
576 destByte++;
577 receivedByte++;
578
579 if (destByte >= (buf + maxBufSize - 1))
580 {
581 LogError("Received message is bigger than the http buffer");
582 receivedByte = http_instance->received_bytes + http_instance->received_bytes_count;
583 endOfSearch = true;
584 break;
585 }
586 }
587 else
588 {
589 receivedByte++;
590 if ((receivedByte < (http_instance->received_bytes + http_instance->received_bytes_count)) && ((*receivedByte) == '\n'))
591 {
592 receivedByte++;
593 }
594 (*destByte) = '\0';
595 resultLineSize = (int)(destByte - buf);
596 endOfSearch = true;
597 break;
598 }
599 }
600
601 http_instance->received_bytes_count -= (receivedByte - http_instance->received_bytes);
602 if (http_instance->received_bytes_count != 0)
603 {
604 (void)memmove(http_instance->received_bytes, receivedByte, http_instance->received_bytes_count);
605 }
606 else
607 {
608 conn_receive_discard_buffer(http_instance);
609 }
610 }
611
612 if (!endOfSearch)
613 {
614 if ((countRetry--) > 0)
615 {
616 /*Codes_SRS_HTTPAPI_COMPACT_21_083: [ The HTTPAPI_ExecuteRequest shall wait, at least, 100 milliseconds between retries. ]*/
617 ThreadAPI_Sleep(RETRY_INTERVAL_IN_MS);
618 }
619 else
620 {
621 /*Codes_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
622 LogError("Receive timeout. The HTTP request is incomplete");
623 endOfSearch = true;
624 }
625 }
626 }
627 }
628
629 return resultLineSize;
630 }
631
632 static int readChunk(HTTP_HANDLE_DATA* http_instance, char* buf, size_t size)
633 {
634 int cur, offset;
635
636 // read content with specified length, even if it is received
637 // only in chunks due to fragmentation in the networking layer.
638 // returns -1 in case of error.
639 offset = 0;
640 while (size > (size_t)0)
641 {
642 cur = conn_receive(http_instance, buf + offset, (int)size);
643
644 // end of stream reached
645 if (cur == 0)
646 {
647 break;
648 }
649
650 // on error
651 if (cur < 0)
652 {
653 return cur;
654 }
655
656 // read cur bytes (might be less than requested)
657 size -= (size_t)cur;
658 offset += cur;
659 }
660
661 return offset;
662 }
663
664 static int skipN(HTTP_HANDLE_DATA* http_instance, size_t n)
665 {
666 // read and abandon response content with specified length
667 // returns -1 in case of error.
668
669 int result;
670
671 if (http_instance == NULL)
672 {
673 LogError("Invalid HTTP instance");
674 result = -1;
675 }
676 else
677 {
678 /*Codes_SRS_HTTPAPI_COMPACT_21_081: [ The HTTPAPI_ExecuteRequest shall try to read the message with the response up to 20 seconds. ]*/
679 int countRetry = MAX_RECEIVE_RETRY;
680 result = (int)n;
681 while (n > 0)
682 {
683 xio_dowork(http_instance->xio_handle);
684
685 /* if any error was detected while receiving then simply break and report it */
686 if (http_instance->is_io_error != 0)
687 {
688 LogError("xio reported error on dowork");
689 result = -1;
690 n = 0;
691 }
692 else
693 {
694 if (http_instance->received_bytes_count <= n)
695 {
696 n -= http_instance->received_bytes_count;
697 http_instance->received_bytes_count = 0;
698 }
699 else
700 {
701 http_instance->received_bytes_count -= n;
702 (void)memmove(http_instance->received_bytes, http_instance->received_bytes + n, http_instance->received_bytes_count);
703 n = 0;
704 }
705
706 if (n > 0)
707 {
708 if ((countRetry--) > 0)
709 {
710 /*Codes_SRS_HTTPAPI_COMPACT_21_083: [ The HTTPAPI_ExecuteRequest shall wait, at least, 100 milliseconds between retries. ]*/
711 ThreadAPI_Sleep(RETRY_INTERVAL_IN_MS);
712 }
713 else
714 {
715 /*Codes_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
716 LogError("Receive timeout. The HTTP request is incomplete");
717 n = 0;
718 result = -1;
719 }
720 }
721 }
722 }
723 }
724
725 return result;
726 }
727
728
729 /*Codes_SRS_HTTPAPI_COMPACT_21_021: [ The HTTPAPI_ExecuteRequest shall execute the http communtication with the provided host, sending a request and reciving the response. ]*/
730 static HTTPAPI_RESULT OpenXIOConnection(HTTP_HANDLE_DATA* http_instance)
731 {
732 HTTPAPI_RESULT result;
733
734 if (http_instance->is_connected != 0)
735 {
736 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
737 result = HTTPAPI_OK;
738 }
739 else
740 {
741 http_instance->is_io_error = 0;
742
743 /*Codes_SRS_HTTPAPI_COMPACT_21_022: [ If a Certificate was provided, the HTTPAPI_ExecuteRequest shall set this option on the transport layer. ]*/
744 if ((http_instance->certificate != NULL) &&
745 (xio_setoption(http_instance->xio_handle, OPTION_TRUSTED_CERT, http_instance->certificate) != 0))
746 {
747 /*Codes_SRS_HTTPAPI_COMPACT_21_023: [ If the transport failed setting the Certificate, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ]*/
748 result = HTTPAPI_SET_OPTION_FAILED;
749 LogInfo("Could not load certificate");
750 }
751 /*Codes_SRS_HTTPAPI_COMPACT_06_003: [ If the x509 client certificate is provided, the HTTPAPI_ExecuteRequest shall set this option on the transport layer. ]*/
752 else if ((http_instance->x509ClientCertificate != NULL) &&
753 (xio_setoption(http_instance->xio_handle, SU_OPTION_X509_CERT, http_instance->x509ClientCertificate) != 0))
754 {
755 /*Codes_SRS_HTTPAPI_COMPACT_06_005: [ If the transport failed setting the client certificate, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ]*/
756 result = HTTPAPI_SET_OPTION_FAILED;
757 LogInfo("Could not load the client certificate");
758 }
759 else if ((http_instance->x509ClientPrivateKey != NULL) &&
760 (xio_setoption(http_instance->xio_handle, SU_OPTION_X509_PRIVATE_KEY, http_instance->x509ClientPrivateKey) != 0))
761 {
762
763 /*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. ] */
764 result = HTTPAPI_SET_OPTION_FAILED;
765 LogInfo("Could not load the client certificate private key");
766 }
767 else if ((http_instance->tls_renegotiation == true) &&
768 (xio_setoption(http_instance->xio_handle, OPTION_SET_TLS_RENEGOTIATION, &http_instance->tls_renegotiation) != 0))
769 {
770 result = HTTPAPI_SET_OPTION_FAILED;
771 LogInfo("Could not load renegotiation flag");
772 }
773 else
774 {
775 /*Codes_SRS_HTTPAPI_COMPACT_21_024: [ The HTTPAPI_ExecuteRequest shall open the transport connection with the host to send the request. ]*/
776 if (xio_open(http_instance->xio_handle, on_io_open_complete, http_instance, on_bytes_received, http_instance, on_io_error, http_instance) != 0)
777 {
778 /*Codes_SRS_HTTPAPI_COMPACT_21_025: [ If the open process failed, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
779 result = HTTPAPI_OPEN_REQUEST_FAILED;
780 }
781 else
782 {
783 int countRetry;
784 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
785 result = HTTPAPI_OK;
786 /*Codes_SRS_HTTPAPI_COMPACT_21_077: [ The HTTPAPI_ExecuteRequest shall wait, at least, 10 seconds for the SSL open process. ]*/
787 countRetry = MAX_OPEN_RETRY;
788 while ((http_instance->is_connected == 0) &&
789 (http_instance->is_io_error == 0))
790 {
791 xio_dowork(http_instance->xio_handle);
792 LogInfo("Waiting for TLS connection");
793 if ((countRetry--) < 0)
794 {
795 /*Codes_SRS_HTTPAPI_COMPACT_21_078: [ If the HTTPAPI_ExecuteRequest cannot open the connection in 10 seconds, it shall fail and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
796 LogError("Open timeout. The HTTP request is incomplete");
797 result = HTTPAPI_OPEN_REQUEST_FAILED;
798 break;
799 }
800 else
801 {
802 /*Codes_SRS_HTTPAPI_COMPACT_21_083: [ The HTTPAPI_ExecuteRequest shall wait, at least, 100 milliseconds between retries. ]*/
803 ThreadAPI_Sleep(RETRY_INTERVAL_IN_MS);
804 }
805 }
806 }
807 }
808 }
809
810 if ((http_instance->is_io_error != 0) && (result == HTTPAPI_OK))
811 {
812 /*Codes_SRS_HTTPAPI_COMPACT_21_025: [ If the open process failed, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
813 result = HTTPAPI_OPEN_REQUEST_FAILED;
814 }
815
816 return result;
817 }
818
819 static HTTPAPI_RESULT conn_send_all(HTTP_HANDLE_DATA* http_instance, const unsigned char* buf, size_t bufLen)
820 {
821 HTTPAPI_RESULT result;
822
823 http_instance->send_completed = 0;
824 http_instance->is_io_error = 0;
825 if (xio_send(http_instance->xio_handle, buf, bufLen, on_send_complete, http_instance) != 0)
826 {
827 /*Codes_SRS_HTTPAPI_COMPACT_21_028: [ If the HTTPAPI_ExecuteRequest cannot send the request header, it shall return HTTPAPI_HTTP_HEADERS_FAILED. ]*/
828 result = HTTPAPI_SEND_REQUEST_FAILED;
829 }
830 else
831 {
832 /*Codes_SRS_HTTPAPI_COMPACT_21_079: [ The HTTPAPI_ExecuteRequest shall wait, at least, 20 seconds to send a buffer using the SSL connection. ]*/
833 int countRetry = MAX_SEND_RETRY;
834 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
835 result = HTTPAPI_OK;
836 while ((http_instance->send_completed == 0) && (result == HTTPAPI_OK))
837 {
838 xio_dowork(http_instance->xio_handle);
839 if (http_instance->is_io_error != 0)
840 {
841 /*Codes_SRS_HTTPAPI_COMPACT_21_028: [ If the HTTPAPI_ExecuteRequest cannot send the request header, it shall return HTTPAPI_HTTP_HEADERS_FAILED. ]*/
842 result = HTTPAPI_SEND_REQUEST_FAILED;
843 }
844 else if ((countRetry--) <= 0)
845 {
846 /*Codes_SRS_HTTPAPI_COMPACT_21_080: [ If the HTTPAPI_ExecuteRequest retries to send the message for 20 seconds without success, it shall fail and return HTTPAPI_SEND_REQUEST_FAILED. ]*/
847 LogError("Send timeout. The HTTP request is incomplete");
848 /*Codes_SRS_HTTPAPI_COMPACT_21_028: [ If the HTTPAPI_ExecuteRequest cannot send the request header, it shall return HTTPAPI_HTTP_HEADERS_FAILED. ]*/
849 result = HTTPAPI_SEND_REQUEST_FAILED;
850 }
851 else
852 {
853 /*Codes_SRS_HTTPAPI_COMPACT_21_083: [ The HTTPAPI_ExecuteRequest shall wait, at least, 100 milliseconds between retries. ]*/
854 ThreadAPI_Sleep(RETRY_INTERVAL_IN_MS);
855 }
856 }
857 }
858
859 return result;
860 }
861
862 /*Codes_SRS_HTTPAPI_COMPACT_21_035: [ The HTTPAPI_ExecuteRequest shall execute resquest for types GET, POST, PUT, DELETE, PATCH, HEAD. ]*/
863 const char httpapiRequestString[6][7] = { "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD" };
864 const char* get_request_type(HTTPAPI_REQUEST_TYPE requestType)
865 {
866 return (const char*)httpapiRequestString[requestType - HTTPAPI_REQUEST_GET];
867 }
868
869 /*Codes_SRS_HTTPAPI_COMPACT_21_026: [ If the open process succeed, the HTTPAPI_ExecuteRequest shall send the request message to the host. ]*/
870 static HTTPAPI_RESULT SendHeadsToXIO(HTTP_HANDLE_DATA* http_instance, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath, HTTP_HEADERS_HANDLE httpHeadersHandle, size_t headersCount)
871 {
872 HTTPAPI_RESULT result;
873 char buf[TEMP_BUFFER_SIZE];
874 int ret;
875
876 //Send request
877 /*Codes_SRS_HTTPAPI_COMPACT_21_038: [ The HTTPAPI_ExecuteRequest shall execute the resquest for the path in relativePath parameter. ]*/
878 /*Codes_SRS_HTTPAPI_COMPACT_21_036: [ The request type shall be provided in the parameter requestType. ]*/
879 if (((ret = snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\n", get_request_type(requestType), relativePath)) < 0) ||
880 ((size_t)ret >= sizeof(buf)))
881 {
882 /*Codes_SRS_HTTPAPI_COMPACT_21_027: [ If the HTTPAPI_ExecuteRequest cannot create a buffer to send the request, it shall not send any request and return HTTPAPI_STRING_PROCESSING_ERROR. ]*/
883 result = HTTPAPI_STRING_PROCESSING_ERROR;
884 }
885 /*Codes_SRS_HTTPAPI_COMPACT_21_028: [ If the HTTPAPI_ExecuteRequest cannot send the request header, it shall return HTTPAPI_HTTP_HEADERS_FAILED. ]*/
886 else if ((result = conn_send_all(http_instance, (const unsigned char*)buf, strlen(buf))) == HTTPAPI_OK)
887 {
888 size_t i;
889 //Send default headers
890 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
891 for (i = 0; ((i < headersCount) && (result == HTTPAPI_OK)); i++)
892 {
893 char* header;
894 if (HTTPHeaders_GetHeader(httpHeadersHandle, i, &header) != HTTP_HEADERS_OK)
895 {
896 /*Codes_SRS_HTTPAPI_COMPACT_21_027: [ If the HTTPAPI_ExecuteRequest cannot create a buffer to send the request, it shall not send any request and return HTTPAPI_STRING_PROCESSING_ERROR. ]*/
897 result = HTTPAPI_STRING_PROCESSING_ERROR;
898 }
899 else
900 {
901 if ((result = conn_send_all(http_instance, (const unsigned char*)header, strlen(header))) == HTTPAPI_OK)
902 {
903 result = conn_send_all(http_instance, (const unsigned char*)"\r\n", (size_t)2);
904 }
905 free(header);
906 }
907 }
908
909 //Close headers
910 if (result == HTTPAPI_OK)
911 {
912 result = conn_send_all(http_instance, (const unsigned char*)"\r\n", (size_t)2);
913 }
914 }
915 return result;
916 }
917
918 /*Codes_SRS_HTTPAPI_COMPACT_21_042: [ The request can contain the a content message, provided in content parameter. ]*/
919 static HTTPAPI_RESULT SendContentToXIO(HTTP_HANDLE_DATA* http_instance, const unsigned char* content, size_t contentLength)
920 {
921 HTTPAPI_RESULT result;
922
923 //Send data (if available)
924 /*Codes_SRS_HTTPAPI_COMPACT_21_045: [ If the contentLength is lower than one, the HTTPAPI_ExecuteRequest shall send the request without content. ]*/
925 if (content && contentLength > 0)
926 {
927 /*Codes_SRS_HTTPAPI_COMPACT_21_044: [ If the content is not NULL, the number of bytes in the content shall be provided in contentLength parameter. ]*/
928 result = conn_send_all(http_instance, content, contentLength);
929 }
930 else
931 {
932 /*Codes_SRS_HTTPAPI_COMPACT_21_043: [ If the content is NULL, the HTTPAPI_ExecuteRequest shall send the request without content. ]*/
933 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
934 result = HTTPAPI_OK;
935 }
936 return result;
937 }
938
939 /*Codes_SRS_HTTPAPI_COMPACT_21_030: [ At the end of the transmission, the HTTPAPI_ExecuteRequest shall receive the response from the host. ]*/
940 static HTTPAPI_RESULT ReceiveHeaderFromXIO(HTTP_HANDLE_DATA* http_instance, unsigned int* statusCode)
941 {
942 HTTPAPI_RESULT result;
943 char buf[TEMP_BUFFER_SIZE];
944 int ret;
945
946 http_instance->is_io_error = 0;
947
948 //Receive response
949 if (readLine(http_instance, buf, TEMP_BUFFER_SIZE) < 0)
950 {
951 /*Codes_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
952 /*Codes_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
953 result = HTTPAPI_READ_DATA_FAILED;
954 }
955 //Parse HTTP response
956 else if (ParseHttpResponse(buf, &ret) != 1)
957 {
958 //Cannot match string, error
959 /*Codes_SRS_HTTPAPI_COMPACT_21_055: [ If the HTTPAPI_ExecuteRequest cannot parser the received message, it shall return HTTPAPI_RECEIVE_RESPONSE_FAILED. ]*/
960 LogInfo("Not a correct HTTP answer");
961 result = HTTPAPI_RECEIVE_RESPONSE_FAILED;
962 }
963 else
964 {
965 /*Codes_SRS_HTTPAPI_COMPACT_21_046: [ The HTTPAPI_ExecuteRequest shall return the http status reported by the host in the received response. ]*/
966 /*Codes_SRS_HTTPAPI_COMPACT_21_048: [ If the statusCode is NULL, the HTTPAPI_ExecuteRequest shall report not report any status. ]*/
967 if (statusCode)
968 {
969 /*Codes_SRS_HTTPAPI_COMPACT_21_047: [ The HTTPAPI_ExecuteRequest shall report the status in the statusCode parameter. ]*/
970 *statusCode = ret;
971 }
972 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
973 result = HTTPAPI_OK;
974 }
975
976 return result;
977 }
978
979 static HTTPAPI_RESULT ReceiveContentInfoFromXIO(HTTP_HANDLE_DATA* http_instance, HTTP_HEADERS_HANDLE responseHeadersHandle, size_t* bodyLength, bool* chunked)
980 {
981 HTTPAPI_RESULT result;
982 char buf[TEMP_BUFFER_SIZE];
983 const char* substr;
984 char* whereIsColon;
985 int lengthInMsg;
986 const char ContentLength[] = "content-length:";
987 const size_t ContentLengthSize = sizeof(ContentLength) - 1;
988 const char TransferEncoding[] = "transfer-encoding:";
989 const size_t TransferEncodingSize = sizeof(TransferEncoding) - 1;
990 const char Chunked[] = "chunked";
991 const size_t ChunkedSize = sizeof(Chunked) - 1;
992
993 http_instance->is_io_error = 0;
994
995 //Read HTTP response headers
996 if (readLine(http_instance, buf, sizeof(buf)) < 0)
997 {
998 /*Codes_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
999 /*Codes_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
1000 result = HTTPAPI_READ_DATA_FAILED;
1001 }
1002 else
1003 {
1004 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
1005 result = HTTPAPI_OK;
1006
1007 while (*buf && (result == HTTPAPI_OK))
1008 {
1009 if (InternStrnicmp(buf, ContentLength, ContentLengthSize) == 0)
1010 {
1011 substr = buf + ContentLengthSize;
1012 if (ParseStringToDecimal(substr, &lengthInMsg) != 1)
1013 {
1014 /*Codes_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
1015 result = HTTPAPI_READ_DATA_FAILED;
1016 }
1017 else
1018 {
1019 (*bodyLength) = (size_t)lengthInMsg;
1020 }
1021 }
1022 else if (InternStrnicmp(buf, TransferEncoding, TransferEncodingSize) == 0)
1023 {
1024 substr = buf + TransferEncodingSize;
1025
1026 while (isspace(*substr)) substr++;
1027
1028 if (InternStrnicmp(substr, Chunked, ChunkedSize) == 0)
1029 {
1030 (*chunked) = true;
1031 }
1032 }
1033
1034 if (result == HTTPAPI_OK)
1035 {
1036 whereIsColon = strchr((char*)buf, ':');
1037 /*Codes_SRS_HTTPAPI_COMPACT_21_049: [ If responseHeadersHandle is provide, the HTTPAPI_ExecuteRequest shall prepare a Response Header usign the HTTPHeaders_AddHeaderNameValuePair. ]*/
1038 if (whereIsColon && (responseHeadersHandle != NULL))
1039 {
1040 *whereIsColon = '\0';
1041 HTTPHeaders_AddHeaderNameValuePair(responseHeadersHandle, buf, whereIsColon + 1);
1042 }
1043
1044 if (readLine(http_instance, buf, sizeof(buf)) < 0)
1045 {
1046 /*Codes_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
1047 /*Codes_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
1048 result = HTTPAPI_READ_DATA_FAILED;
1049 }
1050 }
1051 }
1052 }
1053
1054 return result;
1055 }
1056
1057 static HTTPAPI_RESULT ReadHTTPResponseBodyFromXIO(HTTP_HANDLE_DATA* http_instance, size_t bodyLength, bool chunked, BUFFER_HANDLE responseContent)
1058 {
1059 HTTPAPI_RESULT result;
1060 char buf[TEMP_BUFFER_SIZE];
1061 const unsigned char* receivedContent;
1062
1063 http_instance->is_io_error = 0;
1064
1065 //Read HTTP response body
1066 if (!chunked)
1067 {
1068 if (bodyLength)
1069 {
1070 if (responseContent != NULL)
1071 {
1072 if (BUFFER_pre_build(responseContent, bodyLength) != 0)
1073 {
1074 /*Codes_SRS_HTTPAPI_COMPACT_21_052: [ If any memory allocation get fail, the HTTPAPI_ExecuteRequest shall return HTTPAPI_ALLOC_FAILED. ]*/
1075 result = HTTPAPI_ALLOC_FAILED;
1076 }
1077 else if (BUFFER_content(responseContent, &receivedContent) != 0)
1078 {
1079 (void)BUFFER_unbuild(responseContent);
1080
1081 /*Codes_SRS_HTTPAPI_COMPACT_21_052: [ If any memory allocation get fail, the HTTPAPI_ExecuteRequest shall return HTTPAPI_ALLOC_FAILED. ]*/
1082 result = HTTPAPI_ALLOC_FAILED;
1083 }
1084 else if (readChunk(http_instance, (char*)receivedContent, bodyLength) < 0)
1085 {
1086 /*Codes_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
1087 result = HTTPAPI_READ_DATA_FAILED;
1088 }
1089 else
1090 {
1091 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
1092 result = HTTPAPI_OK;
1093 }
1094 }
1095 else
1096 {
1097 /*Codes_SRS_HTTPAPI_COMPACT_21_051: [ If the responseContent is NULL, the HTTPAPI_ExecuteRequest shall ignore any content in the response. ]*/
1098 if (skipN(http_instance, bodyLength) < 0)
1099 {
1100 /*Codes_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
1101 result = HTTPAPI_READ_DATA_FAILED;
1102 }
1103 else
1104 {
1105 result = HTTPAPI_OK;
1106 }
1107 }
1108 }
1109 else
1110 {
1111 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
1112 result = HTTPAPI_OK;
1113 }
1114 }
1115 else
1116 {
1117 size_t size = 0;
1118 /*Codes_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
1119 result = HTTPAPI_OK;
1120 while (result == HTTPAPI_OK)
1121 {
1122 size_t chunkSize;
1123 if (readLine(http_instance, buf, sizeof(buf)) < 0) // read [length in hex]/r/n
1124 {
1125 /*Codes_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
1126 /*Codes_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
1127 result = HTTPAPI_READ_DATA_FAILED;
1128 }
1129 else if (ParseStringToHexadecimal(buf, &chunkSize) != 1) // chunkSize is length of next line (/r/n is not counted)
1130 {
1131 //Cannot match string, error
1132 /*Codes_SRS_HTTPAPI_COMPACT_21_055: [ If the HTTPAPI_ExecuteRequest cannot parser the received message, it shall return HTTPAPI_RECEIVE_RESPONSE_FAILED. ]*/
1133 result = HTTPAPI_RECEIVE_RESPONSE_FAILED;
1134 }
1135 else if (chunkSize == 0)
1136 {
1137 // 0 length means next line is just '\r\n' and end of chunks
1138 if (readChunk(http_instance, (char*)buf, (size_t)2) < 0
1139 || buf[0] != '\r' || buf[1] != '\n') // skip /r/n
1140 {
1141 (void)BUFFER_unbuild(responseContent);
1142
1143 result = HTTPAPI_READ_DATA_FAILED;
1144 }
1145 break;
1146 }
1147 else
1148 {
1149 if (responseContent != NULL)
1150 {
1151 if (BUFFER_enlarge(responseContent, chunkSize) != 0)
1152 {
1153 (void)BUFFER_unbuild(responseContent);
1154
1155 /*Codes_SRS_HTTPAPI_COMPACT_21_052: [ If any memory allocation get fail, the HTTPAPI_ExecuteRequest shall return HTTPAPI_ALLOC_FAILED. ]*/
1156 result = HTTPAPI_ALLOC_FAILED;
1157 }
1158 else if (BUFFER_content(responseContent, &receivedContent) != 0)
1159 {
1160 (void)BUFFER_unbuild(responseContent);
1161
1162 /*Codes_SRS_HTTPAPI_COMPACT_21_052: [ If any memory allocation get fail, the HTTPAPI_ExecuteRequest shall return HTTPAPI_ALLOC_FAILED. ]*/
1163 result = HTTPAPI_ALLOC_FAILED;
1164 }
1165 else if (readChunk(http_instance, (char*)receivedContent + size, chunkSize) < 0)
1166 {
1167 result = HTTPAPI_READ_DATA_FAILED;
1168 }
1169 }
1170 else
1171 {
1172 /*Codes_SRS_HTTPAPI_COMPACT_21_051: [ If the responseContent is NULL, the HTTPAPI_ExecuteRequest shall ignore any content in the response. ]*/
1173 if (skipN(http_instance, chunkSize) < 0)
1174 {
1175 /*Codes_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
1176 result = HTTPAPI_READ_DATA_FAILED;
1177 }
1178 }
1179
1180 if (result == HTTPAPI_OK)
1181 {
1182 if (readChunk(http_instance, (char*)buf, (size_t)2) < 0
1183 || buf[0] != '\r' || buf[1] != '\n') // skip /r/n
1184 {
1185 result = HTTPAPI_READ_DATA_FAILED;
1186 }
1187 size += chunkSize;
1188 }
1189 }
1190 }
1191
1192 }
1193 return result;
1194 }
1195
1196
1197 /*Codes_SRS_HTTPAPI_COMPACT_21_037: [ If the request type is unknown, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1198 static bool validRequestType(HTTPAPI_REQUEST_TYPE requestType)
1199 {
1200 bool result;
1201
1202 if ((requestType == HTTPAPI_REQUEST_GET) ||
1203 (requestType == HTTPAPI_REQUEST_POST) ||
1204 (requestType == HTTPAPI_REQUEST_PUT) ||
1205 (requestType == HTTPAPI_REQUEST_DELETE) ||
1206 (requestType == HTTPAPI_REQUEST_PATCH) ||
1207 (requestType == HTTPAPI_REQUEST_HEAD))
1208 {
1209 result = true;
1210 }
1211 else
1212 {
1213 result = false;
1214 }
1215
1216 return result;
1217 }
1218
1219 /*Codes_SRS_HTTPAPI_COMPACT_21_021: [ The HTTPAPI_ExecuteRequest shall execute the http communtication with the provided host, sending a request and reciving the response. ]*/
1220 /*Codes_SRS_HTTPAPI_COMPACT_21_050: [ If there is a content in the response, the HTTPAPI_ExecuteRequest shall copy it in the responseContent buffer. ]*/
1221 //Note: This function assumes that "Host:" and "Content-Length:" headers are setup
1222 // by the caller of HTTPAPI_ExecuteRequest() (which is true for httptransport.c).
1223 HTTPAPI_RESULT HTTPAPI_ExecuteRequest(HTTP_HANDLE handle, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath,
1224 HTTP_HEADERS_HANDLE httpHeadersHandle, const unsigned char* content,
1225 size_t contentLength, unsigned int* statusCode,
1226 HTTP_HEADERS_HANDLE responseHeadersHandle, BUFFER_HANDLE responseContent)
1227 {
1228 HTTPAPI_RESULT result = HTTPAPI_ERROR;
1229 size_t headersCount;
1230 size_t bodyLength = 0;
1231 bool chunked = false;
1232 HTTP_HANDLE_DATA* http_instance = (HTTP_HANDLE_DATA*)handle;
1233
1234 /*Codes_SRS_HTTPAPI_COMPACT_21_034: [ If there is no previous connection, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1235 /*Codes_SRS_HTTPAPI_COMPACT_21_037: [ If the request type is unknown, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1236 /*Codes_SRS_HTTPAPI_COMPACT_21_039: [ If the relativePath is NULL or invalid, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1237 /*Codes_SRS_HTTPAPI_COMPACT_21_041: [ If the httpHeadersHandle is NULL or invalid, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1238 /*Codes_SRS_HTTPAPI_COMPACT_21_053: [ The HTTPAPI_ExecuteRequest shall produce a set of http header to send to the host. ]*/
1239 /*Codes_SRS_HTTPAPI_COMPACT_21_040: [ The request shall contain the http header provided in httpHeadersHandle parameter. ]*/
1240 /*Codes_SRS_HTTPAPI_COMPACT_21_054: [ If Http header maker cannot provide the number of headers, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1241 if (http_instance == NULL ||
1242 relativePath == NULL ||
1243 httpHeadersHandle == NULL ||
1244 !validRequestType(requestType) ||
1245 HTTPHeaders_GetHeaderCount(httpHeadersHandle, &headersCount) != HTTP_HEADERS_OK)
1246 {
1247 result = HTTPAPI_INVALID_ARG;
1248 LogError("(result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
1249 }
1250 /*Codes_SRS_HTTPAPI_COMPACT_21_024: [ The HTTPAPI_ExecuteRequest shall open the transport connection with the host to send the request. ]*/
1251 else if ((result = OpenXIOConnection(http_instance)) != HTTPAPI_OK)
1252 {
1253 LogError("Open HTTP connection failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
1254 }
1255 /*Codes_SRS_HTTPAPI_COMPACT_21_026: [ If the open process succeed, the HTTPAPI_ExecuteRequest shall send the request message to the host. ]*/
1256 else if ((result = SendHeadsToXIO(http_instance, requestType, relativePath, httpHeadersHandle, headersCount)) != HTTPAPI_OK)
1257 {
1258 LogError("Send heads to HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
1259 }
1260 /*Codes_SRS_HTTPAPI_COMPACT_21_042: [ The request can contain the a content message, provided in content parameter. ]*/
1261 else if ((result = SendContentToXIO(http_instance, content, contentLength)) != HTTPAPI_OK)
1262 {
1263 LogError("Send content to HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
1264 }
1265 /*Codes_SRS_HTTPAPI_COMPACT_21_030: [ At the end of the transmission, the HTTPAPI_ExecuteRequest shall receive the response from the host. ]*/
1266 /*Codes_SRS_HTTPAPI_COMPACT_21_073: [ The message received by the HTTPAPI_ExecuteRequest shall starts with a valid header. ]*/
1267 else if ((result = ReceiveHeaderFromXIO(http_instance, statusCode)) != HTTPAPI_OK)
1268 {
1269 LogError("Receive header from HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
1270 }
1271 /*Codes_SRS_HTTPAPI_COMPACT_21_074: [ After the header, the message received by the HTTPAPI_ExecuteRequest can contain addition information about the content. ]*/
1272 else if ((result = ReceiveContentInfoFromXIO(http_instance, responseHeadersHandle, &bodyLength, &chunked)) != HTTPAPI_OK)
1273 {
1274 LogError("Receive content information from HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
1275 }
1276 /*Codes_SRS_HTTPAPI_COMPACT_42_084: [ The message received by the HTTPAPI_ExecuteRequest should not contain http body. ]*/
1277 else if (requestType != HTTPAPI_REQUEST_HEAD)
1278 {
1279 /*Codes_SRS_HTTPAPI_COMPACT_21_075: [ The message received by the HTTPAPI_ExecuteRequest can contain a body with the message content. ]*/
1280 if ((result = ReadHTTPResponseBodyFromXIO(http_instance, bodyLength, chunked, responseContent)) != HTTPAPI_OK)
1281 {
1282 LogError("Read HTTP response body from HTTP failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
1283 }
1284 }
1285
1286 conn_receive_discard_buffer(http_instance);
1287
1288 return result;
1289 }
1290
1291 /*Codes_SRS_HTTPAPI_COMPACT_21_056: [ The HTTPAPI_SetOption shall change the HTTP options. ]*/
1292 /*Codes_SRS_HTTPAPI_COMPACT_21_057: [ The HTTPAPI_SetOption shall receive a handle that identiry the HTTP connection. ]*/
1293 /*Codes_SRS_HTTPAPI_COMPACT_21_058: [ The HTTPAPI_SetOption shall receive the option as a pair optionName/value. ]*/
1294 HTTPAPI_RESULT HTTPAPI_SetOption(HTTP_HANDLE handle, const char* optionName, const void* value)
1295 {
1296 HTTPAPI_RESULT result;
1297 HTTP_HANDLE_DATA* http_instance = (HTTP_HANDLE_DATA*)handle;
1298
1299 if (
1300 (http_instance == NULL) ||
1301 (optionName == NULL) ||
1302 (value == NULL)
1303 )
1304 {
1305 /*Codes_SRS_HTTPAPI_COMPACT_21_059: [ If the handle is NULL, the HTTPAPI_SetOption shall return HTTPAPI_INVALID_ARG. ]*/
1306 /*Codes_SRS_HTTPAPI_COMPACT_21_060: [ If the optionName is NULL, the HTTPAPI_SetOption shall return HTTPAPI_INVALID_ARG. ]*/
1307 /*Codes_SRS_HTTPAPI_COMPACT_21_061: [ If the value is NULL, the HTTPAPI_SetOption shall return HTTPAPI_INVALID_ARG. ]*/
1308 result = HTTPAPI_INVALID_ARG;
1309 }
1310 else if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
1311 {
1312 #ifdef DO_NOT_COPY_TRUSTED_CERTS_STRING
1313 result = HTTPAPI_OK;
1314 http_instance->certificate = (char*)value;
1315 #else
1316
1317 if (http_instance->certificate)
1318 {
1319 free(http_instance->certificate);
1320 }
1321
1322 size_t len = strlen((char*)value);
1323 size_t malloc_size = safe_add_size_t(len, 1);
1324 malloc_size = safe_multiply_size_t(malloc_size, sizeof(char));
1325 if (malloc_size == SIZE_MAX)
1326 {
1327 LogError("Invalid malloc size");
1328 http_instance->certificate = NULL;
1329 }
1330 else
1331 {
1332 http_instance->certificate = (char*)malloc(malloc_size);
1333 }
1334
1335 if (http_instance->certificate == NULL)
1336 {
1337 /*Codes_SRS_HTTPAPI_COMPACT_21_062: [ If any memory allocation get fail, the HTTPAPI_SetOption shall return HTTPAPI_ALLOC_FAILED. ]*/
1338 result = HTTPAPI_ALLOC_FAILED;
1339 LogInfo("unable to allocate memory for the certificate in HTTPAPI_SetOption");
1340 }
1341 else
1342 {
1343 /*Codes_SRS_HTTPAPI_COMPACT_21_064: [ If the HTTPAPI_SetOption get success setting the option, it shall return HTTPAPI_OK. ]*/
1344 (void)strcpy(http_instance->certificate, (const char*)value);
1345 result = HTTPAPI_OK;
1346 }
1347 #endif // DO_NOT_COPY_TRUSTED_CERTS_STRING
1348 }
1349 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
1350 {
1351 if (http_instance->x509ClientCertificate)
1352 {
1353 free(http_instance->x509ClientCertificate);
1354 }
1355
1356 size_t len = strlen((char*)value);
1357 size_t malloc_size = safe_add_size_t(len, 1);
1358 malloc_size = safe_multiply_size_t(malloc_size, sizeof(char));
1359 if (malloc_size == SIZE_MAX)
1360 {
1361 LogError("Invalid malloc size");
1362 http_instance->x509ClientCertificate = NULL;
1363 }
1364 else
1365 {
1366 http_instance->x509ClientCertificate = (char*)malloc(malloc_size);
1367 }
1368
1369 if (http_instance->x509ClientCertificate == NULL)
1370 {
1371 /*Codes_SRS_HTTPAPI_COMPACT_21_062: [ If any memory allocation get fail, the HTTPAPI_SetOption shall return HTTPAPI_ALLOC_FAILED. ]*/
1372 result = HTTPAPI_ALLOC_FAILED;
1373 LogInfo("unable to allocate memory for the client certificate in HTTPAPI_SetOption");
1374 }
1375 else
1376 {
1377 /*Codes_SRS_HTTPAPI_COMPACT_21_064: [ If the HTTPAPI_SetOption get success setting the option, it shall return HTTPAPI_OK. ]*/
1378 (void)strcpy(http_instance->x509ClientCertificate, (const char*)value);
1379 result = HTTPAPI_OK;
1380 }
1381 }
1382 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
1383 {
1384 if (http_instance->x509ClientPrivateKey)
1385 {
1386 free(http_instance->x509ClientPrivateKey);
1387 }
1388
1389 size_t len = strlen((char*)value);
1390 size_t malloc_size = safe_add_size_t(len, 1);
1391 malloc_size = safe_multiply_size_t(malloc_size, sizeof(char));
1392 if (malloc_size == SIZE_MAX)
1393 {
1394 LogError("Invalid malloc size");
1395 http_instance->x509ClientPrivateKey = NULL;
1396 }
1397 else
1398 {
1399 http_instance->x509ClientPrivateKey = (char*)malloc(malloc_size);
1400 }
1401
1402 if (http_instance->x509ClientPrivateKey == NULL)
1403 {
1404 /*Codes_SRS_HTTPAPI_COMPACT_21_062: [ If any memory allocation get fail, the HTTPAPI_SetOption shall return HTTPAPI_ALLOC_FAILED. ]*/
1405 result = HTTPAPI_ALLOC_FAILED;
1406 LogInfo("unable to allocate memory for the client private key in HTTPAPI_SetOption");
1407 }
1408 else
1409 {
1410 /*Codes_SRS_HTTPAPI_COMPACT_21_064: [ If the HTTPAPI_SetOption get success setting the option, it shall return HTTPAPI_OK. ]*/
1411 (void)strcpy(http_instance->x509ClientPrivateKey, (const char*)value);
1412 result = HTTPAPI_OK;
1413 }
1414 }
1415 else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0)
1416 {
1417 TLSIO_CONFIG tlsio_config;
1418 HTTP_PROXY_IO_CONFIG proxy_config;
1419 HTTP_PROXY_OPTIONS* proxy_options = (HTTP_PROXY_OPTIONS*)value;
1420
1421 if (proxy_options->host_address == NULL)
1422 {
1423 LogError("NULL host_address in proxy options");
1424 result = HTTPAPI_ERROR;
1425 }
1426 else if (((proxy_options->username == NULL) || (proxy_options->password == NULL)) &&
1427 (proxy_options->username != proxy_options->password))
1428 {
1429 LogError("Only one of username and password for proxy settings was NULL");
1430 result = HTTPAPI_ERROR;
1431 }
1432 else
1433 {
1434
1435 /* Workaround: xio interface is already created when HTTPAPI_CreateConnection is call without proxy support
1436 * need to destroy the interface and create a new one with proxy information
1437 */
1438 OPTIONHANDLER_HANDLE xio_options;
1439 if ((xio_options = xio_retrieveoptions(http_instance->xio_handle)) == NULL)
1440 {
1441 LogError("failed saving underlying I/O transport options");
1442 result = HTTPAPI_ERROR;
1443 }
1444 else
1445 {
1446 xio_destroy(http_instance->xio_handle);
1447
1448 proxy_config.hostname = http_instance->hostName;
1449 proxy_config.proxy_hostname = proxy_options->host_address;
1450 proxy_config.password = proxy_options->password;
1451 proxy_config.username = proxy_options->username;
1452 proxy_config.proxy_port = proxy_options->port;
1453 proxy_config.port = 443;
1454
1455 tlsio_config.hostname = http_instance->hostName;
1456 tlsio_config.port = 443;
1457 tlsio_config.underlying_io_interface = http_proxy_io_get_interface_description();
1458 tlsio_config.underlying_io_parameters = &proxy_config;
1459 tlsio_config.invoke_on_send_complete_callback_for_fragments = true;
1460
1461 http_instance->xio_handle = xio_create(platform_get_default_tlsio(), (void*)&tlsio_config);
1462
1463 if (http_instance->xio_handle == NULL)
1464 {
1465 LogError("Failed to create xio handle with proxy configuration");
1466 result = HTTPAPI_ERROR;
1467 }
1468 else
1469 {
1470 if (OptionHandler_FeedOptions(xio_options, http_instance->xio_handle) != OPTIONHANDLER_OK)
1471 {
1472 LogError("Failed feeding existing options to new xio instance.");
1473 result = HTTPAPI_ERROR;
1474 }
1475 else
1476 {
1477 result = HTTPAPI_OK;
1478 }
1479 }
1480
1481 OptionHandler_Destroy(xio_options);
1482 }
1483 }
1484 }
1485 else if (strcmp(OPTION_SET_TLS_RENEGOTIATION, optionName) == 0)
1486 {
1487 bool tls_renegotiation = *(bool*)value;
1488 http_instance->tls_renegotiation = tls_renegotiation;
1489 result = HTTPAPI_OK;
1490 }
1491 else
1492 {
1493 /*Codes_SRS_HTTPAPI_COMPACT_21_063: [ If the HTTP do not support the optionName, the HTTPAPI_SetOption shall return HTTPAPI_INVALID_ARG. ]*/
1494 result = HTTPAPI_INVALID_ARG;
1495 LogInfo("unknown option %s", optionName);
1496 }
1497
1498 return result;
1499 }
1500
1501 /*Codes_SRS_HTTPAPI_COMPACT_21_065: [ The HTTPAPI_CloneOption shall provide the means to clone the HTTP option. ]*/
1502 /*Codes_SRS_HTTPAPI_COMPACT_21_066: [ The HTTPAPI_CloneOption shall return a clone of the value identified by the optionName. ]*/
1503 HTTPAPI_RESULT HTTPAPI_CloneOption(const char* optionName, const void* value, const void** savedValue)
1504 {
1505 HTTPAPI_RESULT result;
1506 size_t certLen;
1507 char* tempCert;
1508
1509 if (
1510 (optionName == NULL) ||
1511 (value == NULL) ||
1512 (savedValue == NULL)
1513 )
1514 {
1515 /*Codes_SRS_HTTPAPI_COMPACT_21_067: [ If the optionName is NULL, the HTTPAPI_CloneOption shall return HTTPAPI_INVALID_ARG. ]*/
1516 /*Codes_SRS_HTTPAPI_COMPACT_21_068: [ If the value is NULL, the HTTPAPI_CloneOption shall return HTTPAPI_INVALID_ARG. ]*/
1517 /*Codes_SRS_HTTPAPI_COMPACT_21_069: [ If the savedValue is NULL, the HTTPAPI_CloneOption shall return HTTPAPI_INVALID_ARG. ]*/
1518 result = HTTPAPI_INVALID_ARG;
1519 }
1520 else if (strcmp(OPTION_TRUSTED_CERT, optionName) == 0)
1521 {
1522 #ifdef DO_NOT_COPY_TRUSTED_CERTS_STRING
1523 *savedValue = (const void*)value;
1524 result = HTTPAPI_OK;
1525 #else
1526 certLen = strlen((const char*)value);
1527 size_t malloc_size = safe_add_size_t(certLen, 1);
1528 malloc_size = safe_multiply_size_t(malloc_size, sizeof(char));
1529 if (malloc_size == SIZE_MAX)
1530 {
1531 tempCert = NULL;
1532 }
1533 else
1534 {
1535 tempCert = (char*)malloc(malloc_size);
1536 }
1537
1538 if (tempCert == NULL)
1539 {
1540 /*Codes_SRS_HTTPAPI_COMPACT_21_070: [ If any memory allocation get fail, the HTTPAPI_CloneOption shall return HTTPAPI_ALLOC_FAILED. ]*/
1541 result = HTTPAPI_ALLOC_FAILED;
1542 }
1543 else
1544 {
1545 /*Codes_SRS_HTTPAPI_COMPACT_21_072: [ If the HTTPAPI_CloneOption get success setting the option, it shall return HTTPAPI_OK. ]*/
1546 (void)strcpy(tempCert, (const char*)value);
1547 *savedValue = tempCert;
1548 result = HTTPAPI_OK;
1549 }
1550 #endif // DO_NOT_COPY_TRUSTED_CERTS_STRING
1551 }
1552 else if (strcmp(SU_OPTION_X509_CERT, optionName) == 0 || strcmp(OPTION_X509_ECC_CERT, optionName) == 0)
1553 {
1554 certLen = strlen((const char*)value);
1555 size_t malloc_size = safe_add_size_t(certLen, 1);
1556 malloc_size = safe_multiply_size_t(malloc_size, sizeof(char));
1557 if (malloc_size == SIZE_MAX)
1558 {
1559 LogError("Invalid malloc size");
1560 tempCert = NULL;
1561 }
1562 else
1563 {
1564 tempCert = (char*)malloc(malloc_size);
1565 }
1566
1567 if (tempCert == NULL)
1568 {
1569 /*Codes_SRS_HTTPAPI_COMPACT_21_070: [ If any memory allocation get fail, the HTTPAPI_CloneOption shall return HTTPAPI_ALLOC_FAILED. ]*/
1570 result = HTTPAPI_ALLOC_FAILED;
1571 }
1572 else
1573 {
1574 /*Codes_SRS_HTTPAPI_COMPACT_21_072: [ If the HTTPAPI_CloneOption get success setting the option, it shall return HTTPAPI_OK. ]*/
1575 (void)strcpy(tempCert, (const char*)value);
1576 *savedValue = tempCert;
1577 result = HTTPAPI_OK;
1578 }
1579 }
1580 else if (strcmp(SU_OPTION_X509_PRIVATE_KEY, optionName) == 0 || strcmp(OPTION_X509_ECC_KEY, optionName) == 0)
1581 {
1582 certLen = strlen((const char*)value);
1583 size_t malloc_size = safe_add_size_t(certLen, 1);
1584 malloc_size = safe_multiply_size_t(malloc_size, sizeof(char));
1585 if (malloc_size == SIZE_MAX)
1586 {
1587 LogError("Invalid malloc size");
1588 tempCert = NULL;
1589 }
1590 else
1591 {
1592 tempCert = (char*)malloc(malloc_size);
1593 }
1594
1595 if (tempCert == NULL)
1596 {
1597 /*Codes_SRS_HTTPAPI_COMPACT_21_070: [ If any memory allocation get fail, the HTTPAPI_CloneOption shall return HTTPAPI_ALLOC_FAILED. ]*/
1598 result = HTTPAPI_ALLOC_FAILED;
1599 }
1600 else
1601 {
1602 /*Codes_SRS_HTTPAPI_COMPACT_21_072: [ If the HTTPAPI_CloneOption get success setting the option, it shall return HTTPAPI_OK. ]*/
1603 (void)strcpy(tempCert, (const char*)value);
1604 *savedValue = tempCert;
1605 result = HTTPAPI_OK;
1606 }
1607 }
1608 else if (strcmp(OPTION_HTTP_PROXY, optionName) == 0)
1609 {
1610 HTTP_PROXY_OPTIONS* proxy_data = (HTTP_PROXY_OPTIONS*)value;
1611
1612 HTTP_PROXY_OPTIONS* new_proxy_info = malloc(sizeof(HTTP_PROXY_OPTIONS));
1613 if (new_proxy_info == NULL)
1614 {
1615 LogError("unable to allocate proxy option information");
1616 result = HTTPAPI_ERROR;
1617 }
1618 else
1619 {
1620 new_proxy_info->host_address = proxy_data->host_address;
1621 new_proxy_info->port = proxy_data->port;
1622 new_proxy_info->password = proxy_data->password;
1623 new_proxy_info->username = proxy_data->username;
1624 *savedValue = new_proxy_info;
1625 result = HTTPAPI_OK;
1626 }
1627 }
1628 else if (strcmp(OPTION_SET_TLS_RENEGOTIATION, optionName) == 0)
1629 {
1630 bool* temp = (bool*)malloc(sizeof(bool)); /*shall be freed by HTTPAPIEX_Destroy*/
1631 if (temp == NULL)
1632 {
1633 result = HTTPAPI_ERROR;
1634 LogError("malloc failed (result = %" PRI_MU_ENUM ")", MU_ENUM_VALUE(HTTPAPI_RESULT, result));
1635 }
1636 else
1637 {
1638 *temp = *(bool*)value;
1639 *savedValue = temp;
1640 result = HTTPAPI_OK;
1641 }
1642 }
1643 else
1644 {
1645 /*Codes_SRS_HTTPAPI_COMPACT_21_071: [ If the HTTP do not support the optionName, the HTTPAPI_CloneOption shall return HTTPAPI_INVALID_ARG. ]*/
1646 result = HTTPAPI_INVALID_ARG;
1647 LogInfo("unknown option %s", optionName);
1648 }
1649 return result;
1650 }