~/bend-docscommunity

src/protocol.bend checks

raw source on the hub · import bendonnect-rpc@0.1.0.0/src/protocol.bend as Protocol

NOTE: Kind is a reserved word in Bend, so the contract's Kind type is named CallKind. Connect request classification (pure): turns an HTTP request line + headers into a Spec or a Reject. https://connectrpc.com/docs/protocol/

Choices (where the spec leaves room): - query is the RAW query string without the leading '?' (e.g. "message=%7B%7D&encoding=json"). Keys and values are matched/decoded here; unknown params are ignored. - prefix is "" or a path such as "/api" (no trailing slash). Path = prefix "/" Service "/" Method. - Check order: method (405) -> path (404) -> content-type / GET params (415 / 400) -> connect-protocol-version (400) -> timeout (InvalidArgument) -> encoding (Unimplemented). - GET with missing/empty encoding or message is Status 400 (not 415). - Unsupported codecs are NOT rejected here: unknown codecs become Custom{name} (415 is the caller's call once it knows its codec registry). An empty codec name is Status 415. - GET also validates the connect-protocol-version header, if sent, like POST. - encoding: lowercased/trimmed; empty or absent is "identity". - accept: header present -> its comma list (trimmed, lowercased, ";q=" params and empties dropped); absent -> [encoding] (spec: client accepts the encoding it used; identity is implied last). - Methods are matched case-sensitively ("POST", "GET"); anything else is Status 405.

4 imports
import Base
import ./code.bend as Code
import bend-kit-bytes@0.3.2.0/bytes.bend as Bytes
import bend-kit-url@0.4.1.0/url.bend as Url

Types

type Codec source · line 24 · raw

Data

type CallKind source · line 29 · raw

Data

type Spec source · line 34 · raw

Type

type Reject source · line 38 · raw

Data

type Ct source · line 43 · raw

Data

A parsed Content-Type: stream = application/connect+<codec>.

Definitions

def supported_encodings source · line 46 · raw

List<&2, String>

def codec_name source · line 51 · raw

@c:Codec -> String

def content_type source · line 60 · raw

@+stream:Bool -> @c:Codec -> String

def unary_content_type source · line 67 · raw

@c:Codec -> String

def stream_content_type source · line 70 · raw

@c:Codec -> String

def codec_of source · line 74 · raw

@+name:String -> Maybe<&2, Codec>

json / proto / other (non-empty) -> Custom

def media.of source · line 79 · raw

@r:Pair(String, String) -> String

def media source · line 84 · raw

@s:String -> String

text before the first ';'

def ct_make source · line 87 · raw

@stream:Bool -> @mc:Maybe<&2, Codec> -> Maybe<&2, Ct>

def ct_split source · line 94 · raw

@stream:Bool -> @app:Bool -> @+m:String -> Maybe<&2, Ct>

def parse_content_type source · line 106 · raw

@+ct:String -> Maybe<&2, Ct>

"application/json; charset=utf-8" -> Ct{False, Json}; case-insensitive; None if not application/*.

def ct_stream source · line 110 · raw

@c:Ct -> Bool

def ct_codec source · line 115 · raw

@c:Ct -> Codec

def ct_kind source · line 120 · raw

@c:Ct -> CallKind

def hdr.of source · line 131 · raw

@r:Pair(Map<&2, List<&2, String>>, List<&2, String>) -> Maybe<&2, String>

def hdr source · line 140 · raw

@+h:Map<&2, List<&2, String>> -> @+k:String -> Maybe<&2, String>

First value of a (lowercase) header.

def hdr_all.of source · line 143 · raw

@r:Pair(Map<&2, List<&2, String>>, List<&2, String>) -> Maybe<&2, String>

def hdr_all source · line 152 · raw

@+h:Map<&2, List<&2, String>> -> @+k:String -> Maybe<&2, String>

All values of a header joined with ",".

def bad_media source · line 155 · raw

Result<&2, &1, Reject, Ct>

def content_type_of.fin source · line 158 · raw

@m:Maybe<&2, Ct> -> Result<&2, &1, Reject, Ct>

def content_type_of.of source · line 165 · raw

@m:Maybe<&2, String> -> Result<&2, &1, Reject, Ct>

def content_type_of source · line 172 · raw

@+h:Map<&2, List<&2, String>> -> Result<&2, &1, Reject, Ct>

def version_ok source · line 175 · raw

@ok:Bool -> Result<&2, &1, Reject, Unit>

def check_version.of source · line 182 · raw

@v:Maybe<&2, String> -> Result<&2, &1, Reject, Unit>

def check_version source · line 190 · raw

@+h:Map<&2, List<&2, String>> -> Result<&2, &1, Reject, Unit>

connect-protocol-version, if present, must be "1".

def digit source · line 195 · raw

@+c:U32 -> Bool

def sat source · line 199 · raw

@+acc:U32 -> @+d:U32 -> U32

acc*10 + d, clamped to U32 max.

def pt.pick source · line 204 · raw

@ok:Bool -> @rest:(@_:Unit -> Maybe<&2, U32>) -> Maybe<&2, U32>

def pt.go source · line 211 · raw

@s:String -> @+n:U32 -> @+acc:U32 -> Maybe<&2, U32>

def pt.start source · line 218 · raw

@+s:String -> @empty:Bool -> Maybe<&2, U32>

def parse_timeout source · line 226 · raw

@+s:String -> Maybe<&2, U32>

connect-timeout-ms value: 1..10 ASCII digits, clamped to U32 max.

def timeout.chk source · line 229 · raw

@m:Maybe<&2, U32> -> @+s:String -> Result<&2, &1, Reject, Maybe<&2, U32>>

def timeout.of source · line 236 · raw

@v:Maybe<&2, String> -> Result<&2, &1, Reject, Maybe<&2, U32>>

def timeout_of source · line 243 · raw

@+h:Map<&2, List<&2, String>> -> Result<&2, &1, Reject, Maybe<&2, U32>>

def norm.pick source · line 248 · raw

@empty:Bool -> @+e:String -> String

def norm_enc source · line 255 · raw

@+s:String -> String

def enc.fin source · line 259 · raw

@ok:Bool -> @+e:String -> Result<&2, &1, Reject, String>

def check_enc source · line 267 · raw

@+raw:String -> Result<&2, &1, Reject, String>

Normalizes an encoding name; Unimplemented unless it is identity.

def get_enc.of source · line 271 · raw

@v:Maybe<&2, String> -> Result<&2, &1, Reject, String>

def get_enc source · line 278 · raw

@+h:Map<&2, List<&2, String>> -> @+k:String -> Result<&2, &1, Reject, String>

def acc.item source · line 281 · raw

@+s:String -> String

def acc.cons source · line 285 · raw

@empty:Bool -> @x:String -> @r:List<&2, String> -> List<&2, String>

def acc.item2 source · line 292 · raw

@+x:String -> @r:List<&2, String> -> List<&2, String>

def acc.go source · line 295 · raw

@xs:List<&2, String> -> List<&2, String>

def accept_list source · line 302 · raw

@+s:String -> List<&2, String>

def accept.of source · line 305 · raw

@v:Maybe<&2, String> -> @+enc:String -> List<&2, String>

def accept_of source · line 312 · raw

@+h:Map<&2, List<&2, String>> -> @+k:String -> @+enc:String -> List<&2, String>

def bad_path source · line 317 · raw

Result<&2, &1, Reject, String>

def proc.fin source · line 320 · raw

@ok:Bool -> @+p:String -> Result<&2, &1, Reject, String>

def proc.parts source · line 327 · raw

@xs:List<&2, String> -> @+p:String -> Result<&2, &1, Reject, String>

def proc.split source · line 334 · raw

@+p:String -> Result<&2, &1, Reject, String>

def proc.slash source · line 337 · raw

@ok:Bool -> @+rest:String -> Result<&2, &1, Reject, String>

def proc.pre source · line 344 · raw

@ok:Bool -> @+path:String -> @+prefix:String -> Result<&2, &1, Reject, String>

def parse_proc source · line 353 · raw

@+path:String -> @+prefix:String -> Result<&2, &1, Reject, String>

"<prefix>/pkg.Service/Method" -> "pkg.Service/Method"; else Status 404.

def post_enc_key source · line 358 · raw

@stream:Bool -> String

def post_acc_key source · line 365 · raw

@stream:Bool -> String

def post_req source · line 372 · raw

@+path:String -> @+headers:Map<&2, List<&2, String>> -> @+prefix:String -> Result<&2, &1, Reject, Spec>

def qfind.pick source · line 383 · raw

@hit:Bool -> @v:String -> @rest:(@_:Unit -> Maybe<&2, String>) -> Maybe<&2, String>

def qfind.kv source · line 390 · raw

@r:Pair(String, String) -> @+name:String -> @rest:(@_:Unit -> Maybe<&2, String>) -> Maybe<&2, String>

def qfind source · line 395 · raw

@xs:List<&2, String> -> @+name:String -> Maybe<&2, String>

First raw value of query param name in a list of "k=v" items.

def qparam source · line 402 · raw

@+query:String -> @+name:String -> Maybe<&2, String>

def need.fin source · line 405 · raw

@m:Maybe<&2, String> -> @+name:String -> Result<&2, &1, Reject, String>

def need_param source · line 412 · raw

@+query:String -> @+name:String -> Result<&2, &1, Reject, String>

def qversion.ok source · line 415 · raw

@ok:Bool -> Result<&2, &1, Reject, Unit>

def qversion.of source · line 422 · raw

@m:Maybe<&2, String> -> Result<&2, &1, Reject, Unit>

def check_qversion source · line 430 · raw

@+query:String -> Result<&2, &1, Reject, Unit>

connect=v1 if present, else 400.

def b64flag.of source · line 433 · raw

@m:Maybe<&2, String> -> Bool

def codec.fin source · line 440 · raw

@m:Maybe<&2, Codec> -> Result<&2, &1, Reject, Codec>

def comp.of source · line 447 · raw

@m:Maybe<&2, String> -> Result<&2, &1, Reject, String>

def compression_of source · line 454 · raw

@+query:String -> Result<&2, &1, Reject, String>

def b64val source · line 459 · raw

@+c:U32 -> Maybe<&2, U32>

def b64vals.cons source · line 466 · raw

@m:Maybe<&2, U32> -> @r:Maybe<&2, List<&2, U32>> -> Maybe<&2, List<&2, U32>>

def b64vals source · line 477 · raw

@s:String -> Maybe<&2, List<&2, U32>>

def b64count source · line 484 · raw

@xs:List<&2, U32> -> @+acc:U32 -> U32

def b64out source · line 491 · raw

@+n:U32 -> U32

def b64set source · line 495 · raw

@out:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> @+i:U32 -> @+v:U32 -> 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes

def b64go source · line 499 · raw

@vs:List<&2, U32> -> @out:0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes -> @+i:U32 -> Maybe<&1, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes>

Fills out from the 6-bit values; None on a dangling single char.

def b64build source · line 512 · raw

@vs:List<&2, U32> -> @+n:U32 -> Maybe<&1, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes>

def b64run source · line 515 · raw

@m:Maybe<&2, List<&2, U32>> -> Maybe<&1, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes>

def b64strip source · line 522 · raw

@+s:String -> String

def b64url_decode source · line 527 · raw

@+s:String -> Maybe<&1, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes>

Decodes RFC 4648 base64 (url-safe or standard alphabet), padding optional.

def msg.bytes source · line 530 · raw

@m:Maybe<&1, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes> -> Result<&2, &1, Reject, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes>

def msg.kind source · line 537 · raw

@b64:Bool -> @+s:String -> Result<&2, &1, Reject, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes>

def msg.of source · line 544 · raw

@m:Maybe<&2, String> -> @+b64:Bool -> Result<&2, &1, Reject, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes>

def decode_message source · line 552 · raw

@+raw:String -> @+b64:Bool -> Result<&2, &1, Reject, 0x185ae03c75e3e75be1171471f68b43cb/bytes.Bytes>

message param: percent-decoded, then base64-decoded if base64=1.

def get_req source · line 555 · raw

@+path:String -> @+query:String -> @+headers:Map<&2, List<&2, String>> -> @+prefix:String -> Result<&2, &1, Reject, Spec>

def route source · line 570 · raw

@post:Bool -> @get:Bool -> @+path:String -> @+query:String -> @+headers:Map<&2, List<&2, String>> -> @+prefix:String -> Result<&2, &1, Reject, Spec>

def classify source · line 582 · raw

@+method:String -> @+path:String -> @+query:String -> @+headers:Map<&2, List<&2, String>> -> @+prefix:String -> Result<&2, &1, Reject, Spec>

Classifies a request. method is "POST"/"GET"; query is the raw query string (no '?').