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
//! Here, all extensions code is housed.
//!
//! Check out [the page about extensions at kvarn.org](https://kvarn.org/extensions/) for more info.
//!
//! If you want to make new extensions for others to use, make sure to check other extensions,
//! so the priorities are valid. This can be done by using the debug implementation on [`Extensions`].
//! ```
//! # use kvarn::prelude::*;
//! let extensions = Extensions::new();
//! println!("The currently mounted extensions: {:#?}", extensions);
//! ```

use crate::prelude::{internals::*, *};

/// A return type for a `dyn` [`Future`].
///
/// Used as the return type for all extensions,
/// so they can be stored.
#[cfg(feature = "uring")]
pub type RetFut<'a, T> = Pin<Box<(dyn Future<Output = T> + 'a)>>;
/// A return type for a `dyn` [`Future`].
///
/// Used as the return type for all extensions,
/// so they can be stored.
#[cfg(not(feature = "uring"))]
pub type RetFut<'a, T> = Pin<Box<(dyn Future<Output = T> + Send + 'a)>>;
/// Same as [`RetFut`] but also implementing [`Sync`].
///
/// Mostly used for extensions used across yield bounds.
#[cfg(feature = "uring")]
pub type RetSyncFut<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
/// Same as [`RetFut`] but also implementing [`Sync`].
///
/// Mostly used for extensions used across yield bounds.
#[cfg(not(feature = "uring"))]
pub type RetSyncFut<'a, T> = Pin<Box<dyn Future<Output = T> + Send + Sync + 'a>>;

#[cfg(feature = "uring")]
#[doc(hidden)]
pub trait KvarnSendSync {}
#[cfg(feature = "uring")]
impl<T> KvarnSendSync for T {}
#[cfg(not(feature = "uring"))]
#[doc(hidden)]
pub trait KvarnSendSync: Send + Sync {}
#[cfg(not(feature = "uring"))]
impl<T: Send + Sync> KvarnSendSync for T {}

/// A prime extension.
///
/// Can be created using the [`prime!`] macro.
///
/// Requires an object which implements the [`PrimeCall`] trait. See it for details on arguments.
///
/// See [module level documentation](extensions) and [kvarn.org](https://kvarn.org/extensions/) for more info.
pub type Prime = Box<dyn PrimeCall>;
/// Implement this to pass your extension to [`Extensions::add_prime`].
pub trait PrimeCall: KvarnSendSync {
    /// # Arguments
    ///
    /// - An immutable reference to the request.
    /// - An immutable reference to the host this request is to.
    /// - The [`SocketAddr`] of the requester.
    fn call<'a>(
        &'a self,
        request: &'a FatRequest,
        host: &'a Host,
        addr: SocketAddr,
    ) -> RetFut<'a, Option<Uri>>;
}
impl<
        F: for<'a> Fn(&'a FatRequest, &'a Host, SocketAddr) -> RetFut<'a, Option<Uri>> + Send + Sync,
    > PrimeCall for F
{
    fn call<'a>(
        &'a self,
        request: &'a FatRequest,
        host: &'a Host,
        addr: SocketAddr,
    ) -> RetFut<'a, Option<Uri>> {
        self(request, host, addr)
    }
}
/// A prepare extension.
///
/// Keep in mind you have to supply the response content type in the headers. Kvarn defaults to HTML.
/// You also have to handle all the methods (except `HEAD`). So, if you ignore methods, your
/// endpoint will behave the same regardless of if the client sends a `POST` or `GET` request.
///
/// Can be created using the [`prepare!`] macro.
///
/// Requires an object which implements the [`PrepareCall`] trait. See it for details on arguments.
///
/// See [module level documentation](extensions) and [kvarn.org](https://kvarn.org/extensions/) for more info.
pub type Prepare = Box<dyn PrepareCall>;
/// Implement this to pass your extension to [`Extensions::add_prepare_fn`] or
/// [`Extensions::add_prepare_single`].
pub trait PrepareCall: KvarnSendSync {
    /// # Arguments
    ///
    /// - A mutable reference to the request.
    /// - An immutable reference to the host this request is to.
    /// - An [`Option`] of a [`Path`]. See the docs at [`prepare!`] for when this is [`None`].
    /// - The [`SocketAddr`] of the requester.
    fn call<'a>(
        &'a self,
        request: &'a mut FatRequest,
        host: &'a Host,
        path: Option<&'a Path>,
        addr: SocketAddr,
    ) -> RetFut<'a, FatResponse>;
}
impl<
        F: for<'a> Fn(
                &'a mut FatRequest,
                &'a Host,
                Option<&Path>,
                SocketAddr,
            ) -> RetFut<'a, FatResponse>
            + KvarnSendSync,
    > PrepareCall for F
{
    fn call<'a>(
        &'a self,
        request: &'a mut FatRequest,
        host: &'a Host,
        path: Option<&Path>,
        addr: SocketAddr,
    ) -> RetFut<'a, FatResponse> {
        self(request, host, path, addr)
    }
}
/// A present extension.
///
/// Can be created using the [`present!`] macro.
///
/// Requires an object which implements the [`PresentCall`] trait. See it for details on arguments.
///
/// See [module level documentation](extensions) and [kvarn.org](https://kvarn.org/extensions/) for more info.
pub type Present = Box<dyn PresentCall>;
/// Implement this to pass your extension to [`Extensions::add_present_file`] or
/// [`Extensions::add_present_internal`].
pub trait PresentCall: KvarnSendSync {
    /// # Arguments
    ///
    /// [`PresentData`] contains all the references to the data needed.
    fn call<'a>(&'a self, present_data: &'a mut PresentData<'a>) -> RetFut<'a, ()>;
}
impl<F: for<'a> Fn(&'a mut PresentData<'a>) -> RetFut<'a, ()> + KvarnSendSync> PresentCall for F {
    fn call<'a>(&'a self, present_data: &'a mut PresentData<'a>) -> RetFut<'a, ()> {
        self(present_data)
    }
}
/// A package extension.
///
/// Can be created using the [`package!`] macro.
///
/// Requires an object which implements the [`PackageCall`] trait. See it for details on arguments.
///
/// See [module level documentation](extensions) and [kvarn.org](https://kvarn.org/extensions/) for more info.
pub type Package = Box<dyn PackageCall>;
/// Implement this to pass your extension to [`Extensions::add_package`].
pub trait PackageCall: KvarnSendSync {
    /// # Arguments
    ///
    /// - A mutable reference to a [`Response`] without the body.
    /// - An immutable reference to the request.
    /// - An immutable reference to the host this request is to.
    fn call<'a>(
        &'a self,
        response: &'a mut Response<()>,
        request: &'a FatRequest,
        host: &'a Host,
        addr: SocketAddr,
    ) -> RetFut<'a, ()>;
}
impl<
        F: for<'a> Fn(&'a mut Response<()>, &'a FatRequest, &'a Host, SocketAddr) -> RetFut<'a, ()>
            + KvarnSendSync,
    > PackageCall for F
{
    fn call<'a>(
        &'a self,
        response: &'a mut Response<()>,
        request: &'a FatRequest,
        host: &'a Host,
        addr: SocketAddr,
    ) -> RetFut<'a, ()> {
        self(response, request, host, addr)
    }
}
/// A post extension.
///
/// Can be created using the [`post!`] macro.
///
/// Requires an object which implements the [`PostCall`] trait. See it for details on arguments.
///
/// See [module level documentation](extensions) and [kvarn.org](https://kvarn.org/extensions/) for more info.
pub type Post = Box<dyn PostCall>;
/// Implement this to pass your extension to [`Extensions::add_post`].
pub trait PostCall: KvarnSendSync {
    /// # Arguments
    ///
    /// - An immutable reference to the request.
    /// - An immutable reference to the host this request is to.
    /// - A mutable reference to the [`ResponsePipe`].
    /// - The plain text of the body of the response.
    /// - The [`SocketAddr`] of the requester.
    fn call<'a>(
        &'a self,
        request: &'a FatRequest,
        host: &'a Host,
        response_pipe: &'a mut ResponseBodyPipe,
        identity_body: Bytes,
        addr: SocketAddr,
    ) -> RetFut<'a, ()>;
}
impl<
        F: for<'a> Fn(
                &'a FatRequest,
                &'a Host,
                &'a mut ResponseBodyPipe,
                Bytes,
                SocketAddr,
            ) -> RetFut<'a, ()>
            + KvarnSendSync,
    > PostCall for F
{
    fn call<'a>(
        &'a self,
        request: &'a FatRequest,
        host: &'a Host,
        response_pipe: &'a mut ResponseBodyPipe,
        identity_body: Bytes,
        addr: SocketAddr,
    ) -> RetFut<'a, ()> {
        self(request, host, response_pipe, identity_body, addr)
    }
}
/// Dynamic function to check if a extension should be ran.
///
/// Used with [`Prepare`] extensions
#[cfg(feature = "uring")]
pub type If = Box<(dyn Fn(&FatRequest, &Host) -> bool)>;
/// Dynamic function to check if a extension should be ran.
///
/// Used with [`Prepare`] extensions
#[cfg(not(feature = "uring"))]
pub type If = Box<(dyn Fn(&FatRequest, &Host) -> bool + Sync + Send)>;
/// A [`Future`] for writing to a [`ResponsePipe`] after the response is sent.
///
/// Used with [`Prepare`] extensions in their returned [`FatResponse`].
pub type ResponsePipeFuture = Box<dyn ResponsePipeFutureCall>;
/// Implement this to pass your future to [`FatResponse::with_future`].
pub trait ResponsePipeFutureCall: KvarnSendSync {
    /// # Arguments
    ///
    /// - A mutable reference to the [`ResponseBodyPipe`].
    /// - An immutable reference to the host this request is to.
    fn call<'a>(
        &'a mut self,
        response_body_pipe: &'a mut ResponseBodyPipe,
        host: &'a Host,
    ) -> RetFut<'a, ()>;
}

