-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathhttp_perform.c
More file actions
747 lines (674 loc) · 26.4 KB
/
http_perform.c
File metadata and controls
747 lines (674 loc) · 26.4 KB
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
#ifdef MOCK_ENABLED
#include <stddef.h>
#include <stdarg.h>
#include <setjmp.h>
#include <cmocka.h>
#endif
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <snowflake/basic_types.h>
#include <snowflake/client.h>
#include <snowflake/logger.h>
#include <snowflake/Stopwatch.h>
#include "connection.h"
#include "mock_http_perform.h"
#include "error.h"
#include "memory.h"
#include "constants.h"
#include "client_int.h"
#include "snowflake_util.h"
static void
dump(const char *text, FILE *stream, unsigned char *ptr, size_t size,
char nohex);
static int my_trace(CURL *handle, curl_infotype type, char *data, size_t size,
void *userp);
/* Enable curl verbose logging when the environment variable SF_CURL_VERBOSE is set.
* This allows turning on libcurl debug without recompiling with DEBUG. */
static
void dump(const char *text,
FILE *stream, unsigned char *ptr, size_t size,
char nohex) {
size_t i;
size_t c;
unsigned int width = 0x10;
if (nohex)
/* without the hex output, we can fit more on screen */
width = 0x40;
sf_fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
text, (long) size, (long) size);
for (i = 0; i < size; i += width) {
sf_fprintf(stream, "%4.4lx: ", (long) i);
if (!nohex) {
/* hex not disabled, show it */
for (c = 0; c < width; c++)
if (i + c < size)
sf_fprintf(stream, "%02x ", ptr[i + c]);
else
fputs(" ", stream);
}
for (c = 0; (c < width) && (i + c < size); c++) {
/* check for 0D0A; if found, skip past and start a new line of output */
if (nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
ptr[i + c + 1] == 0x0A) {
i += (c + 2 - width);
break;
}
sf_fprintf(stream, "%c",
(ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c]
: '.');
/* check again for 0D0A, to avoid an extra \n if it's at width */
if (nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
ptr[i + c + 2] == 0x0A) {
i += (c + 3 - width);
break;
}
}
fputc('\n', stream); /* newline */
}
fflush(stream);
}
static
int my_trace(CURL *handle, curl_infotype type,
char *data, size_t size,
void *userp) {
struct data *config = (struct data *) userp;
const char *text;
(void) handle; /* prevent compiler warning */
char masked[5000] = {'\0'};
switch (type) {
case CURLINFO_TEXT:
sf_fprintf(stderr, "== Info: %s", data);
/* FALLTHROUGH */
default: /* in case a new one is introduced to shock us */
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
}
terminal_mask(data, size, masked, sizeof(masked));
dump(text, stderr, (unsigned char *) masked, strlen(masked), config->trace_ascii);
return 0;
}
sf_bool STDCALL http_perform(CURL *curl,
SF_REQUEST_TYPE request_type,
char *url,
SF_HEADER *header,
char *body,
PUT_PAYLOAD* put_payload,
cJSON **json,
NON_JSON_RESP *non_json_resp,
char** resp_headers,
int64 retry_timeout,
int64 network_timeout,
sf_bool chunk_downloader,
SF_ERROR_STRUCT *error,
sf_bool insecure_mode,
sf_bool fail_open,
sf_bool crl_check,
sf_bool crl_advisory,
sf_bool crl_allow_no_crl,
sf_bool crl_disk_caching,
sf_bool crl_memory_caching,
long crl_download_timeout,
int8 retry_on_curle_couldnt_connect_count,
int64 renew_timeout,
int8 retry_max_count,
int64 *elapsed_time,
int8 *retried_count,
sf_bool *is_renew,
sf_bool renew_injection,
const char *proxy,
const char *no_proxy,
sf_bool include_retry_reason,
sf_bool is_new_strategy_request) {
CURLcode res;
sf_bool ret = SF_BOOLEAN_FALSE;
sf_bool retry = SF_BOOLEAN_FALSE;
long int http_code = 0;
DECORRELATE_JITTER_BACKOFF djb = {
SF_BACKOFF_BASE, //base
SF_BACKOFF_CAP //cap
};
if (SF_BOOLEAN_TRUE == is_new_strategy_request)
{
djb.cap = SF_NEW_STRATEGY_BACKOFF_CAP;
}
log_debug("Starting http_perform");
Stopwatch stopwatch;
stopwatch_start(&stopwatch);
retry_timeout = (retry_timeout > 0) ? retry_timeout : SF_RETRY_TIMEOUT;
if (elapsed_time) {
retry_timeout -= *elapsed_time;
if (retry_timeout <= 0) {
retry_timeout = 1;
}
}
network_timeout = (network_timeout > 0) ? network_timeout : SF_NETWORK_TIMEOUT;
if (elapsed_time) {
network_timeout -= *elapsed_time;
if (network_timeout <= 0) {
network_timeout = 1;
}
}
if (retry_timeout < network_timeout)
{
network_timeout = retry_timeout;
}
RETRY_CONTEXT curl_retry_ctx = {
retried_count ? *retried_count : 0, //retry_count
0, // retry reason
retry_timeout,
djb.base, // time to sleep
&djb, // Decorrelate jitter
sf_get_current_time_millis() // start time
};
time_t elapsedRetryTime = time(NULL);
RAW_CHAR_BUFFER buffer = {NULL, 0};
RAW_CHAR_BUFFER headerBuffer = { NULL, 0 };
struct data config;
config.trace_ascii = 1;
if (curl == NULL) {
return SF_BOOLEAN_FALSE;
}
// Set libcurl error buffer for detailed diagnostics
char curl_error_buffer[CURL_ERROR_SIZE];
curl_error_buffer[0] = '\0';
do {
// Reset buffer since this may not be our first rodeo
SF_FREE(buffer.buffer);
buffer.size = 0;
SF_FREE(headerBuffer.buffer);
headerBuffer.size = 0;
// Generate new request guid, if request guid exists in url
if (SF_BOOLEAN_TRUE != retry_ctx_update_url(&curl_retry_ctx, url, include_retry_reason)) {
log_error("Failed to update request url");
break;
}
// Set curl timeout to ensure the request won't exceed network timeout when
// there is delay due to network issue
long curl_timeout = network_timeout;
if (renew_timeout > 0) {
curl_timeout = renew_timeout > network_timeout ?
network_timeout : renew_timeout;
}
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, curl_timeout);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, curl_timeout);
// Set parameters
curl_error_buffer[0] = '\0';
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_error_buffer);
res = curl_easy_setopt(curl, CURLOPT_URL, url);
if (res != CURLE_OK) {
log_error("Failed to set URL [%s]", curl_easy_strerror(res));
break;
}
{
int enable_verbose = DEBUG ? 1 : 0;
const char *sf_cv = getenv("SF_CURL_VERBOSE");
if (sf_cv && sf_cv[0] != '0') enable_verbose = 1;
if (enable_verbose) {
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &config);
/* the DEBUGFUNCTION has no effect until we enable VERBOSE */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
}
}
if (header) {
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header->header);
if (res != CURLE_OK) {
log_error("Failed to set header [%s]", curl_easy_strerror(res));
break;
}
}
// Set proxy
res = set_curl_proxy(curl, proxy, no_proxy);
if (res != CURLE_OK) {
log_error("Failed to set proxy [%s]", curl_easy_strerror(res));
break;
}
// Post type stuffs
if (request_type == POST_REQUEST_TYPE) {
res = curl_easy_setopt(curl, CURLOPT_POST, 1);
if (res != CURLE_OK) {
log_error("Failed to set post [%s]", curl_easy_strerror(res));
break;
}
if (body) {
res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
} else {
res = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "");
}
if (res != CURLE_OK) {
log_error("Failed to set body [%s]", curl_easy_strerror(res));
break;
}
}
else if (request_type == HEAD_REQUEST_TYPE)
{
/** we want response header only */
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
}
else if (request_type == PUT_REQUEST_TYPE)
{
// we need to upload the data
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
if (!put_payload)
{
log_error("Invalid payload for put request");
break;
}
/** set read callback function */
res = curl_easy_setopt(curl, CURLOPT_READFUNCTION, put_payload->read_callback);
if (res != CURLE_OK) {
log_error("Failed to set read function [%s]", curl_easy_strerror(res));
break;
}
/** set data object to pass to callback function */
res = curl_easy_setopt(curl, CURLOPT_READDATA, put_payload->buffer);
if (res != CURLE_OK) {
log_error("Failed to set read data [%s]", curl_easy_strerror(res));
break;
}
/** set size of put */
if (put_payload->length <= SF_INT32_MAX)
{
res = curl_easy_setopt(curl, CURLOPT_INFILESIZE, (long)put_payload->length);
}
else
{
res = curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)put_payload->length);
}
}
if (!json && non_json_resp)
{
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, non_json_resp->write_callback);
}
else
{
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (void*)&char_resp_cb);
}
if (res != CURLE_OK) {
log_error("Failed to set writer [%s]", curl_easy_strerror(res));
break;
}
if (!json && non_json_resp)
{
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, non_json_resp->buffer);
}
else
{
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&buffer);
}
if (res != CURLE_OK) {
log_error("Failed to set write data [%s]", curl_easy_strerror(res));
break;
}
res = curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void*)&headerBuffer);
if (res != CURLE_OK) {
log_error("Failed to set header data [%s]", curl_easy_strerror(res));
break;
}
res = curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, (void*)&char_resp_cb);
if (res != CURLE_OK) {
log_error("Failed to set header function [%s]", curl_easy_strerror(res));
break;
}
if (DISABLE_VERIFY_PEER) {
res = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
if (res != CURLE_OK) {
log_error("Failed to disable peer verification [%s]",
curl_easy_strerror(res));
break;
}
}
res = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
if (res != CURLE_OK) {
log_error("Failed to enable redirection [%s]",
curl_easy_strerror(res));
break;
}
if (CA_BUNDLE_FILE) {
res = curl_easy_setopt(curl, CURLOPT_CAINFO, CA_BUNDLE_FILE);
if (res != CURLE_OK) {
log_error("Unable to set certificate file [%s]",
curl_easy_strerror(res));
break;
}
}
res = curl_easy_setopt(curl, CURLOPT_SSLVERSION, SSL_VERSION);
if (res != CURLE_OK) {
log_error("Unable to set SSL Version [%s]",
curl_easy_strerror(res));
break;
}
// If insecure mode is set to true, skip OCSP check not matter the value of SF_OCSP_CHECK (global OCSP variable)
sf_bool ocsp_check;
if (insecure_mode) {
ocsp_check = SF_BOOLEAN_FALSE;
} else {
ocsp_check = SF_OCSP_CHECK;
}
res = curl_easy_setopt(curl, CURLOPT_SSL_SF_OCSP_CHECK, ocsp_check);
if (res != CURLE_OK) {
log_error("Unable to set OCSP check enable/disable [%s]",
curl_easy_strerror(res));
break;
}
res = curl_easy_setopt(curl, CURLOPT_SSL_SF_OCSP_FAIL_OPEN, fail_open);
if (res != CURLE_OK) {
log_error("Unable to set OCSP FAIL_OPEN [%s]",
curl_easy_strerror(res));
break;
}
res = curl_easy_setopt(curl, CURLOPT_SSL_SF_CRL_CHECK, crl_check);
if (res != CURLE_OK) {
log_error("Unable to set CRL CHECK [%s]",
curl_easy_strerror(res));
break;
}
if (crl_check)
{
res = curl_easy_setopt(curl, CURLOPT_SSL_SF_CRL_ADVISORY, crl_advisory);
if (res != CURLE_OK)
{
log_error("Unable to set CRL advisory mode [%s]",
curl_easy_strerror(res));
break;
}
res = curl_easy_setopt(curl, CURLOPT_SSL_SF_CRL_ALLOW_NO_CRL, crl_allow_no_crl);
if (res != CURLE_OK)
{
log_error("Unable to set CRL allow null crl [%s]",
curl_easy_strerror(res));
break;
}
res = curl_easy_setopt(curl, CURLOPT_SSL_SF_CRL_DISK_CACHING, crl_disk_caching);
if (res != CURLE_OK)
{
log_error("Unable to set CRL disk caching [%s]",
curl_easy_strerror(res));
break;
}
res = curl_easy_setopt(curl, CURLOPT_SSL_SF_CRL_MEMORY_CACHING, crl_memory_caching);
if (res != CURLE_OK)
{
log_error("Unable to set CRL memory caching [%s]",
curl_easy_strerror(res));
break;
}
res = curl_easy_setopt(curl, CURLOPT_SSL_SF_CRL_DOWNLOAD_TIMEOUT, crl_download_timeout);
if (res != CURLE_OK)
{
log_error("Unable to set CRL download timeout [%s]",
curl_easy_strerror(res));
break;
}
}
// Set chunk downloader specific stuff here
if (chunk_downloader) {
res = curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
if (res != CURLE_OK) {
log_error("Unable to set accepted content encoding",
curl_easy_strerror(res));
break;
}
if (json)
{
// Set the first character in the buffer as a bracket
buffer.buffer = (char *)SF_CALLOC(1,
2); // Don't forget null terminator
buffer.size = 1;
sf_strncpy(buffer.buffer, 2, "[", 2);
}
}
// Be optimistic
retry = SF_BOOLEAN_FALSE;
log_trace("Running curl call");
res = curl_easy_perform(curl);
// forcely trigger renew timeout on the first attempt
if ((renew_injection) && (renew_timeout > 0) &&
elapsed_time && (*elapsed_time <= 0))
{
sf_sleep_ms(renew_timeout * 1000);
res = CURLE_OPERATION_TIMEDOUT;
}
/* Check for errors */
if (res != CURLE_OK) {
if (curl_error_buffer[0] != '\0') {
log_error("curl error buffer: %s", curl_error_buffer);
}
if (res == CURLE_COULDNT_CONNECT &&
curl_retry_ctx.retry_count < (unsigned)retry_on_curle_couldnt_connect_count)
{
retry = SF_BOOLEAN_TRUE;
uint32 next_sleep_in_secs = retry_ctx_next_sleep(&curl_retry_ctx);
log_error(
"curl_easy_perform() failed connecting to server on attempt %d, "
"will retry after %d second",
curl_retry_ctx.retry_count,
next_sleep_in_secs);
sf_sleep_ms(next_sleep_in_secs*1000);
} else if ((res == CURLE_OPERATION_TIMEDOUT) &&
((renew_timeout > 0) && (curl_timeout == renew_timeout)))
// retry directly without backoff when timeout is triggered by renew
{
retry = SF_BOOLEAN_TRUE;
}
// retry with backoff on any other curl error except particular non-retryable ones
else {
char msg[1024];
sf_sprintf(msg, sizeof(msg), "curl_easy_perform() failed: %s", curl_easy_strerror(res));
msg[sizeof(msg) - 1] = (char)0;
if (res == CURLE_SSL_CACERT_BADFILE) {
sf_sprintf(msg, sizeof(msg), "curl_easy_perform() failed. err: %s, CA Cert file: %s",
curl_easy_strerror(res), CA_BUNDLE_FILE ? CA_BUNDLE_FILE : "Not Specified");
msg[sizeof(msg) - 1] = (char)0;
log_error(msg);
SET_SNOWFLAKE_ERROR(error, SF_STATUS_ERROR_CURL,
msg,
SF_SQLSTATE_UNABLE_TO_CONNECT);
}
else if (res == CURLE_SSL_INVALIDCERTSTATUS) {
log_error("Detected CURLE_SSL_INVALIDCERTSTATUS (91) - likely OCSP/CRL validation failure.");
SET_SNOWFLAKE_ERROR(error, SF_STATUS_ERROR_CURL,
msg,
SF_SQLSTATE_UNABLE_TO_CONNECT);
}
else if (res == CURLE_PEER_FAILED_VERIFICATION)
{
log_error(msg);
SET_SNOWFLAKE_ERROR(error, SF_STATUS_ERROR_CURL,
msg,
SF_SQLSTATE_UNABLE_TO_CONNECT);
}
// otherwise retry with backoff
else {
if (((uint64)(time(NULL) - elapsedRetryTime) < curl_retry_ctx.retry_timeout) &&
((retry_max_count <= 0) || (curl_retry_ctx.retry_count < (unsigned)retry_max_count)))
{
uint32 next_sleep_in_secs = retry_ctx_next_sleep(&curl_retry_ctx);
log_debug(
"retry on network timeout, retry count %d "
"will retry after %d seconds", res,
curl_retry_ctx.retry_count,
next_sleep_in_secs);
sf_sleep_ms(next_sleep_in_secs * 1000);
retry = SF_BOOLEAN_TRUE;
}
else {
log_error(msg);
sf_sprintf(msg, sizeof(msg),
"Exceeded the retry_timeout , curl code: [%d]",
res);
SET_SNOWFLAKE_ERROR(error, SF_STATUS_ERROR_RETRY,
msg,
SF_SQLSTATE_UNABLE_TO_CONNECT);
}
}
}
} else {
if (curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code) !=
CURLE_OK) {
log_error("Unable to get http response code [%s]",
curl_easy_strerror(res));
SET_SNOWFLAKE_ERROR(error, SF_STATUS_ERROR_CURL,
"Unable to get http response code",
SF_SQLSTATE_UNABLE_TO_CONNECT);
} else if (http_code != 200) {
ret = SF_BOOLEAN_TRUE;
retry = is_retryable_http_code(http_code);
if (!retry) {
char msg[1024];
sf_sprintf(msg, sizeof(msg), "Received unretryable http code: [%d]",
http_code);
SET_SNOWFLAKE_ERROR(error,
SF_STATUS_ERROR_RETRY,
msg,
SF_SQLSTATE_UNABLE_TO_CONNECT);
ret = SF_BOOLEAN_FALSE;
} else {
curl_retry_ctx.retry_reason = (uint32)http_code;
}
if (retry) {
if (((time(NULL) - elapsedRetryTime) < curl_retry_ctx.retry_timeout) &&
((retry_max_count <= 0) || (curl_retry_ctx.retry_count < retry_max_count)))
{
uint32 next_sleep_in_secs = retry_ctx_next_sleep(&curl_retry_ctx);
log_debug(
"curl_easy_perform() Got retryable error http code %d, retry count %d "
"will retry after %d seconds", http_code,
curl_retry_ctx.retry_count,
next_sleep_in_secs);
sf_sleep_ms(next_sleep_in_secs * 1000);
}
else {
char msg[1024];
sf_sprintf(msg, sizeof(msg),
"Exceeded the retry_timeout , http code: [%d]",
http_code);
SET_SNOWFLAKE_ERROR(error,
SF_STATUS_ERROR_RETRY,
msg,
SF_SQLSTATE_UNABLE_TO_CONNECT);
retry = SF_BOOLEAN_FALSE;
}
}
} else {
ret = SF_BOOLEAN_TRUE;
}
}
// Reset everything
reset_curl(curl);
http_code = 0;
// When renew timeout is reached, stop retry and return to the caller
// to renew request
sf_bool renew_timeout_reached = retry && (renew_timeout > 0) && ((time(NULL) - elapsedRetryTime) >= renew_timeout);
sf_bool renew_timeout_disabled = retry && renew_timeout < 0;
if (renew_timeout_reached || renew_timeout_disabled) {
retry = SF_BOOLEAN_FALSE;
if (elapsed_time) {
*elapsed_time += (time(NULL) - elapsedRetryTime);
}
if (retried_count) {
*retried_count = curl_retry_ctx.retry_count;
}
if (is_renew) {
*is_renew = SF_BOOLEAN_TRUE;
}
}
}
while (retry);
if (ret && json) {
// We were successful so parse JSON from text
if (chunk_downloader) {
buffer.buffer = (char *) SF_REALLOC(buffer.buffer, buffer.size +
2); // 1 byte for closing bracket, 1 for null terminator
sf_memcpy(&buffer.buffer[buffer.size], 1, "]", 1);
buffer.size += 1;
// Set null terminator
buffer.buffer[buffer.size] = '\0';
}
snowflake_cJSON_Delete(*json);
*json = NULL;
*json = snowflake_cJSON_Parse(buffer.buffer);
if (*json) {
ret = SF_BOOLEAN_TRUE;
if (is_one_time_token_request(*json)) {
snowflake_cJSON_AddNullToObject(*json, "code");
}
} else {
SET_SNOWFLAKE_ERROR(error, SF_STATUS_ERROR_BAD_JSON,
"Unable to parse JSON text response.",
SF_SQLSTATE_UNABLE_TO_CONNECT);
ret = SF_BOOLEAN_FALSE;
}
}
SF_FREE(buffer.buffer);
if (resp_headers)
{
*resp_headers = headerBuffer.buffer;
}
else
{
SF_FREE(headerBuffer.buffer);
}
stopwatch_stop(&stopwatch);
log_debug("Complete http_perform. It took %ld milliseconds.", stopwatch_elapsedMillis(&stopwatch));
return ret;
}
#ifdef MOCK_ENABLED
sf_bool STDCALL __wrap_http_perform(CURL *curl,
SF_REQUEST_TYPE request_type,
char *url,
SF_HEADER *header,
char *body,
PUT_PAYLOAD* put_payload,
cJSON **json,
NON_JSON_RESP *non_json_resp,
char** resp_headers,
int64 retry_timeout,
int64 network_timeout,
sf_bool chunk_downloader,
SF_ERROR_STRUCT *error,
sf_bool insecure_mode,
sf_bool fail_open) {
char *resp;
const char *request_type_str = request_type == POST_REQUEST_TYPE ? "POST" : "GET";
// Remove request ID from URL (since it isn't deterministic
char *found = strchr(url, '?');
found[0] = '\0';
// Check that the generated URL is what we expected
check_expected_ptr(url);
// Check that body is what we expected
check_expected(body);
// Check request type
check_expected_ptr(request_type_str);
// Check service name header
check_expected(header->header_service_name);
// Check auth token header
check_expected(header->header_token);
// Get back mock response (assuming all inputs checked out) and parse as JSON
resp = mock_ptr_type(char *);
*json = snowflake_cJSON_Parse(resp);
return SF_BOOLEAN_TRUE;
}
#endif