picoserve 0.18.0

An async no_std HTTP server suitable for bare-metal environments
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
//! Types and traits for extracting data from requests.
//!
//! A handler function is an async function that takes any number of "extractors" as arguments. All arguments must implement [`FromRequestParts`], and the final extractory may optionally implement [`FromRequest`].
//!
//! For example:
//!
//! + [`State<T>`] will extract part or all of the application state.
//! + [`Form<T: serde::DeserializeOwned>`] will extract the body of a request as Form data.
//!
//! For an example of how to implement [`FromRequest`], see [custom_extractor](https://github.com/sammhicks/picoserve/blob/main/examples/custom_extractor/src/main.rs)
//!
//! ## Requests and Borrowing
//!
//! Although [`RequestHandlerFunctions`](crate::routing::RequestHandlerFunction) may not borrow from request due to restrictions with Higher-Order-Lifetime-Bounds, by using [`from_request`](crate::from_request) and [`from_request_parts`](crate::from_request_parts), [`RequestHandlerServices`](crate::routing::RequestHandlerService) and [`PathRouterServices`](crate::routing::PathRouterService) may do so.

use crate::{
    self as picoserve,
    io::{Error, Read, ReadExt},
    request::{ReadAllBodyError, RequestBody, RequestParts},
    response::{ErrorWithStatusCode, IntoResponse},
};

#[cfg(feature = "json")]
pub mod json {
    pub use crate::json::Json;

    pub use serde_json_core::str;

    /// A JSON encoded value. `UNESCAPE_BUFFER_SIZE` is the size of the temporary buffer used for unescaping strings.
    pub struct JsonWithUnescapeBufferSize<T, const UNESCAPE_BUFFER_SIZE: usize>(pub T);
}

#[cfg(feature = "json")]
pub use json::{Json, JsonWithUnescapeBufferSize};

mod private {
    pub struct ViaRequest;
    pub struct ViaParts;
}

/// Types that can be created from requests parts (everything except the request body).
pub trait FromRequestParts<'r, State>: Sized {
    /// If the extractor fails this “rejection” type is returned, which converted into a response and returned.
    type Rejection: IntoResponse + 'static;

    /// Attempt to extract from the request parts.
    async fn from_request_parts(
        state: &'r State,
        request_parts: &RequestParts<'r>,
    ) -> Result<Self, Self::Rejection>;
}

/// Extract values from Request Parts. Each `$name` must implement [`FromRequestParts`], but may borrow from the request.
/// If extraction is rejected, the rejection is written to `$response_writer` and the function returns.
#[macro_export]
macro_rules! from_request_parts {
    ($state:ident, $request:ident, $response_writer:ident $(,$name:ty)* $(,)?) => {
        (
            $(
                match <$name as $crate::extract::FromRequestParts<_>>::from_request_parts($state, &$request.parts).await {
                    Ok(value) => value,
                    Err(err) => return err.write_to($request.body_connection.finalize().await?, $response_writer).await,
                }
            ),*
        )
    };
}

/// Types that can be created from requests.
pub trait FromRequest<'r, State, M = private::ViaRequest>: Sized {
    /// If the extractor fails this “rejection” type is returned, which converted into a response and returned.
    type Rejection: IntoResponse + 'static;

    /// Attempt to extract from the request.
    async fn from_request<R: Read>(
        state: &'r State,
        request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection>;
}

/// Extract a value from a request. `$name` must implement [`FromRequest`], but may borrow from the request.
/// If extraction is rejected, the rejection is written to `$response_writer` and the function returns.
#[macro_export]
macro_rules! from_request {
    ($state:ident, $request:ident, $response_writer:ident, $name:ty $(,)?) => {
        match <$name as $crate::extract::FromRequest<_, _>>::from_request(
            $state,
            $request.parts,
            $request.body_connection.body(),
        )
        .await
        {
            Ok(value) => value,
            Err(err) => {
                return err
                    .write_to($request.body_connection.finalize().await?, $response_writer)
                    .await
            }
        }
    };
}

impl<'r, State> FromRequest<'r, State> for &'r mut [u8] {
    type Rejection = ReadAllBodyError;

    async fn from_request<R: Read>(
        _state: &'r State,
        _request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        request_body.read_all().await
    }
}