/// A extension Id. The [`Self::priority`] is used for sorting extensions
/// and [`Self::name`] for debugging which extensions are mounted.
///
/// Higher `priority` extensions are ran first.
/// The debug name is useful when you want to see which priorities
/// other extensions use. This is beneficial when creating "plug-and-play" extensions.
///
/// If two extensions with identical [`priority`](Self::priority)s are inserted, the latter will override the
/// prior. This only effects extensions of the same type.
#[derive(Debug, Clone, Copy)]
#[must_use]
pub struct Id {
    priority: i32,
    name: Option<&'static str>,
    no_override: bool,
}
impl Id {
    /// Creates a new Id with `priority` and a `name`.
    pub fn new(priority: i32, name: &'static str) -> Self {
        Self {
            priority,
            name: Some(name),
            no_override: false,
        }
    }
    /// Creates a Id without a name. This is considered a bad practice,
    /// as you cannot see which extensions are mounted to the
    /// [`Extensions`].
    ///
    /// See [`Self::name`] for details about how this affects output.
    pub fn without_name(priority: i32) -> Self {
        Self {
            priority,
            name: None,
            no_override: false,
        }
    }
    /// Always inserts this extension.
    /// If an extension with the same [`priority`](Self::priority) exist, the `priority` is decremented and tried again.
    pub fn no_override(mut self) -> Self {
        self.no_override = true;
        self
    }
    /// Returns the name of this Id.
    ///
    /// If the Id is created with [`Self::without_name`],
    /// this returns `Unnamed`.
    #[must_use]
    pub fn name(&self) -> &'static str {
        self.name.unwrap_or("Unnamed")
    }
    /// Returns the priority of this extension.
    #[must_use]
    pub fn priority(&self) -> i32 {
        self.priority
    }
}
impl Display for Id {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "\"{}\" with priority {}", self.name(), self.priority())
    }
}
impl PartialEq for Id {
    fn eq(&self, other: &Self) -> bool {
        self.priority().eq(&other.priority())
    }
}
impl Eq for Id {}
impl Ord for Id {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.priority().cmp(&other.priority())
    }
}
impl PartialOrd for Id {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

/// Returns a future accepted by all the [`extensions`]
/// yielding immediately with `value`.
#[inline]
#[cfg(not(feature = "uring"))]
pub fn ready<'a, T: 'a + Send>(value: T) -> RetFut<'a, T> {
    Box::pin(core::future::ready(value))
}
/// Returns a future accepted by all the [`extensions`]
/// yielding immediately with `value`.
#[inline]
#[cfg(feature = "uring")]
pub fn ready<'a, T: 'a>(value: T) -> RetFut<'a, T> {
    Box::pin(core::future::ready(value))
}

macro_rules! add_sorted_list {
    ($list: expr, $id: expr, $($other: expr, )+) => {
        let mut id = $id;
        loop {
            match $list.binary_search_by(|probe| id.cmp(&probe.0)) {
                Ok(_) if id.no_override => {
                    if let Some(priority) = id.priority.checked_sub(1) {
                        id.priority = priority;
                    } else {
                        panic!("reached minimum priority when trying to not override extension");
                    }
                    continue;
                }
                Ok(pos) => {
                    $list[pos] = (id, $($other, )*);
                    break;
                }
                Err(pos) => {
                    $list.insert(pos, (id, $($other, )*));
                    break;
                }
            };
        }
    };
}
macro_rules! remove_sorted_list {
    ($list: expr, $id: expr) => {
        $list
            .binary_search_by(|probe| probe.0.cmp(&$id))
            .ok()
            .map(|pos| $list.remove(pos))
    };
}

