curb 1.4.1 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ChangeLog.md +20 -1
- data/README.md +17 -6
- data/ext/curb.h +3 -3
- data/ext/curb_easy.c +389 -86
- data/ext/curb_easy.h +9 -0
- data/ext/curb_multi.c +33 -16
- data/ext/curb_postfield.c +28 -51
- data/ext/curb_upload.c +1 -5
- data/ext/extconf.rb +1 -0
- data/lib/curl/easy.rb +15 -2
- data/lib/curl/multi.rb +30 -8
- data/lib/curl.rb +69 -8
- data/tests/bug_curb_easy_blocks_ruby_threads.rb +1 -1
- data/tests/bug_follow_redirect_288.rb +1 -1
- data/tests/bug_poison.rb +1 -1
- data/tests/bug_raise_on_callback.rb +1 -1
- data/tests/helper.rb +9 -8
- data/tests/tc_curl_easy.rb +239 -4
- data/tests/tc_curl_multi.rb +32 -0
- data/tests/tc_curl_native_coverage.rb +7 -1
- data/tests/tc_curl_network_policy.rb +25 -32
- data/tests/tc_curl_postfield.rb +5 -0
- data/tests/tc_curl_security_hardening.rb +386 -0
- data/tests/tc_fiber_scheduler.rb +46 -6
- data/tests/tc_ractor.rb +5 -1
- metadata +5 -3
data/ext/curb_easy.c
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
#include "curb_multi.h"
|
|
12
12
|
|
|
13
13
|
#include <errno.h>
|
|
14
|
+
#include <limits.h>
|
|
14
15
|
#include <stdlib.h>
|
|
15
16
|
#include <string.h>
|
|
16
17
|
#ifndef _WIN32
|
|
@@ -402,7 +403,7 @@ static char *curb_normalize_host_value_impl(const char *value, int raise_errors)
|
|
|
402
403
|
}
|
|
403
404
|
|
|
404
405
|
for (scan = start; scan < authority_end; scan++) {
|
|
405
|
-
if (*scan == ':')
|
|
406
|
+
if (*scan == ':' && ++colon_count == 2) break;
|
|
406
407
|
}
|
|
407
408
|
|
|
408
409
|
if (colon_count <= 1) {
|
|
@@ -442,6 +443,7 @@ static VALUE curb_normalize_host_list(VALUE hosts) {
|
|
|
442
443
|
VALUE item = rb_ary_entry(list, i);
|
|
443
444
|
VALUE host_value = rb_obj_as_string(item);
|
|
444
445
|
char *host = curb_normalize_host_value(StringValueCStr(host_value));
|
|
446
|
+
RB_GC_GUARD(host_value);
|
|
445
447
|
VALUE normalized_host = rb_str_new_cstr(host);
|
|
446
448
|
|
|
447
449
|
if (!curb_array_includes_string(normalized, normalized_host)) {
|
|
@@ -493,6 +495,7 @@ static void curb_prepare_network_allowed_hosts(ruby_curl_easy *rbce) {
|
|
|
493
495
|
for (i = 0; i < count; i++) {
|
|
494
496
|
VALUE host = rb_ary_entry(hosts, i);
|
|
495
497
|
rules[i] = curb_strdup_cstr(StringValueCStr(host));
|
|
498
|
+
RB_GC_GUARD(host);
|
|
496
499
|
}
|
|
497
500
|
|
|
498
501
|
rbce->network_allowed_hosts = rules;
|
|
@@ -531,6 +534,7 @@ static void curb_prepare_network_allowed_cidr_rules(ruby_curl_easy *rbce) {
|
|
|
531
534
|
for (i = 0; i < count; i++) {
|
|
532
535
|
VALUE cidr = rb_ary_entry(cidrs, i);
|
|
533
536
|
curb_parse_cidr_rule(StringValueCStr(cidr), &rules[i]);
|
|
537
|
+
RB_GC_GUARD(cidr);
|
|
534
538
|
}
|
|
535
539
|
|
|
536
540
|
rbce->network_allowed_cidr_rules = rules;
|
|
@@ -650,6 +654,93 @@ static int curb_host_allowlist_prereq(void *clientp,
|
|
|
650
654
|
}
|
|
651
655
|
#endif
|
|
652
656
|
|
|
657
|
+
#if defined(CURB_HAVE_OPENSOCKET_NETWORK_POLICY) && defined(HAVE_CURLOPT_DOH_URL)
|
|
658
|
+
/* libcurl's DoH probe transfers do not inherit the socket policy callback,
|
|
659
|
+
* so under :public the DoH endpoint must be vetted before the transfer.
|
|
660
|
+
* Only public IP literals are accepted: a hostname would be resolved outside
|
|
661
|
+
* the policy, leaving a DNS-rebinding window. The host is taken from
|
|
662
|
+
* libcurl's own URL parser so it matches what libcurl will connect to. */
|
|
663
|
+
static void curb_check_public_doh_url(ruby_curl_easy *rbce, const char *doh_url) {
|
|
664
|
+
CURLU *parsed_url;
|
|
665
|
+
char *host = NULL;
|
|
666
|
+
char literal[INET6_ADDRSTRLEN + 3];
|
|
667
|
+
char *address = literal;
|
|
668
|
+
size_t len;
|
|
669
|
+
unsigned char ip[16];
|
|
670
|
+
int parsed = 0;
|
|
671
|
+
|
|
672
|
+
parsed_url = curl_url();
|
|
673
|
+
if (!parsed_url) {
|
|
674
|
+
rb_raise(rb_eNoMemError, "Failed to allocate URL parser for DoH URL check");
|
|
675
|
+
}
|
|
676
|
+
if (curl_url_set(parsed_url, CURLUPART_URL, doh_url, 0) == CURLUE_OK &&
|
|
677
|
+
curl_url_get(parsed_url, CURLUPART_HOST, &host, 0) == CURLUE_OK && host) {
|
|
678
|
+
len = strlen(host);
|
|
679
|
+
if (len < sizeof(literal)) {
|
|
680
|
+
memcpy(literal, host, len + 1);
|
|
681
|
+
parsed = 1;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
if (host) curl_free(host);
|
|
685
|
+
curl_url_cleanup(parsed_url);
|
|
686
|
+
|
|
687
|
+
if (!parsed) {
|
|
688
|
+
rb_raise(eCurlErrUnsafeDestination,
|
|
689
|
+
"DoH URL must use a public IP address under public network policy");
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
len = strlen(address);
|
|
693
|
+
if (len >= 2 && address[0] == '[' && address[len - 1] == ']') {
|
|
694
|
+
address[len - 1] = '\0';
|
|
695
|
+
address++;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
if (inet_pton(AF_INET, address, ip) == 1) {
|
|
699
|
+
if (curb_ipv4_is_unsafe_destination(ip) || !curb_cidr_rules_match(rbce, CURB_CIDR_FAMILY_IPV4, ip)) {
|
|
700
|
+
rb_raise(eCurlErrUnsafeDestination,
|
|
701
|
+
"DoH URL address %s is not allowed by public network policy", address);
|
|
702
|
+
}
|
|
703
|
+
} else if (inet_pton(AF_INET6, address, ip) == 1) {
|
|
704
|
+
if (curb_ipv6_is_unsafe_destination(ip) || !curb_cidr_rules_match(rbce, CURB_CIDR_FAMILY_IPV6, ip)) {
|
|
705
|
+
rb_raise(eCurlErrUnsafeDestination,
|
|
706
|
+
"DoH URL address %s is not allowed by public network policy", address);
|
|
707
|
+
}
|
|
708
|
+
} else {
|
|
709
|
+
rb_raise(eCurlErrUnsafeDestination,
|
|
710
|
+
"DoH URL must use a public IP address under public network policy (got host %s)", address);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
#endif
|
|
714
|
+
|
|
715
|
+
#ifdef CURB_HAVE_OPENSOCKET_NETWORK_POLICY
|
|
716
|
+
/* file:// opens no socket, so the public policy's socket check never sees
|
|
717
|
+
* it. Without a protocol allowlist, reject it up front; libcurl's default
|
|
718
|
+
* redirect protocols already exclude file. The scheme comes from libcurl's
|
|
719
|
+
* parser when available. */
|
|
720
|
+
static void curb_check_public_url_scheme(const char *url) {
|
|
721
|
+
int is_file = 0;
|
|
722
|
+
#if LIBCURL_VERSION_NUM >= 0x073e00
|
|
723
|
+
CURLU *parsed_url = curl_url();
|
|
724
|
+
char *scheme = NULL;
|
|
725
|
+
if (!parsed_url) {
|
|
726
|
+
rb_raise(rb_eNoMemError, "Failed to allocate URL parser for scheme check");
|
|
727
|
+
}
|
|
728
|
+
if (curl_url_set(parsed_url, CURLUPART_URL, url, CURLU_GUESS_SCHEME | CURLU_NON_SUPPORT_SCHEME) == CURLUE_OK &&
|
|
729
|
+
curl_url_get(parsed_url, CURLUPART_SCHEME, &scheme, 0) == CURLUE_OK && scheme) {
|
|
730
|
+
is_file = STRCASECMP(scheme, "file") == 0;
|
|
731
|
+
}
|
|
732
|
+
if (scheme) curl_free(scheme);
|
|
733
|
+
curl_url_cleanup(parsed_url);
|
|
734
|
+
#else
|
|
735
|
+
while (*url == ' ' || *url == '\t') url++;
|
|
736
|
+
is_file = STRNCASECMP(url, "file:", 5) == 0;
|
|
737
|
+
#endif
|
|
738
|
+
if (is_file) {
|
|
739
|
+
rb_raise(eCurlErrUnsafeDestination, "file:// URLs are not allowed by public network policy");
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
#endif
|
|
743
|
+
|
|
653
744
|
#ifdef CURB_HAVE_OPENSOCKET_NETWORK_POLICY
|
|
654
745
|
static curl_socket_t curb_public_network_opensocket(void *clientp, curlsocktype purpose, struct curl_sockaddr *address) {
|
|
655
746
|
ruby_curl_easy *rbce = (ruby_curl_easy *)clientp;
|
|
@@ -704,6 +795,14 @@ static curl_socket_t curb_public_network_opensocket(void *clientp, curlsocktype
|
|
|
704
795
|
}
|
|
705
796
|
#endif
|
|
706
797
|
|
|
798
|
+
/* Anything other than an IP socket (e.g. a Unix domain socket configured
|
|
799
|
+
* after the policy was applied) has no address to vet. Refuse it unless
|
|
800
|
+
* Unix sockets were explicitly allowed. */
|
|
801
|
+
if (!checked_destination_address && rbce && !rbce->allow_unix_socket) {
|
|
802
|
+
curb_store_destination_error(rbce, "(non-IP socket)", "is not allowed");
|
|
803
|
+
return CURL_SOCKET_BAD;
|
|
804
|
+
}
|
|
805
|
+
|
|
707
806
|
if (!checked_destination_address && rbce && rbce->network_allowed_cidr_rule_count > 0) {
|
|
708
807
|
curb_store_destination_error(rbce, "unknown", "outside allowed CIDR ranges");
|
|
709
808
|
return CURL_SOCKET_BAD;
|
|
@@ -879,6 +978,13 @@ static int ruby_curl_easy_body_limit_exceeded(ruby_curl_easy *rbce, size_t total
|
|
|
879
978
|
return 0;
|
|
880
979
|
}
|
|
881
980
|
|
|
981
|
+
/* Initial capacity for the default body/header buffers. rb_str_buf_cat grows
|
|
982
|
+
* them geometrically, so starting small costs a few reallocations at most,
|
|
983
|
+
* while a large fixed reservation stays attached to every returned body_str
|
|
984
|
+
* (a 2 KB response used to keep 32 KB alive). */
|
|
985
|
+
#define CURB_DEFAULT_BODY_BUFFER_CAPA 0
|
|
986
|
+
#define CURB_DEFAULT_HEADER_BUFFER_CAPA 0
|
|
987
|
+
|
|
882
988
|
/* Default body handler appends to easy.body_data buffer */
|
|
883
989
|
static size_t default_body_handler(char *stream,
|
|
884
990
|
size_t size,
|
|
@@ -893,7 +999,7 @@ static size_t default_body_handler(char *stream,
|
|
|
893
999
|
}
|
|
894
1000
|
|
|
895
1001
|
if (NIL_P(out)) {
|
|
896
|
-
out = rb_easy_set("body_data", rb_str_buf_new(
|
|
1002
|
+
out = rb_easy_set("body_data", rb_str_buf_new(CURB_DEFAULT_BODY_BUFFER_CAPA));
|
|
897
1003
|
}
|
|
898
1004
|
rb_str_buf_cat(out, stream, total);
|
|
899
1005
|
return total;
|
|
@@ -908,7 +1014,7 @@ static size_t default_header_handler(char *stream,
|
|
|
908
1014
|
size_t total = size * nmemb;
|
|
909
1015
|
VALUE out = rb_easy_get("header_data");
|
|
910
1016
|
if (NIL_P(out)) {
|
|
911
|
-
out = rb_easy_set("header_data", rb_str_buf_new(
|
|
1017
|
+
out = rb_easy_set("header_data", rb_str_buf_new(CURB_DEFAULT_HEADER_BUFFER_CAPA));
|
|
912
1018
|
}
|
|
913
1019
|
rb_str_buf_cat(out, stream, total);
|
|
914
1020
|
return total;
|
|
@@ -1202,6 +1308,9 @@ static void curl_easy_mark(void *ptr) {
|
|
|
1202
1308
|
if (!NIL_P(rbce->opts)) { rb_gc_mark(rbce->opts); }
|
|
1203
1309
|
if (!NIL_P(rbce->multi)) { rb_gc_mark(rbce->multi); }
|
|
1204
1310
|
if (!NIL_P(rbce->callback_error)) { rb_gc_mark(rbce->callback_error); }
|
|
1311
|
+
#ifndef HAVE_CURLOPT_COPYPOSTFIELDS
|
|
1312
|
+
rb_gc_mark(rbce->borrowed_post_body);
|
|
1313
|
+
#endif
|
|
1205
1314
|
}
|
|
1206
1315
|
}
|
|
1207
1316
|
|
|
@@ -1316,6 +1425,36 @@ static void ruby_curl_easy_detach(VALUE self, ruby_curl_easy *rbce) {
|
|
|
1316
1425
|
rbce->multi = Qnil;
|
|
1317
1426
|
}
|
|
1318
1427
|
|
|
1428
|
+
/* Release the curb-owned CURLOPT_STDERR stream. Callers must first stop
|
|
1429
|
+
* libcurl from using it (cleanup, reset, or setopt of a replacement). */
|
|
1430
|
+
static void ruby_curl_easy_close_stderr_file(ruby_curl_easy *rbce) {
|
|
1431
|
+
FILE *fp = rbce->stderr_file;
|
|
1432
|
+
if (fp) {
|
|
1433
|
+
rbce->stderr_file = NULL;
|
|
1434
|
+
fclose(fp);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
/* Open a stream curb owns on a duplicate of fd, so its lifetime is
|
|
1439
|
+
* independent of the Ruby IO it came from. */
|
|
1440
|
+
static FILE *ruby_curl_easy_dup_stderr_file(int fd) {
|
|
1441
|
+
FILE *fp;
|
|
1442
|
+
int dup_fd = rb_cloexec_dup(fd);
|
|
1443
|
+
if (dup_fd < 0) {
|
|
1444
|
+
rb_sys_fail("dup(CURLOPT_STDERR)");
|
|
1445
|
+
}
|
|
1446
|
+
rb_update_max_fd(dup_fd);
|
|
1447
|
+
/* "w" on an existing descriptor neither truncates nor changes O_APPEND. */
|
|
1448
|
+
fp = fdopen(dup_fd, "w");
|
|
1449
|
+
if (!fp) {
|
|
1450
|
+
int saved_errno = errno;
|
|
1451
|
+
close(dup_fd);
|
|
1452
|
+
errno = saved_errno;
|
|
1453
|
+
rb_sys_fail("fdopen(CURLOPT_STDERR)");
|
|
1454
|
+
}
|
|
1455
|
+
return fp;
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1319
1458
|
static void ruby_curl_easy_free(ruby_curl_easy *rbce) {
|
|
1320
1459
|
if (!rbce) {
|
|
1321
1460
|
return;
|
|
@@ -1362,6 +1501,8 @@ static void ruby_curl_easy_free(ruby_curl_easy *rbce) {
|
|
|
1362
1501
|
curl_easy_cleanup(rbce->curl);
|
|
1363
1502
|
rbce->curl = NULL;
|
|
1364
1503
|
}
|
|
1504
|
+
/* After cleanup: libcurl may log while closing connections. */
|
|
1505
|
+
ruby_curl_easy_close_stderr_file(rbce);
|
|
1365
1506
|
|
|
1366
1507
|
rbce->self = Qnil;
|
|
1367
1508
|
}
|
|
@@ -1371,7 +1512,7 @@ static void curl_easy_free(void *ptr) {
|
|
|
1371
1512
|
ruby_curl_easy *rbce = (ruby_curl_easy *)ptr;
|
|
1372
1513
|
if (rbce) {
|
|
1373
1514
|
ruby_curl_easy_free(rbce);
|
|
1374
|
-
|
|
1515
|
+
xfree(rbce);
|
|
1375
1516
|
}
|
|
1376
1517
|
}
|
|
1377
1518
|
|
|
@@ -1431,6 +1572,15 @@ ruby_curl_easy *ruby_curl_easy_get_handle(VALUE self) {
|
|
|
1431
1572
|
return rbce;
|
|
1432
1573
|
}
|
|
1433
1574
|
|
|
1575
|
+
/* Entry points that hand libcurl new request state (bodies, verbs, setup)
|
|
1576
|
+
* must not run while libcurl is still using the old state: replacing it
|
|
1577
|
+
* mid-transfer frees buffers libcurl is reading (use-after-free). */
|
|
1578
|
+
static ruby_curl_easy *ruby_curl_easy_get_idle_handle(VALUE self) {
|
|
1579
|
+
ruby_curl_easy *rbce = ruby_curl_easy_get_handle(self);
|
|
1580
|
+
ruby_curl_easy_check_idle(rbce);
|
|
1581
|
+
return rbce;
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1434
1584
|
/* Keep libcurl's typed getinfo arguments while checking every result. */
|
|
1435
1585
|
#define CURB_GETINFO(handle, info, output) do { \
|
|
1436
1586
|
CURLcode info_result = curl_easy_getinfo(handle, info, output); \
|
|
@@ -1443,6 +1593,9 @@ ruby_curl_easy *ruby_curl_easy_get_handle(VALUE self) {
|
|
|
1443
1593
|
|
|
1444
1594
|
static void ruby_curl_easy_zero(ruby_curl_easy *rbce) {
|
|
1445
1595
|
rbce->opts = rb_hash_new();
|
|
1596
|
+
#ifndef HAVE_CURLOPT_COPYPOSTFIELDS
|
|
1597
|
+
rbce->borrowed_post_body = Qnil;
|
|
1598
|
+
#endif
|
|
1446
1599
|
|
|
1447
1600
|
memset(rbce->err_buf, 0, CURL_ERROR_SIZE);
|
|
1448
1601
|
memset(rbce->unsafe_destination_error, 0, CURL_ERROR_SIZE);
|
|
@@ -1457,6 +1610,8 @@ static void ruby_curl_easy_zero(ruby_curl_easy *rbce) {
|
|
|
1457
1610
|
rbce->network_allowed_hosts = NULL;
|
|
1458
1611
|
rbce->network_allowed_cidr_rule_count = 0;
|
|
1459
1612
|
rbce->network_allowed_host_count = 0;
|
|
1613
|
+
/* Callers that may hold a stream close it before zeroing. */
|
|
1614
|
+
rbce->stderr_file = NULL;
|
|
1460
1615
|
|
|
1461
1616
|
/* various-typed opts */
|
|
1462
1617
|
rbce->local_port = 0;
|
|
@@ -1466,6 +1621,8 @@ static void ruby_curl_easy_zero(ruby_curl_easy *rbce) {
|
|
|
1466
1621
|
rbce->http_auth_types = 0;
|
|
1467
1622
|
rbce->proxy_auth_types = 0;
|
|
1468
1623
|
rbce->max_redirs = -1;
|
|
1624
|
+
rbce->max_redirs_set = 0;
|
|
1625
|
+
rbce->protocols_set = 0;
|
|
1469
1626
|
rbce->timeout = 0;
|
|
1470
1627
|
rbce->timeout_ms = 0;
|
|
1471
1628
|
rbce->connect_timeout = 0;
|
|
@@ -1515,15 +1672,21 @@ static void ruby_curl_easy_zero(ruby_curl_easy *rbce) {
|
|
|
1515
1672
|
*/
|
|
1516
1673
|
static VALUE ruby_curl_easy_allocate(VALUE klass) {
|
|
1517
1674
|
ruby_curl_easy *rbce;
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1675
|
+
/* Wrap a zeroed struct before ruby_curl_easy_zero allocates the opts hash:
|
|
1676
|
+
* the hash is only reachable by the GC once a Ruby object owns the struct.
|
|
1677
|
+
* Zeroed native pointers also keep the free function safe if that
|
|
1678
|
+
* allocation raises. */
|
|
1679
|
+
VALUE self = TypedData_Make_Struct(klass, ruby_curl_easy, &ruby_curl_easy_data_type, rbce);
|
|
1523
1680
|
rbce->opts = Qnil;
|
|
1524
1681
|
rbce->multi = Qnil;
|
|
1682
|
+
rbce->self = Qnil;
|
|
1683
|
+
rbce->callback_error = Qnil;
|
|
1684
|
+
#ifndef HAVE_CURLOPT_COPYPOSTFIELDS
|
|
1685
|
+
rbce->borrowed_post_body = Qnil;
|
|
1686
|
+
#endif
|
|
1525
1687
|
ruby_curl_easy_zero(rbce);
|
|
1526
|
-
|
|
1688
|
+
RB_GC_GUARD(self);
|
|
1689
|
+
return self;
|
|
1527
1690
|
}
|
|
1528
1691
|
|
|
1529
1692
|
/*
|
|
@@ -1632,19 +1795,25 @@ static VALUE duplicate_upload(VALUE upload) {
|
|
|
1632
1795
|
static VALUE ruby_curl_easy_clone(VALUE self) {
|
|
1633
1796
|
ruby_curl_easy *rbce, *newrbce;
|
|
1634
1797
|
|
|
1798
|
+
FILE *clone_stderr_file = NULL;
|
|
1799
|
+
VALUE clone_opts = Qnil;
|
|
1800
|
+
|
|
1635
1801
|
rbce = ruby_curl_easy_get_handle(self);
|
|
1636
1802
|
|
|
1637
|
-
|
|
1638
|
-
if (
|
|
1639
|
-
|
|
1803
|
+
/* Duplicate first: this can raise, and nothing needs unwinding yet. */
|
|
1804
|
+
if (rbce->stderr_file) {
|
|
1805
|
+
clone_stderr_file = ruby_curl_easy_dup_stderr_file(fileno(rbce->stderr_file));
|
|
1640
1806
|
}
|
|
1807
|
+
|
|
1808
|
+
newrbce = ALLOC(ruby_curl_easy);
|
|
1641
1809
|
/* shallow copy */
|
|
1642
1810
|
memcpy(newrbce, rbce, sizeof(ruby_curl_easy));
|
|
1643
1811
|
|
|
1644
1812
|
/* now deep copy */
|
|
1645
1813
|
newrbce->curl = curl_easy_duphandle(rbce->curl);
|
|
1646
1814
|
if (!newrbce->curl) {
|
|
1647
|
-
|
|
1815
|
+
xfree(newrbce);
|
|
1816
|
+
if (clone_stderr_file) fclose(clone_stderr_file);
|
|
1648
1817
|
rb_raise(rb_eNoMemError, "Failed to duplicate Curl::Easy handle");
|
|
1649
1818
|
}
|
|
1650
1819
|
/* duphandle borrows HTTPPOST, whose form is freed when the original
|
|
@@ -1654,7 +1823,8 @@ static VALUE ruby_curl_easy_clone(VALUE self) {
|
|
|
1654
1823
|
CURLcode ecode = curl_easy_setopt(newrbce->curl, CURLOPT_HTTPPOST, NULL);
|
|
1655
1824
|
if (ecode != CURLE_OK) {
|
|
1656
1825
|
curl_easy_cleanup(newrbce->curl);
|
|
1657
|
-
|
|
1826
|
+
xfree(newrbce);
|
|
1827
|
+
if (clone_stderr_file) fclose(clone_stderr_file);
|
|
1658
1828
|
raise_curl_easy_error_exception(ecode);
|
|
1659
1829
|
}
|
|
1660
1830
|
newrbce->multipart_form_active = 0;
|
|
@@ -1669,6 +1839,13 @@ static VALUE ruby_curl_easy_clone(VALUE self) {
|
|
|
1669
1839
|
newrbce->network_allowed_hosts = NULL;
|
|
1670
1840
|
newrbce->network_allowed_host_count = 0;
|
|
1671
1841
|
|
|
1842
|
+
/* duphandle copied the original's CURLOPT_STDERR pointer; give the clone
|
|
1843
|
+
* its own stream so neither handle can outlive the other's. */
|
|
1844
|
+
newrbce->stderr_file = clone_stderr_file;
|
|
1845
|
+
if (clone_stderr_file) {
|
|
1846
|
+
curl_easy_setopt(newrbce->curl, CURLOPT_STDERR, clone_stderr_file);
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1672
1849
|
/* A cloned easy should not retain ownership reference to the original multi. */
|
|
1673
1850
|
newrbce->multi = Qnil;
|
|
1674
1851
|
newrbce->callback_error = Qnil;
|
|
@@ -1678,7 +1855,9 @@ static VALUE ruby_curl_easy_clone(VALUE self) {
|
|
|
1678
1855
|
newrbce->native_active = 0;
|
|
1679
1856
|
|
|
1680
1857
|
if (rbce->opts != Qnil) {
|
|
1681
|
-
newrbce
|
|
1858
|
+
/* Not reachable through newrbce until it is wrapped below. */
|
|
1859
|
+
clone_opts = rb_funcall(rbce->opts, rb_intern("dup"), 0);
|
|
1860
|
+
newrbce->opts = clone_opts;
|
|
1682
1861
|
}
|
|
1683
1862
|
|
|
1684
1863
|
/* Set the error buffer on the new curl handle using the new err_buf */
|
|
@@ -1700,6 +1879,7 @@ static VALUE ruby_curl_easy_clone(VALUE self) {
|
|
|
1700
1879
|
}
|
|
1701
1880
|
|
|
1702
1881
|
VALUE clone = TypedData_Wrap_Struct(cCurlEasy, &ruby_curl_easy_data_type, newrbce);
|
|
1882
|
+
RB_GC_GUARD(clone_opts);
|
|
1703
1883
|
newrbce->self = clone;
|
|
1704
1884
|
curl_easy_setopt(newrbce->curl, CURLOPT_PRIVATE, (void*)newrbce);
|
|
1705
1885
|
|
|
@@ -1789,6 +1969,8 @@ static VALUE ruby_curl_easy_reset(VALUE self) {
|
|
|
1789
1969
|
|
|
1790
1970
|
ruby_curl_easy_cleanup(self, rbce);
|
|
1791
1971
|
curl_easy_reset(rbce->curl);
|
|
1972
|
+
/* reset restored libcurl's default stderr, so ours is unused now. */
|
|
1973
|
+
ruby_curl_easy_close_stderr_file(rbce);
|
|
1792
1974
|
ruby_curl_easy_zero(rbce);
|
|
1793
1975
|
rbce->self = self;
|
|
1794
1976
|
|
|
@@ -2138,6 +2320,14 @@ struct post_body_set_args {
|
|
|
2138
2320
|
int force_http_get_on_nil;
|
|
2139
2321
|
};
|
|
2140
2322
|
|
|
2323
|
+
static void clear_postfields(ruby_curl_easy *rbce) {
|
|
2324
|
+
curl_easy_setopt(rbce->curl, CURLOPT_POSTFIELDS, NULL);
|
|
2325
|
+
curl_easy_setopt(rbce->curl, CURLOPT_POSTFIELDSIZE, 0L);
|
|
2326
|
+
#ifndef HAVE_CURLOPT_COPYPOSTFIELDS
|
|
2327
|
+
rbce->borrowed_post_body = Qnil;
|
|
2328
|
+
#endif
|
|
2329
|
+
}
|
|
2330
|
+
|
|
2141
2331
|
static VALUE ruby_curl_easy_post_body_set_with_mode_body(VALUE argp) {
|
|
2142
2332
|
struct post_body_set_args *args = (struct post_body_set_args *)argp;
|
|
2143
2333
|
VALUE self = args->self;
|
|
@@ -2148,17 +2338,16 @@ static VALUE ruby_curl_easy_post_body_set_with_mode_body(VALUE argp) {
|
|
|
2148
2338
|
|
|
2149
2339
|
VALUE body_str;
|
|
2150
2340
|
VALUE retained_body_str;
|
|
2151
|
-
|
|
2152
|
-
|
|
2341
|
+
VALUE transfer_body;
|
|
2342
|
+
CURLcode result;
|
|
2153
2343
|
|
|
2154
|
-
rbce =
|
|
2344
|
+
rbce = ruby_curl_easy_get_idle_handle(self);
|
|
2155
2345
|
|
|
2156
2346
|
curl = rbce->curl;
|
|
2157
2347
|
|
|
2158
2348
|
if ( post_body == Qnil ) {
|
|
2349
|
+
clear_postfields(rbce);
|
|
2159
2350
|
rb_easy_del("postdata_buffer");
|
|
2160
|
-
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
|
|
2161
|
-
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);
|
|
2162
2351
|
if (force_http_get_on_nil) {
|
|
2163
2352
|
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
|
|
2164
2353
|
}
|
|
@@ -2176,30 +2365,31 @@ static VALUE ruby_curl_easy_post_body_set_with_mode_body(VALUE argp) {
|
|
|
2176
2365
|
|
|
2177
2366
|
StringValue(body_str);
|
|
2178
2367
|
|
|
2179
|
-
// Store the string, since it has to hang around for the duration of the
|
|
2180
|
-
// request. See CURLOPT_POSTFIELDS in the libcurl docs.
|
|
2181
|
-
#ifdef HAVE_CURLOPT_COPYPOSTFIELDS
|
|
2182
|
-
/*
|
|
2183
|
-
* libcurl copies the bytes immediately for COPYPOSTFIELDS, so retain a
|
|
2184
|
-
* matching Ruby snapshot for post_body instead of the caller's mutable
|
|
2185
|
-
* source string.
|
|
2186
|
-
*/
|
|
2187
2368
|
retained_body_str = rb_str_dup(body_str);
|
|
2369
|
+
#ifdef HAVE_CURLOPT_COPYPOSTFIELDS
|
|
2370
|
+
transfer_body = retained_body_str;
|
|
2188
2371
|
#else
|
|
2189
|
-
|
|
2372
|
+
/* libcurl borrows this pointer. Neither the source nor the mutable
|
|
2373
|
+
* post_body getter can own the bytes. Pin the private copy in mark. */
|
|
2374
|
+
transfer_body = rb_str_tmp_new(RSTRING_LEN(retained_body_str));
|
|
2375
|
+
memcpy(RSTRING_PTR(transfer_body), RSTRING_PTR(retained_body_str), RSTRING_LEN(transfer_body));
|
|
2190
2376
|
#endif
|
|
2191
|
-
data = StringValuePtr(retained_body_str);
|
|
2192
|
-
len = RSTRING_LEN(retained_body_str);
|
|
2193
2377
|
rb_easy_set("postdata_buffer", retained_body_str);
|
|
2194
2378
|
|
|
2195
|
-
curl_easy_setopt(curl,
|
|
2196
|
-
|
|
2379
|
+
result = curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, RSTRING_LEN(transfer_body));
|
|
2380
|
+
if (result == CURLE_OK) {
|
|
2197
2381
|
#ifdef HAVE_CURLOPT_COPYPOSTFIELDS
|
|
2198
|
-
|
|
2382
|
+
result = curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, RSTRING_PTR(transfer_body));
|
|
2199
2383
|
#else
|
|
2200
|
-
|
|
2384
|
+
result = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, RSTRING_PTR(transfer_body));
|
|
2385
|
+
rbce->borrowed_post_body = transfer_body;
|
|
2201
2386
|
#endif
|
|
2202
|
-
|
|
2387
|
+
}
|
|
2388
|
+
if (result != CURLE_OK) {
|
|
2389
|
+
clear_postfields(rbce);
|
|
2390
|
+
rb_easy_del("postdata_buffer");
|
|
2391
|
+
raise_curl_easy_error_exception(result);
|
|
2392
|
+
}
|
|
2203
2393
|
return post_body;
|
|
2204
2394
|
}
|
|
2205
2395
|
|
|
@@ -2247,7 +2437,7 @@ static VALUE ruby_curl_easy_put_data_set(VALUE self, VALUE data) {
|
|
|
2247
2437
|
VALUE headers;
|
|
2248
2438
|
VALUE infile_size = Qnil;
|
|
2249
2439
|
|
|
2250
|
-
rbce =
|
|
2440
|
+
rbce = ruby_curl_easy_get_idle_handle(self);
|
|
2251
2441
|
|
|
2252
2442
|
/*
|
|
2253
2443
|
* Validate and prepare Ruby-visible state before mutating the CURL handle.
|
|
@@ -2317,8 +2507,7 @@ static VALUE ruby_curl_easy_put_data_set(VALUE self, VALUE data) {
|
|
|
2317
2507
|
|
|
2318
2508
|
curl_easy_setopt(curl, CURLOPT_NOBODY, 0);
|
|
2319
2509
|
curl_easy_setopt(curl, CURLOPT_POST, 0);
|
|
2320
|
-
|
|
2321
|
-
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 0);
|
|
2510
|
+
clear_postfields(rbce);
|
|
2322
2511
|
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
|
|
2323
2512
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, (curl_read_callback)read_data_handler);
|
|
2324
2513
|
#ifdef HAVE_CURLOPT_SEEKFUNCTION
|
|
@@ -2744,14 +2933,27 @@ static VALUE ruby_curl_easy_proxy_auth_types_get(VALUE self) {
|
|
|
2744
2933
|
* easy.max_redirects = fixnum or nil => fixnum or nil
|
|
2745
2934
|
*
|
|
2746
2935
|
* Set the maximum number of redirections to follow in the following +perform+
|
|
2747
|
-
* calls. Set to
|
|
2748
|
-
*
|
|
2936
|
+
* calls. Set to -1 to allow an infinite number. When nil (the default), at
|
|
2937
|
+
* most 30 redirects are followed, matching libcurl 8.3+ on every libcurl
|
|
2938
|
+
* version. Setting this option only makes sense if +follow_location+ is also
|
|
2939
|
+
* set true.
|
|
2749
2940
|
*
|
|
2750
2941
|
* With libcurl >= 7.15.1, setting this to 0 will cause libcurl to refuse any
|
|
2751
2942
|
* redirect.
|
|
2752
2943
|
*/
|
|
2753
2944
|
static VALUE ruby_curl_easy_max_redirects_set(VALUE self, VALUE max_redirs) {
|
|
2754
|
-
|
|
2945
|
+
ruby_curl_easy *rbce;
|
|
2946
|
+
|
|
2947
|
+
TypedData_Get_Struct(self, ruby_curl_easy, &ruby_curl_easy_data_type, rbce);
|
|
2948
|
+
ruby_curl_easy_check_access(rbce);
|
|
2949
|
+
if (NIL_P(max_redirs)) {
|
|
2950
|
+
rbce->max_redirs = -1;
|
|
2951
|
+
rbce->max_redirs_set = 0;
|
|
2952
|
+
} else {
|
|
2953
|
+
rbce->max_redirs = NUM2LONG(max_redirs);
|
|
2954
|
+
rbce->max_redirs_set = 1;
|
|
2955
|
+
}
|
|
2956
|
+
return max_redirs;
|
|
2755
2957
|
}
|
|
2756
2958
|
|
|
2757
2959
|
/*
|
|
@@ -3318,7 +3520,7 @@ static VALUE ruby_curl_easy_use_netrc_q(VALUE self) {
|
|
|
3318
3520
|
*/
|
|
3319
3521
|
static VALUE ruby_curl_easy_autoreferer_set(VALUE self, VALUE autoreferer) {
|
|
3320
3522
|
ruby_curl_easy *rbce;
|
|
3321
|
-
rbce =
|
|
3523
|
+
rbce = ruby_curl_easy_get_idle_handle(self);
|
|
3322
3524
|
|
|
3323
3525
|
if (Qtrue == autoreferer) {
|
|
3324
3526
|
curl_easy_setopt(rbce->curl, CURLOPT_AUTOREFERER, 1);
|
|
@@ -3819,6 +4021,37 @@ static VALUE ruby_curl_easy_on_debug_set(int argc, VALUE *argv, VALUE self) {
|
|
|
3819
4021
|
|
|
3820
4022
|
/* =================== PERFORM =====================*/
|
|
3821
4023
|
|
|
4024
|
+
/* CR or LF in a header line would let the value inject extra headers or
|
|
4025
|
+
* split the request; libcurl sends header strings verbatim. NUL would
|
|
4026
|
+
* silently truncate the line. */
|
|
4027
|
+
static void curb_check_header_line_len(const char *ptr, long len, const char *what) {
|
|
4028
|
+
if (memchr(ptr, '\r', (size_t)len) || memchr(ptr, '\n', (size_t)len) || memchr(ptr, '\0', (size_t)len)) {
|
|
4029
|
+
rb_raise(rb_eArgError, "%s must not contain CR, LF, or NUL characters", what);
|
|
4030
|
+
}
|
|
4031
|
+
}
|
|
4032
|
+
|
|
4033
|
+
static void curb_check_header_line(VALUE str, const char *what) {
|
|
4034
|
+
curb_check_header_line_len(RSTRING_PTR(str), RSTRING_LEN(str), what);
|
|
4035
|
+
}
|
|
4036
|
+
|
|
4037
|
+
/* A String for the option stored under key, converting like rb_easy_get_str. */
|
|
4038
|
+
static VALUE curb_easy_opt_string(ruby_curl_easy *rbce, const char *key) {
|
|
4039
|
+
VALUE value = rb_hash_aref(rbce->opts, rb_easy_hkey(key));
|
|
4040
|
+
return RB_TYPE_P(value, T_STRING) ? value : rb_str_to_str(value);
|
|
4041
|
+
}
|
|
4042
|
+
|
|
4043
|
+
static int curb_slist_has_header(const struct curl_slist *list, const char *name) {
|
|
4044
|
+
size_t name_len = strlen(name);
|
|
4045
|
+
for (; list; list = list->next) {
|
|
4046
|
+
const char *line = list->data;
|
|
4047
|
+
if (line && STRNCASECMP(line, name, name_len) == 0 &&
|
|
4048
|
+
(line[name_len] == ':' || line[name_len] == ';')) {
|
|
4049
|
+
return 1;
|
|
4050
|
+
}
|
|
4051
|
+
}
|
|
4052
|
+
return 0;
|
|
4053
|
+
}
|
|
4054
|
+
|
|
3822
4055
|
/***********************************************
|
|
3823
4056
|
* This is an rb_iterate callback used to set up http headers.
|
|
3824
4057
|
*/
|
|
@@ -3850,6 +4083,8 @@ static VALUE cb_each_http_header(VALUE header, VALUE wrap, int _c, const VALUE *
|
|
|
3850
4083
|
|
|
3851
4084
|
//rb_p(header_str);
|
|
3852
4085
|
|
|
4086
|
+
StringValue(header_str);
|
|
4087
|
+
curb_check_header_line(header_str, "HTTP header");
|
|
3853
4088
|
struct curl_slist *new_list = curl_slist_append(*list, StringValuePtr(header_str));
|
|
3854
4089
|
if (!new_list) {
|
|
3855
4090
|
rb_raise(rb_eNoMemError, "Failed to append to header list");
|
|
@@ -3886,6 +4121,8 @@ static VALUE cb_each_http_proxy_header(VALUE proxy_header, VALUE wrap, int _c, c
|
|
|
3886
4121
|
|
|
3887
4122
|
//rb_p(header_str);
|
|
3888
4123
|
|
|
4124
|
+
StringValue(proxy_header_str);
|
|
4125
|
+
curb_check_header_line(proxy_header_str, "HTTP proxy header");
|
|
3889
4126
|
struct curl_slist *new_list = curl_slist_append(*list, StringValuePtr(proxy_header_str));
|
|
3890
4127
|
if (!new_list) {
|
|
3891
4128
|
rb_raise(rb_eNoMemError, "Failed to append to proxy header list");
|
|
@@ -4031,6 +4268,17 @@ static VALUE ruby_curl_easy_setup_body(VALUE arg) {
|
|
|
4031
4268
|
curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, rbce->forbid_reuse_set ? rbce->forbid_reuse : 0L);
|
|
4032
4269
|
#endif
|
|
4033
4270
|
}
|
|
4271
|
+
/* With a protocol allowlist, libcurl enforces it (UnsupportedProtocolError)
|
|
4272
|
+
* and an explicit :file entry is a deliberate opt-in. */
|
|
4273
|
+
if (rbce->network_policy == CURB_NETWORK_POLICY_PUBLIC && !rbce->protocols_set) {
|
|
4274
|
+
curb_check_public_url_scheme(StringValueCStr(url));
|
|
4275
|
+
}
|
|
4276
|
+
#ifdef HAVE_CURLOPT_DOH_URL
|
|
4277
|
+
/* After the CIDR rules above are prepared. */
|
|
4278
|
+
if (rbce->network_policy == CURB_NETWORK_POLICY_PUBLIC && !rb_easy_nil("doh_url")) {
|
|
4279
|
+
curb_check_public_doh_url(rbce, rb_easy_get_str("doh_url"));
|
|
4280
|
+
}
|
|
4281
|
+
#endif
|
|
4034
4282
|
#endif
|
|
4035
4283
|
|
|
4036
4284
|
// network stuff and auth
|
|
@@ -4097,7 +4345,7 @@ static VALUE ruby_curl_easy_setup_body(VALUE arg) {
|
|
|
4097
4345
|
/* clear out the body_data if it was set */
|
|
4098
4346
|
rb_easy_del("body_data");
|
|
4099
4347
|
} else {
|
|
4100
|
-
rb_easy_set("body_data", rb_str_buf_new(
|
|
4348
|
+
rb_easy_set("body_data", rb_str_buf_new(CURB_DEFAULT_BODY_BUFFER_CAPA));
|
|
4101
4349
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (curl_write_callback)&default_body_handler);
|
|
4102
4350
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, rbce);
|
|
4103
4351
|
}
|
|
@@ -4108,7 +4356,7 @@ static VALUE ruby_curl_easy_setup_body(VALUE arg) {
|
|
|
4108
4356
|
/* clear out the header_data if it was set */
|
|
4109
4357
|
rb_easy_del("header_data");
|
|
4110
4358
|
} else {
|
|
4111
|
-
rb_easy_set("header_data", rb_str_buf_new(
|
|
4359
|
+
rb_easy_set("header_data", rb_str_buf_new(CURB_DEFAULT_HEADER_BUFFER_CAPA));
|
|
4112
4360
|
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, (curl_write_callback)&default_header_handler);
|
|
4113
4361
|
curl_easy_setopt(curl, CURLOPT_HEADERDATA, rbce);
|
|
4114
4362
|
}
|
|
@@ -4152,14 +4400,20 @@ static VALUE ruby_curl_easy_setup_body(VALUE arg) {
|
|
|
4152
4400
|
|
|
4153
4401
|
curl_easy_setopt(curl, CURLOPT_HEADER, rbce->header_in_body);
|
|
4154
4402
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, rbce->follow_location);
|
|
4403
|
+
/* Unset: cap at 30, libcurl's default since 8.3.0. Passing curb's old
|
|
4404
|
+
* default of -1 made redirects unlimited on every libcurl version. */
|
|
4405
|
+
if (!rbce->max_redirs_set) {
|
|
4406
|
+
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 30L);
|
|
4407
|
+
} else {
|
|
4155
4408
|
#if LIBCURL_VERSION_NUM == 0x081000
|
|
4156
|
-
|
|
4157
|
-
|
|
4158
|
-
|
|
4159
|
-
|
|
4409
|
+
/* Work around 8.16.0 regression that clamps -1 (infinite) to zero */
|
|
4410
|
+
if (rbce->max_redirs >= 0) {
|
|
4411
|
+
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, rbce->max_redirs);
|
|
4412
|
+
}
|
|
4160
4413
|
#else
|
|
4161
|
-
|
|
4414
|
+
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, rbce->max_redirs);
|
|
4162
4415
|
#endif
|
|
4416
|
+
}
|
|
4163
4417
|
|
|
4164
4418
|
curl_easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL,
|
|
4165
4419
|
public_policy_disables_proxy ? 0L : rbce->proxy_tunnel);
|
|
@@ -4275,9 +4529,7 @@ static VALUE ruby_curl_easy_setup_body(VALUE arg) {
|
|
|
4275
4529
|
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
|
|
4276
4530
|
}
|
|
4277
4531
|
|
|
4278
|
-
|
|
4279
|
-
curl_easy_setopt(curl, CURLOPT_COOKIE, rb_easy_get_str("cookies"));
|
|
4280
|
-
}
|
|
4532
|
+
/* cookies= is applied after the HTTP headers below. */
|
|
4281
4533
|
|
|
4282
4534
|
/* Set up HTTPS cert handling if necessary */
|
|
4283
4535
|
if (!rb_easy_nil("cert")) {
|
|
@@ -4322,7 +4574,9 @@ static VALUE ruby_curl_easy_setup_body(VALUE arg) {
|
|
|
4322
4574
|
|
|
4323
4575
|
/* Set the user-agent string if specified */
|
|
4324
4576
|
if (!rb_easy_nil("useragent")) {
|
|
4325
|
-
|
|
4577
|
+
VALUE useragent = curb_easy_opt_string(rbce, "useragent");
|
|
4578
|
+
curb_check_header_line(useragent, "useragent");
|
|
4579
|
+
curl_easy_setopt(curl, CURLOPT_USERAGENT, RSTRING_PTR(useragent));
|
|
4326
4580
|
}
|
|
4327
4581
|
|
|
4328
4582
|
/* Setup can be rerun for safety-policy changes while a handle is attached.
|
|
@@ -4339,6 +4593,7 @@ static VALUE ruby_curl_easy_setup_body(VALUE arg) {
|
|
|
4339
4593
|
rb_block_call(rb_easy_get("headers"), rb_intern("each"), 0, NULL, cb_each_http_header, wrap);
|
|
4340
4594
|
} else {
|
|
4341
4595
|
VALUE headers_str = rb_obj_as_string(rb_easy_get("headers"));
|
|
4596
|
+
curb_check_header_line(headers_str, "HTTP header");
|
|
4342
4597
|
struct curl_slist *new_list = curl_slist_append(*hdrs, StringValuePtr(headers_str));
|
|
4343
4598
|
if (!new_list) {
|
|
4344
4599
|
rb_raise(rb_eNoMemError, "Failed to append to headers list");
|
|
@@ -4351,6 +4606,28 @@ static VALUE ruby_curl_easy_setup_body(VALUE arg) {
|
|
|
4351
4606
|
}
|
|
4352
4607
|
}
|
|
4353
4608
|
|
|
4609
|
+
/* cookies=: libcurl sends CURLOPT_COOKIE to every host a redirect reaches,
|
|
4610
|
+
* but drops a custom Cookie header on cross-host redirects (7.83+). So
|
|
4611
|
+
* send it as a header, unless the cookie engine is on: libcurl would then
|
|
4612
|
+
* emit a second Cookie line, so CURLOPT_COOKIE is kept to merge them. */
|
|
4613
|
+
curl_easy_setopt(curl, CURLOPT_COOKIE, NULL);
|
|
4614
|
+
if (!rb_easy_nil("cookies")) {
|
|
4615
|
+
VALUE cookies = curb_easy_opt_string(rbce, "cookies");
|
|
4616
|
+
curb_check_header_line(cookies, "cookies");
|
|
4617
|
+
if (rbce->enable_cookies || rbce->cookielist_engine_enabled) {
|
|
4618
|
+
curl_easy_setopt(curl, CURLOPT_COOKIE, RSTRING_PTR(cookies));
|
|
4619
|
+
} else if (RSTRING_LEN(cookies) > 0 && !curb_slist_has_header(*hdrs, "Cookie")) {
|
|
4620
|
+
/* An explicit Cookie header already wins over cookies=, as before. */
|
|
4621
|
+
VALUE cookie_header = rb_str_plus(rb_str_new_cstr("Cookie: "), cookies);
|
|
4622
|
+
struct curl_slist *new_list = curl_slist_append(*hdrs, StringValueCStr(cookie_header));
|
|
4623
|
+
if (!new_list) {
|
|
4624
|
+
rb_raise(rb_eNoMemError, "Failed to append to headers list");
|
|
4625
|
+
}
|
|
4626
|
+
*hdrs = new_list;
|
|
4627
|
+
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, *hdrs);
|
|
4628
|
+
}
|
|
4629
|
+
}
|
|
4630
|
+
|
|
4354
4631
|
#ifdef HAVE_CURLOPT_PROXYHEADER
|
|
4355
4632
|
/* Setup HTTP proxy headers if necessary */
|
|
4356
4633
|
curl_easy_setopt(curl, CURLOPT_PROXYHEADER, NULL); // XXX: maybe we shouldn't be clearing this?
|
|
@@ -4361,6 +4638,7 @@ static VALUE ruby_curl_easy_setup_body(VALUE arg) {
|
|
|
4361
4638
|
rb_block_call(rb_easy_get("proxy_headers"), rb_intern("each"), 0, NULL, cb_each_http_proxy_header, wrap);
|
|
4362
4639
|
} else {
|
|
4363
4640
|
VALUE proxy_headers_str = rb_obj_as_string(rb_easy_get("proxy_headers"));
|
|
4641
|
+
curb_check_header_line(proxy_headers_str, "HTTP proxy header");
|
|
4364
4642
|
struct curl_slist *new_list = curl_slist_append(*phdrs, StringValuePtr(proxy_headers_str));
|
|
4365
4643
|
if (!new_list) {
|
|
4366
4644
|
rb_raise(rb_eNoMemError, "Failed to append to proxy headers list");
|
|
@@ -4440,7 +4718,7 @@ VALUE ruby_curl_easy_setup(ruby_curl_easy *rbce) {
|
|
|
4440
4718
|
static VALUE ruby_curl_easy_setup_self(VALUE self) {
|
|
4441
4719
|
ruby_curl_easy *rbce;
|
|
4442
4720
|
|
|
4443
|
-
rbce =
|
|
4721
|
+
rbce = ruby_curl_easy_get_idle_handle(self);
|
|
4444
4722
|
ruby_curl_easy_setup(rbce);
|
|
4445
4723
|
|
|
4446
4724
|
return self;
|
|
@@ -4526,8 +4804,7 @@ static VALUE perform_with_request_restore_ensure(VALUE argp) {
|
|
|
4526
4804
|
}
|
|
4527
4805
|
if (args->clear_postfields) {
|
|
4528
4806
|
curl_easy_setopt(args->curl, CURLOPT_POST, 0L);
|
|
4529
|
-
|
|
4530
|
-
curl_easy_setopt(args->curl, CURLOPT_POSTFIELDSIZE, 0L);
|
|
4807
|
+
clear_postfields(args->rbce);
|
|
4531
4808
|
curl_easy_setopt(args->curl, CURLOPT_HTTPGET, 1L);
|
|
4532
4809
|
if (args->rbce && !NIL_P(args->rbce->opts)) {
|
|
4533
4810
|
rb_hash_delete(args->rbce->opts, rb_easy_hkey("postdata_buffer"));
|
|
@@ -4546,11 +4823,13 @@ static VALUE ruby_curl_easy_perform_verb_str(VALUE self, const char *verb) {
|
|
|
4546
4823
|
CURL *curl;
|
|
4547
4824
|
VALUE retval;
|
|
4548
4825
|
|
|
4549
|
-
rbce =
|
|
4826
|
+
rbce = ruby_curl_easy_get_idle_handle(self);
|
|
4550
4827
|
curl = rbce->curl;
|
|
4551
4828
|
|
|
4552
4829
|
memset(rbce->err_buf, 0, CURL_ERROR_SIZE);
|
|
4553
4830
|
|
|
4831
|
+
if (verb) curb_check_header_line_len(verb, (long)strlen(verb), "HTTP verb");
|
|
4832
|
+
|
|
4554
4833
|
/* Use method override and adjust related options for special verbs. */
|
|
4555
4834
|
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, verb);
|
|
4556
4835
|
|
|
@@ -4734,7 +5013,7 @@ static VALUE ruby_curl_easy_perform_post(int argc, VALUE *argv, VALUE self) {
|
|
|
4734
5013
|
|
|
4735
5014
|
rb_scan_args(argc, argv, "*", &args_ary);
|
|
4736
5015
|
|
|
4737
|
-
rbce =
|
|
5016
|
+
rbce = ruby_curl_easy_get_idle_handle(self);
|
|
4738
5017
|
curl = rbce->curl;
|
|
4739
5018
|
|
|
4740
5019
|
memset(rbce->err_buf, 0, CURL_ERROR_SIZE);
|
|
@@ -4793,7 +5072,7 @@ static VALUE ruby_curl_easy_perform_patch(int argc, VALUE *argv, VALUE self) {
|
|
|
4793
5072
|
VALUE args_ary;
|
|
4794
5073
|
|
|
4795
5074
|
rb_scan_args(argc, argv, "*", &args_ary);
|
|
4796
|
-
rbce =
|
|
5075
|
+
rbce = ruby_curl_easy_get_idle_handle(self);
|
|
4797
5076
|
curl = rbce->curl;
|
|
4798
5077
|
|
|
4799
5078
|
/* Clear the error buffer */
|
|
@@ -4842,7 +5121,7 @@ static VALUE ruby_curl_easy_perform_put(int argc, VALUE *argv, VALUE self) {
|
|
|
4842
5121
|
VALUE args_ary;
|
|
4843
5122
|
|
|
4844
5123
|
rb_scan_args(argc, argv, "*", &args_ary);
|
|
4845
|
-
rbce =
|
|
5124
|
+
rbce = ruby_curl_easy_get_idle_handle(self);
|
|
4846
5125
|
curl = rbce->curl;
|
|
4847
5126
|
|
|
4848
5127
|
memset(rbce->err_buf, 0, CURL_ERROR_SIZE);
|
|
@@ -5747,6 +6026,7 @@ static VALUE ruby_curl_easy_set_opt(VALUE self, VALUE opt, VALUE val) {
|
|
|
5747
6026
|
CURB_OBJECT_HSETTER(ruby_curl_easy, url);
|
|
5748
6027
|
} break;
|
|
5749
6028
|
case CURLOPT_CUSTOMREQUEST:
|
|
6029
|
+
if (!NIL_P(val)) curb_check_header_line(StringValue(val), "custom request method");
|
|
5750
6030
|
curl_easy_setopt(rbce->curl, CURLOPT_CUSTOMREQUEST, NIL_P(val) ? NULL : StringValueCStr(val));
|
|
5751
6031
|
break;
|
|
5752
6032
|
case CURLOPT_HTTP_VERSION: {
|
|
@@ -5822,6 +6102,7 @@ static VALUE ruby_curl_easy_set_opt(VALUE self, VALUE opt, VALUE val) {
|
|
|
5822
6102
|
#ifdef HAVE_CURLOPT_REQUEST_TARGET
|
|
5823
6103
|
case CURLOPT_REQUEST_TARGET: {
|
|
5824
6104
|
/* Forward request-target directly to libcurl as a string. */
|
|
6105
|
+
if (!NIL_P(val)) curb_check_header_line(StringValue(val), "request target");
|
|
5825
6106
|
curl_easy_setopt(rbce->curl, CURLOPT_REQUEST_TARGET, NIL_P(val) ? NULL : StringValueCStr(val));
|
|
5826
6107
|
} break;
|
|
5827
6108
|
#endif
|
|
@@ -5978,23 +6259,36 @@ static VALUE ruby_curl_easy_set_opt(VALUE self, VALUE opt, VALUE val) {
|
|
|
5978
6259
|
curl_easy_setopt(rbce->curl, CURLOPT_HAPROXYPROTOCOL, NUM2LONG(val));
|
|
5979
6260
|
break;
|
|
5980
6261
|
#endif
|
|
5981
|
-
case CURLOPT_STDERR:
|
|
6262
|
+
case CURLOPT_STDERR: {
|
|
5982
6263
|
// libcurl requires raw FILE pointer and this should be IO object in Ruby.
|
|
5983
6264
|
// Tempfile or StringIO won't work.
|
|
6265
|
+
FILE *stderr_file;
|
|
5984
6266
|
Check_Type(val, T_FILE);
|
|
5985
6267
|
GetOpenFile(val, open_f_ptr);
|
|
5986
|
-
|
|
5987
|
-
/*
|
|
5988
|
-
*
|
|
6268
|
+
rb_io_check_writable(open_f_ptr);
|
|
6269
|
+
/* Hand libcurl a stream curb owns on a duplicate descriptor. Passing
|
|
6270
|
+
* the IO's own FILE* left libcurl writing through freed memory once the
|
|
6271
|
+
* caller closed the IO, including after Multi#add. */
|
|
6272
|
+
#ifdef HAVE_RB_IO_DESCRIPTOR
|
|
6273
|
+
stderr_file = ruby_curl_easy_dup_stderr_file(rb_io_descriptor(val));
|
|
6274
|
+
#else
|
|
6275
|
+
stderr_file = ruby_curl_easy_dup_stderr_file(open_f_ptr->fd);
|
|
6276
|
+
#endif
|
|
6277
|
+
curl_easy_setopt(rbce->curl, CURLOPT_STDERR, stderr_file);
|
|
6278
|
+
ruby_curl_easy_close_stderr_file(rbce);
|
|
6279
|
+
rbce->stderr_file = stderr_file;
|
|
6280
|
+
/* Kept so completion can flush the caller's own buffered output. */
|
|
5989
6281
|
rb_easy_set("stderr_io", val);
|
|
5990
|
-
break;
|
|
6282
|
+
} break;
|
|
5991
6283
|
case CURLOPT_PROTOCOLS:
|
|
5992
6284
|
case CURLOPT_REDIR_PROTOCOLS:
|
|
5993
6285
|
curl_easy_setopt(rbce->curl, option, NUM2LONG(val));
|
|
6286
|
+
if (option == CURLOPT_PROTOCOLS) rbce->protocols_set = 1;
|
|
5994
6287
|
break;
|
|
5995
6288
|
#ifdef HAVE_CURLOPT_PROTOCOLS_STR
|
|
5996
6289
|
case CURLOPT_PROTOCOLS_STR:
|
|
5997
6290
|
curl_easy_setopt(rbce->curl, CURLOPT_PROTOCOLS_STR, NIL_P(val) ? NULL : StringValueCStr(val));
|
|
6291
|
+
rbce->protocols_set = !NIL_P(val);
|
|
5998
6292
|
break;
|
|
5999
6293
|
#endif
|
|
6000
6294
|
#ifdef HAVE_CURLOPT_REDIR_PROTOCOLS_STR
|
|
@@ -6181,12 +6475,13 @@ static VALUE ruby_curl_easy_inspect(VALUE self) {
|
|
|
6181
6475
|
|
|
6182
6476
|
struct curl_string_result {
|
|
6183
6477
|
char *data;
|
|
6184
|
-
|
|
6478
|
+
size_t length;
|
|
6185
6479
|
};
|
|
6186
6480
|
|
|
6187
6481
|
static VALUE curl_string_result_body(VALUE argp) {
|
|
6188
6482
|
struct curl_string_result *args = (struct curl_string_result *)argp;
|
|
6189
|
-
|
|
6483
|
+
if (args->length > LONG_MAX) rb_raise(rb_eRangeError, "URL conversion result is too large for a Ruby String");
|
|
6484
|
+
return rb_str_new(args->data, (long)args->length);
|
|
6190
6485
|
}
|
|
6191
6486
|
|
|
6192
6487
|
static VALUE curl_string_result_free(VALUE argp) {
|
|
@@ -6195,12 +6490,25 @@ static VALUE curl_string_result_free(VALUE argp) {
|
|
|
6195
6490
|
return Qnil;
|
|
6196
6491
|
}
|
|
6197
6492
|
|
|
6198
|
-
static VALUE curl_string_result_to_ruby(char *data,
|
|
6493
|
+
static VALUE curl_string_result_to_ruby(char *data, size_t length) {
|
|
6199
6494
|
struct curl_string_result args = { data, length };
|
|
6200
6495
|
return rb_ensure(curl_string_result_body, (VALUE)&args,
|
|
6201
6496
|
curl_string_result_free, (VALUE)&args);
|
|
6202
6497
|
}
|
|
6203
6498
|
|
|
6499
|
+
VALUE curb_escape_string(CURL *curl, VALUE str, VALUE error_class, const char *error_message) {
|
|
6500
|
+
int length = RSTRING_LENINT(str);
|
|
6501
|
+
char *result;
|
|
6502
|
+
if (!length) return rb_str_new("", 0);
|
|
6503
|
+
#ifdef HAVE_CURL_EASY_ESCAPE
|
|
6504
|
+
result = curl_easy_escape(curl, RSTRING_PTR(str), length);
|
|
6505
|
+
#else
|
|
6506
|
+
result = curl_escape(RSTRING_PTR(str), length);
|
|
6507
|
+
#endif
|
|
6508
|
+
if (!result) rb_raise(error_class, "%s", error_message);
|
|
6509
|
+
return curl_string_result_to_ruby(result, strlen(result));
|
|
6510
|
+
}
|
|
6511
|
+
|
|
6204
6512
|
/*
|
|
6205
6513
|
* call-seq:
|
|
6206
6514
|
* easy.escape("some text") => "some%20text"
|
|
@@ -6212,7 +6520,6 @@ static VALUE curl_string_result_to_ruby(char *data, long length) {
|
|
|
6212
6520
|
*/
|
|
6213
6521
|
static VALUE ruby_curl_easy_escape(VALUE self, VALUE svalue) {
|
|
6214
6522
|
ruby_curl_easy *rbce;
|
|
6215
|
-
char *result;
|
|
6216
6523
|
VALUE str = svalue;
|
|
6217
6524
|
|
|
6218
6525
|
rbce = ruby_curl_easy_get_handle(self);
|
|
@@ -6221,14 +6528,7 @@ static VALUE ruby_curl_easy_escape(VALUE self, VALUE svalue) {
|
|
|
6221
6528
|
* String, even if the object's to_s misbehaves and returns a non-String. */
|
|
6222
6529
|
if( rb_type(str) != T_STRING ) { str = rb_obj_as_string(str); }
|
|
6223
6530
|
|
|
6224
|
-
|
|
6225
|
-
result = (char*)curl_easy_escape(rbce->curl, StringValuePtr(str), (int)RSTRING_LEN(str));
|
|
6226
|
-
#else
|
|
6227
|
-
result = (char*)curl_escape(StringValuePtr(str), (int)RSTRING_LEN(str));
|
|
6228
|
-
#endif
|
|
6229
|
-
|
|
6230
|
-
if (!result) rb_raise(eCurlErrError, "Failed to URL-encode string");
|
|
6231
|
-
return curl_string_result_to_ruby(result, (long)strlen(result));
|
|
6531
|
+
return curb_escape_string(rbce->curl, str, eCurlErrError, "Failed to URL-encode string");
|
|
6232
6532
|
}
|
|
6233
6533
|
|
|
6234
6534
|
/*
|
|
@@ -6241,27 +6541,30 @@ static VALUE ruby_curl_easy_escape(VALUE self, VALUE svalue) {
|
|
|
6241
6541
|
*/
|
|
6242
6542
|
static VALUE ruby_curl_easy_unescape(VALUE self, VALUE str) {
|
|
6243
6543
|
ruby_curl_easy *rbce;
|
|
6544
|
+
int length;
|
|
6545
|
+
#if (LIBCURL_VERSION_NUM >= 0x070f04)
|
|
6244
6546
|
int rlen;
|
|
6547
|
+
#endif
|
|
6245
6548
|
char *result;
|
|
6246
6549
|
|
|
6247
6550
|
rbce = ruby_curl_easy_get_handle(self);
|
|
6248
6551
|
|
|
6249
|
-
/* Coerce as its own statement before use below - StringValuePtr and
|
|
6250
|
-
* RSTRING_LEN are sibling args to the same call and C does not guarantee
|
|
6251
|
-
* left-to-right evaluation order between them. */
|
|
6252
6552
|
StringValue(str);
|
|
6553
|
+
length = RSTRING_LENINT(str);
|
|
6554
|
+
if (!length) return rb_str_new("", 0);
|
|
6253
6555
|
|
|
6254
6556
|
#if (LIBCURL_VERSION_NUM >= 0x070f04)
|
|
6255
|
-
result =
|
|
6557
|
+
result = curl_easy_unescape(rbce->curl, RSTRING_PTR(str), length, &rlen);
|
|
6256
6558
|
#else
|
|
6257
|
-
result =
|
|
6559
|
+
result = curl_unescape(RSTRING_PTR(str), length);
|
|
6258
6560
|
#endif
|
|
6259
6561
|
|
|
6260
6562
|
if (!result) rb_raise(eCurlErrError, "Failed to URL-decode string");
|
|
6261
6563
|
#if (LIBCURL_VERSION_NUM < 0x070f04)
|
|
6262
|
-
|
|
6263
|
-
#
|
|
6564
|
+
return curl_string_result_to_ruby(result, strlen(result));
|
|
6565
|
+
#else
|
|
6264
6566
|
return curl_string_result_to_ruby(result, rlen);
|
|
6567
|
+
#endif
|
|
6265
6568
|
}
|
|
6266
6569
|
|
|
6267
6570
|
|