runtrue-wasm-runtime 0.1.0-alpha.6

A standards-first WASI Component runtime with cold, warmish, and warm tiers
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
use crate::{
    Error, PackageTier, Program, Result, WasiProfile,
    runtime::{HostState, PreparedComponent, RuntimeInner},
};
use bytes::Bytes;
use http_body_util::{BodyExt as _, Full, Limited, combinators::UnsyncBoxBody};
use hyper::body::{Body, Frame, SizeHint};
use std::{
    future::Future,
    pin::Pin,
    sync::{
        Arc, Mutex,
        atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering},
    },
    task::{Context, Poll, ready},
    time::{Duration, Instant},
};
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use wasmtime::{Store, StoreContextMut, StoreLimitsBuilder, component::GuestTaskId};
use wasmtime_wasi_http::handler::{
    self, HandlerState, Instance, ProxyHandler, ProxyPre, ShouldAccept, ViewFn, WorkerExpiration,
    WorkerState, WorkerStatus,
};

/// Bounded configuration for a reusable standard WASI HTTP service.
#[derive(Debug, Clone)]
pub struct HttpServiceConfig {
    /// Maximum requests admitted into the handler at once.
    pub max_in_flight: usize,
    /// Maximum total requests handled by one guest instance.
    pub max_instance_reuse_count: usize,
    /// Maximum concurrent requests handled by one WASI 0.3 guest instance.
    /// WASI 0.2 instances are always limited to one.
    pub max_instance_concurrent_reuse_count: usize,
    /// Time an idle live instance remains resident for a fast resumed request.
    pub idle_worker_ttl: Duration,
    /// Maximum time for the guest to produce each response.
    pub request_timeout: Duration,
    /// Exact outbound HTTP capabilities. Empty denies every destination.
    pub outbound_grants: Vec<OutboundHttpGrant>,
}

impl Default for HttpServiceConfig {
    fn default() -> Self {
        Self {
            max_in_flight: 1_000,
            max_instance_reuse_count: 10_000,
            max_instance_concurrent_reuse_count: 32,
            idle_worker_ttl: Duration::from_secs(30),
            request_timeout: Duration::from_secs(30),
            outbound_grants: Vec::new(),
        }
    }
}

/// An exact outbound HTTP origin capability granted to a guest service.
///
/// Grants match scheme, authority (host and optional port), and method.
/// Wildcards, redirects, inherited host networking, and implicit credentials
/// are intentionally unsupported.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutboundHttpGrant {
    scheme: String,
    authority: String,
    methods: Vec<String>,
    max_request_bytes: u64,
    max_response_bytes: u64,
    allow_private_network: bool,
}

impl OutboundHttpGrant {
    /// Construct a grant for one exact origin. Call [`Self::with_methods`] to
    /// explicitly select the permitted methods.
    #[must_use]
    pub fn new(scheme: impl Into<String>, authority: impl Into<String>) -> Self {
        Self {
            scheme: scheme.into().to_ascii_lowercase(),
            authority: authority.into().to_ascii_lowercase(),
            methods: Vec::new(),
            max_request_bytes: 1024 * 1024,
            max_response_bytes: 4 * 1024 * 1024,
            allow_private_network: false,
        }
    }

    /// Replace the explicitly allowed methods.
    #[must_use]
    pub fn with_methods(mut self, methods: impl IntoIterator<Item = impl AsRef<str>>) -> Self {
        self.methods = methods
            .into_iter()
            .map(|method| method.as_ref().to_owned())
            .collect();
        self
    }

    /// Set maximum outbound request and response body sizes.
    #[must_use]
    pub const fn with_body_limits(
        mut self,
        max_request_bytes: u64,
        max_response_bytes: u64,
    ) -> Self {
        self.max_request_bytes = max_request_bytes;
        self.max_response_bytes = max_response_bytes;
        self
    }

    /// Permit private, loopback, link-local, or literal-IP destinations for
    /// this exact origin. Intended for explicitly trusted local services.
    #[must_use]
    pub const fn allow_private_network(mut self, allow: bool) -> Self {
        self.allow_private_network = allow;
        self
    }
}

#[derive(Clone)]
pub(crate) struct HttpHooks {
    grants: Arc<[OutboundHttpGrant]>,
}

impl HttpHooks {
    pub(crate) fn deny() -> Self {
        Self {
            grants: Arc::from([]),
        }
    }

    fn new(grants: Vec<OutboundHttpGrant>) -> Self {
        Self {
            grants: Arc::from(grants),
        }
    }

    fn grant_for<B>(&self, request: &http::Request<B>) -> Option<OutboundHttpGrant> {
        let scheme = request.uri().scheme_str()?;
        let authority = request.uri().authority()?.as_str();
        self.grants
            .iter()
            .find(|grant| {
                grant.scheme.eq_ignore_ascii_case(scheme)
                    && grant.authority.eq_ignore_ascii_case(authority)
                    && grant
                        .methods
                        .iter()
                        .any(|method| method.eq_ignore_ascii_case(request.method().as_str()))
                    && content_length_at_most(request.headers(), grant.max_request_bytes)
            })
            .cloned()
    }
}