/// Contains all extensions.
/// See [kvarn.org on extensions](https://kvarn.org/extensions/) for more info.
#[must_use]
pub struct Extensions {
    prime: Vec<(Id, Prime)>,
    prepare_single: HashMap<CompactString, Prepare>,
    prepare_fn: Vec<(Id, If, Prepare)>,
    present_internal: HashMap<CompactString, Present>,
    present_file: HashMap<CompactString, Present>,
    package: Vec<(Id, Package)>,
    post: Vec<(Id, Post)>,
    // also update Debug implementation when adding fields
}
impl Extensions {
    /// Creates a empty [`Extensions`].
    ///
    /// It is strongly recommended to use [`Extensions::new()`] instead.
    #[inline]
    pub fn empty() -> Self {
        Self {
            prime: Vec::new(),
            prepare_single: HashMap::new(),
            prepare_fn: Vec::new(),
            present_internal: HashMap::new(),
            present_file: HashMap::new(),
            package: Vec::new(),
            post: Vec::new(),
        }
    }
    /// Creates a new [`Extensions`] and adds a few essential extensions.
    ///
    /// For now the following extensions are added. The number in parentheses is the priority.
    /// - A Prime extension (-64) redirecting the user from `<path>/` to `<path>/index.html` and
    ///   `<path>.` to `<path>.html`.
    ///   This was earlier part of parsing of the path, but was moved to an extension for consistency and performance; now `/`, `index.`, and `index.html` is the same entity in cache.
    /// - A Package extension (8) to set `referrer-policy` header to `no-referrer` for max security and privacy.
    ///   This is only done when no other `referrer-policy` header has been set earlier in the response.
    /// - A CORS extension to deny all CORS requests. See [`Self::with_cors`] for CORS management.
    /// - A [nonce](Self::with_nonce) implementation for easy nonce setup. (requires `nonce`
    ///   feature).
    /// - The default [`Csp`] which only allows requests from `self` and allows unsafe inline
    ///   styles. **This should to a large extent mitigate XSS.**
    /// - The `server` header is set to `Kvarn/<version>`. See [`Self::with_server_header`] for
    ///   more info and customization.
    pub fn new() -> Self {
        let mut new = Self::empty();

        new.with_uri_redirect()
            .with_no_referrer()
            .with_disallow_cors()
            .with_csp(Csp::default().arc())
            .with_server_header("Kvarn/0.5.0", false, true);

        #[cfg(feature = "nonce")]
        {
            new.with_nonce();
        }

        new
    }

    /// Adds a prime extension to redirect [`Uri`]s ending with `.` and `/`.
    ///
    /// This routs the requests according to [`host::Options::folder_default`] and
    /// [`host::Options::extension_default`].
    /// See respective documentation for more info.
    pub fn with_uri_redirect(&mut self) -> &mut Self {
        self.add_prime(
            prime!(request, host, _, {
                enum Ending {
                    Dot,
                    Slash,
                    Other,
                }
                impl From<&Uri> for Ending {
                    fn from(uri: &Uri) -> Self {
                        if uri.path().ends_with('.') {
                            Self::Dot
                        } else if uri.path().ends_with('/') {
                            Self::Slash
                        } else {
                            Self::Other
                        }
                    }
                }
                let append = match Ending::from(request.uri()) {
                    Ending::Other => return None,
                    Ending::Dot => host.options.extension_default.as_deref().unwrap_or("html"),
                    Ending::Slash => host
                        .options
                        .folder_default
                        .as_deref()
                        .unwrap_or("index.html"),
                };

                let mut uri = request.uri().clone().into_parts();

                let path = uri
                    .path_and_query
                    .as_ref()
                    .map_or("/", uri::PathAndQuery::path);
                let query = uri
                    .path_and_query
                    .as_ref()
                    .and_then(uri::PathAndQuery::query);
                let path_and_query = build_bytes!(
                    path.as_bytes(),
                    append.as_bytes(),
                    if query.is_none() { "" } else { "?" }.as_bytes(),
                    query.unwrap_or("").as_bytes()
                );

                // This is ok, we only added bytes from a String, which are guaranteed to be valid for a URI path
                uri.path_and_query =
                    Some(uri::PathAndQuery::from_maybe_shared(path_and_query).unwrap());

                // Again ok, see ↑
                let uri = Uri::from_parts(uri).unwrap();

                Some(uri)
            }),
            Id::new(-100, "Expand . and /"),
        );
        self
    }
    /// Adds a [`Package`] extension to set the `referrer-policy` to `no-referrer`
    /// for maximum privacy and security.
    /// If another `referrer-policy` is already present, nothing happens.
    /// This is added when calling [`Extensions::new`].
    pub fn with_no_referrer(&mut self) -> &mut Self {
        self.add_package(
            package!(response, _, _, _, {
                response
                    .headers_mut()
                    .entry("referrer-policy")
                    .or_insert(HeaderValue::from_static("no-referrer"));
            }),
            Id::new(10, "Set the referrer-policy header to no-referrer"),
        );
        self
    }

