hyperlane-core 22.6.9

A lightweight, high-performance, and cross-platform Rust HTTP server library built on Tokio. It simplifies modern web service development by providing built-in support for middleware, WebSocket, Server-Sent Events (SSE), and raw TCP communication. With a unified and ergonomic API across Windows, Linux, and MacOS, it enables developers to build robust, scalable, and event-driven network applications with minimal overhead and maximum flexibility.
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
use super::*;

/// Inherent implementation of [`H2DirectState`].
impl H2DirectState {
    /// Resets the underlying HTTP/2 stream after a framing error or truncation.
    pub(super) fn reset(&mut self) {
        if let Some(mut send_response) = self.get_mut_send_response().take() {
            send_response.send_reset(Reason::INTERNAL_ERROR);
        }
        if let Some(mut send_stream) = self.get_mut_send_stream().take() {
            send_stream.send_reset(Reason::INTERNAL_ERROR);
        }
        self.set_finished(true);
    }

    /// Marks the stream failed, resets it, and builds an I/O error.
    ///
    /// # Arguments
    ///
    /// - `&str` - The error message to report.
    ///
    /// # Returns
    ///
    /// - `io::Error` - The wrapped framing error.
    pub(super) fn fail(&mut self, message: &str) -> io::Error {
        self.set_failed(true);
        self.reset();
        io::Error::other(message.to_owned())
    }

    /// Drains any buffered trailing body bytes extracted after a split head.
    ///
    /// # Arguments
    ///
    /// - `&mut task::Context<'_>` - The task context for flow-control backpressure.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<()>>` - Ready once all buffered body bytes are sent.
    pub(super) fn poll_drain_pending_body(
        &mut self,
        context: &mut task::Context<'_>,
    ) -> Poll<io::Result<()>> {
        while self.get_pending_body_offset() < self.get_pending_body().len() {
            if self.get_suppress_body() {
                self.get_mut_pending_body().clear();
                self.set_pending_body_offset(0);
                return Poll::Ready(Ok(()));
            }
            let remaining: usize = self.get_pending_body().len() - self.get_pending_body_offset();
            let capacity: Poll<Option<Result<usize, H2Error>>> = {
                let Some(send_stream) = self.get_mut_send_stream().as_mut() else {
                    return Poll::Ready(Err(self.fail(MISSING_SEND_CAPACITY)));
                };
                send_stream.reserve_capacity(remaining);
                send_stream.poll_capacity(context)
            };
            match capacity {
                Poll::Ready(Some(Ok(capacity))) if capacity > 0 => {
                    let take: usize = capacity.min(remaining);
                    let start: usize = self.get_pending_body_offset();
                    let end: usize = start + take;
                    let new_len: usize = self.get_body_len() + take;
                    let chunk: Bytes = Bytes::copy_from_slice(&self.get_pending_body()[start..end]);
                    let Some(send_stream) = self.get_mut_send_stream().as_mut() else {
                        return Poll::Ready(Err(self.fail(MISSING_SEND_CAPACITY)));
                    };
                    if let Err(error) = send_stream.send_data(chunk, false) {
                        return Poll::Ready(Err(self.fail(&error.to_string())));
                    }
                    self.set_body_len(new_len);
                    self.set_pending_body_offset(end);
                }
                Poll::Ready(Some(Ok(_))) => continue,
                Poll::Ready(Some(Err(error))) => {
                    return Poll::Ready(Err(self.fail(&error.to_string())));
                }
                Poll::Ready(None) => {
                    return Poll::Ready(Err(self.fail(MISSING_SEND_CAPACITY)));
                }
                Poll::Pending => return Poll::Pending,
            }
        }
        self.get_mut_pending_body().clear();
        self.set_pending_body_offset(0);
        Poll::Ready(Ok(()))
    }

