Rust

HTTP

An HTTP framework on hyper: routes, hooks, parameters with validators, requests and responses.
Rust 5,931 lines (3,889 code)split
PartCodeCommentsBlankTotalFiles
Rust source3,8891,5085345,93117
Rust tests3414353808

Comments include doc comments. Rust tests are tests/ and #[cfg(test)] modules.

Install#

The crate lives in the Appwrite repository; depend on it from Git.

Cargo.toml
[dependencies]
utopia-http = { git = "https://github.com/appwrite/appwrite" }

Getting started#

Common tasks with HTTP, each a complete program you can copy and run.

Utopia HTTP is a small web framework: you declare routes with typed, validated parameters, add hooks that run around them, and write the response. An application is plain data, so you can build a request, run it through the application and read the response in-process, which is how every example below works. Serving it on a socket only adds a server around the same application. This guide covers the common tasks.

Define a route

Register a route for a method and a path, and give it an action that writes the response. run dispatches a request through the application.

Rust
use utopia_http::{Bytes, Http, Request, Response};

let mut http = Http::new("UTC");
http.get("/hello")?.inject("response").action(|scope| {
    Box::pin(async move {
        scope.response.text("Hello, World!");
        Ok(())
    })
});

let request = Request::new("GET", "/hello", [], Bytes::new());
let mut response = Response::new();
http.run(&request, &mut response, &http.context()).await?;

assert_eq!(response.status_code(), 200);
assert_eq!(response.content_type(), "text/plain; charset=UTF-8");
assert_eq!(response.wire().body, b"Hello, World!");

Read path and query parameters

Name a path segment with : and declare it as a param. Query string values are params too. An optional param falls back to its default when the request does not send it.

Rust
use serde_json::{Value, json};
use utopia_http::{Bytes, Http, Param, Request, Response};
use utopia_validators::Text;

let mut http = Http::new("UTC");
http.get("/users/:userId")?
    .param("userId", Param::new(json!(""), Text::new(36), "The user ID."))
    .param("fields", Param::new(json!("name"), Text::new(64), "Fields to return.").optional(true))
    .inject("response")
    .action(|scope| {
        Box::pin(async move {
            let user = scope.param("userId").and_then(Value::as_str).unwrap_or_default();
            let fields = scope.param("fields").and_then(Value::as_str).unwrap_or_default();
            let text = format!("{user}: {fields}");
            scope.response.text(&text);
            Ok(())
        })
    });

let mut bodies = Vec::new();
for target in ["/users/ada?fields=email", "/users/grace"] {
    let request = Request::new("GET", target, [], Bytes::new());
    let mut response = Response::new();
    http.run(&request, &mut response, &http.context()).await?;
    bodies.push(String::from_utf8(response.wire().body.clone())?);
}

assert_eq!(bodies, ["ada: email", "grace: name"]);

Validate parameters

Every param has a validator. A value that fails it, or a missing required param, stops the request with a 400 before the action runs. An error hook turns that into a response.

Rust
use serde_json::{Value, json};
use utopia_http::{Bytes, Http, Param, Request, Response};
use utopia_validators::{Range, WhiteList};

let mut http = Http::new("UTC");
http.get("/files")?
    .param("limit", Param::new(json!("25"), Range::new(1, 100), "Page size.").optional(true))
    .param("order", Param::new(json!("asc"), WhiteList::new(&["asc", "desc"]), "Sort order.").optional(true))
    .inject("response")
    .action(|scope| {
        Box::pin(async move {
            let limit = scope.param("limit").and_then(Value::as_str).unwrap_or_default();
            let order = scope.param("order").and_then(Value::as_str).unwrap_or_default();
            let text = format!("{limit} {order}");
            scope.response.text(&text);
            Ok(())
        })
    });
http.error().inject("error").inject("response").action(|scope| {
    Box::pin(async move {
        let error = scope.error().expect("error hooks receive the error");
        scope.response.set_status_code(error.code())?;
        scope.response.text(&error.to_string());
        Ok(())
    })
});

