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
//! An extensible and efficient forward-thinking web server for the future.
//!
//! Kvarn is a rethought web server tailored for the current needs from web application developers.
//!
//! It handles several things for you, including
//! - Content-Type
//! - Compression of body
//! - Correct and performant HTTP/1 and HTTP/2
//! - Common API across HTTP/1 and HTTP/2
//! - Easy integration with HTTP/2 push promises
//! - Five types of extensions, all backed with intuitive macros
//! - Optional encryption with [`rustls`](https://docs.rs/rustls)
//! - Several checks for illegal requests
//! - [`cache-control`](parse::CacheControl::from_cache_control) and [`kvarn-cache-control`](parse::CacheControl::from_kvarn_cache_control) header limits server cache lifetimes
//!
//! # Getting started
//!
//! To get started, configure a [`RunConfig`]. See the example at [`RunConfig::execute`]
//! on how to get a simple web server running.
//!
//! A battle-tested reference implementation can be found at [GitHub](https://github.com/Icelk/kvarn-reference/).
//! It powers my two websites with minimal resource requirements.
//!
//! # Future plans
//!
//! See the [README @ GitHub](https://github.com/Icelk/kvarn/) and [kvarn.org](https://kvarn.org).
// See https://doc.rust-lang.org/beta/unstable-book/language-features/doc-cfg.html & https://github.com/rust-lang/rust/pull/89596
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_cfg_hide))]
#![cfg_attr(docsrs, doc(cfg_hide(feature = "uring")))]
#![deny(
    unreachable_pub,
    missing_debug_implementations,
    missing_docs,
    clippy::pedantic
)]
#![allow(
    // I WANT A LONG fn!
    clippy::too_many_lines,
    // I know what I'm doing with unwraps. They should all be motivated.
    clippy::missing_panics_doc,
    // When a parameter of a function is prefixed due to `#[cfg]` in an fn.
    clippy::used_underscore_binding,
    // Same as ↑.
    clippy::unused_self,
    // When a enum variant has been conditionally compiled away.
    irrefutable_let_patterns,
)]
#![doc(html_favicon_url = "https://kvarn.org/favicon.svg")]
#![doc(html_logo_url = "https://kvarn.org/logo.svg")]
#![doc(html_root_url = "https://doc.kvarn.org/")]
// not using tokio::net::TcpSocket is sometimes weird
#![cfg_attr(
    not(feature = "async-networking"),
    allow(clippy::must_use_candidate, clippy::unused_async)
)]

// Module declaration
pub mod application;
pub mod comprash;
pub mod cors;
pub mod csp;
#[cfg(feature = "handover")]
pub mod ctl;
pub mod encryption;
pub mod error;
pub mod extensions;
pub mod host;
pub mod limiting;
pub mod prelude;
pub mod read;
pub mod shutdown;
#[cfg(all(feature = "uring", feature = "http3"))]
mod uring_udp;
pub mod vary;
pub mod websocket;

use prelude::{chrono::*, internals::*, networking::*, *};
// When user only imports kvarn::* and not kvarn::prelude::*
pub use error::{default as default_error, default_response as default_error_response};
pub use extensions::{Extensions, Id};
pub use read::{file as read_file, file_cached as read_file_cached};

/// Configuration for [`Self::execute`].
/// This mainly consists of an array of [`PortDescriptor`]s.
///
/// It also allows control of [handover](https://kvarn.org/shutdown-handover.).
///
/// Will bind a [`TcpListener`] on every `port` added using [`Self::bind`]
///
/// > This ↑ will change when HTTP/3 support arrives, then Udp will also be used.
///
/// # Examples
///
/// See [`Self::execute`] as it uses this, created by a macro invocation.
///
/// ```
/// # use kvarn::prelude::*;
/// # async {
/// let host = Host::unsecure("localhost", "web", Extensions::default(), host::Options::default());
/// let data = HostCollection::builder().insert(host).build();
/// let port_descriptor = PortDescriptor::new(8080, data);
///
/// let config = RunConfig::new()
///     .bind(port_descriptor)
///     .set_ctl_path("/run/kvarn-instance-1.sock");
/// config.execute().await.shutdown();
/// # };
/// ```
#[derive(Debug)]
#[must_use = "must start a server if creating a config"]
pub struct RunConfig {
    ports: Vec<PortDescriptor>,
    #[cfg(feature = "handover")]
    ctl: bool,
    #[cfg(feature = "handover")]
    ctl_path: Option<PathBuf>,

    #[cfg(feature = "handover")]
    plugins: ctl::Plugins,
}
impl RunConfig {
    /// Creates an empty [`RunConfig`].
    pub fn new() -> Self {
        RunConfig {
            ports: vec![],
            #[cfg(feature = "handover")]
            ctl: true,
            #[cfg(feature = "handover")]
            ctl_path: None,

            #[cfg(feature = "handover")]
            plugins: ctl::Plugins::default(),
        }
    }

    /// Adds a [`PortDescriptor`] to the Kvarn server.
    pub fn bind(mut self, port: PortDescriptor) -> Self {
        self.ports.push(port);
        self
    }
    /// Disables [handover](https://kvarn.org/shutdown-handover.)
    /// and [ctl](https://kvarn.org/ctl/)
    /// for the instance of Kvarn.
    ///
    /// This can enable multiple Kvarn servers to run on the same machine.
    #[cfg(feature = "handover")]
    pub fn disable_ctl(mut self) -> Self {
        self.ctl = false;
        self
    }
    /// Sets the path of the socket where the [handover](https://kvarn.org/shutdown-handover.)
    /// and [ctl](https://kvarn.org/ctl/) is managed.
    ///
    /// By default, this is `/run/user/<uid>/kvarn.sock` for users and `/run/kvarn.sock` for root
    /// users.
    ///
    /// This can enable multiple Kvarn servers to run on the same machine.
    /// If each application (as in an use for Kvarn) has it's own path, multiple can coexist.
    #[cfg(feature = "handover")]
    pub fn set_ctl_path(mut self, path: impl AsRef<Path>) -> Self {
        self.ctl_path = Some(path.as_ref().to_path_buf());
        self
    }
    /// Add `plugin` to be executed when a command with `name` is received from `kvarnctl`.
    ///
    /// Adding multiple with the same name overrides the old one.
    ///
    /// See [`ctl::Plugins`] for the default [`ctl::Plugin`]s that are added.
    #[cfg(feature = "handover")]
    pub fn add_plugin(mut self, name: impl AsRef<str>, plugin: ctl::Plugin) -> Self {
        self.plugins.add_plugin(name, plugin);
        self
    }

