src/qs.bend checks
raw source on the hub · import ezhttp@0.25.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. What is guaranteed is under laws/, such as laws/UnquoteReadsEscapes.bend.
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
B@byte:U32 -> Tok
W@c:Char -> Tok
type Esc source · line 27 · raw
Data
where the decoding stands: in plain text, after %, or after % and one
hex digit (kept)
PlainEsc
PctEsc
Pct1@d:Char -> Esc
type Utf source · line 138 · 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
IdleUtf
Need@cp:U32 -> @left:Nat -> @lo:U32 -> @hi:U32 -> Utf
type Lead source · line 144 · 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
AsciiLead
BadLead
Opens@cp:U32 -> @left:Nat -> @lo:U32 -> @hi:U32 -> Lead
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 54 · 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 67 · raw
@plus:Bool -> @+c:Char -> @k:(@_:Esc -> List<&2, Tok>) -> List<&2, Tok>
a character in plain text
def tok.pct source · line 72 · 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 86 · 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 100 · 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 115 · raw
@st:Esc -> List<&2, Tok>
the end of the text: an escape left open is written as it stands
def toks source · line 125 · 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 150 · raw
@f4:Bool -> Lead
F4: the last lead, up to U+10FFFF; F5 to FF lead nothing
def lead.f1 source · line 158 · raw
@+b:U32 -> @lt:Bool -> Lead
F1 to F3, else F4 and up
def lead.f0 source · line 166 · raw
@+b:U32 -> @f0:Bool -> Lead
F0 (no overlong four-byte form), else F1 and up
def lead.ee source · line 174 · raw
@+b:U32 -> @lt:Bool -> Lead
EE and EF, else F0 and up
def lead.ed source · line 182 · raw
@+b:U32 -> @ed:Bool -> Lead
ED (no surrogates), else EE and up
def lead.e1 source · line 190 · raw
@+b:U32 -> @lt:Bool -> Lead
E1 to EC, else ED and up
def lead.e0 source · line 198 · raw
@+b:U32 -> @e0:Bool -> Lead
E0 (no overlong three-byte form), else E1 and up
def lead.two source · line 206 · raw
@+b:U32 -> @lt:Bool -> Lead
C2 to DF, else E0 and up
def lead.low source · line 214 · raw
@+b:U32 -> @lt:Bool -> Lead
80 to C1 (continuation bytes, overlong two-byte leads), else C2 and up
def lead.ascii source · line 222 · raw
@+b:U32 -> @lt:Bool -> Lead
ASCII, else 80 and up
def lead source · line 230 · raw
@+b:U32 -> Lead
what a byte between characters is (RFC 3629 §4, Unicode Table 3-7)
def fffd source · line 234 · raw
Char
U+FFFD REPLACEMENT CHARACTER
def utf.add source · line 238 · raw
@+cp:U32 -> @+b:U32 -> U32
a continuation byte's six bits after the code point so far
def utf.lead source · line 242 · raw
@+b:U32 -> @k:(@_:Utf -> String) -> @l:Lead -> String
a byte between characters, as its lead says
def utf.idle source · line 252 · raw
@t:Tok -> @k:(@_:Utf -> String) -> String
a piece between characters
def utf.more source · line 260 · 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 269 · 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 283 · 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 298 · raw
@st:Utf -> @t:Tok -> @k:(@_:Utf -> String) -> String
one piece where the decoding stands
def utf.end source · line 306 · raw
@st:Utf -> String
the end of the pieces: a sequence left unfinished is one U+FFFD
def utf.dec source · line 314 · raw
@ts:List<&2, Tok> -> @st:Utf -> String
pieces as text, piece by piece
def unquote source · line 325 · raw
@s:String -> String
percent-decode a string, as urllib.parse.unquote
def unquote.plus source · line 329 · raw
@s:String -> String
percent-decode a string with + as a space, as urllib.parse.unquote_plus
def plus.one source · line 336 · 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 344 · raw
@s:String -> String
percent-encode a string with a space as +, as urllib.parse.quote_plus
def form.pair source · line 352 · raw
@name:String -> @value:String -> String
one name and value as name=value, both quote_plus'd
def form.more source · line 356 · raw
@ps:List<&2, Kv> -> String
the pairs after the first, each after an &
def urlencode source · line 364 · raw
@ps:List<&2, Kv> -> String
pairs as a query string, as urllib.parse.urlencode
def cut.push source · line 375 · 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 383 · 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 397 · 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 406 · 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 414 · raw
@+piece:String -> Kv
one field as its name and value
def fields.put source · line 418 · 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 430 · raw
@ps:List<&2, String> -> List<&2, Kv>
the pieces as fields, in order
def parse source · line 439 · 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 443 · 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 457 · 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 467 · raw
@s:String -> List<&2, Tok>
octets, one character each, as the pieces utf.dec reads
def octets.text source · line 476 · raw
@o:String -> String
received octets as text: decoded as UTF-8, each ill-formed sequence's maximal subpart one U+FFFD (laws/Utf8DecodesWithReplacement.bend)
def octets.headers source · line 480 · raw
@hs:List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header> -> List<&2, 0x0b59049ea5717d6f2fd30293c7271ede/src/http.Header>
received header fields as text, name and value each decoded