/
httpapi_wince.c
722 lines (623 loc) · 22.4 KB
/
httpapi_wince.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <cstdlib>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include "httpapi.h"
#include "httpheaders.h"
#include "crt_abstractions.h"
#include "iot_logging.h"
#include "string.h"
#include "certs.h"
#include "winsock2.h"
#include "sslsock.h"
#include "schnlsp.h"
#define MAX_HOSTNAME 64
#define TEMP_BUFFER_SIZE 4096
#define CHAR_COUNT(A) (sizeof(A) - 1)
DEFINE_ENUM_STRINGS(HTTPAPI_RESULT, HTTPAPI_RESULT_VALUES)
// data structure that contains all the information required to manage a connection
class HTTP_HANDLE_DATA
{
public:
char host[MAX_HOSTNAME];
SOCKET _sock_fd;
struct sockaddr_in _remoteHost;
};
// Thanks to Luca Calligaris for his article:
// https://lcalligaris.wordpress.com/2011/04/07/implementing-a-secure-socket/
#define SSL_OPT_NO_CHECK 0
#define SSL_OPT_ISSUER_CHECK 1
#define SSL_OPT_STRICT_CHECK 2
#define SSL_OPT_DEFAULT SSL_OPT_STRICT_CHECK
static int g_SslOptions = SSL_OPT_DEFAULT;
// callback called every time a certificate needs to be validated, depending on g_SslOptions global var it can perform
// a strict validation (default) or just validate any certificate (may be needed for testing and debugging, not in production)
int CALLBACK SSLValidateCertHook(DWORD dwType, LPVOID pvArg,
DWORD dwChainLen, LPBLOB pCertChain, DWORD dwFlags)
{
HCERTSTORE hCertStore = NULL;
PCCERT_CONTEXT pCertInStore = NULL, pCertPassed = NULL, pPrevCertContext = NULL;
const LPCTSTR SystemStore[] = { _T("CA"), _T("ROOT"), _T("MY") };
unsigned int i;
int retcode = SSL_ERR_NO_CERT;
//The only thing we can handle
if (dwType != SSL_CERT_X509)
return SSL_ERR_CERT_UNKNOWN;
//Do not check the certificate at all
if (g_SslOptions == SSL_OPT_NO_CHECK)
return SSL_ERR_OKAY;
//Get a context struct from the certificate blob
pCertPassed = CertCreateCertificateContext(X509_ASN_ENCODING, pCertChain->pBlobData, pCertChain->cbSize);
if (!pCertPassed)
return SSL_ERR_FAILED;
//Check for a matching certificate in the stores
for (i = 0; i<(sizeof(SystemStore) / sizeof(SystemStore[0])); i++)
{
//open the store
hCertStore = CertOpenSystemStore((HCRYPTPROV)NULL, SystemStore[i]);
if (!hCertStore)
continue;
pPrevCertContext = NULL;
do
{
//Find a certificate from the same issuer
pCertInStore = CertFindCertificateInStore(hCertStore, X509_ASN_ENCODING,
0, CERT_FIND_ISSUER_NAME, &pCertPassed->pCertInfo->Issuer, pPrevCertContext);
if (pCertInStore)
{
//It's enough if we have a certificate from the same issuer
if (g_SslOptions == SSL_OPT_ISSUER_CHECK)
{
retcode = SSL_ERR_OKAY;
break;
}
else if (g_SslOptions == SSL_OPT_STRICT_CHECK)
{
//Compare (exact match) the certificate with the one we have found
if (CertCompareCertificate(X509_ASN_ENCODING, pCertInStore->pCertInfo, pCertPassed->pCertInfo))
{
retcode = SSL_ERR_OKAY;
break;
}
}
}
pPrevCertContext = pCertInStore;
} while (pCertInStore);
CertCloseStore(hCertStore, 0);
if (retcode != SSL_ERR_NO_CERT)
break;
}
if (pCertInStore)
CertFreeCertificateContext(pCertInStore);
CertFreeCertificateContext(pCertPassed);
return retcode;
}
// Initializes SSL layer, called only once on initialization
int SSLInit(SOCKET s, int SslOptions)
{
DWORD optval = SO_SEC_SSL;
SSLVALIDATECERTHOOK hook;
SSLPROTOCOLS protocolsToUse;
int ret;
g_SslOptions = SslOptions;//WSAIoctl cannot handle the pointer copy, use a global variable
if (setsockopt(s, SOL_SOCKET, SO_SECURE, (LPSTR)&optval, sizeof(optval)) ==
SOCKET_ERROR)
{
goto SSLInit_Error;
}
hook.HookFunc = SSLValidateCertHook;
hook.pvArg = NULL; //WSAIoctl cannot handle the pointer copy, use a global variable
if (WSAIoctl(s, SO_SSL_SET_VALIDATE_CERT_HOOK,
&hook, sizeof(SSLVALIDATECERTHOOK),
NULL, 0, NULL, NULL, NULL) == SOCKET_ERROR)
{
goto SSLInit_Error;
}
//specify TLS1 protocol
protocolsToUse.dwCount = 1;
protocolsToUse.ProtocolList[0].dwFlags = 0;
protocolsToUse.ProtocolList[0].dwProtocol = SSL_PROTOCOL_TLS1;
protocolsToUse.ProtocolList[0].dwVersion = 0;
if (WSAIoctl(s, SO_SSL_SET_PROTOCOLS,
(LPVOID)&protocolsToUse, sizeof(protocolsToUse),
NULL, 0, NULL, NULL, NULL) == SOCKET_ERROR)
{
goto SSLInit_Error;
}
return 0;
SSLInit_Error:
ret = WSAGetLastError();
return ret;
}
HTTPAPI_RESULT HTTPAPI_Init(void)
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
LogInfo("HTTPAPI_Init::Start\r\n");
time_t ctTime;
ctTime = time(NULL);
LogInfo("HTTAPI_Init::Time is now (UTC) %s\r\n", ctime(&ctTime));
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return HTTPAPI_INIT_FAILED;
}
if (LOBYTE(wsaData.wVersion) != 2 ||
HIBYTE(wsaData.wVersion) != 2) {
WSACleanup();
return HTTPAPI_INIT_FAILED;
}
LogInfo("HTTPAPI_Init::End\r\n");
return HTTPAPI_OK;
}
void HTTPAPI_Deinit(void)
{
WSACleanup();
}
HTTP_HANDLE HTTPAPI_CreateConnection(const char* hostName)
{
LogInfo("HTTPAPI_CreateConnection::Start\r\n");
HTTP_HANDLE_DATA* handle = NULL;
if (hostName)
{
LogInfo("HTTPAPI_CreateConnection::Connecting to %s\r\n", hostName);
handle = new HTTP_HANDLE_DATA();
if (strcpy_s(handle->host, MAX_HOSTNAME, hostName) != 0)
{
LogError("HTTPAPI_CreateConnection::Could not strcpy_s\r\n");
delete handle;
handle = NULL;
}
else
{
handle->_sock_fd = -1;
}
}
else
{
LogInfo("HTTPAPI_CreateConnection:: null hostName parameter\r\n");
}
LogInfo("HTTPAPI_CreateConnection::End\r\n");
return (HTTP_HANDLE)handle;
}
void HTTPAPI_CloseConnection(HTTP_HANDLE handle)
{
HTTP_HANDLE_DATA* h = (HTTP_HANDLE_DATA*)handle;
if (h)
{
LogInfo("HTTPAPI_CloseConnection to %s\r\n", h->host);
if (h->_sock_fd != -1)
{
LogInfo("HTTPAPI_CloseConnection to %s\r\n", h->host);
closesocket(h->_sock_fd);
h->_sock_fd = -1;
}
LogInfo("HTTPAPI_CloseConnection (delete h) to %s\r\n", h->host);
delete h;
}
}
static int readLine(SOCKET _sock_fd, char* buf, const size_t size)
{
// reads until \r\n is encountered. writes in buf all the characters
// read until \r\n and returns the number of characters in the buffer.
char* p = buf;
char c;
if (recv(_sock_fd, &c, 1, 0) < 0)
return -1;
while (c != '\r') {
if ((p - buf + 1) >= (int)size)
return -1;
*p++ = c;
if (recv(_sock_fd, &c, 1, 0) < 0)
return -1;
}
*p = 0;
if (recv(_sock_fd, &c, 1, 0) < 0 || c != '\n') // skip \n
return -1;
return p - buf;
}
static int readChunk(SOCKET _sock_fd, char* buf, size_t size)
{
size_t cur, offset;
// read content with specified length, even if it is received
// only in chunks due to fragmentation in the networking layer.
// returns -1 in case of error.
offset = 0;
while (size > 0)
{
cur = recv(_sock_fd, buf + offset, size, 0);
// end of stream reached
if (cur == 0)
return offset;
// read cur bytes (might be less than requested)
size -= cur;
offset += cur;
}
return offset;
}
static int skipN(SOCKET _sock_fd, size_t n, char* buf, size_t size)
{
size_t org = n;
// read and abandon response content with specified length
// returns -1 in case of error.
while (n > size)
{
if (readChunk(_sock_fd, (char*)buf, size) < 0)
return -1;
n -= size;
}
if (readChunk(_sock_fd, (char*)buf, n) < 0)
return -1;
return org;
}
//Note: This function assumes that "Host:" and "Content-Length:" headers are setup
// by the caller of HTTPAPI_ExecuteRequest() (which is true for httptransport.c).
HTTPAPI_RESULT HTTPAPI_ExecuteRequest(HTTP_HANDLE handle, HTTPAPI_REQUEST_TYPE requestType, const char* relativePath,
HTTP_HEADERS_HANDLE httpHeadersHandle, const unsigned char* content,
size_t contentLength, unsigned int* statusCode,
HTTP_HEADERS_HANDLE responseHeadersHandle, BUFFER_HANDLE responseContent)
{
LogInfo("HTTPAPI_ExecuteRequest::Start\r\n");
HTTPAPI_RESULT result;
size_t headersCount;
char buf[TEMP_BUFFER_SIZE];
int ret;
size_t bodyLength = 0;
bool chunked = false;
const unsigned char* receivedContent;
const char* method = (requestType == HTTPAPI_REQUEST_GET) ? "GET"
: (requestType == HTTPAPI_REQUEST_POST) ? "POST"
: (requestType == HTTPAPI_REQUEST_PUT) ? "PUT"
: (requestType == HTTPAPI_REQUEST_DELETE) ? "DELETE"
: (requestType == HTTPAPI_REQUEST_PATCH) ? "PATCH"
: NULL;
if (handle == NULL ||
relativePath == NULL ||
httpHeadersHandle == NULL ||
method == NULL ||
HTTPHeaders_GetHeaderCount(httpHeadersHandle, &headersCount) != HTTP_HEADERS_OK)
{
result = HTTPAPI_INVALID_ARG;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
HTTP_HANDLE_DATA* httpHandle = (HTTP_HANDLE_DATA*)handle;
if (httpHandle->_sock_fd == -1)
{
// Make the connection
if ((httpHandle->_sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
LogError("init_socket failed\r\n");
result = HTTPAPI_INIT_FAILED;
goto exit;
}
else
{
// IP Address
char address[5];
char *p_address = address;
memset(&httpHandle->_remoteHost, 0, sizeof(struct sockaddr_in));
// Dot-decimal notation
int result = sscanf(httpHandle->host, "%3u.%3u.%3u.%3u",
(unsigned int*)&address[0], (unsigned int*)&address[1],
(unsigned int*)&address[2], (unsigned int*)&address[3]);
if (result != 4) {
// Resolve address with DNS
struct hostent *host_address = gethostbyname(httpHandle->host);
if (host_address == NULL)
{
LogError("set_address failed\r\n");
result = HTTPAPI_INVALID_ARG;
goto exit;
}
p_address = (char*)host_address->h_addr_list[0];
}
memcpy((char*)&httpHandle->_remoteHost.sin_addr.s_addr, p_address, 4);
// Address family
httpHandle->_remoteHost.sin_family = AF_INET;
// Set port
httpHandle->_remoteHost.sin_port = htons(443);
if (SSLInit(httpHandle->_sock_fd, SSL_OPT_DEFAULT))
{
LogError("SSLInit failed\r\n");
result = HTTPAPI_ERROR;
goto exit;
}
if (connect(httpHandle->_sock_fd, (const struct sockaddr *) &httpHandle->_remoteHost, sizeof(httpHandle->_remoteHost)) < 0)
{
closesocket(httpHandle->_sock_fd);
httpHandle->_sock_fd = -1;
LogError("connect failed\r\n");
result = HTTPAPI_ERROR;
goto exit;
}
}
}
//Send request
if ((ret = _snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\n", method, relativePath)) < 0
|| ret >= sizeof(buf))
{
result = HTTPAPI_STRING_PROCESSING_ERROR;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
LogInfo("HTTPAPI_ExecuteRequest::Sending=%*.*s\r\n", strlen(buf), strlen(buf), buf);
if (send(httpHandle->_sock_fd, buf, strlen(buf), 0) < 0)
{
result = HTTPAPI_SEND_REQUEST_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
//Send default headers
for (size_t i = 0; i < headersCount; i++)
{
char* header;
if (HTTPHeaders_GetHeader(httpHeadersHandle, i, &header) != HTTP_HEADERS_OK)
{
result = HTTPAPI_HTTP_HEADERS_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
LogInfo("HTTPAPI_ExecuteRequest::Sending=%*.*s\r\n", strlen(header), strlen(header), header);
if (send(httpHandle->_sock_fd, header, strlen(header), 0) < 0)
{
result = HTTPAPI_SEND_REQUEST_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
free(header);
goto exit;
}
if (send(httpHandle->_sock_fd, "\r\n", 2, 0) < 0)
{
result = HTTPAPI_SEND_REQUEST_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
free(header);
goto exit;
}
free(header);
}
//Close headers
if (send(httpHandle->_sock_fd, "\r\n", 2, 0) < 0)
{
result = HTTPAPI_SEND_REQUEST_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
//Send data (if available)
if (content && contentLength > 0)
{
LogInfo("HTTPAPI_ExecuteRequest::Sending data=%*.*s\r\n", contentLength, contentLength, content);
if (send(httpHandle->_sock_fd, (char*)content, contentLength, 0) < 0)
{
result = HTTPAPI_SEND_REQUEST_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
}
//Receive response
if (readLine(httpHandle->_sock_fd, buf, sizeof(buf)) < 0)
{
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
//Parse HTTP response
if (sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &ret) != 1)
{
//Cannot match string, error
LogInfo("HTTPAPI_ExecuteRequest::Not a correct HTTP answer=%s\r\n", buf);
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
if (statusCode)
*statusCode = ret;
LogInfo("HTTPAPI_ExecuteRequest::Received response=%*.*s\r\n", strlen(buf), strlen(buf), buf);
//Read HTTP response headers
if (readLine(httpHandle->_sock_fd, buf, sizeof(buf)) < 0)
{
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
while (buf[0])
{
const char ContentLength[] = "content-length:";
const char TransferEncoding[] = "transfer-encoding:";
LogInfo("Receiving header=%*.*s\r\n", strlen(buf), strlen(buf), buf);
if (_strnicmp(buf, ContentLength, CHAR_COUNT(ContentLength)) == 0)
{
if (sscanf(buf + CHAR_COUNT(ContentLength), " %d", &bodyLength) != 1)
{
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
}
else if (_strnicmp(buf, TransferEncoding, CHAR_COUNT(TransferEncoding)) == 0)
{
const char* p = buf + CHAR_COUNT(TransferEncoding);
while (isspace(*p)) p++;
if (_stricmp(p, "chunked") == 0)
chunked = true;
}
char* whereIsColon = strchr((char*)buf, ':');
if (whereIsColon && responseHeadersHandle != NULL)
{
*whereIsColon = '\0';
HTTPHeaders_AddHeaderNameValuePair(responseHeadersHandle, buf, whereIsColon + 1);
}
if (readLine(httpHandle->_sock_fd, buf, sizeof(buf)) < 0)
{
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
}
//Read HTTP response body
LogInfo("HTTPAPI_ExecuteRequest::Receiving body=%d,%x\r\n", bodyLength, responseContent);
if (!chunked)
{
if (bodyLength)
{
if (responseContent != NULL)
{
if (BUFFER_pre_build(responseContent, bodyLength) != 0)
{
result = HTTPAPI_ALLOC_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
}
else if (BUFFER_content(responseContent, &receivedContent) != 0)
{
(void)BUFFER_unbuild(responseContent);
result = HTTPAPI_ALLOC_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
}
if (readChunk(httpHandle->_sock_fd, (char*)receivedContent, bodyLength) < 0)
{
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
else
{
LogInfo("HTTPAPI_ExecuteRequest::Received response body=%*.*s\r\n", bodyLength, bodyLength, receivedContent);
result = HTTPAPI_OK;
}
}
else
{
(void)skipN(httpHandle->_sock_fd, bodyLength, buf, sizeof(buf));
result = HTTPAPI_OK;
}
}
else
{
result = HTTPAPI_OK;
}
}
else
{
size_t size = 0;
result = HTTPAPI_OK;
for (;;)
{
int chunkSize;
if (readLine(httpHandle->_sock_fd, buf, sizeof(buf)) < 0) // read [length in hex]/r/n
{
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
if (sscanf(buf, "%x", &chunkSize) != 1) // chunkSize is length of next line (/r/n is not counted)
{
//Cannot match string, error
result = HTTPAPI_RECEIVE_RESPONSE_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
if (chunkSize == 0)
{
// 0 length means next line is just '\r\n' and end of chunks
if (readChunk(httpHandle->_sock_fd, (char*)buf, 2) < 0
|| buf[0] != '\r' || buf[1] != '\n') // skip /r/n
{
(void)BUFFER_unbuild(responseContent);
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
break;
}
else
{
if (responseContent != NULL)
{
if (BUFFER_enlarge(responseContent, chunkSize) != 0)
{
(void)BUFFER_unbuild(responseContent);
result = HTTPAPI_ALLOC_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
}
else if (BUFFER_content(responseContent, &receivedContent) != 0)
{
(void)BUFFER_unbuild(responseContent);
result = HTTPAPI_ALLOC_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
}
if (readChunk(httpHandle->_sock_fd, (char*)receivedContent + size, chunkSize) < 0)
{
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
}
else
{
if (skipN(httpHandle->_sock_fd, chunkSize, buf, sizeof(buf)) < 0)
{
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
}
if (readChunk(httpHandle->_sock_fd, (char*)buf, 2) < 0
|| buf[0] != '\r' || buf[1] != '\n') // skip /r/n
{
result = HTTPAPI_READ_DATA_FAILED;
LogError("(result = %s)\r\n", ENUM_TO_STRING(HTTPAPI_RESULT, result));
goto exit;
}
size += chunkSize;
}
}
if (size > 0)
{
LogInfo("HTTPAPI_ExecuteRequest::Received chunk body=%*.*s\r\n", (int)size, (int)size, (const char*)responseContent);
}
}
exit:
LogInfo("HTTPAPI_ExecuteRequest::End=%d\r\n", result);
return result;
}
HTTPAPI_RESULT HTTPAPI_SetOption(HTTP_HANDLE handle, const char* optionName, const void* value)
{
HTTPAPI_RESULT result;
if (
(handle == NULL) ||
(optionName == NULL) ||
(value == NULL)
)
{
result = HTTPAPI_INVALID_ARG;
LogError("invalid parameter (NULL) passed to HTTPAPI_SetOption\r\n");
}
else
{
result = HTTPAPI_INVALID_ARG;
LogError("unknown option %s\r\n", optionName);
}
return result;
}
HTTPAPI_RESULT HTTPAPI_CloneOption(const char* optionName, const void* value, const void** savedValue)
{
HTTPAPI_RESULT result;
if (
(optionName == NULL) ||
(value == NULL) ||
(savedValue == NULL)
)
{
result = HTTPAPI_INVALID_ARG;
LogError("invalid argument(NULL) passed to HTTPAPI_CloneOption\r\n");
}
else
{
*savedValue = NULL;
result = HTTPAPI_INVALID_ARG;
LogError("unknown option %s\r\n", optionName);
}
return result;
}