let mut results = Vec::new();
for target in ["/files?limit=50&order=desc", "/files?limit=500", "/files?order=random"] {
    let request = Request::new("GET", target, [], Bytes::new());
    let mut response = Response::new();
    http.run(&request, &mut response, &http.context()).await?;
    results.push(format!("{} {}", response.status_code(), String::from_utf8(response.wire().body.clone())?));
}

assert_eq!(results, [
    "200 50 desc",
    "400 Invalid `limit` param: Value must be a valid range between 1 and 100",
    "400 Invalid `order` param: Value must be one of (asc, desc)",
]);

Read a JSON body and return JSON

For POST, PUT, PATCH and DELETE, params come from the body: a JSON object or form fields. json encodes a value and sets the content type.

Rust
use serde_json::{Value, json};
use utopia_http::{Bytes, Http, Param, Request, Response};
use utopia_validators::{Range, Text};

let mut http = Http::new("UTC");
http.post("/users")?
    .param("name", Param::new(json!(""), Text::new(128), "Full name."))
    .param("age", Param::new(json!(0), Range::new(0, 150), "Age in years."))
    .inject("response")
    .action(|scope| {
        Box::pin(async move {
            let user = json!({
                "name": scope.param("name").cloned().unwrap_or(Value::Null),
                "age": scope.param("age").cloned().unwrap_or(Value::Null),
            });
            scope.response.set_status_code(201)?;
            scope.response.json(&user)?;
            Ok(())
        })
    });