    /// Adds a [`Prepare`] and a [`Prime`] extension (with a priority of `86881`) which redirects requests using HTTP to HTTPS
    /// with a [`StatusCode::TEMPORARY_REDIRECT`].
    ///
    /// For more info about how it works, see the source of this function.
    #[cfg(feature = "https")]
    pub fn with_http_to_https_redirect(&mut self) -> &mut Self {
        self.add_prepare_fn(
            Box::new(|request, host| {
                request.uri().scheme_str() == Some("http") && request.uri().port().is_none() && {
                    host.certificate.read().unwrap().is_some()
                }
            }),
            prepare!(request, _, _, _, {
                // "/./ path" is special; it will not be accepted from outside; any path containing './' gets rejected.
                // Therefore, we can unwrap on values, making the assumption I implemented them correctly below.
                let uri = request.uri();
                let uri = {
                    let authority = uri.authority().map_or("", uri::Authority::as_str);
                    let bytes = build_bytes!(
                        b"https://",
                        authority.as_bytes(),
                        uri.path().as_bytes(),
                        uri.query().map_or(b"".as_ref(), |_| b"?".as_ref()),
                        uri.query().map_or(b"".as_ref(), str::as_bytes)
                    );
                    // Ok, since we just introduced https:// in the start, which are valid bytes.
                    HeaderValue::from_maybe_shared(bytes).unwrap()
                };

                let response = Response::builder()
                    .status(StatusCode::TEMPORARY_REDIRECT)
                    .header("location", uri);
                // Unwrap is ok; we know this is valid.
                FatResponse::cache(response.body(Bytes::new()).unwrap())
                    .with_server_cache(comprash::ServerCachePreference::None)
                    .with_compress(comprash::CompressPreference::None)
            }),
            extensions::Id::new(86881, "Redirecting to HTTPS"),
        );
        self
    }
    /// Adds a [`Present`] extension triggered by the internal extension `nonce` which adds nonce
    /// tags to all scripts with `nonce=` tags.
    /// You MUST NOT have server caching enabled.
    ///
    /// This integrates with your [`csp`] - if any `nonce` extension is added, the corresponding
    /// information is added to the `content-security-policy` header.
    ///
    /// See [kvarn.org](https://kvarn.org/nonce.) for more details.
    #[cfg(feature = "nonce")]
    pub fn with_nonce(&mut self) -> &mut Self {
        use base64::Engine;
        use rand::Rng;

        const DEFAULT_ENGINE: base64::engine::GeneralPurpose = base64::engine::GeneralPurpose::new(
            &base64::alphabet::STANDARD,
            base64::engine::GeneralPurposeConfig::new().with_encode_padding(true),
        );

        self.add_present_internal(
            "nonce",
            present!(ext, {
                let data: [u8; 16] = rand::thread_rng().gen();
                let mut s = BytesMut::with_capacity(24);
                unsafe { s.set_len(24) };

                let wrote = DEFAULT_ENGINE
                    .encode_slice(data, &mut s)
                    .expect("base64 failed to encode");
                // the padding should do this
                assert_eq!(wrote, 24);

                let body = ext.response.body_mut();
                // let mut new_body = BytesMut::with_capacity(body.len() + 24 * 4);
                let mut replacement = Vec::with_capacity(28);
                let mut last_start = 0;

                while let Some(occurrence) =
                    memchr::memmem::find(&body[last_start + 1..], b"nonce=")
                {
                    let occurrence = occurrence + last_start + 1;
                    // +6 as that's the length of b"nonce="
                    let rest = &body[occurrence + 6..];
                    let first = rest.first();
                    let end = match first {
                        Some(b'"') => memchr::memchr(b'"', &rest[1..]),
                        Some(b'\'') => memchr::memchr(b'\'', &rest[1..]),
                        _ => None,
                    };
                    // we shortened the list by 1
                    let end = end.map(|v| v + 1 + 6);
                    if let Some(end) = end {
                        let double = *first.unwrap() == b'"';
                        last_start = occurrence + end;

                        if double {
                            replacement.push(b'"');
                        } else {
                            replacement.push(b'\'');
                        }
                        replacement.extend_from_slice(&s);

                        if double {
                            replacement.push(b'"');
                        } else {
                            replacement.push(b'\'');
                        }
                    } else {
                        replacement.extend_from_slice(b"\"\"");
                        last_start = occurrence + 6 + 2;
                    }
                    body.replace(occurrence + 6..last_start, &replacement);
                    replacement.clear();
                }

                ext.response.headers_mut().insert(
                    "csp-nonce",
                    HeaderValue::from_maybe_shared(s.freeze())
                        .expect("base64 is valid for a header value"),
                );

                if *ext.server_cache_preference != comprash::ServerCachePreference::None {
                    error!(
                        "Enabled nonce on page with server caching enabled! \
                        This is critical for XSS resilience.\n\
                        nonces don't work with server caching."
                    );
                    *ext.server_cache_preference = comprash::ServerCachePreference::None;
                }
            }),
        );
        self
    }
    /// Set the `server` header to `server_name`.
    /// This is called by default when creating a new [`Extensions`] (except when calling
    /// [`Extensions::empty`]).
    ///
    /// If `add_platform` is true, append the platform the server is running
    /// on to the end of the server header.
    ///
    /// If `override_server_header` is true, remove any previous mentions of the server software.
    /// Set to false if you want reverse proxies to pass through the information (and therefore
    /// return two `server` headers to the user agent (maybe for debugging)).
    /// In most cases, it should be set to true.
    pub fn with_server_header(
        &mut self,
        server_name: impl AsRef<str>,
        add_platform: bool,
        override_server_header: bool,
    ) -> &mut Self {
        #[cfg(target_os = "windows")]
        const PLATFORM: &str = " (Windows)";
        #[cfg(target_os = "macos")]
        const PLATFORM: &str = " (macOS)";
        #[cfg(target_os = "linux")]
        const PLATFORM: &str = " (Linux)";
        #[cfg(target_os = "freebsd")]
        const PLATFORM: &str = " (FreeBSD)";
        #[cfg(target_os = "netbsd")]
        const PLATFORM: &str = " (NetBSD)";
        #[cfg(target_os = "openbsd")]
        const PLATFORM: &str = " (OpenBSD)";
        #[cfg(not(any(
            target_os = "windows",
            target_os = "macos",
            target_os = "linux",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd",
        )))]
        const PLATFORM: &str = "";

        let server_name = server_name.as_ref();
        let bytes = build_bytes!(
            server_name.as_bytes(),
            if add_platform {
                PLATFORM.as_bytes()
            } else {
                &[]
            }
        );
        let header_value = HeaderValue::from_maybe_shared(bytes.freeze())
            .expect("`server` header contains invalid bytes");

        self.add_package(
            package!(
                resp,
                _,
                _,
                _,
                move |header_value: HeaderValue, override_server_header: bool| {
                    if *override_server_header {
                        resp.headers_mut().insert("server", header_value.clone());
                    } else {
                        resp.headers_mut().append("server", header_value.clone());
                    }
                }
            ),
            Id::new(-1327, "add `server` header"),
        );

        self
    }

    /// Adds a [`Prime`] extension.
    pub fn add_prime(&mut self, extension: Prime, id: Id) {
        add_sorted_list!(self.prime, id, extension,);
    }
    /// Removes the [`Prime`] extension (if any) with `id`.
    pub fn remove_prime(&mut self, id: Id) {
        remove_sorted_list!(self.prime, id);
    }
    /// Get a reference to the [`Prime`] extensions.
    pub fn get_prime(&self) -> &[(Id, Prime)] {
        &self.prime
    }
    /// Adds a [`Prepare`] extension for a single URI.
    pub fn add_prepare_single(&mut self, path: impl AsRef<str>, extension: Prepare) {
        self.prepare_single
            .insert(path.as_ref().to_compact_string(), extension);
    }
    /// Removes the [`Prepare`] extension (if any) at `path`.
    pub fn remove_prepare_single(&mut self, path: impl AsRef<str>) {
        self.prepare_single.remove(path.as_ref());
    }
    /// Get a reference to the [`Prepare`] extensions bound to a path.
    #[must_use]
    pub fn get_prepare_single(&self) -> &HashMap<CompactString, Prepare> {
        &self.prepare_single
    }
    /// Adds a [`Prepare`] extension run if `function` return `true`. Higher [`Id::priority()`] extensions are ran first.
    pub fn add_prepare_fn(&mut self, predicate: If, extension: Prepare, id: Id) {
        add_sorted_list!(self.prepare_fn, id, predicate, extension,);
    }
    /// Removes the [`Prepare`] extension (if any) with `id`.
    pub fn remove_prepare_fn(&mut self, id: Id) {
        remove_sorted_list!(self.prepare_fn, id);
    }
    /// Get a reference to the [`Prepare`] extensions using [predicates](If).
    pub fn get_prepare_fn(&self) -> &[(Id, If, Prepare)] {
        &self.prepare_fn
    }
    /// Adds a [`Present`] internal extension, called with files starting with `!> `.
    pub fn add_present_internal(&mut self, name: impl AsRef<str>, extension: Present) {
        self.present_internal
            .insert(name.as_ref().to_compact_string(), extension);
    }
    /// Removes the [`Present`] internal extension (if any) at `path`.
    pub fn remove_present_internal(&mut self, path: impl AsRef<str>) {
        self.present_internal.remove(path.as_ref());
    }
    /// Get a reference to the [`Present`] internal extensions bound to a path.
    #[must_use]
    pub fn get_present_internal(&self) -> &HashMap<CompactString, Present> {
        &self.present_internal
    }
    /// Adds a [`Present`] file extension, called with file extensions matching `name`.
    pub fn add_present_file(&mut self, name: impl AsRef<str>, extension: Present) {
        self.present_file
            .insert(name.as_ref().to_compact_string(), extension);
    }
    /// Removes the [`Present`] file extension (if any) at `path`.
    pub fn remove_present_file(&mut self, path: impl AsRef<str>) {
        self.present_file.remove(path.as_ref());
    }
    /// Get a reference to the [`Present`] file extensions bound to a path.
    #[must_use]
    pub fn get_present_file(&self) -> &HashMap<CompactString, Present> {
        &self.present_file
    }
    /// Adds a [`Package`] extension, used to make last-minute changes to response. Higher [`Id::priority()`] extensions are ran first.
    pub fn add_package(&mut self, extension: Package, id: Id) {
        add_sorted_list!(self.package, id, extension,);
    }
    /// Removes the [`Package`] extension (if any) with `id`.
    pub fn remove_package(&mut self, id: Id) {
        remove_sorted_list!(self.package, id);
    }
    /// Get a reference to the [`Package`] extensions.
    pub fn get_package(&self) -> &[(Id, Package)] {
        &self.package
    }
    /// Adds a [`Post`] extension, used for HTTP/2 push Higher [`Id::priority()`] extensions are ran first.
    pub fn add_post(&mut self, extension: Post, id: Id) {
        add_sorted_list!(self.post, id, extension,);
    }
    /// Removes the [`Post`] extension (if any) with `id`.
    pub fn remove_post(&mut self, id: Id) {
        remove_sorted_list!(self.post, id);
    }
    /// Get a reference to the [`Package`] extensions.
    pub fn get_post(&self) -> &[(Id, Post)] {
        &self.post
    }

    /// The returned [`Uri`] should be the path of the request.
    /// The original request isn't modified, so prepare extensions can rely on it.
    pub(crate) async fn resolve_prime(
        &self,
        request: &mut FatRequest,
        host: &Host,
        address: SocketAddr,
    ) -> Option<Uri> {
        let mut uri = None;
        for (_, prime) in &self.prime {
            if let Some(prime) = prime.call(request, host, address).await {
                if prime.path().starts_with("/./") {
                    uri = Some(prime);
                } else {
                    *request.uri_mut() = prime;
                }
            }
        }
        uri
    }
    pub(crate) async fn resolve_prepare(
        &self,
        request: &mut FatRequest,
        overide_uri: Option<&Uri>,
        host: &Host,
        path: &Option<CompactString>,
        address: SocketAddr,
    ) -> Option<FatResponse> {
        if let Some(extension) = self
            .prepare_single
            .get(overide_uri.unwrap_or_else(|| request.uri()).path())
        {
            Some(
                extension
                    .call(request, host, path.as_deref().map(Path::new), address)
                    .await,
            )
        } else {
            for (_, function, extension) in &self.prepare_fn {
                if function(request, host) {
                    return Some(
                        extension
                            .call(request, host, path.as_deref().map(Path::new), address)
                            .await,
                    );
                }
            }
            None
        }
    }
    // It's an internal function, which should be the same style as all the other `resolve_*` functions.
    #[allow(clippy::too_many_arguments)]
    pub(crate) async fn resolve_present(
        &self,
        request: &mut Request<Body>,
        response: &mut Response<Bytes>,
        client_cache_preference: &mut comprash::ClientCachePreference,
        server_cache_preference: &mut comprash::ServerCachePreference,
        host: &Host,
        address: SocketAddr,
    ) {
        let mut body = LazyRequestBody::new(request.body_mut());
        let body = &mut body;
        let path = utils::parse::uri(request.uri().path());

        let extensions = PresentExtensions::new(Bytes::clone(response.body()));

        if let Some(extensions) = &extensions {
            *response.body_mut() = response.body_mut().split_off(extensions.data_start());
        }

        let (response_head, response_body) = utils::split_response(core::mem::take(response));
        let response_body = utils::BytesCow::Ref(response_body);
        let mut cow_response = response_head.map(|()| response_body);

        if let Some(extensions) = extensions {
            for extension_name_args in extensions {
                if let Some(extension) = self.present_internal.get(extension_name_args.name()) {
                    let mut data = PresentData {
                        address,
                        request,
                        body,
                        host,
                        path: path.map(Path::new),
                        server_cache_preference,
                        client_cache_preference,
                        response: &mut cow_response,
                        args: extension_name_args,
                    };
                    extension.call(&mut data).await;
                }
            }
        }
        if let Some(extension) = path
            .map(Path::new)
            .and_then(Path::extension)
            .and_then(std::ffi::OsStr::to_str)
            .and_then(|s| self.present_file.get(s))
        {
            let mut data = PresentData {
                address,
                request,
                body,
                host,
                path: path.map(Path::new),
                server_cache_preference,
                client_cache_preference,
                response: &mut cow_response,
                args: PresentArguments::empty(),
            };
            extension.call(&mut data).await;
        }

        *response = cow_response.map(utils::BytesCow::freeze);
    }
    pub(crate) async fn resolve_package(
        &self,
        response: &mut Response<()>,
        request: &FatRequest,
        host: &Host,
        addr: SocketAddr,
    ) {
        for (_, extension) in &self.package {
            extension.call(response, request, host, addr).await;
        }
    }
    pub(crate) async fn resolve_post(
        &self,
        request: &FatRequest,
        bytes: Bytes,
        response_pipe: &mut ResponseBodyPipe,
        addr: SocketAddr,
        host: &Host,
    ) {
        for (_, extension) in self.post.iter().take(self.post.len().saturating_sub(1)) {
            extension
                .call(request, host, response_pipe, Bytes::clone(&bytes), addr)
                .await;
        }
        if let Some((_, extension)) = self.post.last() {
            extension
                .call(request, host, response_pipe, bytes, addr)
                .await;
        }
    }
}
impl Default for Extensions {
    fn default() -> Self {
        Self::new()
    }
}
impl Debug for Extensions {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        macro_rules! map {
            ($slice: expr) => {
                &$slice
                    .iter()
                    .map(|ext| (ext.0.as_clean(), "internal extension".as_clean()))
                    .collect::<Vec<_>>()
            };
        }
        let mut s = f.debug_struct(utils::ident_str!(Extensions));
        utils::fmt_fields!(
            s,
            (self.prime, map!(self.prime)),
            (self.prepare_single, map!(self.prepare_single)),
            (self.prepare_fn, map!(self.prepare_fn)),
            (self.present_internal, map!(self.present_internal)),
            (self.present_file, map!(self.present_file)),
            (self.package, map!(self.package)),
            (self.post, map!(self.post)),
        );
        s.finish()
    }
}