    /// Run the Kvarn web server on `ports`.
    ///
    /// This is the last step in getting Kvarn spinning.
    /// You can interact with the caches through the [`Host`] and [`HostCollection`] you created, and
    /// the returned [`shutdown::Manager`], if you have the `graceful-shutdown` feature enabled.
    ///
    /// # Examples
    ///
    /// Will start a bare-bones web server on port `8080`, using the dir `web` to serve files.
    ///
    /// > **Note:** it uses `web` to serve files only if the feature `fs` is enabled. Place them in `web/public`
    /// > to access them in your user-agent.
    /// > It's done this way to enable you to have domain-specific files not being public to the web,
    /// > and for a place to store other important files. Kvarn extensions' template system will in this case
    /// > read template files from `web/templates`.
    ///
    /// ```no_run
    /// use kvarn::prelude::*;
    ///
    /// # async {
    /// // Create a host with hostname "localhost", serving files from directory "./web/public/", with the default extensions and the default options.
    /// let host = Host::unsecure("localhost", "web", Extensions::default(), host::Options::default());
    /// // Create a set of virtual hosts (`HostCollection`) with `host` as the default.
    /// let data = HostCollection::builder().insert(host).build();
    /// // Bind port 8080 with `data`.
    /// let port_descriptor = PortDescriptor::new(8080, data);
    ///
    /// // Run with the configured ports.
    /// let shutdown_manager = run_config![port_descriptor].execute().await;
    /// // Waits for shutdown.
    /// shutdown_manager.wait().await;
    /// # };
    /// ```
    pub async fn execute(self) -> Arc<shutdown::Manager> {
        #[cfg(feature = "async-networking")]
        use socket2::{Domain, Protocol, Type};

        let RunConfig {
            ports,
            #[cfg(feature = "handover")]
            ctl,
            #[cfg(feature = "handover")]
            ctl_path,
            #[cfg(feature = "handover")]
            plugins,
        } = self;
        info!("Starting server on {} ports.", ports.len());

        let len = ports.len();
        let mut shutdown_manager = shutdown::Manager::new(len);

        #[cfg(feature = "handover")]
        let ports_clone = Arc::new(ports.clone());

        // the number of threads to accept connections from
        // since uring is single-threaded, we spawn `instances` number of threads with
        // single-threaded executors
        #[cfg(feature = "uring")]
        let instances = std::thread::available_parallelism()
            .map(std::num::NonZeroUsize::get)
            .unwrap_or(16);
        #[cfg(not(feature = "uring"))]
        let instances = 1;

        // artefact from ↑. When not uring, the outer vec is 1 in length
        let mut all_listeners: Vec<Vec<(AcceptManager, Arc<PortDescriptor>)>> =
            Vec::with_capacity(instances);

        let ports: Vec<_> = ports.into_iter().map(Arc::new).collect();

        for _ in 0..instances {
            let mut listeners = Vec::new();
            #[cfg(feature = "async-networking")]
            for descriptor in &ports {
                fn create_listener(
                    create_socket: impl Fn() -> socket2::Socket,
                    #[allow(unused_variables)] tcp: bool,
                    address: SocketAddr,
                    shutdown_manager: &mut shutdown::Manager,
                    #[allow(unused_variables)] descriptor: &PortDescriptor,
                ) -> AcceptManager {
                    let socket = create_socket();
                    // match MIO's settings
                    socket
                        .set_nonblocking(true)
                        .expect("Failed to set `nonblocking` for socket.");
                    let _ = socket.set_cloexec(true);
                    #[cfg(all(unix, not(target_os = "solaris"), not(target_os = "illumos")))]
                    {
                        if socket.set_reuse_address(true).is_err()
                            || socket.set_reuse_port(true).is_err()
                        {
                            error!("Failed to set reuse address/port. This is needed for graceful shutdown handover.");
                        }
                    }
                    socket
                        .bind(&address.into())
                        .expect("Failed to bind address");

                    // wrap listener
                    #[cfg(all(feature = "http3", not(feature = "uring")))]
                    // #[cfg(feature = "http3")]
                    if !tcp {
                        return shutdown_manager.add_listener(shutdown::Listener::Udp(
                            h3_quinn::Endpoint::new(
                                h3_quinn::quinn::EndpointConfig::default(),
                                Some(h3_quinn::quinn::ServerConfig::with_crypto(
                                    descriptor.server_config.clone().unwrap(),
                                )),
                                socket.into(),
                                h3_quinn::quinn::default_runtime().unwrap(),
                            )
                            .unwrap(),
                        ));
                    }
                    #[cfg(all(feature = "http3", feature = "uring"))]
                    if !tcp {
                        let endpoint = h3_quinn::Endpoint::new_with_abstract_socket(
                            h3_quinn::quinn::EndpointConfig::default(),
                            Some(h3_quinn::quinn::ServerConfig::with_crypto(
                                descriptor.server_config.clone().unwrap(),
                            )),
                            uring_udp::UringUdpSocket::new(
                                tokio_uring::net::UdpSocket::from_std(socket.into()),
                                address.is_ipv4(),
                            )
                            .expect("failed to change socket settings"),
                            h3_quinn::quinn::default_runtime().unwrap(),
                        )
                        .unwrap();
                        return shutdown_manager.add_listener(shutdown::Listener::Udp(endpoint));
                    }

                    socket
                        .listen(1024)
                        .expect("Failed to listen on bound address.");

                    #[cfg(feature = "uring")]
                    let listener = tokio_uring::net::TcpListener::from_std(socket.into());
                    #[cfg(not(feature = "uring"))]
                    let listener = TcpListener::from_std(socket.into()).unwrap();

                    shutdown_manager.add_listener(shutdown::Listener::Tcp(listener))
                }

                if matches!(descriptor.version, BindIpVersion::V4 | BindIpVersion::Both) {
                    let listener = create_listener(
                        || {
                            socket2::Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))
                                .expect("Failed to create a new IPv4 socket configuration")
                        },
                        true,
                        SocketAddr::new(IpAddr::V4(net::Ipv4Addr::UNSPECIFIED), descriptor.port),
                        &mut shutdown_manager,
                        descriptor,
                    );
                    listeners.push((listener, Arc::clone(descriptor)));
                }
                if matches!(descriptor.version, BindIpVersion::V6 | BindIpVersion::Both) {
                    let listener = create_listener(
                        || {
                            socket2::Socket::new(Domain::IPV6, Type::STREAM, Some(Protocol::TCP))
                                .expect("Failed to create a new IPv6 socket configuration")
                        },
                        true,
                        SocketAddr::new(IpAddr::V6(net::Ipv6Addr::UNSPECIFIED), descriptor.port),
                        &mut shutdown_manager,
                        descriptor,
                    );
                    listeners.push((listener, Arc::clone(descriptor)));
                }
                #[cfg(feature = "http3")]
                if descriptor.server_config.is_some() {
                    if matches!(descriptor.version, BindIpVersion::V4 | BindIpVersion::Both) {
                        let listener = create_listener(
                            || {
                                socket2::Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))
                                    .expect("Failed to create a new IPv4 socket configuration")
                            },
                            false,
                            SocketAddr::new(
                                IpAddr::V4(net::Ipv4Addr::UNSPECIFIED),
                                descriptor.port,
                            ),
                            &mut shutdown_manager,
                            descriptor,
                        );
                        listeners.push((listener, Arc::clone(descriptor)));
                    }
                    if matches!(descriptor.version, BindIpVersion::V6 | BindIpVersion::Both) {
                        let listener = create_listener(
                            || {
                                socket2::Socket::new(Domain::IPV6, Type::DGRAM, Some(Protocol::UDP))
                                    .expect("Failed to create a new IPv6 socket configuration")
                            },
                            false,
                            SocketAddr::new(
                                IpAddr::V6(net::Ipv6Addr::UNSPECIFIED),
                                descriptor.port,
                            ),
                            &mut shutdown_manager,
                            descriptor,
                        );
                        listeners.push((listener, Arc::clone(descriptor)));
                    }
                }
            }
            #[cfg(not(feature = "async-networking"))]
            for descriptor in &ports {
                if matches!(descriptor.version, BindIpVersion::V4 | BindIpVersion::Both) {
                    let listener = TcpListener::bind(SocketAddr::new(
                        IpAddr::V4(net::Ipv4Addr::UNSPECIFIED),
                        descriptor.port,
                    ))
                    .expect("Failed to bind to IPv4");
                    listeners.push((
                        shutdown_manager.add_listener(shutdown::Listener::Tcp(listener)),
                        Arc::clone(descriptor),
                    ));
                }
                if matches!(descriptor.version, BindIpVersion::V6 | BindIpVersion::Both) {
                    let listener = TcpListener::bind(SocketAddr::new(
                        IpAddr::V6(net::Ipv6Addr::UNSPECIFIED),
                        descriptor.port,
                    ))
                    .expect("Failed to bind to IPv6");
                    listeners.push((
                        shutdown_manager.add_listener(shutdown::Listener::Tcp(listener)),
                        Arc::clone(descriptor),
                    ));
                }
            }
            all_listeners.push(listeners);
        }

        #[cfg(feature = "handover")]
        let handover_path = ctl_path.unwrap_or_else(ctl::socket_path);

        #[cfg(feature = "graceful-shutdown")]
        {
            shutdown_manager.handover_socket_path = Some(handover_path.clone());
        }

        let shutdown_manager = shutdown_manager.build();

        #[cfg(feature = "handover")]
        if ctl {
            // make sure we shut down before listening
            #[cfg(any(
                not(feature = "graceful-shutdown"),
                target_os = "illumos",
                target_os = "solaris"
            ))]
            ctl::listen(
                plugins,
                ports_clone,
                Arc::clone(&shutdown_manager),
                handover_path,
            )
            .await;
        }

        #[cfg(feature = "uring")]
        info!("Starting {instances} threads with an executor and listener each.");
        #[cfg(feature = "uring")]
        for (n, listeners) in all_listeners.into_iter().enumerate() {
            for (listener, descriptor) in listeners {
                let shutdown_manager = Arc::clone(&shutdown_manager);
                std::thread::spawn(move || {
                    tokio_uring::start(async move {
                        accept(listener, descriptor, &shutdown_manager, n == 0)
                            .await
                            .expect("failed to accept message");
                        shutdown_manager.wait().await;
                    });
                });
            }
        }
        #[cfg(not(feature = "uring"))]
        {
            let listeners = all_listeners.into_iter().next().unwrap();
            for (listener, descriptor) in listeners {
                let shutdown_manager = Arc::clone(&shutdown_manager);
                let future = async move {
                    accept(listener, descriptor, &shutdown_manager, true)
                        .await
                        .expect("Failed to accept message!");
                };

                let _task = spawn(future).await;
            }
        }
        #[cfg(feature = "handover")]
        if ctl {
            #[cfg(all(
                feature = "graceful-shutdown",
                not(target_os = "illumos"),
                not(target_os = "solaris")
            ))]
            ctl::listen(
                plugins,
                ports_clone,
                Arc::clone(&shutdown_manager),
                handover_path,
            )
            .await;
        }

        shutdown_manager
    }
}
impl Default for RunConfig {
    fn default() -> Self {
        Self::new()
    }
}
/// Creates a [`RunConfig`] from [`PortDescriptor`]s.
/// This allows you to configure the [`RunConfig`] and then [`RunConfig::execute`] the server.
///
/// # Examples
///
/// ```
/// # use kvarn::prelude::*;
/// # let host = Host::unsecure("localhost", "web", Extensions::default(), host::Options::default());
/// # let data = HostCollection::builder().insert(host).build();
/// # let port1 = PortDescriptor::new(8080, Arc::clone(&data));
/// # let port2 = PortDescriptor::new(8081, data);
/// let server = run_config!(port1, port2);
#[macro_export]
macro_rules! run_config {
    ($($port_descriptor:expr),+ $(,)?) => {
        $crate::RunConfig::new()$(.bind($port_descriptor))+
    };
}

