sib 0.0.17

A high-performance, secure, and cross-platform modules optimized for efficiency, scalability, and reliability.
Documentation
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
use crate::network::http::session::Session;
use bytes::Bytes;
use http::{HeaderMap, HeaderName, HeaderValue, Method, StatusCode, Uri, Version, header};
use std::net::IpAddr;

#[cfg(feature = "net-ws-server")]
use crate::network::http::ws;

/// Async HTTP/1.x session
pub struct H1SessionAsync<'a, S> {
    peer: IpAddr,

    // request
    method: Method,
    req_body: Bytes,
    req_headers: HeaderMap,
    uri: Uri,
    version: Version,

    // response
    rsp_body: Bytes,
    rsp_headers: HeaderMap,
    rsp_status: StatusCode,

    // connection state
    keep_alive: bool,
    sent: bool,

    // streaming state (HTTP/1.1 chunked)
    h1_streaming_headers_sent: bool,
    h1_streaming: bool,

    // underlying transport
    stream: &'a mut S,

    #[cfg(feature = "net-ws-server")]
    ws_scratch: bytes::BytesMut,

    #[cfg(feature = "net-ws-server")]
    is_ws: bool,
}

impl<'a, S> H1SessionAsync<'a, S> {
    #[allow(unused_variables)]
    pub fn new(
        peer: IpAddr,
        stream: &'a mut S,
        method_version: (Method, Version),
        uri: Uri,
        req: (HeaderMap, Bytes),
        keep_alive: bool,
        is_ws: bool,
    ) -> Self {
        Self {
            peer,
            stream,
            method: method_version.0,
            uri,
            version: method_version.1,
            req_headers: req.0,
            req_body: req.1,

            rsp_body: Bytes::new(),
            rsp_headers: HeaderMap::new(),
            rsp_status: StatusCode::OK,

            keep_alive,
            sent: false,

            h1_streaming_headers_sent: false,
            h1_streaming: false,

            #[cfg(feature = "net-ws-server")]
            ws_scratch: bytes::BytesMut::new(),

            #[cfg(feature = "net-ws-server")]
            is_ws,
        }
    }

    #[inline]
    pub fn keep_alive(&self) -> bool {
        self.keep_alive
    }

    #[inline]
    pub fn response_sent(&self) -> bool {
        self.sent
    }

    fn write_blocking(&mut self, data: &[u8]) -> std::io::Result<()>
    where
        S: tokio::io::AsyncWrite + Unpin,
    {
        use tokio::io::AsyncWriteExt;

        let handle = tokio::runtime::Handle::try_current()
            .map_err(|_| std::io::Error::other("H1SessionAsync requires a Tokio runtime"))?;

        // block_in_place is only valid on multi-thread runtime.
        if handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::CurrentThread {
            return Err(std::io::Error::other(
                "H1SessionAsync blocking writes are not supported on Tokio current-thread runtime. \
                 Use the async APIs (eom_async / streaming async) or run a multi-thread runtime.",
            ));
        }

        tokio::task::block_in_place(|| {
            handle.block_on(async {
                self.stream.write_all(data).await?;
                self.stream.flush().await?;
                Ok::<(), std::io::Error>(())
            })
        })
    }

    #[inline]
    fn response_must_not_have_body(&self) -> bool {
        let code = self.rsp_status.as_u16();
        self.method == Method::HEAD
            || (100..200).contains(&code)
            || self.rsp_status == StatusCode::NO_CONTENT
            || self.rsp_status == StatusCode::NOT_MODIFIED
    }

    #[cfg(feature = "net-ws-server")]
    #[inline]
    pub fn ws_seed(&mut self, data: &[u8]) {
        if !data.is_empty() {
            self.ws_scratch.extend_from_slice(data);
        }
    }
}