impl wasmtime_wasi_http::p2::WasiHttpHooks for HttpHooks {
    fn send_request(
        &mut self,
        request: hyper::Request<wasmtime_wasi_http::p2::body::HyperOutgoingBody>,
        config: wasmtime_wasi_http::p2::types::OutgoingRequestConfig,
    ) -> wasmtime_wasi_http::p2::HttpResult<wasmtime_wasi_http::p2::types::HostFutureIncomingResponse>
    {
        use wasmtime_wasi_http::p2::{bindings::http::types::ErrorCode, types};

        let grant = self
            .grant_for(&request)
            .ok_or(ErrorCode::HttpRequestDenied)?;
        let handle = wasmtime_wasi::runtime::spawn(async move {
            Ok(async move {
                validate_network_destination(request.uri(), &grant)
                    .await
                    .map_err(|()| ErrorCode::DestinationIpProhibited)?;
                let (parts, body) = request.into_parts();
                let request = http::Request::from_parts(
                    parts,
                    LimitBody::new(body, grant.max_request_bytes, |_| {
                        ErrorCode::HttpRequestBodySize(None)
                    })
                    .boxed_unsync(),
                );
                let mut incoming =
                    wasmtime_wasi_http::p2::default_send_request_handler(request, config).await?;
                if !content_length_at_most(incoming.resp.headers(), grant.max_response_bytes) {
                    return Err(ErrorCode::HttpResponseBodySize(None));
                }
                incoming.resp = incoming.resp.map(|body| {
                    LimitBody::new(body, grant.max_response_bytes, |_| {
                        ErrorCode::HttpResponseBodySize(None)
                    })
                    .boxed_unsync()
                });
                Ok::<types::IncomingResponse, ErrorCode>(incoming)
            }
            .await)
        });
        Ok(types::HostFutureIncomingResponse::pending(handle))
    }
}

impl wasmtime_wasi_http::p3::WasiHttpHooks for HttpHooks {
    fn send_request(
        &mut self,
        request: http::Request<
            http_body_util::combinators::UnsyncBoxBody<
                Bytes,
                wasmtime_wasi_http::p3::bindings::http::types::ErrorCode,
            >,
        >,
        options: Option<wasmtime_wasi_http::p3::RequestOptions>,
        fut: Box<
            dyn Future<
                    Output = std::result::Result<
                        (),
                        wasmtime_wasi_http::p3::bindings::http::types::ErrorCode,
                    >,
                > + Send,
        >,
    ) -> Box<
        dyn Future<
                Output = std::result::Result<
                    (
                        http::Response<
                            http_body_util::combinators::UnsyncBoxBody<
                                Bytes,
                                wasmtime_wasi_http::p3::bindings::http::types::ErrorCode,
                            >,
                        >,
                        Box<
                            dyn Future<
                                    Output = std::result::Result<
                                        (),
                                        wasmtime_wasi_http::p3::bindings::http::types::ErrorCode,
                                    >,
                                > + Send,
                        >,
                    ),
                    wasmtime_wasi::TrappableError<
                        wasmtime_wasi_http::p3::bindings::http::types::ErrorCode,
                    >,
                >,
            > + Send,
    > {
        use wasmtime_wasi_http::p3::bindings::http::types::ErrorCode;

        let Some(grant) = self.grant_for(&request) else {
            return Box::new(async { Err(ErrorCode::HttpRequestDenied.into()) });
        };
        Box::new(async move {
            validate_network_destination(request.uri(), &grant)
                .await
                .map_err(|()| ErrorCode::DestinationIpProhibited)?;
            let (parts, body) = request.into_parts();
            let request = http::Request::from_parts(
                parts,
                LimitBody::new(body, grant.max_request_bytes, |_| {
                    ErrorCode::HttpRequestBodySize(None)
                })
                .boxed_unsync(),
            );
            let (response, io) = wasmtime_wasi_http::p3::default_send_request(request, options)
                .await
                .map_err(wasmtime_wasi::TrappableError::from)?;
            if !content_length_at_most(response.headers(), grant.max_response_bytes) {
                return Err(ErrorCode::HttpResponseBodySize(None).into());
            }
            Ok((
                response.map(|body| {
                    LimitBody::new(body, grant.max_response_bytes, |_| {
                        ErrorCode::HttpResponseBodySize(None)
                    })
                    .boxed_unsync()
                }),
                Box::new(async move {
                    let request_result = Box::into_pin(fut).await;
                    let response_result = io.await;
                    request_result.and(response_result)
                })
                    as Box<dyn Future<Output = std::result::Result<(), ErrorCode>> + Send>,
            ))
        })
    }
}

fn content_length_at_most(headers: &http::HeaderMap, limit: u64) -> bool {
    headers
        .get(http::header::CONTENT_LENGTH)
        .map_or(Some(0), |value| value.to_str().ok()?.parse::<u64>().ok())
        .is_some_and(|length| length <= limit)
}