    /// Parses and emits the response head, optionally forwarding inline body bytes.
    ///
    /// # Arguments
    ///
    /// - `&mut task::Context<'_>` - The task context for flow-control backpressure.
    /// - `&[u8]` - The serialized head bytes up to and including the blank line.
    /// - `&[u8]` - Any body bytes already present in the same write.
    ///
    /// # Returns
    ///
    /// - `io::Result<usize>` - Number of inline body bytes consumed directly.
    pub(super) fn emit_head_and_inline_body(
        &mut self,
        context: &mut task::Context<'_>,
        head_bytes: &[u8],
        body_bytes: &[u8],
    ) -> io::Result<usize> {
        let parsed: ParsedProtocolHead =
            match parse_protocol_response_head(head_bytes, self.get_is_head()) {
                Ok(parsed) => parsed,
                Err(error) => return Err(self.fail(&error.to_string())),
            };
        self.set_announced_len(parsed.announced_len);
        self.set_suppress_body(parsed.suppress_body);
        let Some(mut send_response) = self.get_mut_send_response().take() else {
            return Err(self.fail("response head already sent"));
        };
        if parsed.suppress_body {
            if let Err(error) = send_response.send_response(parsed.response, true) {
                return Err(self.fail(&error.to_string()));
            }
            self.set_head_sent(true);
            self.set_finished(true);
            return Ok(body_bytes.len());
        }
        if check_body_budget(self.try_get_announced_len(), 0, body_bytes.len()) {
            send_response.send_reset(Reason::INTERNAL_ERROR);
            return Err(self.fail(OVERFLOWING_RESPONSE_BODY));
        }
        let mut send_stream: H2SendStream =
            match send_response.send_response(parsed.response, false) {
                Ok(stream) => stream,
                Err(error) => return Err(self.fail(&error.to_string())),
            };
        self.set_head_sent(true);
        if body_bytes.is_empty() {
            self.set_send_stream(Some(send_stream));
            return Ok(0);
        }
        send_stream.reserve_capacity(body_bytes.len());
        match send_stream.poll_capacity(context) {
            Poll::Ready(Some(Ok(capacity))) if capacity > 0 => {
                let take: usize = capacity.min(body_bytes.len());
                let chunk: Bytes = Bytes::copy_from_slice(&body_bytes[..take]);
                if let Err(error) = send_stream.send_data(chunk, false) {
                    return Err(self.fail(&error.to_string()));
                }
                self.set_body_len(take);
                self.set_send_stream(Some(send_stream));
                Ok(take)
            }
            Poll::Ready(Some(Ok(_))) | Poll::Pending => {
                self.set_send_stream(Some(send_stream));
                Ok(0)
            }
            Poll::Ready(Some(Err(error))) => Err(self.fail(&error.to_string())),
            Poll::Ready(None) => Err(self.fail(MISSING_SEND_CAPACITY)),
        }
    }
}

/// Inherent implementation of [`H2DirectTransport`].
impl H2DirectTransport {
    /// Creates a direct HTTP/2 transport over one request stream.
    ///
    /// # Arguments
    ///
    /// - `H2SendResponse` - The response half of the HTTP/2 stream.
    /// - `StreamPeers` - The local and remote addresses of the carrying TCP connection.
    /// - `bool` - Whether the incoming request method was `HEAD`.
    ///
    /// # Returns
    ///
    /// - `Self` - The direct HTTP/2 transport.
    pub(crate) fn new(send_response: H2SendResponse, peers: StreamPeers, is_head: bool) -> Self {
        Self {
            state: Mutex::new(H2DirectState {
                send_response: Some(send_response),
                send_stream: None,
                pending_head: Vec::new(),
                pending_body: Vec::new(),
                pending_body_offset: 0,
                head_sent: false,
                suppress_body: is_head,
                is_head,
                announced_len: None,
                body_len: 0,
                finished: false,
                failed: false,
            }),
            peers,
        }
    }
}

/// Ensures an unfinished HTTP/2 stream is reset if dropped before shutdown.
impl Drop for H2DirectTransport {
    /// Resets the stream when dropped before end-of-stream was emitted.
    fn drop(&mut self) {
        if let Ok(state) = self.get_mut_state().get_mut()
            && !state.finished
        {
            state.reset();
        }
    }
}

/// Implements [`Transport`] for [`H2DirectTransport`].
impl Transport for H2DirectTransport {
    /// Reports the remote address of the carrying TCP connection.
    ///
    /// # Returns
    ///
    /// - `Result<SocketAddr, io::Error>` - The peer socket address.
    fn peer_addr(&self) -> Result<SocketAddr, io::Error> {
        Ok(self.get_peers().remote)
    }

