| /- | |
| Copyright (c) 2025 Lean FRO, LLC. All rights reserved. | |
| Released under Apache 2.0 license as described in the file LICENSE. | |
| Authors: Sofia Rodrigues | |
| -/ | |
| module | |
| prelude | |
| public import Std.Http.Data.Extensions | |
| public import Std.Http.Data.Method | |
| public import Std.Http.Data.Version | |
| public import Std.Http.Data.Headers | |
| public import Std.Http.Data.URI | |
| public section | |
| /-! | |
| # Request | |
| This module provides the `Request` type, which represents an HTTP request. It also defines ways | |
| to build a `Request` using functions that make it easier. | |
| References: | |
| * https://httpwg.org/specs/rfc9112.html#request.line | |
| -/ | |
| namespace Std.Http | |
| set_option linter.all true | |
| /-- | |
| The main parts of a request containing the HTTP method, version, and request target URI. | |
| Reference: https://httpwg.org/specs/rfc9112.html#request.line | |
| -/ | |
| structure Request.Head where | |
| /-- | |
| The HTTP method (GET, POST, PUT, DELETE, etc.) for the request | |
| Reference: https://httpwg.org/specs/rfc9112.html#rfc.section.3.1 | |
| -/ | |
| method : Method | |
| /-- | |
| The HTTP protocol version for the request (e.g. HTTP/1.1, HTTP/2.0, HTTP/3.0). | |
| Reference: https://httpwg.org/specs/rfc9112.html#http.version | |
| -/ | |
| version : Version | |
| /-- | |
| The raw request-target string (commonly origin-form path/query, `"*"`, or authority-form). | |
| -/ | |
| uri : RequestTarget := .asteriskForm | |
| /-- | |
| Collection of HTTP headers for the request (Content-Type, Authorization, etc.). | |
| -/ | |
| headers : Headers := .empty | |
| deriving Inhabited, Repr | |
| /-- | |
| HTTP request structure parameterized by body type. | |
| -/ | |
| structure Request (t : Type) where | |
| /-- | |
| The request line information (`method`, `version`, and request-target `uri`). | |
| -/ | |
| line : Request.Head | |
| /-- | |
| The request body content of type t. | |
| -/ | |
| body : t | |
| /-- | |
| Optional dynamic metadata attached to the request. | |
| -/ | |
| extensions : Extensions := .empty | |
| deriving Inhabited | |
| /-- | |
| Builds an HTTP Request. | |
| -/ | |
| structure Request.Builder where | |
| /-- | |
| The request-line of an HTTP request. | |
| -/ | |
| line : Head := { method := .get, version := .v11, uri := .asteriskForm } | |
| /-- | |
| Optional dynamic metadata attached to the request. | |
| -/ | |
| extensions : Extensions := .empty | |
| namespace Request | |
| instance : ToString Head where | |
| toString req := | |
| toString req.method ++ " " ++ | |
| toString req.uri ++ " " ++ | |
| toString req.version ++ | |
| "\r\n" ++ | |
| toString req.headers ++ | |
| "\r\n" | |
| open Std.Http.Internal in | |
| instance : Encode .v11 Head where | |
| encode out req := | |
| out ++ req.method ++ ' ' ++ req.uri ++ ' ' ++ req.version ++ "\r\n" ++ req.headers ++ "\r\n" | |
| /-- | |
| Creates a new HTTP request builder with the default head | |
| (method: GET, version: HTTP/1.1, target: `*`). | |
| -/ | |
| def new : Builder := { } | |
| namespace Builder | |
| /-- | |
| Sets the HTTP method for the request being built. | |
| -/ | |
| def method (builder : Builder) (method : Method) : Builder := | |
| { builder with line := { builder.line with method := method } } | |
| /-- | |
| Sets the HTTP version for the request being built. | |
| -/ | |
| def version (builder : Builder) (version : Version) : Builder := | |
| { builder with line := { builder.line with version := version } } | |
| /-- | |
| Sets the request target/URI for the request being built. | |
| -/ | |
| def uri (builder : Builder) (uri : RequestTarget) : Builder := | |
| { builder with line := { builder.line with uri := uri } } | |
| /-- | |
| Sets the request target/URI for the request being built | |
| -/ | |
| def uri! (builder : Builder) (uri : String) : Builder := | |
| let uri := RequestTarget.parse! uri | |
| { builder with line := { builder.line with uri } } | |
| /-- | |
| Sets the headers for the request being built | |
| -/ | |
| def headers (builder : Builder) (headers : Headers) : Builder := | |
| { builder with line := { builder.line with headers } } | |
| /-- | |
| Adds a single header to the request being built. | |
| -/ | |
| def header (builder : Builder) (key : Header.Name) (value : Header.Value) : Builder := | |
| { builder with line := { builder.line with headers := builder.line.headers.insert key value } } | |
| /-- | |
| Adds a single header to the request being built, panics if the header is invalid. | |
| -/ | |
| def header! (builder : Builder) (key : String) (value : String) : Builder := | |
| let key := Header.Name.ofString! key | |
| let value := Header.Value.ofString! value | |
| { builder with line := { builder.line with headers := builder.line.headers.insert key value } } | |
| /-- | |
| Adds a single header to the request being built. | |
| Returns `none` if the header name or value is invalid. | |
| -/ | |
| def header? (builder : Builder) (key : String) (value : String) : Option Builder := do | |
| let key ← Header.Name.ofString? key | |
| let value ← Header.Value.ofString? value | |
| pure <| { builder with line := { builder.line with headers := builder.line.headers.insert key value } } | |
| /-- | |
| Adds a header to the request being built only if the Option Header.Value is some. | |
| -/ | |
| def headerOpt (builder : Builder) (key : Header.Name) (value : Option Header.Value) : Builder := | |
| match value with | |
| | some v => builder.header key v | |
| | none => builder | |
| /-- | |
| Inserts a typed extension value into the request being built. | |
| -/ | |
| def extension (builder : Builder) [TypeName α] (data : α) : Builder := | |
| { builder with extensions := builder.extensions.insert data } | |
| /-- | |
| Builds and returns the final HTTP Request with the specified body. | |
| -/ | |
| def body (builder : Builder) (body : t) : Request t := | |
| { line := builder.line, body := body, extensions := builder.extensions } | |
| end Builder | |
| /-- | |
| Creates a new HTTP GET Request with the specified URI. | |
| -/ | |
| def get (uri : RequestTarget) : Builder := | |
| new | |
| |>.method .get | |
| |>.uri uri | |
| /-- | |
| Creates a new HTTP POST Request builder with the specified URI. | |
| -/ | |
| def post (uri : RequestTarget) : Builder := | |
| new | |
| |>.method .post | |
| |>.uri uri | |
| /-- | |
| Creates a new HTTP PUT Request builder with the specified URI. | |
| -/ | |
| def put (uri : RequestTarget) : Builder := | |
| new | |
| |>.method .put | |
| |>.uri uri | |
| /-- | |
| Creates a new HTTP DELETE Request builder with the specified URI. | |
| -/ | |
| def delete (uri : RequestTarget) : Builder := | |
| new | |
| |>.method .delete | |
| |>.uri uri | |
| /-- | |
| Creates a new HTTP PATCH Request builder with the specified URI. | |
| -/ | |
| def patch (uri : RequestTarget) : Builder := | |
| new | |
| |>.method .patch | |
| |>.uri uri | |
| /-- | |
| Creates a new HTTP HEAD Request builder with the specified URI. | |
| -/ | |
| def head (uri : RequestTarget) : Builder := | |
| new | |
| |>.method .head | |
| |>.uri uri | |
| /-- | |
| Creates a new HTTP OPTIONS Request builder with the specified URI. | |
| Use `Request.options (RequestTarget.asteriskForm)` for server-wide OPTIONS. | |
| -/ | |
| def options (uri : RequestTarget) : Builder := | |
| new | |
| |>.method .options | |
| |>.uri uri | |
| /-- | |
| Creates a new HTTP CONNECT Request builder with the specified URI. | |
| Typically used with `RequestTarget.authorityForm` for tunneling. | |
| -/ | |
| def connect (uri : RequestTarget) : Builder := | |
| new | |
| |>.method .connect | |
| |>.uri uri | |
| /-- | |
| Creates a new HTTP TRACE Request builder with the specified URI. | |
| -/ | |
| def trace (uri : RequestTarget) : Builder := | |
| new | |
| |>.method .trace | |
| |>.uri uri | |
| end Std.Http.Request |