~/bend-docscommunity

src/qs.bend checks

raw source on the hub · import emerging-ezhttp@0.21.0.0/src/qs.bend as Qs

ezhttp/src/qs: percent-decoding (RFC 3986 §2.1) and query strings (application/x-www-form-urlencoded, WHATWG URL §5), the way Python's urllib.parse.unquote, unquote_plus, quote_plus, urlencode and parse_qsl(keep_blank_values=True) do them. Decoded bytes are read as UTF-8 (RFC 3629 §4), each maximal subpart of an ill-formed sequence replaced by one U+FFFD (Unicode §3.9), as Python's errors="replace" does. Pure. SPEC.md HTTP-QS lists what is guaranteed.

3 imports
import Base
import ./pct.bend as Pct
import ./http.bend as Http

Types

type Tok source · line 21 · raw

Data

one piece of a string being decoded: a byte of an ASCII run, or a character outside ASCII, which is kept as it is and ends the run

type Esc source · line 27 · raw

Data

where the decoding stands: in plain text, after %, or after % and one hex digit (kept)

type Utf source · line 116 · raw

Data

where the UTF-8 decoding stands: between characters, or inside a sequence with code point bits cp so far, needing a byte in lo..hi and left more continuation bytes after it

type Lead source · line 122 · raw

Data

what a byte between characters is: ASCII, the lead of a sequence (its bits, then what the sequence needs next), or a byte no sequence starts with

Definitions

def Kv source · line 14 · raw

Data

a name and a value, as a query field or a cookie: a reusable pair, so a list of them can be looked up more than once

def tok.hex source · line 33 · raw

@c:Char -> Bool

whether a character is a hex digit (0-9, A-F, a-f)

def tok.byte source · line 37 · raw

@plus:Bool -> @+c:Char -> U32

the byte a plain ASCII character stands for: + is a space when plus

def tok.ascii source · line 41 · raw

@plus:Bool -> @+c:Char -> @k:(@_:Esc -> List<&2, Tok>) -> @pct:Bool -> List<&2, Tok>

a plain ASCII character: % opens an escape, any other is its byte

def tok.wide source · line 50 · raw

@plus:Bool -> @+c:Char -> @k:(@_:Esc -> List<&2, Tok>) -> @wide:Bool -> List<&2, Tok>

a plain character once it is known whether it is outside ASCII

def tok.plain source · line 59 · raw

@plus:Bool -> @+c:Char -> @k:(@_:Esc -> List<&2, Tok>) -> List<&2, Tok>

a character in plain text

def tok.pct source · line 64 · raw

@plus:Bool -> @+c:Char -> @k:(@_:Esc -> List<&2, Tok>) -> @hex:Bool -> List<&2, Tok>

a character after %: a hex digit is kept, anything else leaves the % as written

def tok.pct1 source · line 74 · raw

@plus:Bool -> @+d:Char -> @+c:Char -> @k:(@_:Esc -> List<&2, Tok>) -> @hex:Bool -> List<&2, Tok>

a character after % and a hex digit: a second hex digit makes the byte they name, anything else leaves % and the digit as written

def tok.step source · line 83 · raw

@plus:Bool -> @st:Esc -> @+c:Char -> @k:(@_:Esc -> List<&2, Tok>) -> List<&2, Tok>

one character where the decoding stands

def tok.end source · line 94 · raw

@st:Esc -> List<&2, Tok>

the end of the text: an escape left open is written as it stands

def toks source · line 104 · raw

@s:String -> @+plus:Bool -> @st:Esc -> List<&2, Tok>

a string as bytes and wide characters, character by character

def lead.f4 source · line 128 · raw

@f4:Bool -> Lead

F4: the last lead, up to U+10FFFF; F5 to FF lead nothing

def lead.f1 source · line 136 · raw

@+b:U32 -> @lt:Bool -> Lead

F1 to F3, else F4 and up

def lead.f0 source · line 144 · raw

@+b:U32 -> @f0:Bool -> Lead

F0 (no overlong four-byte form), else F1 and up

def lead.ee source · line 152 · raw

@+b:U32 -> @lt:Bool -> Lead

EE and EF, else F0 and up

def lead.ed source · line 160 · raw

@+b:U32 -> @ed:Bool -> Lead

ED (no surrogates), else EE and up

def lead.e1 source · line 168 · raw

@+b:U32 -> @lt:Bool -> Lead

E1 to EC, else ED and up

def lead.e0 source · line 176 · raw

@+b:U32 -> @e0:Bool -> Lead

E0 (no overlong three-byte form), else E1 and up

def lead.two source · line 184 · raw

@+b:U32 -> @lt:Bool -> Lead

C2 to DF, else E0 and up

def lead.low source · line 192 · raw

@+b:U32 -> @lt:Bool -> Lead

80 to C1 (continuation bytes, overlong two-byte leads), else C2 and up

def lead.ascii source · line 200 · raw