    /// Reports the local address of the carrying TCP connection.
    ///
    /// # Returns
    ///
    /// - `Result<SocketAddr, io::Error>` - The local socket address.
    fn local_addr(&self) -> Result<SocketAddr, io::Error> {
        Ok(self.get_peers().local)
    }
}

/// Implements [`AsyncRead`] for [`H2DirectTransport`].
impl AsyncRead for H2DirectTransport {
    /// Immediately signals EOF because request bodies are collected beforehand.
    ///
    /// # Arguments
    ///
    /// - `Pin<&mut Self>` - The pinned transport.
    /// - `&mut task::Context<'_>` - The task context.
    /// - `&mut ReadBuf<'_>` - The destination read buffer.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<()>>` - Ready with zero bytes read.
    fn poll_read(
        self: Pin<&mut Self>,
        _context: &mut task::Context<'_>,
        buffer: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        buffer.advance(0);
        Poll::Ready(Ok(()))
    }
}

/// Implements [`AsyncWrite`] for [`H2DirectTransport`].
impl AsyncWrite for H2DirectTransport {
    /// Parses and frames written HTTP/1.1 bytes directly onto the HTTP/2 stream.
    ///
    /// # Arguments
    ///
    /// - `Pin<&mut Self>` - The pinned transport.
    /// - `&mut task::Context<'_>` - The task context.
    /// - `&[u8]` - The bytes written by the handler or middleware.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<usize>>` - The number of bytes consumed.
    fn poll_write(
        self: Pin<&mut Self>,
        context: &mut task::Context<'_>,
        bytes: &[u8],
    ) -> Poll<io::Result<usize>> {
        let Ok(state) = self.get_mut().get_mut_state().get_mut() else {
            return Poll::Ready(Err(io::Error::other(MISSING_SEND_CAPACITY)));
        };
        if state.failed {
            return Poll::Ready(Err(io::Error::other(MISSING_SEND_CAPACITY)));
        }
        if state.pending_body_offset < state.pending_body.len() {
            match state.poll_drain_pending_body(context) {
                Poll::Ready(Ok(())) => {}
                Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
                Poll::Pending => return Poll::Pending,
            }
        }
        if bytes.is_empty() {
            return Poll::Ready(Ok(0));
        }
        if !state.head_sent {
            if state.pending_head.is_empty() {
                if let Some(body_start) = find_head_end(bytes) {
                    if body_start > RESPONSE_HEAD_MAX {
                        return Poll::Ready(Err(state.fail(RESPONSE_HEAD_TOO_LONG)));
                    }
                    let consumed_body: usize = match state.emit_head_and_inline_body(
                        context,
                        &bytes[..body_start],
                        &bytes[body_start..],
                    ) {
                        Ok(consumed) => consumed,
                        Err(error) => return Poll::Ready(Err(error)),
                    };
                    return Poll::Ready(Ok(body_start + consumed_body));
                }
                if bytes.len() > RESPONSE_HEAD_MAX {
                    return Poll::Ready(Err(state.fail(RESPONSE_HEAD_TOO_LONG)));
                }
                state.pending_head.extend_from_slice(bytes);
                return Poll::Ready(Ok(bytes.len()));
            }
            let accepted: usize = bytes
                .len()
                .min(RESPONSE_HEAD_MAX - state.pending_head.len());
            if accepted == 0 {
                return Poll::Ready(Err(state.fail(RESPONSE_HEAD_TOO_LONG)));
            }
            state.pending_head.extend_from_slice(&bytes[..accepted]);
            let Some(body_start) = find_head_end(&state.pending_head) else {
                if state.pending_head.len() > RESPONSE_HEAD_MAX {
                    return Poll::Ready(Err(state.fail(RESPONSE_HEAD_TOO_LONG)));
                }
                return Poll::Ready(Ok(accepted));
            };
            if body_start > RESPONSE_HEAD_MAX {
                return Poll::Ready(Err(state.fail(RESPONSE_HEAD_TOO_LONG)));
            }
            let pending_head: Vec<u8> = mem::take(&mut state.pending_head);
            let consumed_body: usize = match state.emit_head_and_inline_body(
                context,
                &pending_head[..body_start],
                &pending_head[body_start..],
            ) {
                Ok(consumed) => consumed,
                Err(error) => return Poll::Ready(Err(error)),
            };
            let trailing_start: usize = body_start + consumed_body;
            if !state.suppress_body && trailing_start < pending_head.len() {
                state
                    .pending_body
                    .extend_from_slice(&pending_head[trailing_start..]);
            }
            return Poll::Ready(Ok(accepted));
        }
        if state.suppress_body {
            return Poll::Ready(Ok(bytes.len()));
        }
        if check_body_budget(state.announced_len, state.body_len, bytes.len()) {
            return Poll::Ready(Err(state.fail(OVERFLOWING_RESPONSE_BODY)));
        }
        let current_body_len: usize = state.body_len;
        let Some(send_stream) = state.send_stream.as_mut() else {
            return Poll::Ready(Err(state.fail(OVERFLOWING_RESPONSE_BODY)));
        };
        send_stream.reserve_capacity(bytes.len());
        match send_stream.poll_capacity(context) {
            Poll::Ready(Some(Ok(capacity))) if capacity > 0 => {
                let take: usize = capacity.min(bytes.len());
                let new_len: usize = current_body_len + take;
                let chunk: Bytes = Bytes::copy_from_slice(&bytes[..take]);
                if let Err(error) = send_stream.send_data(chunk, false) {
                    return Poll::Ready(Err(state.fail(&error.to_string())));
                }
                state.body_len = new_len;
                Poll::Ready(Ok(take))
            }
            Poll::Ready(Some(Ok(_))) | Poll::Pending => Poll::Pending,
            Poll::Ready(Some(Err(error))) => Poll::Ready(Err(state.fail(&error.to_string()))),
            Poll::Ready(None) => Poll::Ready(Err(state.fail(MISSING_SEND_CAPACITY))),
        }
    }