async fn validate_network_destination(
    uri: &http::Uri,
    grant: &OutboundHttpGrant,
) -> std::result::Result<(), ()> {
    if grant.allow_private_network {
        return Ok(());
    }
    let authority = uri.authority().ok_or(())?;
    let port = authority
        .port_u16()
        .unwrap_or(if grant.scheme == "https" { 443 } else { 80 });
    let addresses = tokio::net::lookup_host((authority.host(), port))
        .await
        .map_err(|_| ())?
        .collect::<Vec<_>>();
    if addresses.is_empty() || addresses.iter().any(|address| !is_public_ip(address.ip())) {
        return Err(());
    }
    Ok(())
}

fn is_public_ip(address: std::net::IpAddr) -> bool {
    match address {
        std::net::IpAddr::V4(address) => {
            let [a, b, c, _] = address.octets();
            !(a == 0
                || address.is_private()
                || address.is_loopback()
                || address.is_link_local()
                || address.is_broadcast()
                || address.is_documentation()
                || address.is_unspecified()
                || address.is_multicast()
                || (a == 100 && (64..=127).contains(&b))
                || (a == 192 && b == 0 && c == 0)
                || (a == 192 && b == 88 && c == 99)
                || (a == 198 && matches!(b, 18 | 19))
                || a >= 240)
        }
        std::net::IpAddr::V6(address) => {
            let segments = address.segments();
            (segments[0] & 0xe000) == 0x2000 && !(segments[0] == 0x2001 && segments[1] == 0x0db8)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::is_public_ip;

    #[test]
    fn public_address_filter_fails_closed_for_special_ranges() {
        for address in [
            "0.1.2.3",
            "10.0.0.1",
            "100.64.0.1",
            "127.0.0.1",
            "169.254.1.1",
            "192.0.0.1",
            "192.88.99.1",
            "198.18.0.1",
            "203.0.113.1",
            "240.0.0.1",
            "::1",
            "fc00::1",
            "fe80::1",
            "2001:db8::1",
            "::ffff:127.0.0.1",
        ] {
            assert!(
                !is_public_ip(address.parse().expect("IP address")),
                "{address}"
            );
        }
        assert!(is_public_ip("8.8.8.8".parse().unwrap()));
        assert!(is_public_ip("2606:4700:4700::1111".parse().unwrap()));
    }
}

struct LimitBody<B, E> {
    body: Pin<Box<B>>,
    remaining: u64,
    error: fn(u64) -> E,
}

impl<B, E> LimitBody<B, E> {
    fn new(body: B, limit: u64, error: fn(u64) -> E) -> Self {
        Self {
            body: Box::pin(body),
            remaining: limit,
            error,
        }
    }
}

impl<B, E> hyper::body::Body for LimitBody<B, E>
where
    B: hyper::body::Body<Data = Bytes> + Send + 'static,
    B::Error: Into<E>,
    E: 'static,
{
    type Data = Bytes;
    type Error = E;

    fn poll_frame(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<std::result::Result<hyper::body::Frame<Bytes>, E>>> {
        let me = self.get_mut();
        let frame = ready!(me.body.as_mut().poll_frame(cx));
        match frame {
            Some(Ok(frame)) => {
                let bytes = frame.data_ref().map_or(0, Bytes::len) as u64;
                if bytes > me.remaining {
                    me.remaining = 0;
                    Poll::Ready(Some(Err((me.error)(bytes))))
                } else {
                    me.remaining -= bytes;
                    Poll::Ready(Some(Ok(frame)))
                }
            }
            Some(Err(error)) => Poll::Ready(Some(Err(error.into()))),
            None => Poll::Ready(None),
        }
    }

    fn is_end_stream(&self) -> bool {
        self.body.is_end_stream()
    }

    fn size_hint(&self) -> hyper::body::SizeHint {
        let mut hint = self.body.size_hint();
        if hint.upper().is_none_or(|upper| upper > self.remaining) {
            hint.set_upper(self.remaining);
        }
        hint
    }
}

/// Complete buffered request dispatched to a standard WASI HTTP handler.
#[derive(Debug, Clone)]
pub struct HttpRequest {
    /// HTTP method.
    pub method: String,
    /// Absolute or origin-form request URI.
    pub uri: String,
    /// Header name/value pairs. Duplicate names are preserved.
    pub headers: Vec<(String, Vec<u8>)>,
    /// Buffered request body.
    pub body: Vec<u8>,
}

impl HttpRequest {
    /// Construct a request with no headers.
    #[must_use]
    pub fn new(
        method: impl Into<String>,
        uri: impl Into<String>,
        body: impl Into<Vec<u8>>,
    ) -> Self {
        Self {
            method: method.into(),
            uri: uri.into(),
            headers: Vec::new(),
            body: body.into(),
        }
    }

    /// Append a header.
    #[must_use]
    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<Vec<u8>>) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }
}

/// Buffered response returned by a standard WASI HTTP handler.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct HttpResponse {
    /// HTTP status code.
    pub status: u16,
    /// Header name/value pairs. Duplicate names are preserved.
    pub headers: Vec<(String, Vec<u8>)>,
    /// Buffered response body.
    pub body: Vec<u8>,
    /// End-to-end handler time, including admission and worker creation.
    pub elapsed: Duration,
    /// Package tier observed when the request entered the service.
    pub package_tier: PackageTier,
    /// Whether at least one new worker was created while this request ran.
    pub worker_created: bool,
}