macro_rules! ret_log_app_error {
    ($e:expr) => {
        match $e {
            Err(err) => {
                if let application::Error::ClientRefusedResponse = &err {
                    return Ok(());
                }
                error!("An error occurred while sending a response. {:?}", &err);
                return Err(err.into());
            }
            Ok(val) => val,
        }
    };
}

/// Spawn a task.
///
/// **Must be awaited once.** The second await waits for the value to resolve.
///
/// Use this instead of [`tokio::spawn`] in extensions, since Kvarn's futures can have different
/// requirements based on the [chosen features](https://kvarn.org/cargo-features.html).
#[cfg(feature = "uring")]
#[allow(clippy::unused_async)]
#[inline]
pub async fn spawn<T: 'static>(task: impl Future<Output = T> + 'static) -> impl Future<Output = T> {
    let handle = tokio_uring::spawn(task);
    async move { handle.await.unwrap() }
}
/// Spawn a task.
///
/// **Must be awaited once.** The second await waits for the value to resolve.
///
/// Use this instead of [`tokio::spawn`] in extensions, since Kvarn's futures can have different
/// requirements based on the [chosen features](https://kvarn.org/cargo-features.html).
#[cfg(any(not(feature = "async-networking"), not(feature = "uring")))]
#[allow(clippy::unused_async)]
#[inline]
pub async fn spawn<T: Send + 'static>(
    task: impl Future<Output = T> + Send + 'static,
) -> impl Future<Output = T> {
    #[cfg(not(feature = "async-networking"))]
    {
        task
    }
    #[cfg(feature = "async-networking")]
    {
        let handle = tokio::spawn(task);
        async move { handle.await.unwrap() }
    }
}

/// An incoming connection, before it's wrapped with HTTP.
#[derive(Debug)]
pub enum Incoming {
    /// Used for HTTP/1 & HTTP/2
    Tcp(TcpStream),
    /// Used for HTTP/3
    #[cfg(feature = "http3")]
    Udp(h3_quinn::quinn::Connection),
}
async fn accept(
    mut listener: AcceptManager,
    descriptor: Arc<PortDescriptor>,
    shutdown_manager: &Arc<shutdown::Manager>,
    first: bool,
) -> Result<(), io::Error> {
    let local_addr = listener.inner().local_addr();
    if first {
        info!(
            "Started listening on port {} using {}/{}",
            local_addr.port(),
            match listener.inner() {
                shutdown::Listener::Tcp(_) => "TCP",
                #[cfg(feature = "http3")]
                shutdown::Listener::Udp(_) => "QUIC",
            },
            if local_addr.is_ipv4() { "IPv4" } else { "IPv6" }
        );
    }

    let mut fails_without_accepting = 0usize;
    let fails_without_accepting_threshold = 100;
    loop {
        let (stream, addr) = match listener.accept(shutdown_manager).await {
            AcceptAction::Shutdown => {
                if first {
                    info!(
                        "Closing listener on port {} with {}",
                        local_addr.port(),
                        if local_addr.is_ipv4() { "IPv4" } else { "IPv6" }
                    );
                }
                return Ok(());
            }
            AcceptAction::AcceptTcp(result) => match result {
                Ok((stream, addr)) => (Incoming::Tcp(stream), addr),
                Err(err) => {
                    #[cfg(feature = "graceful-shutdown")]
                    let connections = format!(
                        " {} current connections.",
                        shutdown_manager.get_connecions()
                    );
                    #[cfg(not(feature = "graceful-shutdown"))]
                    let connections = "";

                    // An error occurred
                    error!("Failed to accept() on TCP listener.{connections}");
                    fails_without_accepting += 1;

                    if fails_without_accepting > fails_without_accepting_threshold {
                        return Err(err);
                    }
                    continue;
                }
            },
            #[cfg(feature = "http3")]
            AcceptAction::AcceptUdp(result) => match result {
                Ok(stream) => {
                    let addr = stream.remote_address();
                    (Incoming::Udp(stream), addr)
                }
                Err(err) => {
                    if err.kind() == io::ErrorKind::TimedOut {
                        continue;
                    }
                    #[cfg(feature = "graceful-shutdown")]
                    let connections = format!(
                        " {} current connections.",
                        shutdown_manager.get_connecions()
                    );
                    #[cfg(not(feature = "graceful-shutdown"))]
                    let connections = "";

                    // An error occurred
                    error!("Failed to accept() on UDP listener.{connections}");

                    fails_without_accepting += 1;

                    if fails_without_accepting > fails_without_accepting_threshold {
                        return Err(err);
                    }
                    continue;
                }
            },
        };

        fails_without_accepting = 0;

        debug!(
            "Accepting stream from {addr:?}: {}",
            match stream {
                Incoming::Tcp(_) => "TCP",
                #[cfg(feature = "http3")]
                Incoming::Udp(_) => "QUIC",
            }
        );

        match descriptor.data.limiter().register(addr.ip()) {
            LimitAction::Drop => {
                drop(stream);
                return Ok(());
            }
            LimitAction::Send | LimitAction::Passed => {}
        }

        let descriptor = Arc::clone(&descriptor);

        #[cfg(feature = "graceful-shutdown")]
        let shutdown_manager = Arc::clone(shutdown_manager);
        let _task = spawn(async move {
            #[cfg(feature = "graceful-shutdown")]
            shutdown_manager.add_connection();
            let _result = handle_connection(stream, addr, descriptor, || {
                #[cfg(feature = "async-networking")]
                {
                    #[cfg(feature = "graceful-shutdown")]
                    {
                        !shutdown_manager.get_shutdown(threading::Ordering::Relaxed)
                    }
                    #[cfg(not(feature = "graceful-shutdown"))]
                    {
                        true
                    }
                }
                #[cfg(not(feature = "async-networking"))]
                {
                    false
                }
            })
            .await;
            #[cfg(feature = "graceful-shutdown")]
            shutdown_manager.remove_connection();
        })
        .await;
    }
}