/// Add data pretending to present state in creating the response.
///
/// See [module level documentation](crate::extensions).
#[allow(missing_docs)]
#[derive(Debug)]
pub struct PresentData<'a> {
    // Regarding request
    pub address: SocketAddr,
    pub request: &'a FatRequest,
    pub body: &'a mut LazyRequestBody,
    pub host: &'a Host,
    pub path: Option<&'a Path>,
    // Regarding response
    pub server_cache_preference: &'a mut comprash::ServerCachePreference,
    pub client_cache_preference: &'a mut comprash::ClientCachePreference,
    pub response: &'a mut Response<utils::BytesCow>,
    // Regarding extension
    pub args: PresentArguments,
}

/// A [`Request`] [`Body`] which is lazily read.
#[derive(Debug)]
#[must_use]
pub struct LazyRequestBody {
    body: *mut Body,
    result: Option<Bytes>,
}
impl LazyRequestBody {
    /// This struct must be `dropped` before `body` or Undefined Behaviour occurs.
    ///
    /// The `body` is converted to a `*mut` which can be dereferenced safely, as long as we wait for this to be dropped.
    /// It can also not be referenced in any other way while this is not dropped.
    #[inline]
    pub(crate) fn new(body: &mut Body) -> Self {
        Self { body, result: None }
    }
    /// Reads the `Bytes` from the request body.
    /// `max_len` can be used to limit memory allocation. 16MB is often enough for every case.
    ///
    /// # Errors
    ///
    /// Returns any errors from reading the inner [`Body`].
    #[inline]
    pub async fn get(&mut self, max_len: usize) -> io::Result<&Bytes> {
        if let Some(ref result) = self.result {
            Ok(result)
        } else {
            let buffer = unsafe { &mut *self.body }.read_to_bytes(max_len).await?;
            self.result.replace(buffer);
            // ok; we've just assigned to it
            Ok(self.result.as_ref().unwrap())
        }
    }
}
unsafe impl Send for LazyRequestBody {}
unsafe impl Sync for LazyRequestBody {}