/// Allocation-free per-request telemetry for the streaming HTTP path.
///
/// A control plane can read this from [`StreamingHttpBody::metadata`] before
/// handing the response body to its network server. Request identifiers are
/// monotonically increasing and unique within one [`HttpService`].
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct HttpDispatchMetadata {
    /// Service-local request identifier passed to the WASI HTTP handler.
    pub request_id: u64,
    /// Package tier observed when the request entered the service.
    pub package_tier: PackageTier,
    /// Whether at least one new worker was created while producing headers.
    pub worker_created: bool,
    /// Time from entering the service until response headers were available.
    pub headers_elapsed: Duration,
}

/// Streaming response body returned by [`HttpService::handle_streaming`].
///
/// The body keeps its service admission permit until it is fully consumed or
/// dropped. Response bytes are bounded by the runtime output limit without
/// buffering or reconstructing the response.
pub struct StreamingHttpBody {
    inner: UnsyncBoxBody<Bytes, wasmtime::Error>,
    remaining: usize,
    completion: Option<HttpRequestCompletion>,
    metadata: HttpDispatchMetadata,
}

impl std::fmt::Debug for StreamingHttpBody {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("StreamingHttpBody")
            .field("metadata", &self.metadata)
            .finish_non_exhaustive()
    }
}

impl StreamingHttpBody {
    /// Per-request tier, worker, identifier, and header timing telemetry.
    #[must_use]
    pub const fn metadata(&self) -> HttpDispatchMetadata {
        self.metadata
    }
}

impl Body for StreamingHttpBody {
    type Data = Bytes;
    type Error = Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        context: &mut Context<'_>,
    ) -> Poll<Option<std::result::Result<Frame<Self::Data>, Self::Error>>> {
        let body = self.get_mut();
        match Pin::new(&mut body.inner).poll_frame(context) {
            Poll::Ready(Some(Ok(frame))) => {
                if let Some(data) = frame.data_ref() {
                    if data.len() > body.remaining {
                        body.remaining = 0;
                        body.completion.take();
                        return Poll::Ready(Some(Err(Error::Limit("output bytes"))));
                    }
                    body.remaining -= data.len();
                }
                Poll::Ready(Some(Ok(frame)))
            }
            Poll::Ready(Some(Err(error))) => {
                body.completion.take();
                Poll::Ready(Some(Err(Error::Execution(error.to_string()))))
            }
            Poll::Ready(None) => {
                if let Some(completion) = body.completion.take() {
                    completion.complete();
                }
                Poll::Ready(None)
            }
            Poll::Pending => Poll::Pending,
        }
    }

    fn is_end_stream(&self) -> bool {
        self.inner.is_end_stream()
    }

    fn size_hint(&self) -> SizeHint {
        self.inner.size_hint()
    }
}

/// Observable residency state for an HTTP service.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum HttpServiceState {
    /// No live guest instance is retained; the next request creates one.
    NoResidentWorker,
    /// One or more requests are executing.
    Active,
    /// A live reusable guest instance is idle and waiting for a request.
    PausedResident,
}

/// Point-in-time HTTP worker counters.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct HttpServiceMetrics {
    /// Workers created since service construction.
    pub workers_created: u64,
    /// Workers currently retaining a Store and component instance.
    pub live_workers: usize,
    /// Workers dropped because their idle TTL expired.
    pub idle_evictions: u64,
    /// Requests currently admitted.
    pub in_flight: usize,
    /// Successfully returned responses.
    pub completed_requests: u64,
}

/// A reusable standard WASI HTTP handler with bounded admission and idle eviction.
#[derive(Clone)]
pub struct HttpService {
    handler: ProxyHandler<HttpHandlerState>,
    admission: Arc<Semaphore>,
    metrics: Arc<HttpMetrics>,
    package_tier: Arc<AtomicUsize>,
    profile: WasiProfile,
    startup_from: PackageTier,
    startup_prepare: Duration,
    startup_total: Duration,
}

impl std::fmt::Debug for HttpService {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("HttpService")
            .field("profile", &self.profile)
            .field("state", &self.state())
            .field("metrics", &self.metrics())
            .finish_non_exhaustive()
    }
}