/// Handles a single connection. This includes encrypting it, extracting the HTTP header information,
/// optionally (HTTP/2 & HTTP/3) decompressing them, and passing the request to [`handle_cache()`].
/// It will also recognize which host should handle the connection.
///
/// Here, both [layer 2](https://kvarn.org/pipeline.#layer-2--encryption)
/// and [layer 3](https://kvarn.org/pipeline.#layer-3--http)
/// are handled.
///
/// # Errors
///
/// Will pass any errors from reading the request, making a TLS handshake, and writing the response.
/// See [`handle_cache()`] and [`handle_request()`]; errors from them are passed up, through this fn.
pub async fn handle_connection(
    stream: Incoming,
    address: SocketAddr,
    descriptor: Arc<PortDescriptor>,
    mut continue_accepting: impl FnMut() -> bool,
) -> io::Result<()> {
    let (mut http, sni, version) = match stream {
        Incoming::Tcp(stream) => {
            // LAYER 2
            #[cfg(feature = "https")]
            let encrypted = {
                encryption::Encryption::new_tcp(stream, descriptor.server_config.clone())
                    .await
                    .map_err(|err| match err {
                        encryption::Error::Io(io) => io,
                        encryption::Error::Tls(tls) => {
                            io::Error::new(io::ErrorKind::InvalidData, tls)
                        }
                    })
            }?;
            #[cfg(not(feature = "https"))]
            let encrypted = encryption::Encryption::new_tcp(stream);

            let version = match encrypted.alpn_protocol() {
                Some(b"h2") => Version::HTTP_2,
                None | Some(b"http/1.1") => Version::HTTP_11,
                Some(b"http/1.0") => Version::HTTP_10,
                Some(b"http/0.9") => Version::HTTP_09,
                Some(proto) => {
                    warn!(
                        "HTTP version not supported. \
                        Something is probably wrong with your alpn config. \
                        Client requested {}",
                        String::from_utf8_lossy(proto)
                    );
                    return Ok(());
                }
            };
            let sni = encrypted.server_name().map(|s| s.to_compact_string());
            debug!("New connection requesting hostname '{sni:?}'");

            // LAYER 3
            let http = application::HttpConnection::new(encrypted, version)
                .await
                .map_err::<io::Error, _>(application::Error::into)?;
            (http, sni, version)
        }
        #[cfg(feature = "http3")]
        Incoming::Udp(stream) => {
            let handshake_data: Box<h3_quinn::quinn::crypto::rustls::HandshakeData> = stream
                .handshake_data()
                .expect("connection is established")
                .downcast()
                .expect("we're using rustls");
            (
                application::HttpConnection::Http3(
                    h3::server::builder()
                        .build(h3_quinn::Connection::new(stream))
                        .await
                        .map_err(application::Error::H3)?,
                ),
                handshake_data.server_name.map(CompactString::from),
                Version::HTTP_3,
            )
        }
    };

    debug!("Accepting requests from {}", address);

    #[allow(unused_variables)]
    let port = descriptor.port();
    #[cfg(feature = "https")]
    let secure = descriptor.server_config.is_some();
    #[cfg(not(feature = "https"))]
    let secure = false;

    #[cfg(feature = "http3")]
    let alt_svc_header = Some(format!("h3=\":{port}\";ma=2592000"));
    #[cfg(not(feature = "http3"))]
    let alt_svc_header: Option<String> = None;
    let alt_svc_header = alt_svc_header.map(|h| Bytes::from(h.into_bytes()));

    while let Ok((mut request, response_pipe)) = http
        .accept(
            descriptor
                .data
                .get_default()
                .map(|host| host.name.as_bytes()),
        )
        .await
    {
        debug!("We got a new request on connection.");
        trace!("Got request {:#?}", request);
        let host = if let Some(host) = descriptor.data.get_from_request(&request, sni.as_deref()) {
            host
        } else {
            debug!(
                "Failed to get host: {}",
                utils::parse::Error::NoHost.as_str()
            );

            let (mut response, body) = utils::split_response(
                default_error(
                    StatusCode::CONFLICT,
                    None,
                    Some(b"The host you're looking for wasn't found."),
                )
                .await,
            );
            response_pipe.ensure_version_and_length(&mut response, body.len());

            let mut body_pipe =
                ret_log_app_error!(response_pipe.send_response(response, false).await);

            ret_log_app_error!(body_pipe.send_with_maybe_close(body, true).await);

            return Ok(());
        };

        match host.limiter.register(address.ip()) {
            LimitAction::Drop => return Ok(()),
            LimitAction::Send => {
                let (mut response, body) = utils::split_response(limiting::get_too_many_requests());
                response_pipe.ensure_version_and_length(&mut response, body.len());

                let mut body_pipe =
                    ret_log_app_error!(response_pipe.send_response(response, false).await);

                ret_log_app_error!(body_pipe.send_with_maybe_close(body, true).await);

                continue;
            }
            LimitAction::Passed => {}
        }
        debug!("Accepting new connection from {} on {}", address, host.name);

        debug_assert!(descriptor.data.get_host(&host.name).is_some());
        let hostname = host.name.clone();
        let moved_host_collection = Arc::clone(&descriptor.data);
        let alt_svc_header = alt_svc_header.clone();
        let future = async move {
            // UNWRAP: This host must be part of the Collection, as we got it from there.
            let host = moved_host_collection.get_host(&hostname).unwrap();
            #[allow(unused_mut)]
            let mut response = handle_cache(&mut request, address, host).await;

            if let Some(alt_svc_header) = alt_svc_header {
                if secure && version != Version::HTTP_3 {
                    response.response.headers_mut().append(
                        HeaderName::from_static("alt-svc"),
                        HeaderValue::from_maybe_shared(alt_svc_header).unwrap(),
                    );
                }
            }

            if let Err(err) = SendKind::Send(response_pipe)
                .send(response, &request, host, address)
                .await
            {
                error!("Got error when writing response: {:?}", err);
            }
            drop(request);
        };

        // When version is HTTP/1, we block the socket if we begin listening to it again.
        match version {
            Version::HTTP_09 | Version::HTTP_10 | Version::HTTP_11 => future.await,
            _ => {
                let _task = spawn(future).await;
            }
        }

        if !continue_accepting() {
            break;
        }
    }
    debug!("Connection finished.");
    http.shutdown().await;

    Ok(())
}