impl<'r, State> FromRequest<'r, State> for &'r [u8] {
    type Rejection = ReadAllBodyError;

    async fn from_request<R: Read>(
        state: &'r State,
        request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        <&'r mut [u8]>::from_request(state, request_parts, request_body)
            .await
            .map(|body| &*body)
    }
}

impl<'r, State, const N: usize> FromRequest<'r, State> for heapless::Vec<u8, N> {
    type Rejection = ReadAllBodyError;

    async fn from_request<R: Read>(
        _state: &'r State,
        _request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        let mut buffer = Self::new();

        let content_length = request_body.content_length();

        buffer
            .resize(request_body.content_length(), 0)
            .map_err(|()| ReadAllBodyError::BufferIsTooSmall {
                content_length,
                buffer_length: N,
            })?;

        request_body
            .reader()
            .read_exact(buffer.as_mut_slice())
            .await
            .map_err(|error| match error {
                embedded_io_async::ReadExactError::UnexpectedEof => ReadAllBodyError::UnexpectedEof,
                embedded_io_async::ReadExactError::Other(error) => {
                    ReadAllBodyError::IO(error.kind())
                }
            })?;

        Ok(buffer)
    }
}

#[cfg(any(test, feature = "alloc"))]
impl<'r, State> FromRequest<'r, State> for alloc::vec::Vec<u8> {
    type Rejection = ReadAllBodyError;

    async fn from_request<R: Read>(
        _state: &'r State,
        _request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        let mut buffer = alloc::vec::Vec::new();

        let content_length = request_body.content_length();

        buffer.try_reserve_exact(content_length).map_err(|_| {
            ReadAllBodyError::BufferIsTooSmall {
                content_length,
                buffer_length: request_body.buffer_length(),
            }
        })?;

        buffer.resize(content_length, 0);

        request_body
            .reader()
            .read_exact(buffer.as_mut_slice())
            .await
            .map_err(|error| match error {
                embedded_io_async::ReadExactError::UnexpectedEof => ReadAllBodyError::UnexpectedEof,
                embedded_io_async::ReadExactError::Other(error) => {
                    ReadAllBodyError::IO(error.kind())
                }
            })?;

        Ok(buffer)
    }
}

#[cfg(any(test, feature = "alloc"))]
impl<'r, State> FromRequest<'r, State> for alloc::borrow::Cow<'r, [u8]> {
    type Rejection = ReadAllBodyError;

    async fn from_request<R: Read>(
        state: &'r State,
        request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        if request_body.entire_body_fits_into_buffer() {
            <&'r [u8]>::from_request(state, request_parts, request_body)
                .await
                .map(alloc::borrow::Cow::Borrowed)
        } else {
            alloc::vec::Vec::<u8>::from_request(state, request_parts, request_body)
                .await
                .map(alloc::borrow::Cow::Owned)
        }
    }
}

/// Errors arising while reading the entire body as a UTF-8 String.
#[derive(Debug, thiserror::Error, ErrorWithStatusCode)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum FailedToExtractEntireBodyAsStringError {
    #[error(transparent)]
    #[status_code(transparent)]
    FailedToExtractEntireBody(ReadAllBodyError),
    #[error("Body is not UTF-8: {0}")]
    #[status_code(BAD_REQUEST)]
    StringIsNotUtf8(#[cfg_attr(feature = "defmt", defmt(Debug2Format))] core::str::Utf8Error),
}

impl<'r, State> FromRequest<'r, State> for &'r mut str {
    type Rejection = FailedToExtractEntireBodyAsStringError;

    async fn from_request<R: Read>(
        state: &'r State,
        request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        core::str::from_utf8_mut(
            <&'r mut [u8]>::from_request(state, request_parts, request_body)
                .await
                .map_err(FailedToExtractEntireBodyAsStringError::FailedToExtractEntireBody)?,
        )
        .map_err(FailedToExtractEntireBodyAsStringError::StringIsNotUtf8)
    }
}

impl<'r, State> FromRequest<'r, State> for &'r str {
    type Rejection = FailedToExtractEntireBodyAsStringError;

    async fn from_request<R: Read>(
        state: &'r State,
        request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        <&'r mut str>::from_request(state, request_parts, request_body)
            .await
            .map(|body| &*body)
    }
}