impl Program {
    /// Prepare this component as a standard WASI HTTP service.
    ///
    /// The returned service automatically retains idle workers as the fastest
    /// tier and evicts them after `idle_worker_ttl`, demoting package code to
    /// warmish so later requests can restart without recompilation.
    ///
    /// # Errors
    ///
    /// Returns an error for invalid service limits, a non-HTTP component, or
    /// failed preparation/pre-instantiation.
    pub async fn http_service(&self, config: HttpServiceConfig) -> Result<HttpService> {
        validate_config(&config, self.runtime.inner.config.limits.max_timeout)?;
        let total_started = Instant::now();
        let startup_from = self.tier();
        let prepare_started = Instant::now();
        let prepared = self
            .runtime
            .inner
            .prepare(&self.digest, Arc::clone(&self.bytes))
            .await?;
        let startup_prepare = prepare_started.elapsed();
        if !prepared.profile.is_http() {
            return Err(Error::UnsupportedComponent(
                "expected wasi:http/handler@0.3.x or incoming-handler@0.2.x".to_owned(),
            ));
        }

        let pre = make_proxy_pre(&self.runtime.inner, &prepared)?;
        let metrics = Arc::new(HttpMetrics::default());
        let pre_slot = Arc::new(Mutex::new(Some(Arc::new(pre))));
        let package_tier = Arc::new(AtomicUsize::new(package_tier_code(PackageTier::Warm)));
        let state = HttpHandlerState {
            runtime: Arc::clone(&self.runtime.inner),
            digest: self.digest.clone(),
            bytes: Arc::clone(&self.bytes),
            pre: Arc::clone(&pre_slot),
            profile: prepared.profile,
            config: config.clone(),
            metrics: Arc::clone(&metrics),
            package_tier: Arc::clone(&package_tier),
            next_request: AtomicU64::new(1),
        };
        Ok(HttpService {
            handler: ProxyHandler::new(state),
            admission: Arc::new(Semaphore::new(config.max_in_flight)),
            metrics,
            package_tier,
            profile: prepared.profile,
            startup_from,
            startup_prepare,
            startup_total: total_started.elapsed(),
        })
    }
}

impl HttpService {
    /// Standard WASI HTTP profile selected for this component.
    #[must_use]
    pub const fn profile(&self) -> WasiProfile {
        self.profile
    }

    /// Package tier observed before service preparation began.
    #[must_use]
    pub const fn startup_from(&self) -> PackageTier {
        self.startup_from
    }

    /// Time spent preparing code while the service was constructed.
    #[must_use]
    pub const fn startup_prepare(&self) -> Duration {
        self.startup_prepare
    }

    /// Total time spent constructing the service, including pre-instantiation.
    #[must_use]
    pub const fn startup_total(&self) -> Duration {
        self.startup_total
    }

    /// Current package code tier.
    #[must_use]
    pub fn package_tier(&self) -> PackageTier {
        package_tier_from_code(self.package_tier.load(Ordering::Relaxed))
    }

    /// Current worker residency state.
    #[must_use]
    pub fn state(&self) -> HttpServiceState {
        let in_flight = self.metrics.in_flight.load(Ordering::Acquire);
        if in_flight > 0 {
            HttpServiceState::Active
        } else if self.metrics.live_workers.load(Ordering::Acquire) > 0 {
            HttpServiceState::PausedResident
        } else {
            HttpServiceState::NoResidentWorker
        }
    }

    /// Current service counters.
    #[must_use]
    pub fn metrics(&self) -> HttpServiceMetrics {
        HttpServiceMetrics {
            workers_created: self.metrics.workers_created.load(Ordering::Acquire),
            live_workers: self.metrics.live_workers.load(Ordering::Acquire),
            idle_evictions: self.metrics.idle_evictions.load(Ordering::Acquire),
            in_flight: self.metrics.in_flight.load(Ordering::Acquire),
            completed_requests: self.metrics.completed_requests.load(Ordering::Acquire),
        }
    }

    /// Dispatch one buffered request to the standard guest handler.
    ///
    /// # Errors
    ///
    /// Returns an error for malformed HTTP input, admission shutdown, guest
    /// instantiation/trap/timeout, or response-size limit violations.
    pub async fn handle(&self, request: HttpRequest) -> Result<HttpResponse> {
        if request.body.len() > self.handler.state().runtime.config.limits.max_input_bytes {
            return Err(Error::Limit("input bytes"));
        }
        let permit = Arc::clone(&self.admission)
            .acquire_owned()
            .await
            .map_err(|_| Error::InvalidState("HTTP service is closed"))?;
        let started = Instant::now();
        let tier = self.package_tier();
        let workers_before = self.metrics.workers_created.load(Ordering::Relaxed);
        self.metrics.in_flight.fetch_add(1, Ordering::Relaxed);
        let completion = HttpRequestCompletion {
            metrics: Arc::clone(&self.metrics),
            _permit: permit,
        };
        let request = build_request(request)?;
        let id = self
            .handler
            .state()
            .next_request
            .fetch_add(1, Ordering::Relaxed);
        let response = self.handler.handle(id, request).await;
        let response = response.map_err(|error| Error::Execution(error.to_string()))?;
        let (parts, body) = response.into_parts();
        let body = body
            .collect()
            .await
            .map_err(|error| Error::Execution(error.to_string()))?
            .to_bytes();
        if body.len() > self.handler.state().runtime.config.limits.max_output_bytes {
            return Err(Error::Limit("output bytes"));
        }
        completion.complete();
        Ok(HttpResponse {
            status: parts.status.as_u16(),
            headers: parts
                .headers
                .iter()
                .map(|(name, value)| (name.to_string(), value.as_bytes().to_vec()))
                .collect(),
            body: body.to_vec(),
            elapsed: started.elapsed(),
            package_tier: tier,
            worker_created: self.metrics.workers_created.load(Ordering::Relaxed) > workers_before,
        })
    }