/// How to send data to the client.
///
/// Most often, this is `Send`, but when a push promise is created,
/// this will be `Push`. This can be used by [`extensions::Post`].
#[derive(Debug)]
pub enum SendKind {
    /// Send the response normally.
    Send(application::ResponsePipe),
    /// Send the response as a HTTP/2 push.
    Push(application::PushedResponsePipe),
}
impl SendKind {
    /// Ensures correct version and length (only applicable for HTTP/1 connections)
    /// of a response according to inner enum variants.
    #[inline]
    pub fn ensure_version_and_length<T>(&self, response: &mut Response<T>, len: usize) {
        match self {
            Self::Send(p) => p.ensure_version_and_length(response, len),
            Self::Push(p) => p.ensure_version(response),
        }
    }
    /// Sends the `response` to this pipe.
    ///
    /// # Errors
    ///
    /// returns any errors with sending the data.
    #[inline]
    pub async fn send(
        self,
        response: CacheReply,
        request: &FatRequest,
        host: &Host,
        address: SocketAddr,
    ) -> io::Result<()> {
        let CacheReply {
            mut response,
            identity_body,
            sanitize_data: data,
            future,
        } = response;

        let overriden_len = future.as_ref().and_then(|(_, len)| len.as_ref().copied());
        if let Ok(data) = &data {
            match data.apply_to_response(&mut response, overriden_len) {
                Err(SanitizeError::RangeNotSatisfiable) => {
                    response = default_error(
                        StatusCode::RANGE_NOT_SATISFIABLE,
                        Some(host),
                        Some(b"Range start after end of body"),
                    )
                    .await;
                }
                Err(SanitizeError::UnsafePath) => {
                    response = default_error(StatusCode::BAD_REQUEST, Some(host), None).await;
                }
                Ok(()) => {}
            }
        }

        #[allow(clippy::or_fun_call)] // it's then len, so just compiles down to a int lookup
        let len = overriden_len.unwrap_or(response.body().len());
        self.ensure_version_and_length(&mut response, len);

        let (mut response, body) = utils::split_response(response);

        host.extensions
            .resolve_package(&mut response, request, host, address)
            .await;

        match self {
            SendKind::Send(response_pipe) => {
                // Send response
                let mut body_pipe =
                    ret_log_app_error!(response_pipe.send_response(response, false).await);

                if utils::method_has_response_body(request.method())
                    // bug in several web apps: they send back content on methods which don't allow
                    // it. We just allow all methods now. Except HEAD, there we don't send anything.
                    || (!body.is_empty() && request.method() != Method::HEAD)
                {
                    // Send body
                    ret_log_app_error!(body_pipe.send_with_maybe_close(body, false).await);
                }

                if let Some((mut future, _)) = future {
                    future.call(&mut body_pipe, host).await;
                }

                // Process post extensions
                host.extensions
                    .resolve_post(request, identity_body, &mut body_pipe, address, host)
                    .await;

                // Close the pipe.
                ret_log_app_error!(body_pipe.close().await);
            }
            SendKind::Push(push_pipe) => {
                let send_body =
                    utils::method_has_response_body(request.method()) || !body.is_empty();

                // Send response
                let mut body_pipe = ret_log_app_error!(
                    push_pipe.send_response(response, !send_body && future.is_none())
                );
                if send_body {
                    // Send body
                    ret_log_app_error!(
                        body_pipe
                            .send_with_maybe_close(body, future.is_none())
                            .await
                    );
                }
                if let Some((mut future, _)) = future {
                    future.call(&mut body_pipe, host).await;
                }

                if !send_body {
                    ret_log_app_error!(body_pipe.close().await);
                }
            }
        }
        Ok(())
    }
}

/// The returned data from [`handle_cache`].
///
/// Can be used to get responses from Kvarn without sending a request over HTTP.
pub struct CacheReply {
    /// The response.
    /// Duh.
    pub response: Response<Bytes>,
    /// The response body without compression.
    pub identity_body: Bytes,
    /// The returned value from [`utils::sanitize_request()`].
    ///
    /// Internally used in [`SendKind`] to apply [`utils::CriticalRequestComponents`] to the response.
    pub sanitize_data: Result<utils::CriticalRequestComponents, SanitizeError>,
    /// Must be awaited.
    ///
    /// Can be used for WebSocket connections.
    pub future: Option<(ResponsePipeFuture, Option<usize>)>,
    // also update Debug implementation when adding fields
}
impl Debug for CacheReply {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let mut s = f.debug_struct(utils::ident_str!(CacheReply));

        utils::fmt_fields!(
            s,
            (self.response),
            (self.identity_body),
            (self.sanitize_data),
            (self.future, &"[internal future]".as_clean()),
        );

        s.finish()
    }
}

mod handle_cache_helpers {
    use crate::prelude::*;

