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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
//! Async client entry point.
use std::sync::Arc;
use std::time::Duration;
#[cfg(any(
feature = "transport-http1",
feature = "transport-http2",
feature = "advanced-routing"
))]
use crate::transport::hyper_client::{build_hyper_client, HyperClientPolicy};
#[cfg(feature = "advanced-routing")]
use crate::transport::hyper_client::{
BoundedClientCache, RESOLVED_CLIENT_CACHE_MAX_ENTRIES, SNI_CLIENT_CACHE_MAX_ENTRIES,
};
#[cfg(feature = "proxy")]
use crate::transport::hyper_client::{
CONNECT_CLIENT_CACHE_MAX_ENTRIES, FORWARD_CLIENT_CACHE_MAX_ENTRIES,
SOCKS_CLIENT_CACHE_MAX_ENTRIES,
};
#[cfg(feature = "high-level-url")]
use http::Method;
#[cfg(any(feature = "advanced-routing", feature = "proxy"))]
use tokio::sync::Mutex;
use crate::error::{Error, Result};
use crate::headers::Headers;
use crate::http_version::HttpVersionPolicy;
use crate::limits::Limits;
use crate::pool::{Pool, PoolConfig, PoolMetrics};
#[cfg(feature = "proxy")]
use crate::proxy::Proxy;
#[cfg(feature = "redirects")]
use crate::redirect::RedirectPolicy;
#[cfg(feature = "high-level-url")]
use crate::request::{Request, RequestBuilder};
#[cfg(feature = "high-level-url")]
use crate::response::Response;
#[cfg(feature = "logical-retry")]
use crate::retry::RetryPolicy;
use crate::timeout::Timeout;
#[cfg(feature = "advanced-routing")]
use crate::transport::dialer::Dialer;
#[cfg(any(
feature = "transport-http1",
feature = "transport-http2",
feature = "advanced-routing"
))]
use crate::transport::lifecycle::LifecycleConfig;
use crate::transport::lifecycle::{PhysicalConnectionPolicy, TransportIoTimeout};
use crate::transport_hints::NativeRequestOptions;
mod config;
use config::ClientConfig;
mod connectors;
mod routes;
#[cfg(feature = "advanced-routing")]
use connectors::build_custom_connector;
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
use connectors::build_standard_connector;
#[cfg(all(test, any(feature = "transport-http1", feature = "transport-http2")))]
use connectors::build_standard_connector_with_resolver;
#[cfg(feature = "tls-rustls")]
pub(crate) use connectors::configure_tls_alpn;
#[cfg(feature = "advanced-routing")]
use routes::ResolvedRouteKey;
#[cfg(feature = "cookies")]
use crate::cookie::CookieJar;
/// Async HTTP client.
///
/// The client manages connection pooling and shared configuration. Create one
/// with [`Client::new`] or [`Client::builder`].
#[derive(Clone)]
pub struct Client {
inner: Arc<ClientInner>,
}
impl std::fmt::Debug for Client {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Client").finish_non_exhaustive()
}
}
pub(crate) struct ClientInner {
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
pub(crate) hyper_client: Option<crate::transport::TimeoutHyperClient>,
/// Hyper client backed by the caller-supplied native dialer.
#[cfg(feature = "advanced-routing")]
pub(crate) custom_client: Option<crate::transport::TimeoutCustomClient>,
/// Error captured while building the configured TLS policy, if any.
///
/// `ClientBuilder::build` is intentionally infallible, so HTTPS requests
/// surface this error at dispatch time instead of falling back to a
/// different trust policy.
#[cfg(feature = "tls-rustls")]
pub(crate) tls_config_error: Option<String>,
/// Direct connector for requests with advanced socket options or local
/// address binding. Uses a custom connector instead of the standard
/// hyper-rustls connector path.
#[cfg(feature = "advanced-routing")]
pub(crate) direct_client: Option<crate::transport::TimeoutDirectClient>,
/// Base direct-connector configuration (local address / socket options)
/// used to construct SNI-override clients so they preserve source
/// binding and socket tuning.
#[cfg(feature = "advanced-routing")]
pub(crate) direct_connector_config:
Option<crate::transport::direct_connector::DirectConnectorConfig>,
/// Cached hyper clients keyed by TLS SNI hostname override.
///
/// When a request carries `TransportHints.sni_hostname`, the pipeline
/// uses a DirectConnector-based client from this cache. The connector
/// separates DNS/TCP (to the original URL host) from TLS (with the
/// SNI override hostname), keeping the default path unchanged.
///
/// Bounded by [`SNI_CLIENT_CACHE_MAX_ENTRIES`] via the shared
/// [`BoundedClientCache`] mechanics (see `transport::hyper_client`).
///
/// The cache is keyed only by hostname because every other
/// connection-affecting dimension is immutable at `ClientInner` scope
/// after `ClientBuilder::build`: `http_version_policy` and `tls_config`
/// ALPN, `direct_connector_config` socket/local-address policy,
/// `timeout.connect` phase policy, and the absence of a custom dialer
/// on this path. If any of those became per-request mutable, this
/// cache would need to include the changed dimension in the key to
/// avoid serving a client with stale policy (e.g. H2 vs H1) for the
/// same hostname. Request-scoped state (total/read/write/pool
/// deadlines, retry/redirect, body, trace/failure context,
/// decompression limits, cookies/auth headers) is never keyed and
/// never retained by the cached connector.
#[cfg(feature = "advanced-routing")]
pub(crate) sni_clients:
Mutex<BoundedClientCache<String, crate::transport::TimeoutDirectClient>>,
/// Cached custom-dialer clients for per-request SNI overrides.
#[cfg(feature = "advanced-routing")]
pub(crate) sni_custom_clients:
Mutex<BoundedClientCache<String, crate::transport::TimeoutCustomClient>>,
/// Cached Hyper clients for direct resolved-target routes.
///
/// Bounded by [`RESOLVED_CLIENT_CACHE_MAX_ENTRIES`] via the shared
/// [`BoundedClientCache`] mechanics (see `transport::hyper_client`).
/// Hyper remains the physical pool owner: each entry is a configured
/// Hyper client whose H1 keep-alive / H2 multiplexed connections are
/// reused only when the full [`ResolvedRouteKey`] (logical origin +
/// ordered address snapshot + exact SNI override) matches.
///
/// The key contains origin + ordered target + SNI because each of those
/// dimensions changes the physical/TLS route: different logical origins
/// must never share a pool even when the addresses coincide, address
/// order controls failover, and SNI changes certificate identity.
/// Immutable client-wide TLS/ALPN/socket/connect/lifecycle/idle policy
/// is intentionally not duplicated into every key because it is fixed at
/// `ClientInner` scope after `ClientBuilder::build`; if any such field
/// became request-scoped, the key must be expanded first. Ordinary
/// DNS/direct clients and resolved-target clients never share an entry,
/// and proxy resolved routing stays in the SOCKS/forward/CONNECT caches.
#[cfg(feature = "advanced-routing")]
pub(crate) resolved_clients:
Mutex<BoundedClientCache<ResolvedRouteKey, crate::transport::TimeoutDirectClient>>,
/// Hyper client for Unix domain socket requests.
#[cfg(unix)]
#[cfg(feature = "advanced-routing")]
pub(crate) uds_client: Option<crate::transport::TimeoutUdsClient>,
/// Persistent Hyper clients keyed by effective SOCKS route.
///
/// Bounded like [`ClientInner::sni_clients`] via the shared
/// [`BoundedClientCache`] mechanics (see `transport::hyper_client`).
/// SOCKS route clients share the resolved Hyper idle-pool policy with
/// every other persistent Hyper family.
#[cfg(feature = "proxy")]
pub(crate) socks_clients: Mutex<
BoundedClientCache<
crate::transport::socks::SocksRouteKey,
crate::transport::TimeoutSocksClient,
>,
>,
/// Persistent Hyper clients for ordinary HTTP forward-proxy routes.
/// Keys include the destination origin and every proxy-leg policy that
/// can affect a reusable connection. The cache is bounded because
/// request-scoped proxy overrides are allowed.
#[cfg(feature = "proxy")]
pub(crate) forward_clients: Mutex<
BoundedClientCache<
crate::transport::proxy::ForwardRouteKey,
crate::transport::TimeoutForwardClient,
>,
>,
/// Persistent Hyper clients for one HTTPS origin reached through CONNECT.
#[cfg(feature = "proxy")]
pub(crate) connect_clients: Mutex<
BoundedClientCache<
crate::transport::connect::ConnectRouteKey,
crate::transport::TimeoutConnectClient,
>,
>,
pub(crate) config: ClientConfig,
pub(crate) pool: Pool,
/// Transport observability counters shared by all connectors owned by
/// this client. Distinct from [`Pool`] logical-permit metrics.
pub(crate) transport_metrics: Arc<crate::transport::metrics::TransportMetrics>,
/// Shared admission permits and established-I/O timeout policy.
#[cfg(any(
feature = "transport-http1",
feature = "transport-http2",
feature = "advanced-routing"
))]
pub(crate) lifecycle: Arc<LifecycleConfig>,
#[cfg(feature = "http3")]
pub(crate) h3_connector: Option<crate::transport::http3::H3Connector>,
/// Bounded Alt-Svc discovery state (separate from QUIC sessions).
/// Owned here so all routes share one view. Only present with `http3`;
/// without it there is no discovery and no state to keep.
#[cfg(feature = "http3")]
pub(crate) alt_svc_state: Arc<crate::transport::alt_svc::AltSvcState>,
}
impl Client {
/// Create a new client with default settings.
#[must_use]
pub fn new() -> Self {
Self::builder().build()
}
/// Create a [`ClientBuilder`] for configuring a client.
#[must_use]
pub fn builder() -> ClientBuilder {
ClientBuilder::new()
}
/// Create a GET request builder for the given URL.
///
/// # Errors
///
/// Returns [`Error::InvalidUrl`] if `url` cannot be parsed.
#[cfg(feature = "high-level-url")]
pub fn get(&self, url: &str) -> Result<RequestBuilder> {
self.request(Method::GET, url)
}
/// Create a POST request builder for the given URL.
///
/// # Errors
///
/// Returns [`Error::InvalidUrl`] if `url` cannot be parsed.
#[cfg(feature = "high-level-url")]
pub fn post(&self, url: &str) -> Result<RequestBuilder> {
self.request(Method::POST, url)
}
/// Create a PUT request builder for the given URL.
///
/// # Errors
///
/// Returns [`Error::InvalidUrl`] if `url` cannot be parsed.
#[cfg(feature = "high-level-url")]
pub fn put(&self, url: &str) -> Result<RequestBuilder> {
self.request(Method::PUT, url)
}
/// Create a PATCH request builder for the given URL.
///
/// # Errors
///
/// Returns [`Error::InvalidUrl`] if `url` cannot be parsed.
#[cfg(feature = "high-level-url")]
pub fn patch(&self, url: &str) -> Result<RequestBuilder> {
self.request(Method::PATCH, url)
}
/// Create a DELETE request builder for the given URL.
///
/// # Errors
///
/// Returns [`Error::InvalidUrl`] if `url` cannot be parsed.
#[cfg(feature = "high-level-url")]
pub fn delete(&self, url: &str) -> Result<RequestBuilder> {
self.request(Method::DELETE, url)
}
/// Create a HEAD request builder for the given URL.
///
/// # Errors
///
/// Returns [`Error::InvalidUrl`] if `url` cannot be parsed.
#[cfg(feature = "high-level-url")]
pub fn head(&self, url: &str) -> Result<RequestBuilder> {
self.request(Method::HEAD, url)
}
/// Create an OPTIONS request builder for the given URL.
///
/// # Errors
///
/// Returns [`Error::InvalidUrl`] if `url` cannot be parsed.
#[cfg(feature = "high-level-url")]
pub fn options(&self, url: &str) -> Result<RequestBuilder> {
self.request(Method::OPTIONS, url)
}
/// Create a request builder for the given method and URL.
///
/// # Errors
///
/// Returns [`Error::InvalidUrl`] if `url` cannot be parsed.
#[cfg(feature = "high-level-url")]
pub fn request(&self, method: Method, url: &str) -> Result<RequestBuilder> {
let parsed = parse_url(url)?;
Ok(RequestBuilder::new(self.clone(), method, parsed))
}
/// Returns a reference to the connection pool metrics.
///
/// These count logical request-permit waits/cancellations. For
/// connector/protocol observations see [`Self::transport_metrics`].
#[must_use]
pub fn pool_metrics(&self) -> &PoolMetrics {
self.inner.pool.metrics()
}
/// Returns a reference to the transport observability counters.
///
/// Counts connector events (DNS/connect/TLS attempts) and protocol
/// connections (H3 sessions, 101 upgrades) where observable. Hyper
/// socket-reuse counts are intentionally absent.
#[must_use]
pub fn transport_metrics(&self) -> &crate::transport::metrics::TransportMetrics {
&self.inner.transport_metrics
}
/// Returns a reference to the client's cookie jar.
///
/// Only available when the `cookies` feature is enabled.
#[cfg(feature = "cookies")]
#[must_use]
pub fn cookies(&self) -> &CookieJar {
&self.inner.config.cookie_jar
}
/// Returns a reference to the shared client configuration.
#[must_use]
pub(crate) fn config(&self) -> &ClientConfig {
&self.inner.config
}
/// Send a request and return the response, following redirects if
/// the client's redirect policy allows.
///
/// When the `redirects` feature is enabled, the redirect loop enforces
/// `max_redirects`, performs method rewrites per HTTP semantics, strips
/// sensitive headers on cross-origin hops, and records redirect history.
/// Without it, 3xx responses are returned as ordinary responses.
///
/// When the `logical-retry` feature is enabled and a retry policy is
/// configured (on the client or request), the entire logical request is
/// retried on failure according to the policy. Without it, each
/// high-level request is dispatched once under the outer total deadline.
/// Hyper's distinct canceled-idle-request retry remains governed by
/// `retry_canceled_requests` in all profiles.
///
/// # Errors
///
/// Returns an error if the request fails at any stage (connect, TLS,
/// protocol, body) or if a timeout elapses.
#[cfg(feature = "high-level-url")]
pub(crate) async fn send(&self, request: Request) -> Result<Response> {
#[cfg(feature = "logical-retry")]
{
Box::pin(crate::pipeline::send_with_retry(self, request)).await
}
#[cfg(all(not(feature = "logical-retry"), feature = "redirects"))]
{
Box::pin(crate::pipeline::send_with_redirects(self, request)).await
}
#[cfg(all(not(feature = "logical-retry"), not(feature = "redirects")))]
{
Box::pin(crate::pipeline::send_lean(self, request)).await
}
}
/// Send a request and return optional structured native failure detail.
///
/// The ordinary [`Error`] remains unchanged and can be recovered with
/// [`RequestFailure::into_error`](crate::RequestFailure::into_error).
/// Network classifications are reported only when the selected route
/// exposes typed evidence; callers must not treat an absent subtype as a
/// positive diagnosis.
///
/// # Errors
///
/// Returns the original request error wrapped in [`crate::RequestFailure`].
#[cfg(feature = "high-level-url")]
pub async fn send_detailed(
&self,
mut request: Request,
) -> std::result::Result<Response, crate::RequestFailure> {
let context = crate::error::RequestFailureContext::new();
context.clear();
request.set_failure_context(Some(context.clone()));
#[cfg(feature = "logical-retry")]
let result = Box::pin(crate::pipeline::send_with_retry(self, request)).await;
#[cfg(all(not(feature = "logical-retry"), feature = "redirects"))]
let result = Box::pin(crate::pipeline::send_with_redirects(self, request)).await;
#[cfg(all(not(feature = "logical-retry"), not(feature = "redirects")))]
let result = Box::pin(crate::pipeline::send_lean(self, request)).await;
match result {
Ok(response) => Ok(response),
Err(error) => Err(crate::RequestFailure::from_context(error, &context)),
}
}
/// Send a single HTTP request and return the streaming response.
///
/// This handles pool acquisition, timeout application, and body
/// processing for one request/response cycle. It does NOT handle
/// redirects—that is the responsibility of [`Client::send`].
#[cfg(feature = "high-level-url")]
pub(crate) async fn send_single_request(
&self,
request: Request,
timeout: &Timeout,
) -> Result<Response> {
crate::pipeline::send_single_request(&self.inner, request, timeout).await
}
/// Execute a native `http_body::Body` using eggfetch's HTTP/TLS
/// transport engine.
///
/// This is the lower-level native embedding API. Request DATA and
/// trailer frames are passed through without flattening. The response is
/// an ordinary `http::Response` whose opaque [`crate::NativeResponseBody`]
/// preserves DATA and trailer frames and holds the logical pool lease
/// until the body reaches EOF, errors, or is dropped.
///
/// Native execution does not implicitly apply redirects, logical
/// retries, cookies, authentication, response decompression, or decoded
/// body limits. The request body is one-shot. Built-in proxy, HTTP/3,
/// and protocol-upgrade routes are rejected because their current
/// adapters do not expose the frame-preserving contract.
///
/// # Errors
///
/// Returns an error for an invalid logical URI, unsupported route, pool
/// or transport failure, or a timeout.
pub async fn execute_http_body<B>(
&self,
request: http::Request<B>,
options: NativeRequestOptions,
) -> Result<http::Response<crate::NativeResponseBody>>
where
B: http_body::Body<Data = bytes::Bytes> + Send + 'static,
B::Error: std::error::Error + Send + Sync + 'static,
{
crate::pipeline::send_native_http_body(&self.inner, request, options).await
}
/// Execute a native body request with default native transport options.
///
/// # Errors
///
/// Returns the same URI, pool, transport, and timeout errors as
/// [`Self::execute_http_body`].
pub async fn execute_http_body_default<B>(
&self,
request: http::Request<B>,
) -> Result<http::Response<crate::NativeResponseBody>>
where
B: http_body::Body<Data = bytes::Bytes> + Send + 'static,
B::Error: std::error::Error + Send + Sync + 'static,
{
self.execute_http_body(request, NativeRequestOptions::default())
.await
}
}
impl Default for Client {
fn default() -> Self {
Self::new()
}
}
/// Builder for configuring a [`Client`].
pub struct ClientBuilder {
default_headers: Headers,
user_agent: Option<String>,
pool_config: PoolConfig,
timeout: Option<Timeout>,
limits: Option<Limits>,
#[cfg(feature = "redirects")]
redirect: RedirectPolicy,
auth: Option<crate::auth::AuthScheme>,
#[cfg(feature = "cookies")]
cookie_jar: Option<CookieJar>,
automatic_decompression: Option<bool>,
max_decoded_body_size: Option<usize>,
max_decompression_ratio: Option<f64>,
#[cfg(feature = "proxy")]
proxy: Option<Proxy>,
#[cfg(feature = "proxy")]
environment_proxies: Vec<Proxy>,
#[cfg(feature = "tls-rustls")]
tls_config: Option<crate::tls::TlsConfig>,
#[cfg(feature = "logical-retry")]
retry: Option<RetryPolicy>,
retry_canceled_requests: bool,
#[cfg(feature = "advanced-routing")]
dialer: Option<Arc<dyn Dialer>>,
physical_connection_policy: PhysicalConnectionPolicy,
transport_io_timeout: TransportIoTimeout,
http_version_policy: HttpVersionPolicy,
/// Advanced direct-connector config for socket options / local address.
#[cfg(feature = "advanced-routing")]
direct_connector_config: Option<crate::transport::direct_connector::DirectConnectorConfig>,
/// Unix domain socket path. When set, all requests use UDS transport.
#[cfg(feature = "advanced-routing")]
uds_path: Option<String>,
/// Caller-owned datagram dialer for experimental HTTP/3 routing.
#[cfg(feature = "http3")]
datagram_dialer: Option<Arc<dyn crate::transport::datagram::DatagramDialer>>,
}
impl Default for ClientBuilder {
fn default() -> Self {
Self::new()
}
}
impl ClientBuilder {
/// Create a new client builder with defaults.
#[must_use]
pub fn new() -> Self {
Self {
default_headers: Headers::new(),
user_agent: None,
pool_config: PoolConfig::default(),
timeout: None,
limits: None,
#[cfg(feature = "redirects")]
redirect: RedirectPolicy::default(),
auth: None,
#[cfg(feature = "cookies")]
cookie_jar: None,
automatic_decompression: None,
max_decoded_body_size: None,
max_decompression_ratio: None,
#[cfg(feature = "proxy")]
proxy: None,
#[cfg(feature = "proxy")]
environment_proxies: Vec::new(),
#[cfg(feature = "tls-rustls")]
tls_config: None,
#[cfg(feature = "logical-retry")]
retry: None,
retry_canceled_requests: true,
#[cfg(feature = "advanced-routing")]
dialer: None,
physical_connection_policy: PhysicalConnectionPolicy::default(),
transport_io_timeout: TransportIoTimeout::default(),
http_version_policy: HttpVersionPolicy::default(),
#[cfg(feature = "advanced-routing")]
direct_connector_config: None,
#[cfg(feature = "advanced-routing")]
uds_path: None,
#[cfg(feature = "http3")]
datagram_dialer: None,
}
}
/// Add a default header to all requests made by this client.
///
/// # Errors
///
/// Returns an error if `name` or `value` is not a valid header field.
pub fn default_header(mut self, name: &str, value: &str) -> Result<Self> {
self.default_headers.insert(name, value)?;
Ok(self)
}
/// Set the default headers for all requests made by this client.
///
/// This preserves valid header values containing HTTP `obs-text` bytes.
#[must_use]
pub fn default_headers(mut self, headers: Headers) -> Self {
self.default_headers = headers;
self
}
/// Set the default user-agent header.
#[must_use]
pub fn user_agent(mut self, agent: &str) -> Self {
self.user_agent = Some(agent.to_owned());
self
}
/// Set the maximum number of idle (unused) connections kept per host.
///
/// This is a convenience alias for [`max_idle_connections_per_host`]:
/// the transport enforces the limit on a per-host basis, so total
/// idle connections grow with the number of distinct origins. Use
/// [`max_connections`] for a global concurrency cap.
///
/// [`max_idle_connections_per_host`]: Self::max_idle_connections_per_host
/// [`max_connections`]: Self::max_connections
#[must_use]
pub fn max_idle_connections(mut self, max: usize) -> Self {
self.pool_config.max_idle_connections = Some(max);
self
}
/// Set the maximum number of idle connections per individual host.
#[must_use]
pub fn max_idle_connections_per_host(mut self, max: usize) -> Self {
self.pool_config.max_idle_connections_per_host = Some(max);
self
}
/// Set the maximum total number of concurrent in-flight requests.
///
/// Compatibility alias for [`Self::max_in_flight_requests`] kept for
/// the pre-1.0 line. One permit equals one logical request, not one
/// TCP connection (H2/H3 multiplex many requests over one
/// connection). Prefer `max_in_flight_requests` in new code.
#[must_use]
pub fn max_connections(mut self, max: usize) -> Self {
self.pool_config.max_connections = Some(max);
self
}
/// Set the maximum number of concurrent in-flight requests per origin.
///
/// Compatibility alias for [`Self::max_in_flight_requests_per_origin`].
#[must_use]
pub fn max_connections_per_host(mut self, max: usize) -> Self {
self.pool_config.max_connections_per_host = Some(max);
self
}
/// Set the maximum total number of concurrent in-flight requests.
///
/// Preferred native name. Bounds logical request concurrency (one
/// permit per request). Under H1 one request typically owns its
/// connection slot; under H2/H3 many permits multiplex over one
/// TCP/QUIC connection. When both this and `max_connections` are
/// set, this wins.
#[must_use]
pub fn max_in_flight_requests(mut self, max: usize) -> Self {
self.pool_config.max_in_flight_requests = Some(max);
self
}
/// Set the maximum number of concurrent in-flight requests per origin.
///
/// Preferred native name. When both this and
/// `max_connections_per_host` are set, this wins.
#[must_use]
pub fn max_in_flight_requests_per_origin(mut self, max: usize) -> Self {
self.pool_config.max_in_flight_requests_per_origin = Some(max);
self
}
/// Set the duration after which idle connections are closed.
#[must_use]
pub fn idle_timeout(mut self, timeout: Duration) -> Self {
self.pool_config.idle_timeout = Some(timeout);
self
}
/// Set the default timeout for all requests made by this client.
#[must_use]
pub fn timeout(mut self, timeout: Timeout) -> Self {
self.timeout = Some(timeout);
self
}
/// Merge a partial timeout configuration into the currently configured
/// timeout.
///
/// Unlike [`timeout`](Self::timeout), which replaces the entire
/// configuration, each phase set in `timeout` overrides only that
/// phase; unset phases keep any previously configured value (or
/// remain unset when no timeout was configured yet).
#[must_use]
pub fn merge_timeout(mut self, timeout: Timeout) -> Self {
let merged = match self.timeout {
Some(existing) => existing.merge(Some(timeout)),
None => timeout,
};
self.timeout = Some(merged);
self
}
/// Set resource limits for the connection pool.
///
/// Limits control logical request concurrency (pool permits) and
/// physical connection behavior (idle connection caps and expiry).
/// When set, the limits are applied to the pool configuration.
#[must_use]
pub fn limits(mut self, limits: Limits) -> Self {
self.limits = Some(limits);
self
}
/// Set the redirect policy for this client.
///
/// Only available with the `redirects` feature (enabled by the `http1`/`http2`
/// compatibility aliases). The lean profile omits redirect following and
/// returns 3xx responses without a second hop.
#[cfg(feature = "redirects")]
#[must_use]
pub fn follow_redirects(mut self, follow: bool) -> Self {
self.redirect.follow = follow;
self
}
/// Set the maximum number of redirects to follow.
///
/// Only available with the `redirects` feature.
#[cfg(feature = "redirects")]
#[must_use]
pub fn max_redirects(mut self, max: usize) -> Self {
self.redirect.max_redirects = max;
self
}
/// Set the full redirect policy.
///
/// Only available with the `redirects` feature.
#[cfg(feature = "redirects")]
#[must_use]
pub fn redirect_policy(mut self, policy: RedirectPolicy) -> Self {
self.redirect = policy;
self
}
/// Set the HTTPS-downgrade rule for redirects without replacing the
/// rest of the redirect policy.
///
/// Only available with the `redirects` feature.
///
/// Select
/// [`RedirectDowngradePolicy::Deny`](crate::redirect::RedirectDowngradePolicy::Deny)
/// to reject HTTPS -> HTTP downgrades before the downgraded request
/// is dispatched. The default is
/// [`Allow`](crate::redirect::RedirectDowngradePolicy::Allow) for
/// compatibility.
#[cfg(feature = "redirects")]
#[must_use]
pub fn redirect_downgrade_policy(
mut self,
policy: crate::redirect::RedirectDowngradePolicy,
) -> Self {
self.redirect.downgrade = policy;
self
}
/// Set a shared cookie jar for this client.
///
/// When set, the client will automatically inject matching cookies
/// into requests and update the jar from `Set-Cookie` response headers.
///
/// Only available when the `cookies` feature is enabled.
#[cfg(feature = "cookies")]
#[must_use]
pub fn cookie_jar(mut self, jar: CookieJar) -> Self {
self.cookie_jar = Some(jar);
self
}
/// Set default authentication for all requests made by this client.
///
/// The configured auth is applied to every request unless overridden
/// or disabled at the request level. Auth is recomputed per redirect
/// hop; cross-origin redirects never carry client auth.
#[must_use]
pub fn auth(mut self, auth: impl Into<crate::auth::AuthScheme>) -> Self {
self.auth = Some(auth.into());
self
}
/// Set the default proxy for all requests made by this client.
///
/// When set, all matching requests are routed through the specified
/// proxy. Can be overridden or disabled per-request.
#[cfg(feature = "proxy")]
#[must_use]
pub fn proxy(mut self, proxy: Proxy) -> Self {
self.proxy = Some(proxy);
self
}
/// Add a scheme-specific proxy used when no explicit proxy is configured.
#[cfg(feature = "proxy")]
#[must_use]
pub fn environment_proxy(mut self, proxy: Proxy) -> Self {
self.environment_proxies.push(proxy);
self
}
/// Opt into environment-proxy routing from an explicit snapshot.
///
/// The native default stays environment-independent: without this call
/// the client never reads proxy environment variables. Pass
/// [`crate::proxy::ProxyEnvironment::from_env`] at the call site to
/// preserve command-line environment reachability, or
/// [`crate::proxy::ProxyEnvironment::from_map`] in tests for a pure
/// snapshot without global environment mutation.
///
/// Each configured value becomes a scheme-scoped fallback proxy
/// (specific before `ALL_PROXY`); `NO_PROXY` bypass is attached to
/// every proxy. Invalid values fail closed with a redacted error.
///
/// # Errors
///
/// Returns [`crate::error::Error::InvalidProxyUrl`] when a configured
/// proxy URL or the `NO_PROXY` value cannot be parsed.
#[cfg(feature = "proxy")]
pub fn proxy_environment(
mut self,
env: &crate::proxy::ProxyEnvironment,
) -> crate::error::Result<Self> {
for proxy in env.client_proxies()? {
self.environment_proxies.push(proxy);
}
Ok(self)
}
/// Set `NO_PROXY` bypass rules for the default proxy.
///
/// When set, URLs matching any bypass rule are sent directly
/// without going through the proxy.
#[cfg(feature = "proxy")]
#[must_use]
pub fn no_proxy(mut self, no_proxy: crate::proxy::NoProxy) -> Self {
if let Some(proxy) = self.proxy.take() {
self.proxy = Some(proxy.no_proxy(no_proxy));
}
self
}
/// Set a custom TLS configuration for this client.
///
/// When set, the client uses the provided [`crate::tls::TlsConfig`] for all
/// TLS connections instead of the default native-root-with-WebPKI-fallback
/// strategy (or the packaged-WebPKI default when `tls-native-roots` is not
/// enabled).
#[cfg(feature = "tls-rustls")]
#[must_use]
pub fn tls_config(mut self, config: crate::tls::TlsConfig) -> Self {
self.tls_config = Some(config);
self
}
/// Enable or disable insecure TLS verification while retaining other
/// TLS configuration already applied to this builder.
#[cfg(feature = "tls-rustls")]
#[must_use]
pub fn danger_accept_invalid_certs(mut self, accept: bool) -> Self {
let config = self.tls_config.take().unwrap_or_default();
self.tls_config = Some(config.danger_accept_invalid_certs(accept));
self
}
/// Enable or disable automatic response decompression.
///
/// When enabled (the default), the client sends an
/// `Accept-Encoding` header and transparently decompresses
/// response bodies. Decoded `Content-Encoding` and
/// `Content-Length` headers are removed from the response.
///
/// Can be overridden per-request via
/// [`RequestBuilder::decompress`].
#[must_use]
pub fn automatic_decompression(mut self, enabled: bool) -> Self {
self.automatic_decompression = Some(enabled);
self
}
/// Set the maximum decoded response body size in bytes.
///
/// The limit applies to bytes made available after decoding and also to
/// ordinary unencoded/identity response bodies, in both buffered and
/// streaming paths. Exceeding it produces [`Error::DecodedBodyTooLarge`]
/// without requiring a second caller-side accumulation loop. A wire
/// `Content-Length` may be used for an early metadata check, but this
/// body limit remains authoritative when the header is absent, incorrect,
/// or describes encoded bytes.
#[must_use]
pub fn max_decoded_body_size(mut self, max: usize) -> Self {
self.max_decoded_body_size = Some(max);
self
}
/// Set the maximum decompression ratio (decoded bytes / compressed bytes).
/// When set, responses whose expansion ratio exceeds this limit produce
/// an error. This guards against zip-bomb style attacks.
///
/// # Panics
///
/// Panics if `ratio` is not finite or not positive.
#[must_use]
pub fn max_decompression_ratio(mut self, ratio: f64) -> Self {
assert!(
ratio.is_finite() && ratio > 0.0,
"max_decompression_ratio must be finite and positive, got {ratio}"
);
self.max_decompression_ratio = Some(ratio);
self
}
/// Set the retry policy for this client.
///
/// Only available with the `logical-retry` feature (enabled by the `http1`/`http2`
/// compatibility aliases). The lean profile dispatches once under the outer
/// total deadline with no retry backoff/jitter/status policy.
///
/// When set, failed requests that match the policy (safe methods,
/// retryable statuses/errors, replayable bodies) are automatically
/// retried with exponential backoff.
#[cfg(feature = "logical-retry")]
#[must_use]
pub fn retry(mut self, policy: RetryPolicy) -> Self {
self.retry = Some(policy);
self
}
/// Allow or disallow Hyper's implicit retry when a reused idle
/// connection is found unusable before a request starts writing.
///
/// This is separate from eggfetch's explicit logical retry (the
/// `logical-retry` feature and [`RetryPolicy`]). The
/// default is `true`, preserving Hyper's existing behavior.
#[must_use]
pub fn retry_canceled_requests(mut self, enabled: bool) -> Self {
self.retry_canceled_requests = enabled;
self
}
/// Install a client-scoped caller-owned raw-stream dialer.
///
/// Eggfetch continues to own HTTP framing, destination TLS, SNI,
/// certificate verification, redirects, retries, and response bodies.
/// The dialer is incompatible with built-in proxy, UDS, resolved-target,
/// local-address, and socket-option routing; those combinations fail
/// closed before network I/O.
///
/// Only available with the `advanced-routing` feature (enabled by the
/// `native-http1`/`native-http2` compatibility slices). The lean
/// standard-route profile omits custom dialing.
#[cfg(feature = "advanced-routing")]
#[must_use]
pub fn dialer<D>(mut self, dialer: D) -> Self
where
D: Dialer,
{
self.dialer = Some(Arc::new(dialer));
self
}
/// Install a caller-owned datagram dialer for experimental HTTP/3 routing.
///
/// Every HTTP/3 connection generation, whether it comes from an explicit
/// `Http3Only` request or from an Alt-Svc alternative, is established over
/// one fixed-target [`DatagramRoute`](crate::DatagramRoute) returned by this
/// dialer. TCP requests are unaffected: [`Self::dialer`] still governs
/// HTTP/1.1 and HTTP/2.
///
/// Configuring a dialer means Eggfetch creates **no** direct QUIC UDP
/// socket. A route that fails or refuses can therefore never fall back to
/// silently opening one; the request fails with the dialer's typed
/// [`DialErrorKind`](crate::DialErrorKind) instead.
///
/// Eggfetch still owns the whole QUIC stack. The dialer supplies a datagram
/// carrier only: no handshake, no certificate material, and no QUIC frames
/// cross this boundary. SNI and certificate validation always use the
/// logical origin, even when the route's physical peer is a relay.
///
/// Requires `http3` plus `advanced-routing`, which `http3` always enables.
/// Lean profiles without `http3` omit this hook entirely.
#[cfg(feature = "http3")]
#[must_use]
pub fn datagram_dialer<D>(mut self, dialer: D) -> Self
where
D: crate::transport::datagram::DatagramDialer,
{
self.datagram_dialer = Some(Arc::new(dialer));
self
}
/// Install a shared caller-scoped live physical-connection policy.
///
/// This controls established Hyper connections, including idle pooled
/// connections. It does not change the logical request limits in
/// [`PoolConfig`]. A `max_live` value of zero fails
/// requests closed because no useful client can be created from it.
#[must_use]
pub fn physical_connection_policy(mut self, policy: PhysicalConnectionPolicy) -> Self {
self.physical_connection_policy = policy;
self
}
/// Install established-transport read/write inactivity guardrails.
///
/// These timers are distinct from [`Timeout`]'s request
/// body and response-stream semantics and are disabled by default.
#[must_use]
pub fn transport_io_timeout(mut self, timeout: TransportIoTimeout) -> Self {
self.transport_io_timeout = timeout;
self
}
/// Set the HTTP version policy for this client.
///
/// Controls which HTTP protocol versions the client may negotiate.
/// The default is [`HttpVersionPolicy::Auto`], which allows HTTP/2
/// negotiation via ALPN when the `http2` feature is enabled.
///
/// When the `http2` feature is not compiled in, `Http2Only` and `Auto`
/// are silently downgraded to `Http1Only`.
#[must_use]
pub fn http_version_policy(mut self, policy: HttpVersionPolicy) -> Self {
self.http_version_policy = policy;
self
}
/// Set a local address to bind outbound sockets to before connecting.
///
/// When set, all direct (non-proxy, non-UDS) connections will bind
/// to the specified local address before connecting to the remote.
/// This uses a custom connector path instead of the standard
/// hyper-rustls connector.
///
/// Only available with `advanced-routing`; omitted by the lean profile.
#[cfg(feature = "advanced-routing")]
#[must_use]
pub fn local_address(mut self, addr: std::net::SocketAddr) -> Self {
let config = self.direct_connector_config.get_or_insert_with(|| {
crate::transport::direct_connector::DirectConnectorConfig {
local_address: None,
socket_options: Vec::new(),
}
});
config.local_address = Some(addr);
self
}
/// Set socket options to apply to outbound TCP connections.
///
/// Options are applied to the socket before the connect operation.
/// Recognized options are applied via `tokio::net::TcpSocket` setters;
/// unrecognized options produce a connection error (never silently
/// ignored, per the plan's Track 2.4 requirement).
///
/// Only available with `advanced-routing`; omitted by the lean profile.
#[cfg(feature = "advanced-routing")]
#[must_use]
pub fn socket_options(
mut self,
options: Vec<crate::transport::direct_connector::SocketOption>,
) -> Self {
let config = self.direct_connector_config.get_or_insert_with(|| {
crate::transport::direct_connector::DirectConnectorConfig {
local_address: None,
socket_options: Vec::new(),
}
});
config.socket_options = options;
self
}
/// Set a Unix domain socket path for all connections.
///
/// When set, all requests are routed through the specified UDS path
/// instead of making TCP connections. The URL in the request still
/// provides HTTP scheme/authority/path semantics.
///
/// Only supported on Unix platforms. On non-Unix platforms, this
/// option is accepted but will produce an error at request time.
///
/// Only available with `advanced-routing`; omitted by the lean profile.
#[cfg(feature = "advanced-routing")]
#[must_use]
pub fn uds_path(mut self, path: String) -> Self {
self.uds_path = Some(path);
self
}
/// Build the client.
///
/// Native system roots are preferred. If the platform root store is
/// unavailable, the client falls back to the packaged Mozilla root set
/// while retaining certificate and hostname verification.
#[must_use]
#[allow(
clippy::too_many_lines,
reason = "the builder resolves the root store, the HTTP version policy and the pool limits in one pass, and splitting it would scatter the cfg-gated branches across helpers"
)]
pub fn build(self) -> Client {
#[cfg(any(
feature = "transport-http1",
feature = "transport-http2",
feature = "advanced-routing"
))]
use crate::http_version::HttpVersionPolicyEnabler;
#[cfg(any(
feature = "transport-http1",
feature = "transport-http2",
feature = "advanced-routing"
))]
let enabler = HttpVersionPolicyEnabler::from_policy(self.http_version_policy);
let transport_metrics = Arc::new(crate::transport::metrics::TransportMetrics::new());
let mut pool_config = self.pool_config;
if let Some(limits) = self.limits {
let limits_config: PoolConfig = limits.into();
if limits_config.max_connections.is_some() {
pool_config.max_connections = limits_config.max_connections;
}
if limits_config.max_connections_per_host.is_some() {
pool_config.max_connections_per_host = limits_config.max_connections_per_host;
}
if limits_config.max_in_flight_requests.is_some() {
pool_config.max_in_flight_requests = limits_config.max_in_flight_requests;
}
if limits_config.max_in_flight_requests_per_origin.is_some() {
pool_config.max_in_flight_requests_per_origin =
limits_config.max_in_flight_requests_per_origin;
}
if limits_config.max_idle_connections.is_some() {
pool_config.max_idle_connections = limits_config.max_idle_connections;
}
if limits_config.max_idle_connections_per_host.is_some() {
pool_config.max_idle_connections_per_host =
limits_config.max_idle_connections_per_host;
}
if limits_config.idle_timeout.is_some() {
pool_config.idle_timeout = limits_config.idle_timeout;
}
}
#[cfg(any(
feature = "transport-http1",
feature = "transport-http2",
feature = "advanced-routing"
))]
let lifecycle = Arc::new(LifecycleConfig::from_policy(
self.physical_connection_policy,
self.transport_io_timeout,
transport_metrics.clone(),
));
// Shared builder policy for the persistent per-client Hyper
// singletons below (standard, direct, UDS, custom dialer). The cached
// and isolated route clients resolve the same policy at use time from
// the live pool, so the two families never diverge.
#[cfg(any(
feature = "transport-http1",
feature = "transport-http2",
feature = "advanced-routing"
))]
let persistent_policy = HyperClientPolicy::new(
self.retry_canceled_requests,
pool_config.idle_timeout,
pool_config.effective_max_idle_per_host(),
enabler,
);
#[cfg(feature = "cookies")]
let cookie_jar = self.cookie_jar.unwrap_or_default();
let automatic_decompression = self.automatic_decompression.unwrap_or(true);
#[cfg(feature = "tls-rustls")]
let tls_config_result = self
.tls_config
.clone()
.unwrap_or_default()
.build_rustls_config();
#[cfg(feature = "tls-rustls")]
let tls_config_error = tls_config_result
.as_ref()
.err()
.map(|error| format!("failed to build TLS config: {error}"));
// `Http3Only` is strict direct-QUIC and needs no Hyper client.
// `Auto { allow_http3: true }` needs both: H1/H2 for discovery and
// safe fallback plus QUIC for discovered routes. All other policies
// need Hyper only.
#[cfg(feature = "tls-rustls")]
let hyper_client = if matches!(
self.http_version_policy,
crate::HttpVersionPolicy::Http3Only
) || tls_config_error.is_some()
{
None
} else if let Ok(rc) = tls_config_result {
let https = build_standard_connector(rc, enabler);
let connect_timeout = self.timeout.as_ref().and_then(|t| t.connect);
Some(build_hyper_client(
https,
&persistent_policy,
connect_timeout,
&lifecycle,
))
} else {
None
};
#[cfg(all(
not(feature = "tls-rustls"),
any(feature = "transport-http1", feature = "transport-http2")
))]
let hyper_client = if matches!(
self.http_version_policy,
crate::HttpVersionPolicy::Http3Only
) {
None
} else {
let connector = build_standard_connector(enabler);
let connect_timeout = self.timeout.as_ref().and_then(|t| t.connect);
Some(build_hyper_client(
connector,
&persistent_policy,
connect_timeout,
&lifecycle,
))
};
#[cfg(feature = "http3")]
let h3_connector = if enabler.use_http3() {
crate::transport::http3::H3Connector::with_datagram_route(
self.tls_config.clone(),
&pool_config,
Some(transport_metrics.clone()),
self.datagram_dialer.clone(),
)
.ok()
} else {
None
};
// Build the direct connector client for advanced socket options / local
// address binding. This uses a custom connector path instead of the
// standard hyper-rustls connector.
#[cfg(feature = "advanced-routing")]
let stored_direct_config = self.direct_connector_config.clone();
#[cfg(feature = "advanced-routing")]
let direct_client = if let Some(dc_config) = self.direct_connector_config {
let connect_timeout = self.timeout.as_ref().and_then(|t| t.connect);
// Build a TLS connector for HTTPS through the direct path.
#[cfg(feature = "tls-rustls")]
let tls_connector = match self
.tls_config
.clone()
.unwrap_or_default()
.build_rustls_config()
{
Ok(rc) => Some(tokio_rustls::TlsConnector::from(Arc::new(
configure_tls_alpn(
rc,
crate::http_version::HttpVersionPolicyEnabler::from_policy(
self.http_version_policy,
),
),
))),
Err(_) => None,
};
#[cfg(not(feature = "tls-rustls"))]
let tls_connector = None;
let direct_connector =
crate::transport::direct_connector::DirectConnector::with_metrics(
dc_config,
tls_connector,
transport_metrics.clone(),
);
Some(build_hyper_client(
direct_connector,
&persistent_policy,
connect_timeout,
&lifecycle,
))
} else {
None
};
// The UDS path is accepted on all platforms (see `uds_path`) but
// only consumed on Unix; without this the field is never read on
// non-Unix targets.
#[cfg(all(not(unix), feature = "advanced-routing"))]
let _ = &self.uds_path;
#[cfg(all(unix, feature = "advanced-routing"))]
let uds_client = self.uds_path.clone().map(|path| {
#[cfg(feature = "tls-rustls")]
let tls_connector = self
.tls_config
.clone()
.unwrap_or_default()
.build_rustls_config()
.ok()
.map(|rc| {
tokio_rustls::TlsConnector::from(Arc::new(configure_tls_alpn(
rc,
crate::http_version::HttpVersionPolicyEnabler::from_policy(
self.http_version_policy,
),
)))
});
#[cfg(not(feature = "tls-rustls"))]
let tls_connector = None;
let connector = crate::transport::uds::UdsConnector::with_metrics(
path,
tls_connector,
transport_metrics.clone(),
);
let connect_timeout = self.timeout.as_ref().and_then(|timeout| timeout.connect);
build_hyper_client(connector, &persistent_policy, connect_timeout, &lifecycle)
});
#[cfg(feature = "advanced-routing")]
let custom_client = if let Some(dialer) = self.dialer.clone() {
#[cfg(feature = "tls-rustls")]
let custom_config = match self
.tls_config
.clone()
.unwrap_or_default()
.build_rustls_config()
{
Ok(config) => Some(config),
Err(e) => {
#[cfg(feature = "tracing")]
tracing::warn!("eggfetch: custom-route TLS config failed: {e}");
#[cfg(not(feature = "tracing"))]
let _ = &e;
None
}
};
#[cfg(not(feature = "tls-rustls"))]
let custom_config = Some(());
custom_config.and_then(|custom_config| {
let connector = match build_custom_connector(custom_config, dialer, enabler, None) {
Ok(connector) => connector,
Err(e) => {
// Surface the root cause: returning `None` here falls
// back to the standard route with no diagnostic.
#[cfg(feature = "tracing")]
tracing::warn!("eggfetch: custom connector build failed: {e}");
#[cfg(not(feature = "tracing"))]
let _ = &e;
return None;
}
};
let connect_timeout = self.timeout.as_ref().and_then(|timeout| timeout.connect);
Some(build_hyper_client(
connector,
&persistent_policy,
connect_timeout,
&lifecycle,
))
})
} else {
None
};
let config = ClientConfig {
default_headers: self.default_headers,
user_agent: self.user_agent,
timeout: self.timeout,
#[cfg(feature = "redirects")]
redirect: self.redirect,
auth: self.auth,
#[cfg(feature = "cookies")]
cookie_jar,
automatic_decompression,
max_decoded_body_size: self.max_decoded_body_size,
max_decompression_ratio: self.max_decompression_ratio,
#[cfg(feature = "proxy")]
proxy: self.proxy,
#[cfg(feature = "proxy")]
environment_proxies: self.environment_proxies,
#[cfg(feature = "tls-rustls")]
tls_config: self.tls_config,
#[cfg(feature = "logical-retry")]
retry: self.retry,
retry_canceled_requests: self.retry_canceled_requests,
#[cfg(feature = "advanced-routing")]
dialer: self.dialer,
#[cfg(feature = "advanced-routing")]
uds_configured: self.uds_path.is_some(),
http_version_policy: self.http_version_policy,
};
let pool = Pool::new(pool_config);
Client {
inner: Arc::new(ClientInner {
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
hyper_client,
#[cfg(feature = "advanced-routing")]
custom_client,
#[cfg(feature = "tls-rustls")]
tls_config_error,
#[cfg(feature = "advanced-routing")]
direct_client,
#[cfg(feature = "advanced-routing")]
direct_connector_config: stored_direct_config,
#[cfg(feature = "advanced-routing")]
sni_clients: Mutex::new(BoundedClientCache::new(SNI_CLIENT_CACHE_MAX_ENTRIES)),
#[cfg(feature = "advanced-routing")]
sni_custom_clients: Mutex::new(BoundedClientCache::new(
SNI_CLIENT_CACHE_MAX_ENTRIES,
)),
#[cfg(feature = "advanced-routing")]
resolved_clients: Mutex::new(BoundedClientCache::new(
RESOLVED_CLIENT_CACHE_MAX_ENTRIES,
)),
#[cfg(all(unix, feature = "advanced-routing"))]
uds_client,
#[cfg(feature = "proxy")]
socks_clients: Mutex::new(BoundedClientCache::new(SOCKS_CLIENT_CACHE_MAX_ENTRIES)),
#[cfg(feature = "proxy")]
forward_clients: Mutex::new(BoundedClientCache::new(
FORWARD_CLIENT_CACHE_MAX_ENTRIES,
)),
#[cfg(feature = "proxy")]
connect_clients: Mutex::new(BoundedClientCache::new(
CONNECT_CLIENT_CACHE_MAX_ENTRIES,
)),
config,
pool,
transport_metrics,
#[cfg(any(
feature = "transport-http1",
feature = "transport-http2",
feature = "advanced-routing"
))]
lifecycle,
#[cfg(feature = "http3")]
h3_connector,
#[cfg(feature = "http3")]
alt_svc_state: Arc::new(crate::transport::alt_svc::AltSvcState::new()),
}),
}
}
}
#[cfg(feature = "high-level-url")]
fn parse_url(url_str: &str) -> Result<url::Url> {
// Deliberately do not echo url_str into the error: parsing has
// failed, so redact_url_string would fall back to the raw input
// (potentially containing credentials). url::ParseError renders
// only a static description and never embeds the input.
let url = url::Url::parse(url_str).map_err(|e| Error::InvalidUrl(e.to_string()))?;
if !url.username().is_empty() || url.password().is_some() {
// URL userinfo is both easy to leak through diagnostics and
// surprising for an HTTP client with explicit auth APIs.
return Err(Error::InvalidUrl(
"URL userinfo is not supported; configure authentication explicitly".into(),
));
}
match url.scheme() {
"http" | "https" => Ok(url),
other => Err(Error::Unsupported(format!(
"URL scheme '{other}' is not supported; use http or https"
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "proxy")]
use crate::proxy::ProxyAuth;
use bytes::Bytes;
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
use hyper_util::rt::TokioExecutor;
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
use std::future::{ready, Ready};
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
use std::net::SocketAddr;
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
use std::task::{Context, Poll};
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
use tower_service::Service;
#[test]
fn client_constructs() {
let _client = Client::new();
}
#[test]
fn client_config_debug_redacts_secrets() {
let mut config = ClientConfig::default();
config
.default_headers
.insert("authorization", "Bearer header-secret-token")
.unwrap();
#[cfg(feature = "cookies")]
config
.cookie_jar
.set_default_cookie("session".to_owned(), "jar-secret-token".to_owned())
.expect("valid test cookie");
let debug = format!("{config:?}");
assert!(
!debug.contains("header-secret-token"),
"ClientConfig Debug must not leak header secrets: {debug}"
);
#[cfg(feature = "cookies")]
assert!(
!debug.contains("jar-secret-token"),
"ClientConfig Debug must not leak jar secrets: {debug}"
);
}
#[cfg(feature = "high-level-url")]
#[test]
fn client_default() {
let _client = Client::default();
}
#[cfg(feature = "high-level-url")]
#[test]
fn client_builder() {
let _client = Client::builder().user_agent("test-agent").build();
}
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
#[cfg(feature = "high-level-url")]
#[tokio::test]
async fn standard_connector_preserves_resolver_failure_for_detailed_mapping() {
#[derive(Clone, Copy)]
struct FailingResolver;
impl Service<hyper_util::client::legacy::connect::dns::Name> for FailingResolver {
type Response = std::vec::IntoIter<SocketAddr>;
type Error = std::io::Error;
type Future = Ready<std::result::Result<Self::Response, Self::Error>>;
fn poll_ready(
&mut self,
_: &mut Context<'_>,
) -> Poll<std::result::Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, _: hyper_util::client::legacy::connect::dns::Name) -> Self::Future {
ready(Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"synthetic standard resolver failure",
)))
}
}
let enabler = crate::http_version::HttpVersionPolicyEnabler::from_policy(
HttpVersionPolicy::Http1Only,
);
#[cfg(feature = "tls-rustls")]
let connector = build_standard_connector_with_resolver(
rustls::ClientConfig::builder()
.with_root_certificates(rustls::RootCertStore::empty())
.with_no_client_auth(),
enabler,
FailingResolver,
);
#[cfg(not(feature = "tls-rustls"))]
let connector = build_standard_connector_with_resolver(FailingResolver, enabler);
let client =
hyper_util::client::legacy::Client::builder(TokioExecutor::new()).build(connector);
#[cfg(feature = "tls-rustls")]
let uri = "https://synthetic.example/";
#[cfg(not(feature = "tls-rustls"))]
let uri = "http://synthetic.example/";
let request = http::Request::get(uri)
.body(http_body_util::Empty::<Bytes>::new())
.expect("synthetic request is valid");
let error = client
.request(request)
.await
.expect_err("synthetic resolver should fail");
let context = crate::error::RequestFailureContext::new();
let mapped = crate::transport::direct::map_send_error_with_context(error, Some(&context));
assert_eq!(mapped.kind(), "hyper_client");
let failure = crate::error::RequestFailure::from_context(mapped, &context);
assert_eq!(
failure.network_failure_kind(),
Some(crate::error::NetworkFailureKind::Dns)
);
}
#[test]
fn client_builder_merge_timeout_preserves_other_phases() {
// Phase-specific setters must merge into any existing timeout
// configuration instead of replacing it wholesale.
let client = Client::builder()
.timeout(Timeout::from_secs(30))
.merge_timeout(
Timeout::builder()
.connect(std::time::Duration::from_secs(5))
.build(),
)
.merge_timeout(
Timeout::builder()
.read(std::time::Duration::from_secs(10))
.build(),
)
.build();
let timeout = client.inner.config.timeout.expect("timeout configured");
let d = |secs: u64| std::time::Duration::from_secs(secs);
assert_eq!(timeout.connect, Some(d(5)), "last connect wins");
assert_eq!(timeout.read, Some(d(10)), "last read wins");
assert_eq!(
timeout.write,
Some(d(30)),
"unset phases keep earlier values"
);
assert_eq!(timeout.pool, Some(d(30)));
}
#[test]
fn client_builder_merge_timeout_without_prior_config() {
let client = Client::builder()
.merge_timeout(
Timeout::builder()
.connect(std::time::Duration::from_secs(5))
.build(),
)
.build();
let timeout = client.inner.config.timeout.expect("timeout configured");
assert_eq!(timeout.connect, Some(std::time::Duration::from_secs(5)));
assert_eq!(timeout.read, None);
}
#[cfg(any(feature = "proxy", feature = "http3"))]
#[test]
fn client_builder_danger_accept_invalid_certs_preserves_tls_config() {
let client = Client::builder()
.tls_config(crate::tls::TlsConfig::builder().sni(false).build())
.danger_accept_invalid_certs(true)
.build();
let tls = client
.inner
.config
.tls_config
.as_ref()
.expect("TLS config should be retained");
assert!(!tls.sni_enabled());
assert!(!tls.verify_certificate());
assert!(!tls.verify_hostname());
}
#[test]
#[cfg(feature = "tls-rustls")]
fn tls_root_store_paths_construct() {
// Native roots are environment-dependent; the production connector
// always receives the same policy-built config as direct connectors.
let enabler =
crate::http_version::HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Auto {
allow_http3: false,
});
let config = crate::tls::TlsConfig::default()
.build_rustls_config()
.expect("default TLS policy builds");
let _ = build_standard_connector(config, enabler);
}
#[test]
fn client_builder_http1_only() {
let client = Client::builder()
.http_version_policy(HttpVersionPolicy::Http1Only)
.build();
assert_eq!(
client.inner.config.http_version_policy,
HttpVersionPolicy::Http1Only
);
}
#[test]
fn client_builder_http2_auto() {
let client = Client::builder()
.http_version_policy(HttpVersionPolicy::Auto { allow_http3: false })
.build();
assert_eq!(
client.inner.config.http_version_policy,
HttpVersionPolicy::Auto { allow_http3: false }
);
}
#[test]
#[cfg(feature = "transport-http2")]
fn client_builder_http2_only() {
let client = Client::builder()
.http_version_policy(HttpVersionPolicy::Http2Only)
.build();
assert_eq!(
client.inner.config.http_version_policy,
HttpVersionPolicy::Http2Only
);
}
#[cfg(feature = "high-level-url")]
#[test]
fn connector_builds_for_all_policies() {
for policy in [
HttpVersionPolicy::Http1Only,
HttpVersionPolicy::Auto { allow_http3: false },
#[cfg(feature = "transport-http2")]
HttpVersionPolicy::Http2Only,
#[cfg(feature = "http3")]
HttpVersionPolicy::Http3Only,
] {
let enabler = crate::http_version::HttpVersionPolicyEnabler::from_policy(policy);
if !enabler.use_http3() {
#[cfg(feature = "tls-rustls")]
let config = crate::tls::TlsConfig::default()
.build_rustls_config()
.expect("default TLS policy builds");
#[cfg(feature = "tls-rustls")]
let _ = build_standard_connector(config, enabler);
}
}
}
#[cfg(feature = "transport-http2")]
#[cfg(feature = "high-level-url")]
#[test]
fn custom_connector_alpn_matches_http_version_policy() {
let config = rustls::ClientConfig::builder()
.with_root_certificates(rustls::RootCertStore::empty())
.with_no_client_auth();
let h2_only = configure_tls_alpn(
config.clone(),
crate::http_version::HttpVersionPolicyEnabler::from_policy(
HttpVersionPolicy::Http2Only,
),
);
assert_eq!(h2_only.alpn_protocols, vec![b"h2".to_vec()]);
let auto = configure_tls_alpn(
config,
crate::http_version::HttpVersionPolicyEnabler::from_policy(HttpVersionPolicy::Auto {
allow_http3: false,
}),
);
assert_eq!(
auto.alpn_protocols,
vec![b"h2".to_vec(), b"http/1.1".to_vec()]
);
}
#[cfg(feature = "high-level-url")]
#[test]
fn parse_valid_urls() {
assert!(parse_url("https://example.com").is_ok());
assert!(parse_url("http://localhost:8080").is_ok());
}
#[cfg(feature = "high-level-url")]
#[test]
fn parse_invalid_schemes() {
assert!(parse_url("ftp://example.com").is_err());
assert!(parse_url("file:///tmp/test").is_err());
}
#[cfg(feature = "high-level-url")]
#[test]
fn parse_invalid_urls() {
assert!(parse_url("not a url").is_err());
}
#[cfg(feature = "high-level-url")]
#[test]
fn get_request_builder() {
let client = Client::new();
let builder = client.get("https://example.com").unwrap();
let req = builder.build().unwrap();
assert_eq!(*req.method(), Method::GET);
}
#[cfg(feature = "high-level-url")]
#[test]
fn hyper_canceled_request_retry_defaults_on_and_is_independently_configurable() {
assert!(Client::new().inner.config.retry_canceled_requests);
let strict = Client::builder().retry_canceled_requests(false).build();
assert!(!strict.inner.config.retry_canceled_requests);
#[cfg(feature = "logical-retry")]
assert!(strict.inner.config.retry.is_none());
}
#[cfg(feature = "high-level-url")]
#[test]
fn physical_policy_and_transport_io_timeout_are_separate_from_logical_pooling() {
let client = Client::builder()
.physical_connection_policy(PhysicalConnectionPolicy {
max_live: Some(2),
admission_timeout: Some(Duration::from_secs(1)),
})
.transport_io_timeout(TransportIoTimeout {
read: Some(Duration::from_secs(2)),
write: Some(Duration::from_secs(3)),
})
.build();
assert_eq!(
client.inner.lifecycle.admission_timeout,
Some(Duration::from_secs(1))
);
assert_eq!(
client.inner.lifecycle.io_timeout.read,
Some(Duration::from_secs(2))
);
assert_eq!(
client.inner.lifecycle.io_timeout.write,
Some(Duration::from_secs(3))
);
}
#[cfg(feature = "high-level-url")]
#[test]
fn apply_content_length_empty_body() {
let headers = Headers::new();
let body = crate::body::RequestBody::Empty;
let out = crate::pipeline::apply_content_length(headers, &body).unwrap();
assert_eq!(out.get("content-length").unwrap().to_str().unwrap(), "0");
}
#[cfg(feature = "high-level-url")]
#[test]
fn apply_content_length_bytes_body() {
let headers = Headers::new();
let body = crate::body::RequestBody::from(Bytes::from("hello"));
let out = crate::pipeline::apply_content_length(headers, &body).unwrap();
assert_eq!(out.get("content-length").unwrap().to_str().unwrap(), "5");
}
#[cfg(feature = "high-level-url")]
#[test]
fn apply_content_length_stream_known() {
let headers = Headers::new();
let stream = futures_util::stream::empty::<Result<Bytes>>();
let body = crate::body::RequestBody::from_stream(stream, Some(7));
let out = crate::pipeline::apply_content_length(headers, &body).unwrap();
assert_eq!(out.get("content-length").unwrap().to_str().unwrap(), "7");
}
#[cfg(feature = "high-level-url")]
#[test]
fn apply_content_length_stream_unknown() {
let headers = Headers::new();
let stream = futures_util::stream::empty::<Result<Bytes>>();
let body = crate::body::RequestBody::from_stream(stream, None);
let out = crate::pipeline::apply_content_length(headers, &body).unwrap();
assert!(out.get("content-length").is_none());
}
#[cfg(feature = "high-level-url")]
#[test]
fn apply_content_length_user_matches() {
let mut headers = Headers::new();
headers.insert("content-length", "5").unwrap();
let body = crate::body::RequestBody::from(Bytes::from("hello"));
let out = crate::pipeline::apply_content_length(headers, &body).unwrap();
assert_eq!(out.get("content-length").unwrap().to_str().unwrap(), "5");
}
#[cfg(feature = "high-level-url")]
#[test]
fn apply_content_length_user_mismatch_errors() {
let mut headers = Headers::new();
headers.insert("content-length", "10").unwrap();
let body = crate::body::RequestBody::from(Bytes::from("hello"));
let err = crate::pipeline::apply_content_length(headers, &body).unwrap_err();
assert_eq!(err.kind(), "request_build");
}
#[cfg(feature = "high-level-url")]
#[test]
fn apply_content_length_rejects_invalid_value() {
let mut headers = Headers::new();
headers.insert("content-length", "not-a-number").unwrap();
let body = crate::body::RequestBody::from(Bytes::from("hello"));
let err = crate::pipeline::apply_content_length(headers, &body).unwrap_err();
assert_eq!(err.kind(), "invalid_header_value");
}
#[cfg(feature = "high-level-url")]
#[test]
fn apply_content_length_rejects_unknown_stream_override() {
let mut headers = Headers::new();
headers.insert("content-length", "5").unwrap();
let stream = futures_util::stream::empty::<Result<Bytes>>();
let body = crate::body::RequestBody::from_stream(stream, None);
let err = crate::pipeline::apply_content_length(headers, &body).unwrap_err();
assert_eq!(err.kind(), "request_build");
}
#[cfg(feature = "high-level-url")]
#[test]
fn parse_url_rejects_userinfo_without_echoing_credentials() {
let err = parse_url("https://user:secret@example.com").unwrap_err();
assert_eq!(err.kind(), "invalid_url");
assert!(!err.to_string().contains("secret"));
}
#[cfg(feature = "proxy")]
#[cfg(feature = "high-level-url")]
#[tokio::test]
async fn proxy_auth_conflict_with_header() {
let proxy = Proxy::all("http://proxy.example:8080")
.unwrap()
.auth(ProxyAuth::basic("user", "pass").unwrap());
let client = Client::builder().proxy(proxy).build();
let request = client
.get("http://destination.example")
.unwrap()
.header("proxy-authorization", "Basic dXNlcjpwYXNz")
.build()
.unwrap();
let err = client.send(request).await.unwrap_err();
assert_eq!(err.kind(), "conflicting_auth");
assert!(err.to_string().contains("Proxy-Authorization"));
}
#[cfg(feature = "proxy")]
#[cfg(feature = "high-level-url")]
#[tokio::test]
async fn proxy_auth_no_conflict_without_header() {
let proxy = Proxy::all("http://proxy.example:8080")
.unwrap()
.auth(ProxyAuth::basic("user", "pass").unwrap());
let client = Client::builder().proxy(proxy).build();
let request = client
.get("http://destination.example")
.unwrap()
.build()
.unwrap();
let err = client.send(request).await.unwrap_err();
assert_ne!(err.kind(), "conflicting_auth");
}
#[cfg(feature = "proxy")]
#[cfg(feature = "high-level-url")]
#[tokio::test]
async fn proxy_auth_no_conflict_with_header_only() {
let proxy = Proxy::all("http://proxy.example:8080").unwrap();
let client = Client::builder().proxy(proxy).build();
let request = client
.get("http://destination.example")
.unwrap()
.header("proxy-authorization", "Basic dXNlcjpwYXNz")
.build()
.unwrap();
let err = client.send(request).await.unwrap_err();
assert_ne!(err.kind(), "conflicting_auth");
}
#[cfg(feature = "advanced-routing")]
#[cfg(feature = "high-level-url")]
#[test]
fn resolved_route_key_matrix() {
use crate::transport_hints::ResolvedTarget;
// Keys intentionally lack `Debug`/`Display` (connection identity
// only, never request state), so compare with `==`/`!=`.
let origin = url::Url::parse("http://origin.example/").unwrap();
let target: ResolvedTarget =
ResolvedTarget::new(["127.0.0.1:80".parse::<SocketAddr>().unwrap()]).unwrap();
let base = ResolvedRouteKey::new(&origin, &target, None);
let same = ResolvedRouteKey::new(&origin, &target, None);
assert!(
base == same,
"same origin + same ordered addresses + same SNI must reuse"
);
// Path/query/fragment never participate: same logical origin.
let with_path = url::Url::parse("http://origin.example/a?b=1#f").unwrap();
let other_path = url::Url::parse("http://origin.example/other").unwrap();
assert!(
ResolvedRouteKey::new(&with_path, &target, None)
== ResolvedRouteKey::new(&other_path, &target, None),
"path/query/fragment must not fragment resolved identity"
);
// Explicit default port normalizes to the same origin.
let explicit_default = url::Url::parse("http://origin.example:80/").unwrap();
assert!(
ResolvedRouteKey::new(&origin, &target, None)
== ResolvedRouteKey::new(&explicit_default, &target, None),
"explicit default port must reuse"
);
// HTTP vs HTTPS: logical/TLS origin differs.
let https = url::Url::parse("https://origin.example/").unwrap();
let https_target: ResolvedTarget =
ResolvedTarget::new(["127.0.0.1:443".parse::<SocketAddr>().unwrap()]).unwrap();
assert!(
base != ResolvedRouteKey::new(&https, &https_target, None),
"scheme change must fragment resolved identity"
);
// Host differs: logical Host/TLS identity differs.
let other_host = url::Url::parse("http://other.example/").unwrap();
assert!(
base != ResolvedRouteKey::new(&other_host, &target, None),
"host change must fragment resolved identity"
);
// Effective port differs: origin/socket identity differs.
let other_port = url::Url::parse("http://origin.example:8080/").unwrap();
let other_port_target: ResolvedTarget =
ResolvedTarget::new(["127.0.0.1:8080".parse::<SocketAddr>().unwrap()]).unwrap();
assert!(
base != ResolvedRouteKey::new(&other_port, &other_port_target, None),
"effective port change must fragment resolved identity"
);
// One address differs: physical route differs.
let other_addr: ResolvedTarget =
ResolvedTarget::new(["127.0.0.2:80".parse::<SocketAddr>().unwrap()]).unwrap();
assert!(
base != ResolvedRouteKey::new(&origin, &other_addr, None),
"address change must fragment resolved identity"
);
// Same addresses, different order: attempt/failover order differs.
let ordered: ResolvedTarget = ResolvedTarget::new([
"127.0.0.1:80".parse::<SocketAddr>().unwrap(),
"127.0.0.2:80".parse::<SocketAddr>().unwrap(),
])
.unwrap();
let reordered: ResolvedTarget = ResolvedTarget::new([
"127.0.0.2:80".parse::<SocketAddr>().unwrap(),
"127.0.0.1:80".parse::<SocketAddr>().unwrap(),
])
.unwrap();
assert!(
ResolvedRouteKey::new(&origin, &ordered, None)
!= ResolvedRouteKey::new(&origin, &reordered, None),
"address reorder must fragment resolved identity"
);
// SNI None vs override: TLS identity differs.
assert!(
base != ResolvedRouteKey::new(&origin, &target, Some("sni.example")),
"adding an SNI override must fragment resolved identity"
);
// SNI override A vs B: TLS identity differs.
assert!(
ResolvedRouteKey::new(&origin, &target, Some("a.example"))
!= ResolvedRouteKey::new(&origin, &target, Some("b.example")),
"SNI override change must fragment resolved identity"
);
assert!(
ResolvedRouteKey::new(&origin, &target, Some("a.example"))
== ResolvedRouteKey::new(&origin, &target, Some("a.example")),
"same SNI override must reuse"
);
}
#[cfg(feature = "advanced-routing")]
#[test]
fn resolved_route_key_from_native_origin() {
use crate::http_origin::HttpOrigin;
use crate::transport_hints::ResolvedTarget;
let uri: http::Uri = "http://origin.example/".parse().expect("valid URI");
let origin = HttpOrigin::from_uri(&uri).expect("native origin parses");
let target = ResolvedTarget::new(["127.0.0.1:80".parse::<SocketAddr>().unwrap()]).unwrap();
let base = ResolvedRouteKey::from_origin(&origin, &target, None);
let same = ResolvedRouteKey::from_origin(&origin, &target, None);
// Keys intentionally lack `Debug`/`Display`; compare with `==`/`!=`.
assert!(
base == same,
"same native origin + addresses + SNI must reuse"
);
// Explicit default port shares identity; scheme/host/port changes
// fragment; address order and SNI fragment.
let explicit: http::Uri = "http://origin.example:80/a?b=1".parse().expect("valid URI");
let explicit_origin = HttpOrigin::from_uri(&explicit).expect("origin parses");
assert!(
base == ResolvedRouteKey::from_origin(&explicit_origin, &target, None),
"explicit default port must reuse"
);
let https: http::Uri = "https://origin.example/".parse().expect("valid URI");
let https_origin = HttpOrigin::from_uri(&https).expect("origin parses");
let https_target =
ResolvedTarget::new(["127.0.0.1:443".parse::<SocketAddr>().unwrap()]).unwrap();
assert!(
base != ResolvedRouteKey::from_origin(&https_origin, &https_target, None),
"scheme change must fragment resolved identity"
);
assert!(
base != ResolvedRouteKey::from_origin(&origin, &target, Some("sni.example")),
"SNI override must fragment resolved identity"
);
}
#[cfg(any(feature = "transport-http1", feature = "transport-http2"))]
#[cfg(feature = "high-level-url")]
#[cfg(feature = "advanced-routing")]
#[test]
fn resolved_route_cache_is_bounded() {
use crate::transport::hyper_client::{
BoundedClientCache, RESOLVED_CLIENT_CACHE_MAX_ENTRIES,
};
use crate::transport_hints::ResolvedTarget;
assert_eq!(
RESOLVED_CLIENT_CACHE_MAX_ENTRIES, 64,
"resolved-route cache uses the conservative 64-entry bound"
);
let mut cache: BoundedClientCache<ResolvedRouteKey, usize> =
BoundedClientCache::new(RESOLVED_CLIENT_CACHE_MAX_ENTRIES);
for index in 0..(RESOLVED_CLIENT_CACHE_MAX_ENTRIES + 16) {
let origin =
url::Url::parse(&format!("http://host-{index}.example/")).expect("valid URL");
let target =
ResolvedTarget::new(["127.0.0.1:80".parse::<SocketAddr>().expect("valid addr")])
.expect("non-empty");
cache.insert(ResolvedRouteKey::new(&origin, &target, None), index);
assert!(
cache.len() <= RESOLVED_CLIENT_CACHE_MAX_ENTRIES,
"insertion beyond capacity must not grow the cache"
);
}
assert_eq!(cache.len(), RESOLVED_CLIENT_CACHE_MAX_ENTRIES);
}
}