/// A set of rules applicable to certain paths.
/// See the note at [`Self::empty`] on how paths are matched.
#[must_use]
#[derive(Debug, Clone)]
pub struct RuleSet<R> {
    rules: Vec<(String, R)>,
}
impl<R> RuleSet<R> {
    /// Creates a new ruleset without any rules.
    pub fn empty() -> Self {
        Self { rules: Vec::new() }
    }
    /// Adds `rule` to `path`.
    ///
    /// To use this with [`Host::vary`], use [`Self::add_mut`], which this uses internally.
    ///
    /// By default, `path` will only match requests with the exact path.
    /// This can be changed by appending `*` to the end of the path, which
    /// will then check if the request path start with `path`.
    pub fn add(mut self, path: impl AsRef<str>, rule: impl Into<R>) -> Self {
        self.add_mut(path, rule);
        self
    }
    /// Same as [`Self::add`] but operating on a mutable reference.
    pub fn add_mut(&mut self, path: impl AsRef<str>, rule: impl Into<R>) -> &mut Self {
        let path = path.as_ref().to_owned();

        if let Ok(idx) = self.rules.binary_search_by(|probe| probe.0.cmp(&path)) {
            // not swap_remove, because ordering!
            self.rules.remove(idx);
        }
        self.rules.push((path, rule.into()));

        self.rules.sort_unstable_by(|a, b| {
            use std::cmp::Ordering;
            if a.0.ends_with('*') == b.0.ends_with('*') {
                b.0.len().cmp(&a.0.len())
            } else if a.0.ends_with('*') {
                Ordering::Greater
            } else {
                Ordering::Less
            }
        });

        self
    }

    /// Puts `self` in a [`Arc`].
    ///
    /// Useful for e.g. adding a [`Cors`] ruleset with [`Extensions::with_cors`].
    #[must_use]
    pub fn arc(self) -> Arc<Self> {
        Arc::new(self)
    }
    /// Gets the rule (if any) at `uri_path`.
    ///
    /// For info about how this is matched, see [`Self::add`].
    #[must_use]
    pub fn get(&self, uri_path: &str) -> Option<&R> {
        for (path, allow) in &self.rules {
            if path == uri_path
                || (path
                    .strip_suffix('*')
                    .map_or(false, |path| uri_path.starts_with(path)))
            {
                return Some(allow);
            }
        }
        None
    }
}

/// Prepare extension to stream body instead of: reading it fully, caching, then responding.
/// **Use with care!**
///
/// Does not support present extensions, nor post extensions.
#[must_use]
#[allow(clippy::cast_possible_truncation, unused_mut)]
pub fn stream_body() -> Box<dyn PrepareCall> {
    prepare!(req, host, path, _addr, {
        debug!("Streaming body for {:?}", req.uri().path());
        if let Some(path) = path {
            let file = fs::File::open(path).await;
            let meta = if let Ok(_file) = &file {
                #[cfg(feature = "uring")]
                {
                    tokio_uring::fs::statx(path).await.ok()
                }
                #[cfg(not(feature = "uring"))]
                {
                    _file.metadata().await.ok()
                }
            } else {
                None
            };
            if let (Ok(mut file), Some(meta)) = (file, meta) {
                let mut response = Response::new(Bytes::new());
                response
                    .headers_mut()
                    .insert("vary", HeaderValue::from_static("range"));

                let first_bytes = {
                    let mut v = vec![0; 16];
                    #[cfg(feature = "uring")]
                    let (Ok(read), mut v) = file.read_at(v, 0).await
                    else {
                        return default_error_response(StatusCode::NOT_FOUND, host, None).await;
                    };
                    #[cfg(not(feature = "uring"))]
                    let Ok(read) = file.read(&mut v).await
                    else {
                        return default_error_response(StatusCode::NOT_FOUND, host, None).await;
                    };
                    v.truncate(read);
                    v
                };

                // Mime
                if !response.headers().contains_key("content-type") {
                    let mime = comprash::get_mime(
                        path.extension()
                            .and_then(std::ffi::OsStr::to_str)
                            .unwrap_or(""),
                        &first_bytes,
                    );
                    let mime = if comprash::is_text(&mime) {
                        let b = mime.to_string().into_bytes();
                        build_bytes!(&b, b"; charset=utf-8").freeze()
                    } else {
                        mime.to_string().into_bytes().into()
                    };

                    // Mime will only contains valid bytes.
                    let content_type = HeaderValue::from_maybe_shared::<Bytes>(mime).unwrap();
                    response.headers_mut().insert("content-type", content_type);
                }

                #[cfg(feature = "uring")]
                let len = meta.stx_size as usize;
                #[cfg(not(feature = "uring"))]
                let len = meta.len() as usize;

                #[allow(clippy::uninit_vec)]
                let fut = response_pipe_fut!(response, _host, move |file: fs::File| {
                    let mut buf = Vec::with_capacity(1024 * 64);
                    #[cfg(feature = "uring")]
                    let mut pos = 0;
                    unsafe { buf.set_len(buf.capacity()) };
                    loop {
                        #[cfg(feature = "uring")]
                        let r = {
                            let (r, b) = file.read_at(buf, pos).await;
                            buf = b;
                            r
                        };
                        #[cfg(not(feature = "uring"))]
                        let r = file.read(&mut buf).await;
                        match r {
                            Ok(read) => {
                                if read == 0 {
                                    break;
                                }
                                #[cfg(feature = "uring")]
                                {
                                    pos += read as u64;
                                }
                                match response.send(Bytes::copy_from_slice(&buf[..read])).await {
                                    Ok(()) => {}
                                    Err(_) => {
                                        break;
                                    }
                                }
                            }
                            Err(err) => {
                                warn!("Failed to stream body from file: {err}");
                                break;
                            }
                        }
                    }
                });

                FatResponse::new(response, comprash::ServerCachePreference::None)
                    .with_future_and_len(fut, len)
            } else {
                default_error_response(StatusCode::NOT_FOUND, host, None).await
            }
        } else {
            default_error_response(StatusCode::NOT_FOUND, host, None).await
        }
    })
}