impl<'r, State, const N: usize> FromRequest<'r, State> for heapless::String<N> {
    type Rejection = FailedToExtractEntireBodyAsStringError;

    async fn from_request<R: Read>(
        state: &'r State,
        request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        heapless::String::from_utf8(
            heapless::Vec::from_request(state, request_parts, request_body)
                .await
                .map_err(FailedToExtractEntireBodyAsStringError::FailedToExtractEntireBody)?,
        )
        .map_err(FailedToExtractEntireBodyAsStringError::StringIsNotUtf8)
    }
}

#[cfg(any(test, feature = "alloc"))]
impl<'r, State> FromRequest<'r, State> for alloc::string::String {
    type Rejection = FailedToExtractEntireBodyAsStringError;

    async fn from_request<R: Read>(
        state: &'r State,
        request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        alloc::string::String::from_utf8(
            alloc::vec::Vec::from_request(state, request_parts, request_body)
                .await
                .map_err(FailedToExtractEntireBodyAsStringError::FailedToExtractEntireBody)?,
        )
        .map_err(|err| FailedToExtractEntireBodyAsStringError::StringIsNotUtf8(err.utf8_error()))
    }
}

#[cfg(any(test, feature = "alloc"))]
impl<'r, State> FromRequest<'r, State> for alloc::borrow::Cow<'r, str> {
    type Rejection = FailedToExtractEntireBodyAsStringError;

    async fn from_request<R: Read>(
        state: &'r State,
        request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        if request_body.entire_body_fits_into_buffer() {
            <&'r str>::from_request(state, request_parts, request_body)
                .await
                .map(alloc::borrow::Cow::Borrowed)
        } else {
            alloc::string::String::from_request(state, request_parts, request_body)
                .await
                .map(alloc::borrow::Cow::Owned)
        }
    }
}

impl<'r, State, T: FromRequestParts<'r, State>> FromRequest<'r, State, private::ViaParts> for T
where
    T::Rejection: 'static,
{
    type Rejection = <Self as FromRequestParts<'r, State>>::Rejection;

    async fn from_request<R: Read>(
        state: &'r State,
        request_parts: RequestParts<'r>,
        _request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        Self::from_request_parts(state, &request_parts).await
    }
}

/// Extractor that deserializes query strings into some type.
pub struct Query<T: serde::de::DeserializeOwned>(pub T);

impl<T: serde::de::DeserializeOwned> core::ops::Deref for Query<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: serde::de::DeserializeOwned> core::ops::DerefMut for Query<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// Rejection used for [`Query`].
#[derive(Debug, thiserror::Error, ErrorWithStatusCode)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[error("Bad Query")]
#[status_code(BAD_REQUEST)]
pub struct QueryRejection;

impl<'r, State, T: serde::de::DeserializeOwned> FromRequestParts<'r, State> for Query<T> {
    type Rejection = QueryRejection;

    async fn from_request_parts(
        _state: &'r State,
        request_parts: &RequestParts<'r>,
    ) -> Result<Self, Self::Rejection> {
        super::url_encoded::deserialize_form(request_parts.query().unwrap_or_default())
            .map(Self)
            .map_err(|super::url_encoded::FormDeserializationError| QueryRejection)
    }
}

/// URL encoded extractor.
pub struct Form<T: serde::de::DeserializeOwned>(pub T);

impl<T: serde::de::DeserializeOwned> core::ops::Deref for Form<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: serde::de::DeserializeOwned> core::ops::DerefMut for Form<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// Rejection used for [`Form`].
#[derive(Debug, thiserror::Error, ErrorWithStatusCode)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[status_code(BAD_REQUEST)]
pub enum FormRejection {
    /// Error decoding the body as UTF-8
    #[error("Body is not UTF-8")]
    BodyIsNotUtf8,
    /// Error deserializing Form
    #[error("Bad Form")]
    BadForm,
}

impl<'r, State, T: serde::de::DeserializeOwned> FromRequest<'r, State> for Form<T> {
    type Rejection = FormRejection;