@+b:U32 -> @lt:Bool -> Lead

ASCII, else 80 and up

def lead source · line 208 · raw

@+b:U32 -> Lead

what a byte between characters is (RFC 3629 §4, Unicode Table 3-7)

def fffd source · line 212 · raw

Char

U+FFFD REPLACEMENT CHARACTER

def utf.add source · line 216 · raw

@+cp:U32 -> @+b:U32 -> U32

a continuation byte's six bits after the code point so far

def utf.lead source · line 220 · raw

@+b:U32 -> @k:(@_:Utf -> String) -> @l:Lead -> String

a byte between characters, as its lead says

def utf.idle source · line 230 · raw

@t:Tok -> @k:(@_:Utf -> String) -> String

a piece between characters

def utf.more source · line 238 · raw

@+cp:U32 -> @left:Nat -> @+b:U32 -> @k:(@_:Utf -> String) -> String

a continuation byte that fits: the character ends, or more are needed

def utf.byte source · line 247 · raw

@+cp:U32 -> @left:Nat -> @+b:U32 -> @k:(@_:Utf -> String) -> @fits:Bool -> String

a byte inside a sequence: it continues it when it fits, else the sequence so far is one U+FFFD and the byte is read again between characters

def utf.need source · line 256 · raw

@+cp:U32 -> @left:Nat -> @+lo:U32 -> @+hi:U32 -> @t:Tok -> @k:(@_:Utf -> String) -> String

a piece inside a sequence: a wide character ends it unfinished

def utf.step source · line 265 · raw

@st:Utf -> @t:Tok -> @k:(@_:Utf -> String) -> String

one piece where the decoding stands

def utf.end source · line 273 · raw

@st:Utf -> String

the end of the pieces: a sequence left unfinished is one U+FFFD

def utf.dec source · line 281 · raw

@ts:List<&2, Tok> -> @st:Utf -> String

pieces as text, piece by piece

def unquote source · line 291 · raw

@s:String -> String

percent-decode a string, as urllib.parse.unquote

def unquote.plus source · line 295 · raw

@s:String -> String

percent-decode a string with + as a space, as urllib.parse.unquote_plus

def plus.one source · line 301 · raw

@+cp:U32 -> @rest:String -> @space:Bool -> String

one character: a space as +, any other as quote_component writes it

def quote.plus source · line 309 · raw

@s:String -> String

percent-encode a string with a space as +, as urllib.parse.quote_plus

def form.pair source · line 317 · raw

@name:String -> @value:String -> String

one name and value as name=value, both quote_plus'd

def form.more source · line 321 · raw

@ps:List<&2, Kv> -> String

the pairs after the first, each after an &

def urlencode source · line 329 · raw

@ps:List<&2, Kv> -> String

pairs as a query string, as urllib.parse.urlencode

def cut.push source · line 339 · raw

@+c:Char -> @m:Maybe<&2, Kv> -> Maybe<&2, Kv>

one character put back in front of what the rest of the cut found

def cut.at source · line 347 · raw

@+c:Char -> @t:String -> @eq:Bool -> @rest:(@_:Unit -> Maybe<&2, Kv>) -> Maybe<&2, Kv>

one character of the cut: the first = ends the text before it

def cut source · line 357 · raw

@s:String -> Maybe<&2, Kv>

a string cut at its first =: the text before it and the text after it, none when it holds no =

def field.of source · line 366 · raw

@m:Maybe<&2, Kv> -> @whole:String -> Kv

a field once it is cut: the name before its first = and the value after it, or all name and an empty value when it has no =

def field source · line 374 · raw

@+piece:String -> Kv

one field as its name and value

def fields.put source · line 378 · raw

@+piece:String -> @rest:List<&2, Kv> -> @empty:Bool -> List<&2, Kv>

one piece in front of the rest's fields, skipped when empty

def fields source · line 387 · raw

@ps:List<&2, String> -> List<&2, Kv>

the pieces as fields, in order

def parse source · line 396 · raw

@text:String -> List<&2, Kv>

a query string as its name and value pairs, as urllib.parse.parse_qsl(text, keep_blank_values=True)

def get.pick source · line 400 · raw

@later:Maybe<&2, String> -> @value:String -> @hit:Bool -> Maybe<&2, String>

the value a later pair gives, else this pair's when it is named

def get source · line 411 · raw

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

the value of the last pair with this name, none when no pair has it

def octets.toks source · line 421 · raw

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

octets, one character each, as the pieces utf.dec reads

def octets.text source · line 430 · raw

@o:String -> String

received octets as text: decoded as UTF-8, each ill-formed sequence's maximal subpart one U+FFFD (HTTP-QS-2)

def octets.headers source · line 434 · raw

@hs:List<&2, 0xedb2120a04a47dc18c3a756ec7c62750/src/http.Header> -> List<&2, 0xedb2120a04a47dc18c3a756ec7c62750/src/http.Header>

received header fields as text, name and value each decoded