mod macros {
    /// Create a pinned future, compatible with [`crate::RetFut`].
    ///
    /// # Examples
    ///
    /// This creates a future which prints `Hello world!` and awaits it.
    /// ```
    /// # async {
    /// # use kvarn::box_fut;
    /// let fut = box_fut!({
    ///     println!("Hello world!");
    /// });
    /// fut.await;
    /// # };
    /// ```
    #[macro_export]
    macro_rules! box_fut {
        ($code:block) => {
            Box::pin(async move { $code }) as $crate::extensions::RetFut<_>
        };
    }

    /// The ultimate extension-creation macro.
    ///
    /// This is used in the various other macros which expand to extensions; **use them instead**!
    ///
    /// # Stability
    ///
    /// This macro isn't considered stable and may change at any time.
    ///
    /// # Examples
    ///
    /// This is similar to the `prepare!` macro.
    ///
    /// ```
    /// # use kvarn::prelude::*;
    /// extension!(
    ///     kvarn::extensions::PrepareCall,
    ///     FatResponse,
    ///     | request: &'a mut FatRequest: &mut FatRequest: arg1,
    ///     host: &'a Host: &Host: arg2,
    ///     path: Option<&'a Path>: Option<&Path>: arg3,
    ///     addr: SocketAddr: SocketAddr: arg4 |, , {
    ///         println!("Hello world, from extension macro!");
    ///         FatResponse::no_cache(Response::new(Bytes::from_static(b"Hi!")))
    ///     }
    /// );
    /// ```
    #[macro_export]
    macro_rules! extension {
        // pat to also match _
        // the $meta and $mut (in move || section) signals that this needs to be mut - if both are
        // specified (see `response_pipe_fut` below), the impl and provided variables are mutable.
        //
        // `name` for the params is used to locally bind the params, as the `param` can be `_`.
        ($trait: ty, $ret: ty, $(($meta:tt) ,)? | $($param:tt: $param_type:ty: $param_type_no_lifetimes:ty :$name:ident ),* |, $(($($(($mut:tt))? $move:ident:$ty:ty),+))?, $code:block) => {{
            // we go through all this hassle of having a closure to capture dynamic environment.
            #[cfg(feature = "uring")]
            // not requirement of Send + Sync
            struct Ext<F: for<'a> Fn($($param_type,)* $($(&'a $($mut)? $ty,)+)?) -> $crate::extensions::RetFut<'a, $ret>> {
                ext_function_private: F,
                $($($move:$ty,)+)?
            }
            #[cfg(feature = "uring")]
            // not requirement of Send + Sync
            impl<F: for<'a> Fn($($param_type,)* $($(&'a $($mut)? $ty,)+)?) -> $crate::extensions::RetFut<'a, $ret>> $trait for Ext<F> {
                fn call<'a>(
                    &'a $($meta)? self,
                    $($name: $param_type,)*
                ) -> $crate::extensions::RetFut<'a, $ret> {
                    let Self {
                        ext_function_private,
                        $($($move,)+)?
                    } = self;
                    (ext_function_private)($($name,)* $($($move,)+)?)
                }
            }

            #[cfg(not(feature = "uring"))]
            struct Ext<F: for<'a> Fn($($param_type,)* $($(&'a $($mut)? $ty,)+)?) -> $crate::extensions::RetFut<'a, $ret> + Send + Sync> {
                ext_function_private: F,
                $($($move:$ty,)+)?
            }

            #[cfg(not(feature = "uring"))]
            impl<F: for<'a> Fn($($param_type,)* $($(&'a $($mut)? $ty,)+)?) -> $crate::extensions::RetFut<'a, $ret> + Send + Sync> $trait for Ext<F> {
                fn call<'a>(
                    &'a $($meta)? self,
                    $($name: $param_type,)*
                ) -> $crate::extensions::RetFut<'a, $ret> {
                    let Self {
                        ext_function_private,
                        $($($move,)+)?
                    } = self;
                    (ext_function_private)($($name,)* $($($move,)+)?)
                }
            }
            Box::new(Ext {
                ext_function_private: move |$($param: $param_type_no_lifetimes,)* $($($move: & $($mut)? $ty,)+)?| {
                    Box::pin(async move {
                        $code
                    })
                },
                $($($move,)+)?
            })
        }};
    }

    /// Construct a [`Prime`](super::Prime) extension like you write closures.
    ///
    /// See [`crate::prepare!`] for usage and useful examples.
    /// See [`super::PrimeCall`] for a list of arguments.
    ///
    /// # Examples
    ///
    /// ```
    /// # use kvarn::prelude::*;
    /// let extension = prime!(_, _host, _addr, {
    ///     Some(Uri::from_static("https://doc.icelk.dev/"))
    /// });
    /// ```
    #[macro_export]
    macro_rules! prime {
        // pat to also match `_`
        ($request:pat, $host:pat, $addr:pat, $(move |$($move:ident:$ty:ty ),+|)? $code:block) => {
            $crate::extension!(
                $crate::extensions::PrimeCall,
                Option<$crate::prelude::Uri>,
                |$request: &'a $crate::FatRequest: &$crate::FatRequest: a1,
                $host: &'a $crate::prelude::Host: &$crate::prelude::Host: a2,
                $addr: $crate::prelude::SocketAddr: $crate::prelude::SocketAddr: a3|,
                $(($($move:$ty),+))?,
                $code
            ) as $crate::extensions::Prime
        }
    }
    /// Construct a [`Prepare`](super::Prepare) extension like you write closures.
    ///
    /// See [`super::PrepareCall`] for a list of arguments.
    ///
    /// > The `path` will be [`None`] if and only if [`crate::host::Options::disable_fs`] is true *or* percent
    /// > decoding failed. `request.uri().path()` will not have it's percent encoding decoded.
    ///
    /// See example below. Where `times_called` is defined in the arguments of the macro, you can enter several `Arc`s to capture from the environment.
    /// They will be cloned before being moved to the future, mitigating the error `cannot move out of 'times_called', a captured variable in an 'Fn' closure`.
    /// **Only `Arc`s** will work, since the variable has to be `Send` and `Sync`.
    ///
    /// You have to have kvarn imported as `kvarn`.
    ///
    /// # Examples
    ///
    /// > **These examples are applicable to all other extension-creation macros,
    /// > but with different parameters. See their respective documentation.**
    ///
    /// ```
    /// # use kvarn::prelude::*;
    /// use std::sync::{Arc, atomic};
    ///
    /// let times_called = Arc::new(atomic::AtomicUsize::new(0));
    ///
    /// prepare!(req, host, _path, _, move |times_called: Arc<atomic::AtomicUsize>| {
    ///     let times_called = times_called.fetch_add(1, atomic::Ordering::Relaxed);
    ///     println!("Called {} time(s). Request {:?}", times_called, req);
    ///
    ///     default_error_response(StatusCode::NOT_FOUND, host, None).await
    /// });
    /// ```
    ///
    /// To capture no variables, just leave out the `move ||`.
    ///
    /// ```
    /// # use kvarn::prelude::*;
    /// prepare!(_request, host, _, _addr, {
    ///     default_error_response(StatusCode::METHOD_NOT_ALLOWED, host, None).await
    /// });
    /// ```
    #[macro_export]
    macro_rules! prepare {
        // pat to also match `_`
        ($request:pat, $host:pat, $path:pat, $addr:pat, $(move |$($move:ident:$ty:ty),+|)? $code:block) => {
            $crate::extension!($crate::extensions::PrepareCall, $crate::FatResponse, |
                $request: &'a mut $crate::FatRequest: &mut $crate::FatRequest: a1,
                $host: &'a $crate::prelude::Host: &$crate::prelude::Host: a2,
                $path: Option<&'a $crate::prelude::Path>: Option<&$crate::prelude::Path>: a3,
                $addr: $crate::prelude::SocketAddr: $crate::prelude::SocketAddr: a4 |,
                $(($($move:$ty),+))?,
                $code
            ) as $crate::extensions::Prepare
        }
    }
    /// Construct a [`Present`](super::Present) extension like you write closures.
    ///
    /// See [`crate::prepare!`] for usage and useful examples.
    /// See [`super::PresentCall`] for a list of arguments.
    ///
    /// # Examples
    ///
    /// ```
    /// # use kvarn::prelude::*;
    /// let extension = present!(data, {
    ///     println!("Calling uri {}", data.request.uri());
    /// });
    /// ```
    #[macro_export]
    macro_rules! present {
        ($data:pat, $(move |$($move:ident:$ty:ty ),+|)? $code:block) => {
            $crate::extension!(
                $crate::extensions::PresentCall,
                (),
                |$data: &'a mut $crate::extensions::PresentData<'a>: &mut $crate::extensions::PresentData: a1|,
                $(($($move:$ty),+))?,
                $code
            ) as $crate::extensions::Present
        }
    }
    /// Construct a [`Package`](super::Package) extension like you write closures.
    ///
    /// See [`crate::prepare!`] for usage and useful examples.
    /// See [`super::PackageCall`] for a list of arguments.
    ///
    /// # Examples
    ///
    /// ```
    /// # use kvarn::prelude::*;
    /// let extension = package!(response, _, _, _, {
    ///     response.headers_mut().insert("x-author", HeaderValue::from_static("Icelk"));
    ///     println!("Response headers {:#?}", response.headers());
    /// });
    /// ```
    #[macro_export]
    macro_rules! package {
        ($response:pat, $request:pat, $host:pat, $addr:pat, $(move |$($move:ident:$ty:ty ),+|)? $code:block) => {
            $crate::extension!(
                $crate::extensions::PackageCall,
                (),
                |$response: &'a mut $crate::prelude::Response<()>: &mut $crate::prelude::Response<()>: a1,
                $request: &'a $crate::FatRequest: &$crate::FatRequest: a2,
                $host: &'a $crate::prelude::Host: &$crate::prelude::Host: a3,
                $addr: $crate::prelude::SocketAddr: $crate::prelude::SocketAddr: a4 |,
                $(($($move:$ty),+))?,
                $code
            ) as $crate::extensions::Package
        }
    }
    /// Construct a [`Post`](super::Post) extension like you write closures.
    ///
    /// See [`crate::prepare!`] for usage and useful examples.
    /// See [`super::PostCall`] for a list of arguments.
    ///
    /// # Examples
    ///
    /// ```
    /// # use kvarn::prelude::*;
    /// let extension = post!(_, _, response_pipe, _, _, {
    ///     match response_pipe {
    ///         application::ResponseBodyPipe::Http1(c) => println!("This is an HTTP/1 connection. {c:?}"),
    ///         application::ResponseBodyPipe::Http2(c, _) => println!("This is an HTTP/2 connection. {c:?}"),
    ///         application::ResponseBodyPipe::Http3(c) => println!("This is an HTTP/3 connection."),
    ///     }
    /// });
    /// ```
    #[macro_export]
    macro_rules! post {
        ($request:pat, $host:pat, $response_pipe:pat, $bytes:pat, $addr:pat, $(move |$($move:ident:$ty:ty ),+|)? $code:block) => {
            $crate::extension!(
                $crate::extensions::PostCall,
                (),
                |$request: &'a $crate::FatRequest: &$crate::FatRequest: a1,
                $host: &'a $crate::prelude::Host: &$crate::prelude::Host: a2,
                $response_pipe: &'a mut $crate::application::ResponseBodyPipe: &mut $crate::application::ResponseBodyPipe: a3,
                $bytes: $crate::prelude::Bytes: $crate::prelude::Bytes: a4,
                $addr: $crate::prelude::SocketAddr: $crate::prelude::SocketAddr: a5|,
                $(($($move:$ty),+))?,
                $code
            ) as $crate::extensions::Post
        }
    }
    /// Creates a [`super::ResponsePipeFuture`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use kvarn::prelude::*;
    /// prepare!(_req, host, _, addr, {
    ///     let response = default_error_response(StatusCode::METHOD_NOT_ALLOWED, host, None).await;
    ///     response.with_future(response_pipe_fut!(response_pipe, host, move |addr: SocketAddr| {
    ///         response_pipe.send(Bytes::from_static(b"This will be appended to the body!")).await;
    ///     }))
    /// });
    /// ```
    #[macro_export]
    macro_rules! response_pipe_fut {
        ($response:pat, $host:pat, $(move |$($move:ident:$ty:ty),+|)? $code:block) => {
            $crate::extension!(
                $crate::extensions::ResponsePipeFutureCall,
                (),
                (mut),
                |$response: &'a mut $crate::application::ResponseBodyPipe: &mut $crate::application::ResponseBodyPipe: a1,
                $host: &'a $crate::prelude::Host: &$crate::prelude::Host: a2|,
                $(($((mut) $move:$ty),+))?, $code)
        };
    }
}