    async fn from_request<R: Read>(
        _state: &'r State,
        _request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        super::url_encoded::deserialize_form(crate::url_encoded::UrlEncodedString(
            core::str::from_utf8(
                request_body
                    .read_all()
                    .await
                    .map_err(|_| FormRejection::BadForm)?,
            )
            .map_err(|core::str::Utf8Error { .. }| FormRejection::BodyIsNotUtf8)?,
        ))
        .map(Self)
        .map_err(|super::url_encoded::FormDeserializationError| FormRejection::BadForm)
    }
}

/// Rejection used for [`Json`].
#[cfg(feature = "json")]
#[derive(Debug, thiserror::Error, ErrorWithStatusCode)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum JsonRejection {
    #[error("IO Error")]
    #[status_code(INTERNAL_SERVER_ERROR)]
    IoError,
    #[error("Failed to parse JSON body: {0}")]
    #[status_code(BAD_REQUEST)]
    #[cfg(feature = "json")]
    DeserializationError(serde_json_core::de::Error),
}

#[cfg(feature = "json")]
impl<'r, State, T: serde::Deserialize<'r>, const UNESCAPE_BUFFER_SIZE: usize>
    FromRequest<'r, State, T> for JsonWithUnescapeBufferSize<T, UNESCAPE_BUFFER_SIZE>
{
    type Rejection = JsonRejection;

    async fn from_request<R: Read>(
        _state: &'r State,
        _request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        serde_json_core::from_slice_escaped(
            request_body
                .read_all()
                .await
                .map_err(|_| JsonRejection::IoError)?,
            &mut [0; UNESCAPE_BUFFER_SIZE],
        )
        .map(|(value, _)| Self(value))
        .map_err(JsonRejection::DeserializationError)
    }
}

#[cfg(feature = "json")]
impl<'r, State, T: serde::Deserialize<'r>> FromRequest<'r, State, T> for Json<T> {
    type Rejection = JsonRejection;

    async fn from_request<R: Read>(
        state: &'r State,
        request_parts: RequestParts<'r>,
        request_body: RequestBody<'r, R>,
    ) -> Result<Self, Self::Rejection> {
        JsonWithUnescapeBufferSize::<T, 32>::from_request(state, request_parts, request_body)
            .await
            .map(|JsonWithUnescapeBufferSize(payload)| Self(payload))
    }
}

/// Used to do reference to value conversions, mainly used with the [`State`] extractor to extract parts of the application state.
pub trait FromRef<T> {
    /// Perform the reference to value conversion
    fn from_ref(input: &T) -> Self;
}

impl<T: Clone> FromRef<T> for T {
    fn from_ref(input: &T) -> Self {
        input.clone()
    }
}

/// Extracts part of the application state.
///
/// `T` must implement [`FromRef<S>`] for application state `S`.
pub struct State<T>(
    /// The value extracted from the application state
    pub T,
);

impl<'r, S, T: FromRef<S>> FromRequestParts<'r, S> for State<T> {
    type Rejection = core::convert::Infallible;

    async fn from_request_parts(
        state: &'r S,
        _request_parts: &RequestParts<'r>,
    ) -> Result<Self, Self::Rejection> {
        Ok(State(T::from_ref(state)))
    }
}

/// The Connection could not be upgraded because the "Upgrade" headed was missing.
#[derive(Debug, thiserror::Error, ErrorWithStatusCode)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[error("Connection header did not include `upgrade`")]
#[status_code(BAD_REQUEST)]
pub struct NoUpgradeHeaderError;

/// A token which allows a connection to be upgraded. Verifies that the "Upgrade" header has been set.
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct UpgradeToken(());

impl<'r, State> FromRequestParts<'r, State> for UpgradeToken {
    type Rejection = NoUpgradeHeaderError;

    async fn from_request_parts(
        _state: &'r State,
        request_parts: &RequestParts<'r>,
    ) -> Result<Self, Self::Rejection> {
        request_parts
            .headers()
            .get("upgrade")
            .map(|_| Self(()))
            .ok_or(NoUpgradeHeaderError)
    }
}

impl UpgradeToken {
    pub(crate) async fn discard_all_data<R: Read>(
        connection: crate::response::Connection<'_, R>,
    ) -> Result<(), R::Error> {
        // Consumes and discards all data, so cannot gain access to the next requests data,
        // and the connection is consumed so cannot be upgraded after this call

        connection
            .upgrade(UpgradeToken(()))
            .discard_all_data()
            .await
    }
}