    /// Get a [`comprash::CompressedResponse`].
    ///
    /// Handles `sanitize_data` and present extensions.
    pub(super) async fn get_response(
        request: &mut FatRequest,
        host: &Host,
        sanitize_data: &Result<utils::CriticalRequestComponents, SanitizeError>,
        address: SocketAddr,
        overide_uri: Option<&Uri>,
    ) -> (
        comprash::CompressedResponse,
        comprash::ClientCachePreference,
        comprash::ServerCachePreference,
        Option<(ResponsePipeFuture, Option<usize>)>,
        comprash::PathQuery,
    ) {
        let path_query = comprash::PathQuery::from(request.uri());
        let (mut resp, mut client_cache, mut server_cache, compress, future) =
            match sanitize_data {
                Ok(_) => {
                    let path = if host.options.disable_fs {
                        None
                    } else if let Ok(decoded) =
                        percent_encoding::percent_decode_str(request.uri().path()).decode_utf8()
                    {
                        Some(utils::make_path(
                            &host.path,
                            host.options.public_data_dir.as_deref().unwrap_or("public"),
                            // Ok, since Uri's have to start with a `/` (https://github.com/hyperium/http/issues/465).
                            // We also are OK with all Uris, since we did a check on the
                            // incoming and presume all internal extension changes are good.
                            utils::parse::uri(&decoded).unwrap(),
                            None,
                        ))
                    } else {
                        warn!("Invalid percent encoding in path.");
                        None
                    };

                    handle_request(request, overide_uri, address, host, &path).await
                }
                Err(err) => error::sanitize_error_into_response(*err, host).await,
            }
            .into_parts();

        host.extensions
            .resolve_present(
                request,
                &mut resp,
                &mut client_cache,
                &mut server_cache,
                host,
                address,
            )
            .await;

        let extension = match Path::new(request.uri().path())
            .extension()
            .and_then(std::ffi::OsStr::to_str)
        {
            Some(ext) => ext,
            None => match host.options.extension_default.as_ref() {
                Some(ext) => ext.as_str(),
                None => "",
            },
        };
        (
            comprash::CompressedResponse::new(
                resp,
                compress,
                client_cache,
                extension,
                host.options.disable_client_cache,
            ),
            client_cache,
            server_cache,
            future,
            path_query,
        )
    }
    /// Cache `response` if allowed by the other arguments.
    ///
    /// Returns the `response` if it wasn't cached.
    pub(super) fn maybe_cache<T>(
        host: &Host,
        server_cache: comprash::ServerCachePreference,
        path_query: PathQuery,
        response: VariedResponse,
        method: &Method,
        future: &Option<T>,
    ) -> Option<VariedResponse> {
        if future.is_none() {
            if let Some(response_cache) = &host.response_cache {
                // Call `host::Options::status_code_cache_filter`
                let cache_action = (host.options.status_code_cache_filter)(
                    response.first().0.get_identity().status(),
                );

                if server_cache.cache(cache_action, method) {
                    let key = if server_cache.query_matters() {
                        comprash::UriKey::PathQuery(path_query)
                    } else {
                        comprash::UriKey::Path(path_query.into_path())
                    };
                    debug!("Caching uri {:?}!", &key);
                    response_cache.insert_cache_item(key, response);
                    return None;
                }
            }
        } else {
            debug!("Not caching; a Prepare extension has captured. If we cached, it would not be called again.");
        }
        Some(response)
    }
    pub(super) async fn handle_vary_missing(
        request: &mut FatRequest,
        host: &Host,
        sanitize_data: &Result<utils::CriticalRequestComponents, SanitizeError>,
        address: SocketAddr,
        overide_uri: Option<&Uri>,
        uri_key: UriKey,
        params: vary::CacheParams,
    ) -> (
        Arc<(comprash::CompressedResponse, vary::HeaderCollection)>,
        Option<(extensions::ResponsePipeFuture, Option<usize>)>,
    ) {
        let (compressed_response, _, server_cache, future, path_query) =
            get_response(request, host, sanitize_data, address, overide_uri).await;

        // Try to get back varied response. If not there, recreate it, as the
        // match-arm below does.
        let cached = if let Some(cache) = &host.response_cache {
            {
                // inline `UriKey::call_all` because of annoying Rust semantics
                // regarding calling impl Fns. We also had to deal with this in
                // `kvarn::extensions`.
                match cache.get_cache_item(&uri_key).into_option() {
                    Some(t) => (uri_key, Some(t)),
                    None => match uri_key {
                        UriKey::Path(_) => (uri_key, None),
                        UriKey::PathQuery(path_query) => {
                            let uri_key = UriKey::Path(path_query.into_path());
                            let result = cache.get_cache_item(&uri_key).into_option();
                            (uri_key, result)
                        }
                    },
                }
            }
        } else {
            (uri_key, None)
        };
        let arc = match cached {
            (key, Some((resp, lifetime))) => {
                let mut resp = (*resp).clone();
                let a = Arc::clone(resp.push_response(compressed_response, params));
                if let Some(cache) = &host.response_cache {
                    cache.insert(
                        0,
                        lifetime.2.map(|dur| {
                            dur.saturating_sub(
                                (OffsetDateTime::now_utc() - lifetime.0)
                                    .max(time::Duration::ZERO)
                                    .unsigned_abs(),
                            )
                        }),
                        key,
                        resp,
                    );
                }
                a
            }
            (_, None) => {
                let vary_rules = host.vary.rules_from_request(request);

                // SAFETY: The requirements are met; the cache we're storing this is is part of the
                // `host`; the `host` will outlive this struct.
                let varied_response = unsafe {
                    VariedResponse::new(compressed_response, request, vary_rules.as_ref())
                };

                let arc = Arc::clone(varied_response.first());

                handle_cache_helpers::maybe_cache(
                    host,
                    server_cache,
                    path_query,
                    varied_response,
                    request.method(),
                    &future,
                );

                arc
            }
        };
        (arc, future)
    }
}
/// Will handle a single request, check the cache, process if needed, and caches it.
/// This is where the response is sent.
///
/// This is [layer 4](https://kvarn.org/pipeline.#layer-4--caching-and-compression)
pub async fn handle_cache(
    request: &mut FatRequest,
    address: SocketAddr,
    host: &Host,
) -> CacheReply {
    let sanitize_data = utils::sanitize_request(request);

    let overide_uri = host.extensions.resolve_prime(request, host, address).await;

    let uri_key =
        comprash::UriKey::path_and_query(overide_uri.as_ref().unwrap_or_else(|| request.uri()));

    let cached = if let Some(cache) = &host.response_cache {
        match cache.get_cache_item(&uri_key).into_option() {
            Some(t) => (uri_key, Some(t)),
            None => match uri_key {
                UriKey::Path(_) => (uri_key, None),
                UriKey::PathQuery(path_query) => {
                    let uri_key = UriKey::Path(path_query.into_path());
                    let result = cache.get_cache_item(&uri_key).into_option();
                    (uri_key, result)
                }
            },
        }
    } else {
        (uri_key, None)
    };
    #[allow(clippy::single_match_else, clippy::unnested_or_patterns)]
    let (response, identity, future) = match cached {
        (uri_key, Some((resp, (creation, creation_formatted, _))))
            if sanitize_data.is_ok()
                && matches!(request.method(), &Method::GET | &Method::HEAD) =>
        {
            debug!("Found in cache!");

            // Handle `if-modified-since` header.
            let if_modified_since: Option<OffsetDateTime> =
                if host.options.disable_if_modified_since {
                    None
                } else {
                    request
                        .headers()
                        .get("if-modified-since")
                        .and_then(|h| h.to_str().ok())
                        .and_then(|s| {
                            time::PrimitiveDateTime::parse(s, &comprash::HTTP_DATE)
                                .ok()
                                .map(time::PrimitiveDateTime::assume_utc)
                        })
                };

            let client_request_is_fresh = if_modified_since.map_or(false, |timestamp| {
                // - 1s because the sent datetime floors the seconds, so the `creation`
                // datetime is 0-1s ahead.
                timestamp >= creation - 1.seconds()
            });

            // We don't need to check for `host.options.disable_if_modified_since`
            // but `if_modified_since` is `None` and therefore `client_request` is false
            // if the option is enabled, as defined in the if in the `if_modified_since`
            // definition.
            let mut response_data = if client_request_is_fresh {
                let mut response = Response::new(Bytes::new());
                *response.status_mut() = StatusCode::NOT_MODIFIED;
                (response, Bytes::new(), None)
            } else {
                // get the cached response
                let (resp_vary, future) = match resp.get_by_request(request) {
                    Ok(arc) => {
                        let arc = Arc::clone(arc);
                        (arc, None)
                    }
                    // the varied response didn't have any version which matches the request.
                    Err(params) => {
                        // Drop lock during response creation
                        // in a sepparate function as this is a cold path and to reduce the length
                        // of this fn
                        handle_cache_helpers::handle_vary_missing(
                            request,
                            host,
                            &sanitize_data,
                            address,
                            overide_uri.as_ref(),
                            uri_key,
                            params,
                        )
                        .await
                    }
                };
                let (resp, vary) = &*resp_vary;
                // Here, the lock is always (irrelevant of which arm the code runs) dropped, which
                // enables us to do computationally heavy things, such as compression.
                let mut response = if future.as_ref().map_or(false, |(_, len)| len.is_some()) {
                    let body = resp.get_identity().body().clone();
                    utils::empty_clone_response(resp.get_identity()).map(|()| body)
                } else {
                    match resp
                        .clone_preferred(request, &host.compression_options)
                        .await
                    {
                        Err(message) => {
                            error::default(
                                StatusCode::NOT_ACCEPTABLE,
                                Some(host),
                                Some(message.as_bytes()),
                            )
                            .await
                        }
                        Ok(response) => response,
                    }
                };

                vary::apply_header(&mut response, vary);

                let identity_body = Bytes::clone(resp.get_identity().body());

                (response, identity_body, future)
            };
            if !host.options.disable_if_modified_since {
                response_data
                    .0
                    .headers_mut()
                    .insert("last-modified", creation_formatted);
            }
            response_data
        }
        _ => {
            let sanitize_data = &sanitize_data;
            let overide_uri = overide_uri.as_ref();
            let (compressed_response, _, server_cache, future, path_query) =
                handle_cache_helpers::get_response(
                    request,
                    host,
                    sanitize_data,
                    address,
                    overide_uri,
                )
                .await;

            let vary_rules = host.vary.rules_from_request(request);

            // SAFETY: The requirements are met; the cache we're storing this is is part of the
            // `host`; the `host` will outlive this struct.
            let varied_response =
                unsafe { VariedResponse::new(compressed_response, request, vary_rules.as_ref()) };

            let compressed_response = &varied_response.first().0;

            let mut response = if future.as_ref().map_or(false, |(_, len)| len.is_some()) {
                let body = compressed_response.get_identity().body().clone();
                utils::empty_clone_response(compressed_response.get_identity()).map(|()| body)
            } else {
                match compressed_response
                    .clone_preferred(request, &host.compression_options)
                    .await
                {
                    Err(message) => {
                        error::default(
                            StatusCode::NOT_ACCEPTABLE,
                            Some(host),
                            Some(message.as_bytes()),
                        )
                        .await
                    }
                    Ok(response) => response,
                }
            };

            let identity_body = Bytes::clone(compressed_response.get_identity().body());

            let vary = &varied_response.first().1;
            vary::apply_header(&mut response, vary);

            let cache_rejected = handle_cache_helpers::maybe_cache(
                host,
                server_cache,
                path_query,
                varied_response,
                request.method(),
                &future,
            );

            if !host.options.disable_if_modified_since && cache_rejected.is_none() {
                let last_modified = HeaderValue::from_str(
                    &OffsetDateTime::now_utc()
                        .format(&comprash::HTTP_DATE)
                        .expect("failed to format datetime"),
                )
                .expect("We know these bytes are valid.");
                response
                    .headers_mut()
                    .insert("last-modified", last_modified);
            }

            (response, identity_body, future)
        }
    };

    CacheReply {
        response,
        identity_body: identity,
        sanitize_data,
        future,
    }
}

