32 releases (12 breaking)

new 0.13.2 Dec 10, 2024
0.12.3 Nov 24, 2024
0.12.0 Jul 22, 2024
0.10.2 Mar 19, 2024
0.2.3 Nov 18, 2023

#138 in HTTP server

Download history 73/week @ 2024-08-20 288/week @ 2024-08-27 161/week @ 2024-09-03 639/week @ 2024-09-10 467/week @ 2024-09-17 200/week @ 2024-09-24 66/week @ 2024-10-01 46/week @ 2024-10-08 20/week @ 2024-10-15 34/week @ 2024-10-22 22/week @ 2024-10-29 52/week @ 2024-11-05 13/week @ 2024-11-12 126/week @ 2024-11-19 53/week @ 2024-11-26 243/week @ 2024-12-03

467 downloads per month
Used in stemma-soil-sensor-embass…

MIT license

260KB
6.5K SLoC

picoserve

An async no_std HTTP server suitable for bare-metal environments, heavily inspired by axum. It was designed with embassy on the Raspberry Pi Pico W in mind, but should work with other embedded runtimes and hardware.

Features:

  • No heap usage
  • Handler functions are just async functions that accept zero or more extractors as arguments and returns something that implements IntoResponse
  • Query and Form parsing using serde
  • JSON responses
  • Server-Sent Events
  • Web Sockets
  • HEAD method is automatically handled

Shortcomings:

  • While in version 0.*.*, there may be breaking API changes
  • URL-Encoded strings, for example in Query and Form parsing, have a maximum length of 1024.
  • This has relatively little stress-testing so I advise not to expose it directly to the internet, but place it behind a proxy such as nginx, which will act as a security layer.
  • Certain serialization methods, such as the DebugValue response and JSON serialisation might be called several times if the response payload is large. The caller MUST ensure that the output of serialisation is the same during repeated calls with the same value.
  • The framework does not verify that the specified length of a reponse body, i.e. the value stored in the "Content-Length" header is actually the length of the body.

Usage examples

tokio (for testing purposes)

use std::time::Duration;

use picoserve::routing::get;

#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
    let port = 8000;

    let app =
        std::rc::Rc::new(picoserve::Router::new().route("/", get(|| async { "Hello World" })));

    let config = picoserve::Config::new(picoserve::Timeouts {
        start_read_request: Some(Duration::from_secs(5)),
        read_request: Some(Duration::from_secs(1)),
        write: Some(Duration::from_secs(1)),
    })
    .keep_connection_alive();

    let socket = tokio::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, port)).await?;

    println!("http://localhost:{port}/");

    tokio::task::LocalSet::new()
        .run_until(async {
            loop {
                let (stream, remote_address) = socket.accept().await?;

                println!("Connection from {remote_address}");

                let app = app.clone();
                let config = config.clone();

                tokio::task::spawn_local(async move {
                    match picoserve::serve(&app, &config, &mut [0; 2048], stream).await {
                        Ok(handled_requests_count) => {
                            println!(
                                "{handled_requests_count} requests handled from {remote_address}"
                            )
                        }
                        Err(err) => println!("{err:?}"),
                    }
                });
            }
        })
        .await
}

Dependencies

~2–11MB
~122K SLoC