← all modules
module

stdlib::http::extract

defined in C:\Users\bye45\.glide\bin/src/stdlib/http/extract.glide

Pull a typed value out of an incoming request. Implementors decide

trait trait FromRequest

Pull a typed value out of an incoming request. Implementors decide (a) how to extract, (b) what HTTP status to return when extraction fails (encoded as a "<status>:<msg>" prefix on the err string).

glide
pub struct ApiKey { pub key: string }

impl FromRequest for ApiKey {
    fn from_request(req: *HttpRequest) -> !ApiKey {
        let v: string = req.header("X-Api-Key");
        if v.len() == 0 { return err("401:missing X-Api-Key"); }
        return ok(ApiKey { key: v });
    }
}

Now usable as a handler param:
@handler fn h(key: ApiKey) -> HttpResponse { ... }
struct struct Bearer

Token extracted from Authorization: Bearer <token>. Stripping the scheme prefix is mechanical so the handler can validate the token directly (HMAC verify, DB lookup, etc).

glide
@handler
fn protected(auth: Bearer) -> HttpResponse {
    if !valid(auth.token) { return HttpResponse::with_status(403); }
    return HttpResponse::ok().text("hi");
}
struct struct Headers

Direct access to request headers from a handler. The wrapper just pins the request pointer; lookups go through req.header(name) (case-insensitive linear scan) on demand.

glide
@handler
fn echo_ua(headers: Headers) -> HttpResponse {
    return HttpResponse::ok().text(headers.get("User-Agent"));
}
trait trait AuthScheme

Parse the Authorization header into a typed scheme value. Implementors get the raw header (including the "Bearer " / "Basic " prefix) and decide how to validate it.

struct struct Authorization

Generic auth wrapper. Authorization<Bearer> extracts a Bearer token; Authorization<Basic> extracts user+pass. The 401 status + "missing/wrong scheme" semantics are shared.

struct struct Basic

Decoded user + password from Authorization: Basic base64(user:pass).

Note: v1 doesn't decode base64 — it treats the value after Basic as the literal user:pass string. Hook in stdlib::base64::decode when ready.

struct struct State

Application state injected via Router::state(p). The handler reaches it without closure capture or globals — just declare a state: State<Db> (or whatever T) param and let @handler do the lookup.

**Special-cased by the macro.** Like Path<T>, the macro emits State::extract(req) and pins the let-ty so T monomorphises correctly. Empty state slot returns 500.

glide
setup
let db: *Db = open_db();
let r: *Router = Router::new();
r.state(db as *void);

handler
@handler
fn list_users(state: State<Db>) -> Json<UserList> {
    return Json::wrap(state.val.all_users());
}
trait trait FromPath

One-byte conversion for typed path params. Reused for Query<T> fields too once we ship that.

struct struct Path

Typed access to a :name path segment captured by the router.

**Special-cased by the @handler macro.** The lookup key is the PARAM IDENT — fn h(id: Path<int>) extracts req.param("id") and converts via int::from_path. There's no annotation overhead.

glide
route /users/:id
@handler
fn get_user(id: Path<int>) -> HttpResponse {
    return HttpResponse::ok().text("user #".concat(id.val.to_string()));
}