/// Handles a single request and returns response with cache and compress preference.
///
/// This is [layer 5](https://kvarn.org/pipeline.#layer-5--pathing)
pub async fn handle_request(
    request: &mut FatRequest,
    overide_uri: Option<&Uri>,
    address: SocketAddr,
    host: &Host,
    path: &Option<CompactString>,
) -> FatResponse {
    let mut response = None;
    let mut client_cache = None;
    let mut server_cache = None;
    let mut compress = None;
    let mut future = None;

    #[allow(unused_mut)]
    let mut status = None;

    {
        if let Some(resp) = host
            .extensions
            .resolve_prepare(request, overide_uri, host, path, address)
            .await
        {
            let resp = resp.into_parts();
            response.replace(resp.0);
            client_cache.replace(resp.1);
            server_cache.replace(resp.2);
            compress.replace(resp.3);
            if let Some(f) = resp.4 {
                future.replace(f);
            }
        }
    }

    if response.is_none() {
        // path is none if disable_fs is on
        if let Some(path) = path {
            match *request.method() {
                Method::GET | Method::HEAD => {
                    if let Some(content) = read_file(&path, host.file_cache.as_ref()).await {
                        response = Some(Response::new(content));
                    }
                }
                _ => status = Some(StatusCode::METHOD_NOT_ALLOWED),
            }
        }
    }

    let response = match response {
        Some(r) => r,
        None => {
            error::default_response(status.unwrap_or(StatusCode::NOT_FOUND), host, None)
                .await
                .response
        }
    };

    macro_rules! maybe_with {
        ($response: expr, $option: expr, $method: tt) => {
            if let Some(t) = $option {
                $response = $response.$method(t);
            }
        };
    }
    let mut response = FatResponse::cache(response);
    maybe_with!(response, client_cache, with_client_cache);
    maybe_with!(response, server_cache, with_server_cache);
    maybe_with!(response, compress, with_compress);
    maybe_with!(response, future, with_future_and_maybe_len);

    response
}

/// Which version of the [Internet Protocol](https://en.wikipedia.org/wiki/Internet_Protocol)
/// to bind to.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[must_use]
pub enum BindIpVersion {
    /// Bind to IPv4
    V4,
    /// Bind to IPv6
    V6,
    /// Bind to IPv4 and IPv6
    Both,
}

/// Describes port, certificate, and host data for
/// a single port to bind.
///
/// See the note at the bottom of [`Host`] for an explanation
/// about the relationship between [`Self::new`] and [`Self::unsecure`].
#[derive(Clone)]
#[must_use]
pub struct PortDescriptor {
    port: u16,
    #[cfg(feature = "https")]
    server_config: Option<Arc<rustls::ServerConfig>>,
    data: Arc<HostCollection>,
    version: BindIpVersion,
    // also update Debug implementation when adding fields
}
/// Creation and configuration.
///
/// Used when creating a server.
impl PortDescriptor {
    /// Uses the defaults for non-secure HTTP with `host_data`
    pub fn http(host_data: Arc<HostCollection>) -> Self {
        Self {
            port: 80,
            #[cfg(feature = "https")]
            server_config: None,
            data: host_data,
            version: BindIpVersion::Both,
        }
    }
    /// Uses the defaults for secure HTTP, HTTPS, with `host_data`.
    /// Gets a [`rustls::ServerConfig`] from [`HostCollection::make_config()`].
    #[cfg(feature = "https")]
    pub fn https(host_data: Arc<HostCollection>) -> Self {
        Self {
            port: 443,
            server_config: Some(Arc::new(host_data.make_config())),
            data: host_data,
            version: BindIpVersion::Both,
        }
    }
    /// Creates a new descriptor for `port` with `host_data` and an optional [`rustls::ServerConfig`].
    #[cfg(feature = "https")]
    pub fn with_server_config(
        port: u16,
        host_data: Arc<HostCollection>,
        server_config: Option<Arc<rustls::ServerConfig>>,
    ) -> Self {
        Self {
            port,
            server_config,
            data: host_data,
            version: BindIpVersion::Both,
        }
    }
    /// Creates a new descriptor for `port` with `host_data`.
    /// If the feature `https` is enabled, a `rustls::ServerConfig` is created
    /// from the `host_data`.
    pub fn new(port: u16, host_data: Arc<HostCollection>) -> Self {
        Self {
            port,
            #[cfg(feature = "https")]
            server_config: Some(Arc::new(host_data.make_config())),
            data: host_data,
            version: BindIpVersion::Both,
        }
    }
    /// Creates a new non-secure descriptor for `port` with `host_data`.
    /// Does not try to assign a certificate.
    pub fn unsecure(port: u16, host_data: Arc<HostCollection>) -> Self {
        Self {
            port,
            #[cfg(feature = "https")]
            server_config: None,
            data: host_data,
            version: BindIpVersion::Both,
        }
    }
    /// Binds to IPv4 only.
    /// The default is to bind both.
    ///
    /// This disables IPv6 for this port.
    pub fn ipv4_only(mut self) -> Self {
        self.version = BindIpVersion::V4;
        self
    }
    /// Binds to IPv6 only.
    /// The default is to bind both.
    ///
    /// This disables IPv4 for this port.
    pub fn ipv6_only(mut self) -> Self {
        self.version = BindIpVersion::V6;
        self
    }
}
/// Inspection.
///
/// Used in [`ctl::Plugin`]s.
// these return references of the Arc values so they can't escape the Plugins.
// This is just restrictive in case we change the API later.
impl PortDescriptor {
    /// Get the port this description is associated with.
    #[must_use]
    pub fn port(&self) -> u16 {
        self.port
    }
    /// Get a reference to this port's optional TLS config.
    #[cfg(feature = "https")]
    #[must_use]
    pub fn tls_config(&self) -> Option<&rustls::ServerConfig> {
        self.server_config.as_deref()
    }
    /// Get the associated hosts.
    ///
    /// This can be used to remove entries from the response & file cache.
    ///
    /// Remember, this collection can be the same as for any other port descriptor.
    pub fn hosts(&self) -> &HostCollection {
        &self.data
    }
    /// Get the version of the internet protocol (IP) we are listening on
    /// through [`Self::port`].
    pub fn internet_protocol(&self) -> BindIpVersion {
        self.version
    }
}
impl Debug for PortDescriptor {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let mut s = f.debug_struct(utils::ident_str!(PortDescriptor));