    /// Dispatch a standard HTTP request without buffering or reconstructing
    /// its body, headers, URI, or response.
    ///
    /// This is the low-overhead host integration path. It preserves admission,
    /// guest timeouts, worker reuse, capability enforcement, metrics, and
    /// request/response byte limits. The returned body must be consumed or
    /// dropped to release its admission permit.
    ///
    /// # Errors
    ///
    /// Returns an error for admission shutdown, guest instantiation, traps, or
    /// failure to produce response headers. Streaming body failures are
    /// reported by [`StreamingHttpBody`].
    pub async fn handle_streaming<B>(
        &self,
        request: http::Request<B>,
    ) -> Result<http::Response<StreamingHttpBody>>
    where
        B: Body<Data = Bytes> + Send + 'static,
        B::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
    {
        let started = Instant::now();
        let package_tier = self.package_tier();
        let max_input_bytes = self.handler.state().runtime.config.limits.max_input_bytes;
        let max_input_u64 = u64::try_from(max_input_bytes).unwrap_or(u64::MAX);
        let size_hint = request.body().size_hint();
        if size_hint.lower() > max_input_u64
            || size_hint.upper().is_some_and(|upper| upper > max_input_u64)
        {
            return Err(Error::Limit("input bytes"));
        }
        let request = request.map(|body| {
            Limited::new(body, max_input_bytes)
                .map_err(streaming_request_error)
                .boxed_unsync()
        });
        let permit = Arc::clone(&self.admission)
            .acquire_owned()
            .await
            .map_err(|_| Error::InvalidState("HTTP service is closed"))?;
        self.metrics.in_flight.fetch_add(1, Ordering::Relaxed);
        let completion = HttpRequestCompletion {
            metrics: Arc::clone(&self.metrics),
            _permit: permit,
        };
        let workers_before = self.metrics.workers_created.load(Ordering::Relaxed);
        let id = self
            .handler
            .state()
            .next_request
            .fetch_add(1, Ordering::Relaxed);
        let response = match self.handler.handle(id, request).await {
            Ok(response) => response,
            Err(error) => {
                drop(completion);
                return Err(Error::Execution(error.to_string()));
            }
        };
        let metadata = HttpDispatchMetadata {
            request_id: id,
            package_tier,
            worker_created: self.metrics.workers_created.load(Ordering::Relaxed) > workers_before,
            headers_elapsed: started.elapsed(),
        };
        let max_output_bytes = self.handler.state().runtime.config.limits.max_output_bytes;
        Ok(response.map(|body| {
            let mut completion = Some(completion);
            if body.is_end_stream()
                && let Some(completion) = completion.take()
            {
                completion.complete();
            }
            StreamingHttpBody {
                inner: body,
                remaining: max_output_bytes,
                completion,
                metadata,
            }
        }))
    }
}

fn streaming_request_error(error: Box<dyn std::error::Error + Send + Sync>) -> handler::ErrorCode {
    use wasmtime_wasi_http::p3::bindings::http::types::ErrorCode;

    let error = match error.downcast::<http_body_util::LengthLimitError>() {
        Ok(_) => ErrorCode::HttpRequestBodySize(None),
        Err(error) => ErrorCode::InternalError(Some(error.to_string())),
    };
    error.into()
}

struct HttpRequestCompletion {
    metrics: Arc<HttpMetrics>,
    _permit: OwnedSemaphorePermit,
}

impl HttpRequestCompletion {
    fn complete(self) {
        self.metrics
            .completed_requests
            .fetch_add(1, Ordering::Relaxed);
    }
}

impl Drop for HttpRequestCompletion {
    fn drop(&mut self) {
        self.metrics.in_flight.fetch_sub(1, Ordering::Relaxed);
    }
}

const fn package_tier_code(tier: PackageTier) -> usize {
    match tier {
        PackageTier::Cold => 0,
        PackageTier::DiskAot => 1,
        PackageTier::Warmish => 2,
        PackageTier::Warm => 3,
    }
}

const fn package_tier_from_code(code: usize) -> PackageTier {
    match code {
        0 => PackageTier::Cold,
        1 => PackageTier::DiskAot,
        2 => PackageTier::Warmish,
        _ => PackageTier::Warm,
    }
}

fn build_request(request: HttpRequest) -> Result<handler::Request> {
    let mut builder = http::Request::builder()
        .method(request.method.as_str())
        .uri(request.uri.as_str());
    for (name, value) in request.headers {
        builder = builder.header(name, value);
    }
    builder
        .body(
            Full::new(Bytes::from(request.body))
                .map_err(|never| match never {})
                .boxed_unsync(),
        )
        .map_err(|error| Error::Execution(format!("invalid HTTP request: {error}")))
}