    /// Flushes any buffered trailing body bytes onto the HTTP/2 stream.
    ///
    /// # Arguments
    ///
    /// - `Pin<&mut Self>` - The pinned transport.
    /// - `&mut task::Context<'_>` - The task context.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<()>>` - Ready once flushed.
    fn poll_flush(self: Pin<&mut Self>, context: &mut task::Context<'_>) -> Poll<io::Result<()>> {
        let Ok(state) = self.get_mut().get_mut_state().get_mut() else {
            return Poll::Ready(Err(io::Error::other(MISSING_SEND_CAPACITY)));
        };
        if state.failed {
            return Poll::Ready(Err(io::Error::other(MISSING_SEND_CAPACITY)));
        }
        state.poll_drain_pending_body(context)
    }

    /// Validates the completed response and emits the final EOS flag if needed.
    ///
    /// # Arguments
    ///
    /// - `Pin<&mut Self>` - The pinned transport.
    /// - `&mut task::Context<'_>` - The task context.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<()>>` - Ready once finalized or reset on protocol violation.
    fn poll_shutdown(
        self: Pin<&mut Self>,
        context: &mut task::Context<'_>,
    ) -> Poll<io::Result<()>> {
        let Ok(state) = self.get_mut().get_mut_state().get_mut() else {
            return Poll::Ready(Err(io::Error::other(MISSING_SEND_CAPACITY)));
        };
        if state.failed {
            state.reset();
            return Poll::Ready(Err(io::Error::other(MISSING_SEND_CAPACITY)));
        }
        match state.poll_drain_pending_body(context) {
            Poll::Ready(Ok(())) => {}
            Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
            Poll::Pending => return Poll::Pending,
        }
        if !state.head_sent {
            return Poll::Ready(Err(state.fail(MISSING_RESPONSE_HEAD)));
        }
        if !state.suppress_body
            && let Some(announced) = state.announced_len
            && state.body_len != announced
        {
            return Poll::Ready(Err(state.fail(TRUNCATED_RESPONSE_BODY)));
        }
        if !state.finished {
            state.finished = true;
            if let Some(mut send_stream) = state.send_stream.take()
                && let Err(error) = send_stream.send_data(Bytes::new(), true)
            {
                return Poll::Ready(Err(state.fail(&error.to_string())));
            }
        }
        Poll::Ready(Ok(()))
    }
}