        utils::fmt_fields!(
            s,
            (self.port),
            #[cfg(feature = "https")]
            (
                self.server_config,
                &self
                    .server_config
                    .as_ref()
                    .map(|_| "[opaque certificate]".as_clean())
            ),
            (self.data),
            (self.version),
        );

        s.finish()
    }
}

/// The `Request` used within Kvarn.
pub type FatRequest = Request<application::Body>;
/// A `Response` returned by [`handle_request()`].
///
/// Contains all preference information to the lower-level
/// functions. Most things like `content-length`, `content-encoding`,
/// `content-type`, `cache-control`, and server caching will be
/// automatically handled.
#[must_use = "send the response"]
pub struct FatResponse {
    response: Response<Bytes>,
    client: comprash::ClientCachePreference,
    server: comprash::ServerCachePreference,
    compress: comprash::CompressPreference,

    future: Option<(ResponsePipeFuture, Option<usize>)>,
    // also update Debug implementation when adding fields
}
impl FatResponse {
    /// Create a new [`FatResponse`] with `server_cache_preference` advising Kvarn of how to cache the content.
    /// All other preferences are set to `Full` with a `future` of [`None`].
    ///
    /// Choose
    /// - [`comprash::ServerCachePreference::Full`] if the page is one regularly accessed,
    /// - [`comprash::ServerCachePreference::None`] if the page is rarely accessed or if the runtime cost of
    ///   getting the page is minimal.
    /// - [`comprash::ServerCachePreference::QueryMatters`] should be avoided. It should be used when
    ///   you have a page dictated by the query. Consider using a [`Prime`] extension
    ///   to make all requests act as only one of a few queries to increase performance
    ///   by reducing cache size.
    pub fn new(
        response: Response<Bytes>,
        server_cache_preference: comprash::ServerCachePreference,
    ) -> Self {
        Self {
            response,
            client: comprash::ClientCachePreference::Full,
            server: server_cache_preference,
            compress: comprash::CompressPreference::Full,

            future: None,
        }
    }
    /// Create a new [`FatResponse`] with all preferences set to `Full` and no `Future`.
    ///
    /// Use the `with_*` methods to change the defaults.
    pub fn cache(response: Response<Bytes>) -> Self {
        Self::new(response, comprash::ServerCachePreference::Full)
    }
    /// Create a new [`FatResponse`] with all cache preferences set to `None`,
    /// compress preference set to `Full`, and no `Future`.
    ///
    /// Use the `with_*` methods to change the defaults.
    pub fn no_cache(response: Response<Bytes>) -> Self {
        Self {
            response,
            client: comprash::ClientCachePreference::None,
            server: comprash::ServerCachePreference::None,
            compress: comprash::CompressPreference::Full,
            future: None,
        }
    }
    /// Set the inner [`comprash::ClientCachePreference`].
    pub fn with_client_cache(mut self, preference: comprash::ClientCachePreference) -> Self {
        self.client = preference;
        self
    }
    /// Set the inner [`comprash::ServerCachePreference`].
    pub fn with_server_cache(mut self, preference: comprash::ServerCachePreference) -> Self {
        self.server = preference;
        self
    }
    /// Set the inner [`comprash::CompressPreference`].
    pub fn with_compress(mut self, preference: comprash::CompressPreference) -> Self {
        self.compress = preference;
        self
    }
    /// Set the inner `future`.
    pub fn with_future(mut self, future: ResponsePipeFuture) -> Self {
        self.future = Some((future, None));
        self
    }
    /// Set the inner `future` and
    /// overrides the length of the body. Only has an effect on HTTP/1.1 connections. Should be used
    /// with caution.
    ///
    /// This is used in the streaming body extension.
    pub fn with_future_and_len(mut self, future: ResponsePipeFuture, new_len: usize) -> Self {
        self.future = Some((future, Some(new_len)));
        self
    }

    /// See [`Self::with_future_and_len`].
    pub fn with_future_and_maybe_len(
        mut self,
        future: (ResponsePipeFuture, Option<usize>),
    ) -> Self {
        self.future = Some(future);
        self
    }

    /// Set the `content-type` header of the inner response to `content_type`.
    ///
    /// # Panics
    ///
    /// Panics if the display implementation of `content_type` produces illegal bytes for
    /// [`HeaderValue`].
    ///
    /// It's unknown if this can even happen at all.
    /// If it does happen, it's in the [`Mime::params`].
    pub fn with_content_type(mut self, content_type: &Mime) -> Self {
        self.response.headers_mut().insert(
            "content-type",
            // UNWRAP: We know the mime type is a valid HeaderValue.
            HeaderValue::from_maybe_shared::<Bytes>(content_type.to_string().into_bytes().into())
                .unwrap(),
        );
        self
    }

    /// Turn `self` into a tuple of all it's parts.
    #[allow(clippy::type_complexity)] // that's the point
    pub fn into_parts(
        self,
    ) -> (
        Response<Bytes>,
        comprash::ClientCachePreference,
        comprash::ServerCachePreference,
        comprash::CompressPreference,
        Option<(ResponsePipeFuture, Option<usize>)>,
    ) {
        (
            self.response,
            self.client,
            self.server,
            self.compress,
            self.future,
        )
    }
}
impl Debug for FatResponse {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        enum BytesOrStr<'a> {
            Str(&'a str),
            Bytes(&'a [u8]),
        }
        impl<'a> Debug for BytesOrStr<'a> {
            fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
                match self {
                    Self::Str(s) => f.write_str(s),
                    Self::Bytes(_) => f.write_str("[binary data]"),
                }
            }
        }
        let response = utils::empty_clone_response(&self.response);
        let body = if let Ok(s) = str::from_utf8(self.response.body()) {
            BytesOrStr::Str(s)
        } else {
            BytesOrStr::Bytes(self.response.body())
        };
        let response = response.map(|()| body);
        let mut s = f.debug_struct(utils::ident_str!(FatResponse));

        utils::fmt_fields!(
            s,
            (self.response, &response),
            (self.client),
            (self.server),
            (self.compress),
            (self.future, &"[opaque Future]".as_clean()),
        );

        s.finish()
    }
}