#[async_trait::async_trait(?Send)]
impl<'a, S> Session for H1SessionAsync<'a, S>
where
    S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin,
{
    #[inline]
    fn peer_addr(&self) -> &IpAddr {
        &self.peer
    }

    #[inline]
    fn req_host(&self) -> Option<(String, Option<u16>)> {
        let v = self.req_headers.get(header::HOST)?.to_str().ok()?;
        crate::network::http::server::parse_authority(v)
    }

    #[inline]
    fn req_method(&self) -> Method {
        self.method.clone()
    }

    #[inline]
    fn req_method_str(&self) -> Option<&str> {
        Some(self.method.as_str())
    }

    #[inline]
    fn req_path(&self) -> String {
        self.uri.path().to_string()
    }

    #[inline]
    fn req_path_bytes(&self) -> &[u8] {
        self.uri.path().as_bytes()
    }

    #[inline]
    fn req_query(&self) -> String {
        self.uri.query().map(|q| q.to_string()).unwrap_or_default()
    }

    #[inline]
    fn req_http_version(&self) -> Version {
        self.version
    }

    #[inline]
    fn req_headers(&self) -> HeaderMap {
        self.req_headers.clone()
    }

    #[inline]
    fn req_header(&self, header: &HeaderName) -> Option<HeaderValue> {
        self.req_headers.get(header).cloned()
    }

    #[inline]
    fn req_body(&mut self, _timeout: std::time::Duration) -> std::io::Result<&[u8]> {
        Ok(&self.req_body)
    }

    #[inline]
    async fn req_body_async(
        &mut self,
        _timeout: std::time::Duration,
    ) -> Option<std::io::Result<Bytes>> {
        Some(Ok(self.req_body.clone()))
    }

    #[inline]
    fn write_all_eom(&mut self, status: &[u8]) -> std::io::Result<()> {
        self.status_code(StatusCode::from_bytes(status).map_err(std::io::Error::other)?);
        self.eom()
    }

    #[inline]
    fn status_code(&mut self, code: StatusCode) -> &mut Self {
        self.rsp_status = code;
        self
    }

    #[inline]
    fn start_h1_streaming(&mut self) -> std::io::Result<()> {
        // If we’re on a current-thread runtime, this returns an error instead of panicking.
        // Prefer calling the handler using async + send_h1_data_async() via the service layer
        // or switch the server runtime to multi-thread
        if self.sent {
            return Err(std::io::Error::other("response already sent"));
        }
        if self.h1_streaming {
            return Ok(());
        }
        self.h1_streaming = true;
        self.rsp_headers.remove(header::CONTENT_LENGTH);
        self.rsp_headers.insert(
            header::TRANSFER_ENCODING,
            HeaderValue::from_static("chunked"),
        );

        if !self.keep_alive {
            self.rsp_headers
                .insert(header::CONNECTION, HeaderValue::from_static("close"));
        } else if self.version == Version::HTTP_10 {
            self.rsp_headers
                .insert(header::CONNECTION, HeaderValue::from_static("keep-alive"));
        }

        let ver = match self.version {
            Version::HTTP_10 => "HTTP/1.0",
            _ => "HTTP/1.1",
        };
        let reason = self.rsp_status.canonical_reason().unwrap_or("OK");

        let mut head = String::with_capacity(256);
        head.push_str(ver);
        head.push(' ');
        head.push_str(self.rsp_status.as_str());
        head.push(' ');
        head.push_str(reason);
        head.push_str("\r\n");

        for (k, v) in self.rsp_headers.iter() {
            head.push_str(k.as_str());
            head.push_str(": ");
            head.push_str(v.to_str().unwrap_or_default());
            head.push_str("\r\n");
        }
        head.push_str("\r\n");

        self.write_blocking(head.as_bytes())?;
        self.h1_streaming_headers_sent = true;
        Ok(())
    }

    /// Async header flush for H1 streaming (chunked).
    async fn start_h1_streaming_async(&mut self) -> std::io::Result<()> {
        use tokio::io::AsyncWriteExt;

        if self.sent {
            return Err(std::io::Error::other("response already sent"));
        }
        if self.h1_streaming {
            return Ok(());
        }

        self.h1_streaming = true;
        self.h1_streaming_headers_sent = false;

        // Switch to chunked mode
        self.rsp_headers.remove(header::CONTENT_LENGTH);
        self.rsp_headers.insert(
            header::TRANSFER_ENCODING,
            HeaderValue::from_static("chunked"),
        );

        // Connection header
        if !self.keep_alive {
            self.rsp_headers
                .insert(header::CONNECTION, HeaderValue::from_static("close"));
        } else if self.version == Version::HTTP_10 {
            self.rsp_headers
                .insert(header::CONNECTION, HeaderValue::from_static("keep-alive"));
        }

        // Status line + headers
        let ver = match self.version {
            Version::HTTP_10 => "HTTP/1.0",
            _ => "HTTP/1.1",
        };
        let reason = self.rsp_status.canonical_reason().unwrap_or("OK");

        let mut head = String::with_capacity(256);
        head.push_str(ver);
        head.push(' ');
        head.push_str(self.rsp_status.as_str());
        head.push(' ');
        head.push_str(reason);
        head.push_str("\r\n");

        for (k, v) in self.rsp_headers.iter() {
            head.push_str(k.as_str());
            head.push_str(": ");
            head.push_str(v.to_str().unwrap_or_default());
            head.push_str("\r\n");
        }
        head.push_str("\r\n");

        self.stream.write_all(head.as_bytes()).await?;
        self.stream.flush().await?;
        self.h1_streaming_headers_sent = true;
        Ok(())
    }

    #[inline]
    fn start_h2_streaming(&mut self) -> std::io::Result<super::h2_session::H2Stream> {
        Err(std::io::Error::other(
            "start_h2_streaming is not supported in H1SessionAsync",
        ))
    }

    #[inline]
    async fn start_h3_streaming(&mut self) -> std::io::Result<()> {
        Err(std::io::Error::other(
            "start_h3_streaming is not supported in H1SessionAsync",
        ))
    }

    #[inline]
    fn send_h1_data(&mut self, data: &[u8], last: bool) -> std::io::Result<()> {
        if self.sent {
            return Err(std::io::Error::other("response already sent"));
        }
        if !self.h1_streaming {
            return Err(std::io::Error::other(
                "start_h1_streaming() must be called before send_h1_data()",
            ));
        }
        if !self.h1_streaming_headers_sent {
            return Err(std::io::Error::other(
                "internal error: streaming headers not sent",
            ));
        }

        // Chunk frame: "<HEX>\r\n<data>\r\n"
        if !data.is_empty() {
            use core::fmt::Write;
            let mut s = heapless::String::<32>::new();
            write!(&mut s, "{:X}\r\n", data.len()).map_err(std::io::Error::other)?;
            self.write_blocking(s.as_bytes())?;
            self.write_blocking(data)?;
            self.write_blocking(b"\r\n")?;
        }

        if last {
            self.write_blocking(b"0\r\n\r\n")?;
            self.sent = true;
        }

        Ok(())
    }

    /// Async chunk send. If `last` is true, sends terminating chunk.
    async fn send_h1_data_async(&mut self, data: &[u8], last: bool) -> std::io::Result<()> {
        use core::fmt::Write as _;
        use tokio::io::AsyncWriteExt;

        if self.sent {
            return Err(std::io::Error::other("response already sent"));
        }
        if !self.h1_streaming {
            return Err(std::io::Error::other(
                "start_h1_streaming() must be called before send_h1_data()",
            ));
        }
        if !self.h1_streaming_headers_sent {
            // If someone toggled state without emitting headers, fix it
            self.start_h1_streaming_async().await?;
        }

        // Chunk frame: "<HEX>\r\n<data>\r\n"
        if !data.is_empty() {
            let mut s = heapless::String::<32>::new();
            write!(&mut s, "{:X}\r\n", data.len()).map_err(std::io::Error::other)?;
            self.stream.write_all(s.as_bytes()).await?;
            self.stream.write_all(data).await?;
            self.stream.write_all(b"\r\n").await?;
        }

        if last {
            self.stream.write_all(b"0\r\n\r\n").await?;
            self.stream.flush().await?;
            self.sent = true;
        } else {
            self.stream.flush().await?;
        }

        Ok(())
    }

    #[inline]
    async fn send_h3_data(&mut self, _chunk: Bytes, _end_stream: bool) -> std::io::Result<()> {
        Err(std::io::Error::other(
            "send_h3_data is not supported in H1SessionAsync",
        ))
    }

    #[inline]
    fn header(&mut self, key: HeaderName, val: HeaderValue) -> std::io::Result<&mut Self> {
        self.rsp_headers.insert(key, val);
        Ok(self)
    }

    #[inline]
    fn header_str(&mut self, name: &str, value: &str) -> std::io::Result<&mut Self> {
        let header_name = HeaderName::from_bytes(name.as_bytes()).map_err(std::io::Error::other)?;
        let header_value = HeaderValue::from_str(value).map_err(std::io::Error::other)?;
        self.rsp_headers.insert(header_name, header_value);
        Ok(self)
    }

    #[inline]
    fn headers(&mut self, headers: &HeaderMap) -> std::io::Result<&mut Self> {
        for (k, v) in headers.iter() {
            self.rsp_headers.insert(k.clone(), v.clone());
        }
        Ok(self)
    }

    #[inline]
    fn headers_str(&mut self, header_val: &[(&str, &str)]) -> std::io::Result<&mut Self> {
        for (name, value) in header_val.iter() {
            let header_name =
                HeaderName::from_bytes(name.as_bytes()).map_err(std::io::Error::other)?;
            let header_value = HeaderValue::from_str(value).map_err(std::io::Error::other)?;
            self.rsp_headers.insert(header_name, header_value);
        }
        Ok(self)
    }

    #[inline]
    fn body(&mut self, body: Bytes) -> &mut Self {
        self.rsp_body = body;
        self
    }

    #[inline]
    fn eom(&mut self) -> std::io::Result<()> {
        if self.sent {
            return Err(std::io::Error::other("response already sent"));
        }

        if self.h1_streaming {
            if !self.h1_streaming_headers_sent {
                // will error (not panic) on current-thread runtime
                self.start_h1_streaming()?;
            }
            self.write_blocking(b"0\r\n\r\n")?;
            self.sent = true;
            return Ok(());
        }

        let handle = tokio::runtime::Handle::try_current()
            .map_err(|_| std::io::Error::other("H1SessionAsync requires a Tokio runtime"))?;

        if handle.runtime_flavor() == tokio::runtime::RuntimeFlavor::CurrentThread {
            return Err(std::io::Error::other(
                "H1SessionAsync::eom() (sync) is not supported on Tokio current-thread runtime; \
                 use eom_async().await or run a multi-thread runtime.",
            ));
        }

        tokio::task::block_in_place(|| handle.block_on(async { self.eom_async().await }))
    }

    #[inline]
    async fn eom_async(&mut self) -> std::io::Result<()> {
        use tokio::io::AsyncWriteExt;

        // If the response MUST NOT have a body, force empty body and Content-Length: 0
        if self.response_must_not_have_body() {
            self.rsp_body = Bytes::new();
            self.rsp_headers.remove(header::TRANSFER_ENCODING);
            self.rsp_headers
                .insert(header::CONTENT_LENGTH, HeaderValue::from_static("0"));
        } else {
            // Ensure Content-Length cause we’re not doing chunked responses here
            if !self.rsp_headers.contains_key(header::CONTENT_LENGTH) {
                let len = self.rsp_body.len().to_string();
                self.rsp_headers.insert(
                    header::CONTENT_LENGTH,
                    HeaderValue::from_str(&len).map_err(std::io::Error::other)?,
                );
            }
        }

        // Connection header
        if !self.keep_alive {
            self.rsp_headers
                .insert(header::CONNECTION, HeaderValue::from_static("close"));
        } else if self.version == Version::HTTP_10 {
            self.rsp_headers
                .insert(header::CONNECTION, HeaderValue::from_static("keep-alive"));
        }

        let ver = match self.version {
            Version::HTTP_10 => "HTTP/1.0",
            _ => "HTTP/1.1",
        };
        let reason = self.rsp_status.canonical_reason().unwrap_or("OK");

        let mut head = String::with_capacity(256);
        head.push_str(ver);
        head.push(' ');
        head.push_str(self.rsp_status.as_str());
        head.push(' ');
        head.push_str(reason);
        head.push_str("\r\n");

        for (k, v) in self.rsp_headers.iter() {
            head.push_str(k.as_str());
            head.push_str(": ");
            head.push_str(v.to_str().unwrap_or_default());
            head.push_str("\r\n");
        }
        head.push_str("\r\n");

        self.stream.write_all(head.as_bytes()).await?;
        if !self.rsp_body.is_empty() {
            self.stream.write_all(&self.rsp_body).await?;
        }
        self.stream.flush().await?;
        self.sent = true;
        Ok(())
    }

    #[cfg(feature = "net-ws-server")]
    #[inline]
    fn is_ws(&self) -> bool {
        self.is_ws
    }

    #[cfg(all(feature = "net-ws-server", feature = "net-h1-server"))]
    #[inline]
    fn ws_accept(&mut self) -> io::Result<()> {
        Err(std::io::Error::other(
            "ws_accept is not implemented for H1SessionAsync, use ws_accept_async instead",
        ))
    }

    #[cfg(feature = "net-ws-server")]
    async fn ws_accept_async(&mut self) -> std::io::Result<()> {
        use crate::network::http::ws;

        let key = self
            .req_headers
            .get("sec-websocket-key")
            .and_then(|v| v.to_str().ok())
            .ok_or_else(|| std::io::Error::other("missing sec-websocket-key"))?;

        let accept = ws::sec_websocket_accept(key)?;
        // You already have response builder primitives; simplest:
        self.status_code(http::StatusCode::SWITCHING_PROTOCOLS);
        self.header(
            http::header::UPGRADE,
            http::HeaderValue::from_static("websocket"),
        )?;
        self.header(
            http::header::CONNECTION,
            http::HeaderValue::from_static("Upgrade"),
        )?;
        self.header(
            http::header::SEC_WEBSOCKET_ACCEPT,
            http::HeaderValue::from_str(&accept).map_err(std::io::Error::other)?,
        )?;

        // End response headers with empty body
        self.body(bytes::Bytes::new());
        self.eom_async().await?;

        self.is_ws = true;
        Ok(())
    }

    #[cfg(all(feature = "net-ws-server", feature = "net-h1-server"))]
    #[inline]
    fn ws_read(&mut self) -> io::Result<(crate::network::http::ws::OpCode, &[u8], bool)> {
        Err(std::io::Error::other(
            "ws_read is not implemented for H1SessionAsync, use ws_read_async instead",
        ))
    }

    #[cfg(feature = "net-ws-server")]
    async fn ws_read_async(&mut self) -> std::io::Result<(ws::OpCode, bytes::Bytes, bool)> {
        use crate::network::http::ws;
        if !self.is_ws {
            return Err(std::io::Error::other(
                "ws_read_async before ws_accept_async",
            ));
        }
        ws::ws_read_from_io(&mut self.stream, &mut self.ws_scratch, 1 << 20).await
    }

    #[cfg(all(feature = "net-ws-server", feature = "net-h1-server"))]
    #[inline]
    fn ws_write(
        &mut self,
        _op: crate::network::http::ws::OpCode,
        _payload: &[u8],
        _fin: bool,
    ) -> io::Result<()> {
        Err(std::io::Error::other(
            "ws_write is not implemented for H1SessionAsync, use ws_write_async instead",
        ))
    }

    #[cfg(feature = "net-ws-server")]
    async fn ws_write_async(
        &mut self,
        op: ws::OpCode,
        payload: bytes::Bytes,
        fin: bool,
    ) -> std::io::Result<()> {
        use crate::network::http::ws;
        if !self.is_ws {
            return Err(std::io::Error::other(
                "ws_write_async before ws_accept_async",
            ));
        }
        ws::ws_write_to_io(&mut self.stream, op, payload, fin).await
    }

    #[cfg(feature = "net-ws-server")]
    async fn ws_close_async(&mut self, reason: Option<bytes::Bytes>) -> std::io::Result<()> {
        use crate::network::http::ws;
        let payload = reason.unwrap_or_else(|| ws::close_payload(1000, "bye"));
        let _ = self.ws_write_async(ws::OpCode::Close, payload, true).await;
        Ok(())
    }

    #[cfg(all(feature = "net-ws-server", feature = "net-h1-server"))]
    fn ws_close(&mut self, _reason: Option<&[u8]>) -> std::io::Result<()> {
        Err(std::io::Error::other(
            "ws_close is not implemented for H1SessionAsync",
        ))
    }
}