let body = Bytes::from_static(br#"{"name":"Ada Lovelace","age":36}"#);
let request = Request::new("POST", "/users", [("content-type", "application/json")], body);
let mut response = Response::new();
http.run(&request, &mut response, &http.context()).await?;

assert_eq!(response.status_code(), 201);
assert_eq!(response.content_type(), "application/json; charset=UTF-8");
assert_eq!(response.wire().body, br#"{"name":"Ada Lovelace","age":36}"#);

Set headers and cookies

Read request headers from the request, and add headers and cookies to the response before sending it. Header names are case-insensitive and cookie names are lowercased.

Rust
use utopia_http::{Bytes, Cookie, Http, Request, Response};

let mut http = Http::new("UTC");
http.get("/session")?.inject("request").inject("response").action(|scope| {
    Box::pin(async move {
        let agent = scope.request.header_line_or("user-agent", "unknown");
        scope.response.add_header("Cache-Control", "no-store");
        scope.response.add_cookie(Cookie {
            name: "Session".into(),
            value: Some("abc123".into()),
            http_only: Some(true),
            ..Cookie::default()
        });
        scope.response.text(&agent);
        Ok(())
    })
});

let request = Request::new("GET", "/session", [("User-Agent", "curl/8.0")], Bytes::new());
let mut response = Response::new();
http.run(&request, &mut response, &http.context()).await?;

assert_eq!(response.header_line("cache-control").as_deref(), Some("no-store"));
assert_eq!(response.cookies()[0].1.name, "session");
assert_eq!(response.wire().body, b"curl/8.0");

Run code before and after actions

Init hooks run before the action and shutdown hooks after it. A hook without groups runs for every route. A hook with groups runs only for routes in one of them, after the global ones.

Rust
use std::sync::{Arc, Mutex};
use utopia_http::{Bytes, Http, Request, Response};

type Log = Mutex<Vec<&'static str>>;

let mut http = Http::new("UTC");
let log: Arc<Log> = Arc::default();
http.resources().set("log", log.clone());

http.init().inject("log").action(|scope| {
    Box::pin(async move {
        scope.resource::<Log>("log").unwrap().lock().unwrap().push("init");
        Ok(())
    })
});
http.init().groups(&["api"]).inject("log").action(|scope| {
    Box::pin(async move {
        scope.resource::<Log>("log").unwrap().lock().unwrap().push("api init");
        Ok(())
    })
});
http.shutdown().inject("log").action(|scope| {
    Box::pin(async move {
        scope.resource::<Log>("log").unwrap().lock().unwrap().push("shutdown");
        Ok(())
    })
});
http.get("/v1/users")?.groups(&["api"]).inject("log").action(|scope| {
    Box::pin(async move {
        scope.resource::<Log>("log").unwrap().lock().unwrap().push("action");
        Ok(())
    })
});

let request = Request::new("GET", "/v1/users", [], Bytes::new());
http.run(&request, &mut Response::new(), &http.context()).await?;

assert_eq!(*log.lock().unwrap(), ["init", "api init", "action", "shutdown"]);

Handle errors and unknown routes

An action fails by returning an error with a message and an HTTP code. The error hooks receive it, and they also run with a 404 when no route matches.

Rust
use serde_json::json;
use utopia_http::{Bytes, Error, Http, Request, Response};

let mut http = Http::new("UTC");
http.get("/teams/:teamId")?.action(|_| Box::pin(async { Err(Error::http("Team not found", 404)) }));
http.error().inject("error").inject("response").action(|scope| {
    Box::pin(async move {
        let error = scope.error().expect("error hooks receive the error");
        scope.response.set_status_code(error.code())?;
        scope.response.json(&json!({ "message": error.to_string(), "code": error.code() }))?;
        Ok(())
    })
});

let mut bodies = Vec::new();
for target in ["/teams/missing", "/nowhere"] {
    let request = Request::new("GET", target, [], Bytes::new());
    let mut response = Response::new();
    http.run(&request, &mut response, &http.context()).await?;
    bodies.push(String::from_utf8(response.wire().body.clone())?);
}

assert_eq!(bodies, [r#"{"message":"Team not found","code":404}"#, r#"{"message":"Not Found","code":404}"#]);

Share resources with actions

Register services such as a database or a mailer once, and inject them by name into the routes and hooks that need them. Resources set on a request's context are visible to that request only.

Rust
use std::sync::Arc;
use serde_json::{Value, json};
use utopia_http::{Bytes, Http, Param, Request, Response};
use utopia_validators::Text;

struct Greeter {
    greeting: String,
}

let mut http = Http::new("UTC");
http.resources().set("greeter", Arc::new(Greeter { greeting: "Hello".into() }));

http.get("/greet/:name")?
    .param("name", Param::new(json!(""), Text::new(64), "Who to greet."))
    .inject("greeter")
    .inject("locale")
    .inject("response")
    .action(|scope| {
        Box::pin(async move {
            let greeting = &scope.resource::<Greeter>("greeter").unwrap().greeting;
            let locale = scope.resource::<String>("locale").unwrap();
            let name = scope.param("name").and_then(Value::as_str).unwrap_or_default();
            let text = format!("{greeting}, {name}! ({locale})");
            scope.response.text(&text);
            Ok(())
        })
    });

let context = http.context();
context.set("locale", Arc::new("en".to_owned()));
let request = Request::new("GET", "/greet/Ada", [], Bytes::new());
let mut response = Response::new();
http.run(&request, &mut response, &context).await?;

assert_eq!(response.wire().body, b"Hello, Ada! (en)");

Serve a route under several paths and methods

alias registers the same route under another path, for example to keep a legacy URL working. routes registers one route for several methods. The action, params and hooks are defined once.

Rust
use serde_json::{Value, json};
use utopia_http::{Bytes, Http, Param, Request, Response};
use utopia_validators::Text;

let mut http = Http::new("UTC");
let mut route = http
    .get("/users/:userId")?
    .param("userId", Param::new(json!(""), Text::new(36), "The user ID."))
    .inject("response")
    .action(|scope| {
        Box::pin(async move {
            let user = scope.param("userId").and_then(Value::as_str).unwrap_or_default().to_owned();
            scope.response.text(&user);
            Ok(())
        })
    });
route.alias("/members/:userId")?;

http.routes(&["GET", "POST"], "/oauth/userinfo")?.inject("request").inject("response").action(|scope| {
    Box::pin(async move {
        let method = scope.request.method().to_owned();
        scope.response.text(&method);
        Ok(())
    })
});

let mut bodies = Vec::new();
for (method, target) in [("GET", "/users/ada"), ("GET", "/members/ada"), ("GET", "/oauth/userinfo"), ("POST", "/oauth/userinfo")] {
    let request = Request::new(method, target, [], Bytes::new());
    let mut response = Response::new();
    http.run(&request, &mut response, &http.context()).await?;
    bodies.push(String::from_utf8(response.wire().body.clone())?);
}

assert_eq!(bodies, ["ada", "ada", "GET", "POST"]);

Reference#

Utopia HTTP: a small web framework on hyper, with routes, hooks, validated parameters, dependency injection, requests, responses and a server.

An Http application holds routes (methods, path templates with :params, aliases, * wildcards, a catch-all wildcard), hooks by group (init, shutdown, options, error, start, request) and static files. A request is matched, its params are resolved (path, query or body, aliases, defaults, validators) together with the injected resources, and the route's action runs between its hooks. Request parses headers, cookies, query strings, form and multipart bodies, and JSON bodies (an empty JSON object stays an object); Response sends status, headers, cookies and the body once, compressed when the client accepts it, or in chunks. serve runs an application on a TCP socket.

Modules#

Each module groups one concept of the library. Internal ones hold items the crate re-exports at its root.

compression4 items
Response compression: choosing an algorithm from the client's `Accept-Encoding` header, and compressing bodies with it.
encodeinternal
Of response data, written straight from JSON values.
errorinternal
Errors raised by the framework and by route actions, and the `Result` type that carries them.
filesinternal
Static files loaded into memory and served by `crate::Http::run` for an exact URI match.
headersinternal
Header maps: lowercased names, each with one or more values, in the order the names were first set.
hookinternal
Hooks and routes: callbacks with declared params and injections, and the resources they are injected from.
httpinternal
The application: routes, hooks by group, request dispatch with params, validators and injections, and static files.
http::mode3 items
The names of the modes an application can run in (see `Http::set_mode`).
multipartinternal
Parsing of `multipart/form-data` bodies into form fields and uploaded files.
paramsinternal
Request parameters as JSON values: parsing query strings, form bodies and JSON bodies into `Params`.
requestinternal
The incoming request: method, path, headers, cookies, query string, form, multipart and JSON bodies, and the client's address and scheme.
responseinternal
The outgoing response: status, headers and cookies set by the application, sent once through `Response::send` (or streamed with `Response::chunk`); what reaches the connection is recorded as a `Wire`.
response::content_type13 items
Common content types.
response::same_site3 items
The `SameSite` values of a cookie.
routerinternal
The routing table: routes by HTTP method and path template, matched against request paths.
serverinternal
The HTTP/1.1 server on hyper: serves an application on a TCP socket.
telemetry7 items
The HTTP server metrics `crate::Http::run` records, following the OpenTelemetry semantic conventions, and the instrument traits an exporter implements to receive them.
trustedinternal
Which forwarded headers the server believes for the client's address and scheme, when it runs behind a proxy.
viewinternal
Server-side views: templates with escaped params, filters, nesting and HTML minification.

API#

What utopia_http exports at its root.

Structs

AppServes an `Http` application: each request runs `Http::run` in a fresh context.CompressionResponse compression settings of an application.CookieA cookie added with `Response::add_cookie`. `None` means not given.FileA loaded file: its contents and MIME type.FilesFiles loaded from directories, by URI (the path relative to the load root, starting with `/`).HeadersHeaders by lowercased name, each with its values in the order they were added. Lookups are case-insensitive.HookA callback with its declared params, injections, groups and labels.HttpA web application: its routes, hooks, static files, settings and shared resources.MatchA matched route and its path params.ParamA param a hook or route declares: its default, validator and metadata.RangeA `Range` or `Content-Range` header, parsed.RequestAn incoming HTTP request.ResourcesA container of resources by name, falling back to a parent container.ResponseAn outgoing response.RouteA route: a hook bound to HTTP methods and a path.RouteBuilderA route being configured: `Http::get` and the other route methods return it, and its setters chain.RouteMatchA request's match: the route's id and its path params.RouterThe routing table: values of type `T` (an application stores `Route`(crate::Route)s) by method and path template, each with a stable id.ScopeWhat a callback receives: its params and injections, the request being dispatched and its response.ServerOptionsServer tuning for `serve`.SettingsThe server settings of a `Mode`, with the CPU count limited by the cgroup quota. `ServerOptions` can be built from them.TrustedHeadersHeaders naming the client address and the scheme it used, read in order; the first usable value wins. Names are lowercased and trimmed, empty ones dropped.ViewA view: a template path or function, its params and filters, and an optional parent view.WireThe response on the wire: the status, headers, cookies and body bytes the response has sent so far.