fn make_proxy_pre(
    runtime: &RuntimeInner,
    prepared: &PreparedComponent,
) -> Result<ProxyPre<HostState>> {
    let instance = match prepared.profile {
        WasiProfile::Http0_3 => runtime.p3_linker()?.instantiate_pre(&prepared.component),
        WasiProfile::Http0_2 => runtime.p2_linker()?.instantiate_pre(&prepared.component),
        WasiProfile::Cli0_3 | WasiProfile::Cli0_2 => unreachable!("validated above"),
    }
    .map_err(|error| Error::Preparation(error.to_string()))?;
    match prepared.profile {
        WasiProfile::Http0_3 => wasmtime_wasi_http::p3::bindings::ServicePre::new(instance)
            .map(ProxyPre::P3)
            .map_err(|error| Error::Preparation(error.to_string())),
        WasiProfile::Http0_2 => wasmtime_wasi_http::p2::bindings::ProxyPre::new(instance)
            .map(ProxyPre::P2)
            .map_err(|error| Error::Preparation(error.to_string())),
        WasiProfile::Cli0_3 | WasiProfile::Cli0_2 => unreachable!("validated above"),
    }
}

#[derive(Default)]
struct HttpMetrics {
    workers_created: AtomicU64,
    live_workers: AtomicUsize,
    idle_evictions: AtomicU64,
    in_flight: AtomicUsize,
    completed_requests: AtomicU64,
}

struct HttpHandlerState {
    runtime: Arc<RuntimeInner>,
    digest: String,
    bytes: Arc<[u8]>,
    pre: Arc<Mutex<Option<Arc<ProxyPre<HostState>>>>>,
    profile: WasiProfile,
    config: HttpServiceConfig,
    metrics: Arc<HttpMetrics>,
    package_tier: Arc<AtomicUsize>,
    next_request: AtomicU64,
}

impl HandlerState for HttpHandlerState {
    type StoreData = HostState;
    type WorkerExpiration = HttpWorkerExpiration;
    type WorkerState = HttpWorkerState;

    async fn instantiate(
        &self,
    ) -> wasmtime::Result<Instance<Self::StoreData, Self::WorkerExpiration, Self::WorkerState>>
    {
        let existing_pre = self.pre.lock().expect("HTTP pre lock poisoned").clone();
        let pre = if let Some(pre) = existing_pre {
            pre
        } else {
            let prepared = self
                .runtime
                .prepare(&self.digest, Arc::clone(&self.bytes))
                .await
                .map_err(|error| wasmtime::format_err!(error.to_string()))?;
            let candidate = Arc::new(
                make_proxy_pre(&self.runtime, &prepared)
                    .map_err(|error| wasmtime::format_err!(error.to_string()))?,
            );
            let mut slot = self.pre.lock().expect("HTTP pre lock poisoned");
            Arc::clone(slot.get_or_insert(candidate))
        };
        let limits = StoreLimitsBuilder::new()
            .memory_size(self.runtime.config.limits.max_memory_bytes)
            .table_elements(self.runtime.config.limits.max_table_elements)
            .instances(self.runtime.config.limits.max_instances)
            .tables(100)
            .memories(100)
            .trap_on_grow_failure(true)
            .build();
        let mut wasi = wasmtime_wasi::WasiCtx::builder();
        wasi.allow_tcp(false)
            .allow_udp(false)
            .allow_ip_name_lookup(false);
        let mut store = Store::new(
            &self.runtime.engine,
            HostState {
                wasi: wasi.build(),
                http: wasmtime_wasi_http::WasiHttpCtx::new(),
                http_hooks: HttpHooks::new(self.config.outbound_grants.clone()),
                table: wasmtime::component::ResourceTable::new(),
                limits,
            },
        );
        store.limiter(|state| &mut state.limits);
        store.set_fuel(self.runtime.config.limits.fuel)?;
        // ProxyHandler enforces request and idle deadlines outside the guest
        // event loop. Keep the shared epoch ticker from interrupting this
        // Store independently.
        store.set_epoch_deadline(u64::MAX / 2);
        let proxy = pre.instantiate_async(&mut store).await?;
        self.package_tier
            .store(package_tier_code(PackageTier::Warm), Ordering::Relaxed);
        self.metrics.workers_created.fetch_add(1, Ordering::AcqRel);
        self.metrics.live_workers.fetch_add(1, Ordering::AcqRel);
        let idle_expired = Arc::new(AtomicBool::new(false));
        Ok(Instance {
            store,
            proxy,
            view: match self.profile {
                WasiProfile::Http0_3 => ViewFn::P3(wasmtime_wasi_http::p3::WasiHttpView::http),
                WasiProfile::Http0_2 => ViewFn::P2(wasmtime_wasi_http::p2::WasiHttpView::http),
                WasiProfile::Cli0_3 | WasiProfile::Cli0_2 => unreachable!(),
            },
            expiration: HttpWorkerExpiration {
                idle_timeout: self.config.idle_worker_ttl,
                request_timeout: self.config.request_timeout,
                sleep: Box::pin(tokio::time::sleep(Duration::MAX)),
                idle_expired: Arc::clone(&idle_expired),
            },
            state: HttpWorkerState {
                max_reuse: self.config.max_instance_reuse_count,
                max_concurrent: if self.profile == WasiProfile::Http0_3 {
                    self.config.max_instance_concurrent_reuse_count
                } else {
                    1
                },
                request_timeout: self.config.request_timeout,
                idle_expired,
                metrics: Arc::clone(&self.metrics),
                runtime: Arc::clone(&self.runtime),
                digest: self.digest.clone(),
                pre: Arc::clone(&self.pre),
                package_tier: Arc::clone(&self.package_tier),
            },
        })
    }
}

struct HttpWorkerExpiration {
    idle_timeout: Duration,
    request_timeout: Duration,
    sleep: Pin<Box<tokio::time::Sleep>>,
    idle_expired: Arc<AtomicBool>,
}

impl WorkerExpiration for HttpWorkerExpiration {
    fn poll(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        status: WorkerStatus,
        start: Instant,
    ) -> Poll<()> {
        let me = self.get_mut();
        let timeout = match status {
            WorkerStatus::Idle => me.idle_timeout,
            WorkerStatus::Requests | WorkerStatus::PostReturn => me.request_timeout,
        };
        let Some(deadline) = start.checked_add(timeout) else {
            return Poll::Pending;
        };
        let deadline = tokio::time::Instant::from_std(deadline);
        if deadline != me.sleep.deadline() {
            me.sleep.as_mut().reset(deadline);
        }
        match me.sleep.as_mut().poll(cx) {
            Poll::Ready(()) => {
                if status == WorkerStatus::Idle {
                    me.idle_expired.store(true, Ordering::Release);
                }
                Poll::Ready(())
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

struct HttpWorkerState {
    max_reuse: usize,
    max_concurrent: usize,
    request_timeout: Duration,
    idle_expired: Arc<AtomicBool>,
    metrics: Arc<HttpMetrics>,
    runtime: Arc<RuntimeInner>,
    digest: String,
    pre: Arc<Mutex<Option<Arc<ProxyPre<HostState>>>>>,
    package_tier: Arc<AtomicUsize>,
}

impl WorkerState for HttpWorkerState {
    type StoreData = HostState;
    type RequestId = u64;

    fn should_accept_request(&self, concurrent_count: usize, total_count: usize) -> ShouldAccept {
        if total_count >= self.max_reuse {
            ShouldAccept::Never
        } else if concurrent_count >= self.max_concurrent {
            ShouldAccept::No
        } else {
            ShouldAccept::Yes
        }
    }

    fn on_request_start(
        &self,
        _store: StoreContextMut<'_, HostState>,
        _id: u64,
        _task: GuestTaskId,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>> {
        Box::pin(tokio::time::sleep(self.request_timeout))
    }

    fn drop(&self, store: Store<HostState>, _result: wasmtime::Result<()>) {
        drop(store);
        let previous_live = self.metrics.live_workers.fetch_sub(1, Ordering::AcqRel);
        if previous_live == 1 && self.idle_expired.load(Ordering::Acquire) {
            self.metrics.idle_evictions.fetch_add(1, Ordering::AcqRel);
            *self.pre.lock().expect("HTTP pre lock poisoned") = None;
            self.runtime.demote(&self.digest);
            self.package_tier
                .store(package_tier_code(PackageTier::Warmish), Ordering::Relaxed);
        }
    }
}

fn validate_config(config: &HttpServiceConfig, runtime_max_timeout: Duration) -> Result<()> {
    if config.max_in_flight == 0
        || config.max_instance_reuse_count == 0
        || config.max_instance_concurrent_reuse_count == 0
        || config.idle_worker_ttl.is_zero()
        || config.request_timeout.is_zero()
    {
        return Err(Error::Configuration(
            "HTTP capacities, idle TTL, and request timeout must be positive".to_owned(),
        ));
    }
    if config.request_timeout > runtime_max_timeout {
        return Err(Error::Limit("timeout"));
    }
    for grant in &config.outbound_grants {
        if !matches!(grant.scheme.as_str(), "http" | "https") {
            return Err(Error::Configuration(
                "outbound HTTP grant scheme must be http or https".to_owned(),
            ));
        }
        if grant.authority.is_empty()
            || grant.authority.contains('@')
            || grant.methods.is_empty()
            || grant
                .methods
                .iter()
                .any(|method| http::Method::from_bytes(method.as_bytes()).is_err())
            || grant.max_request_bytes == 0
            || grant.max_response_bytes == 0
        {
            return Err(Error::Configuration(
                "outbound HTTP grants require an authority, at least one method, and positive body limits"
                    .to_owned(),
            ));
        }
        http::Uri::builder()
            .scheme(grant.scheme.as_str())
            .authority(grant.authority.as_str())
            .path_and_query("/")
            .build()
            .map_err(|error| {
                Error::Configuration(format!("invalid outbound HTTP origin: {error}"))
            })?;
    }
    Ok(())
}