URI Encoding #
This module provides utilities for percent-encoding URI components according to RFC 3986. It includes character validation, encoding/decoding functions, and types that maintain encoding invariants through Lean's dependent type system.
Reference: https://www.rfc-editor.org/rfc/rfc3986.html#section-2.1
Checks if a byte is a valid character in a percent-encoded URI component. Valid characters are unreserved characters or the percent sign (for escape sequences).
Equations
Instances For
Checks if a byte is valid in a percent-encoded query string component. Extends isEncodedChar to also
allow '+' which represents space in application/x-www-form-urlencoded format.
Equations
- Std.Http.URI.isEncodedQueryChar rule c = decide (Std.Http.URI.isEncodedChar rule c = true ∨ c = '+'.toUInt8)
Instances For
Checks if all characters in a ByteArray are allowed in an encoded URI component. This is a fast check
that only verifies the character set, not full encoding validity.
Equations
- Std.Http.URI.IsAllowedEncodedChars rule s = (s.data.all (Std.Http.URI.isEncodedChar rule) = true)
Instances For
Equations
Checks if all characters in a ByteArray are allowed in an encoded query parameter. Allows '+' as an
alternative encoding for space (application/x-www-form-urlencoded).
Equations
- Std.Http.URI.IsAllowedEncodedQueryChars rule s = (s.data.all (Std.Http.URI.isEncodedQueryChar rule) = true)
Instances For
Validates that all percent signs in a byte array are followed by exactly two hexadecimal digits. This ensures proper percent-encoding according to RFC 3986.
For example:
%20is valid (percent followed by two hex digits)%is invalid (percent with no following digits)%2is invalid (percent followed by only one digit)%GGis invalid (percent followed by non-hex characters)
Instances For
Converts a hexadecimal digit character to its numeric value (0-15).
Returns none if the character is not a valid hex digit.
Equations
- One or more equations did not get rendered due to their size.
Instances For
A percent-encoded URI component with a compile-time proof that it contains only valid encoded characters. This provides type-safe URI encoding without runtime validation.
The invariant guarantees that the string contains only unreserved characters (alphanumeric, hyphen, period, underscore, tilde) and percent signs (for escape sequences).
- toByteArray : ByteArray
The underlying byte array containing the percent-encoded data.
- valid : IsAllowedEncodedChars r self.toByteArray
Proof that all characters in the byte array are valid encoded characters.
Instances For
Creates an empty encoded string.
Equations
- Std.Http.URI.EncodedString.empty = { toByteArray := ByteArray.empty, valid := ⋯ }
Instances For
Equations
Encodes a raw string into an EncodedString with automatic proof construction. Unreserved characters
(alphanumeric, hyphen, period, underscore, tilde) are kept as-is, while all other characters are percent-encoded.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Attempts to create an EncodedString from a ByteArray. Returns some if the byte array contains only
valid encoded characters and all percent signs are followed by exactly two hex digits, none otherwise.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Creates an EncodedString from a ByteArray, panicking if the byte array is invalid.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Creates an EncodedString from a String by checking if it's already a valid percent-encoded string.
Returns some if valid, none otherwise.
Instances For
Creates an EncodedString from a String, panicking if the string is not a valid percent-encoded string.
Instances For
Creates an EncodedString from a ByteArray with compile-time proofs.
Use this when you have proofs that the byte array is valid.
Equations
- Std.Http.URI.EncodedString.new ba valid _validEncoding = { toByteArray := ba, valid := valid }
Instances For
Equations
- Std.Http.URI.EncodedString.instToString = { toString := fun (es : Std.Http.URI.EncodedString r) => { toByteArray := es.toByteArray, isValidUTF8 := ⋯ } }
Decodes an EncodedString back to a regular String. Converts percent-encoded sequences (e.g., "%20")
back to their original characters. Returns none if the decoded bytes are not valid UTF-8.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
- Std.Http.URI.EncodedString.instRepr = { reprPrec := fun (es : Std.Http.URI.EncodedString r) (n : Nat) => reprPrec (toString es) n }
Equations
- Std.Http.URI.EncodedString.instBEq = { beq := fun (x y : Std.Http.URI.EncodedString r) => decide (x.toByteArray = y.toByteArray) }
Equations
- Std.Http.URI.EncodedString.instHashable = { hash := fun (x : Std.Http.URI.EncodedString r) => hash x.toByteArray }
A percent-encoded query string component with a compile-time proof that it contains only valid encoded
query characters. Extends EncodedString to support the '+' character for spaces, following the
application/x-www-form-urlencoded format.
This type is specifically designed for encoding query parameters where spaces can be represented as '+' instead of "%20".
- toByteArray : ByteArray
The underlying byte array containing the percent-encoded query data.
- valid : IsAllowedEncodedQueryChars r self.toByteArray
Proof that all characters in the byte array are valid encoded query characters.
Instances For
Creates an empty encoded query string.
Equations
- Std.Http.URI.EncodedQueryString.empty = { toByteArray := ByteArray.empty, valid := ⋯ }
Instances For
Equations
Attempts to create an EncodedQueryString from a ByteArray. Returns some if the byte array contains
only valid encoded query characters and all percent signs are followed by exactly two hex digits, none otherwise.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Creates an EncodedQueryString from a ByteArray, panicking if the byte array is invalid.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Creates an EncodedQueryString from a String by checking if it's already a valid percent-encoded string.
Returns some if valid, none otherwise.
Equations
Instances For
Creates an EncodedQueryString from a String, panicking if the string is not a valid percent-encoded string.
Equations
Instances For
Creates an EncodedQueryString from a ByteArray with compile-time proofs.
Use this when you have proofs that the byte array is valid.
Equations
- Std.Http.URI.EncodedQueryString.new ba valid _validEncoding = { toByteArray := ba, valid := valid }
Instances For
Encodes a raw string into an EncodedQueryString with automatic proof construction. Unreserved characters
are kept as-is, spaces are encoded as '+', and all other characters are percent-encoded.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Converts an EncodedQueryString to a String, given a proof that all characters satisfying r are ASCII.
Equations
- es.toString = { toByteArray := es.toByteArray, isValidUTF8 := ⋯ }
Instances For
Decodes an EncodedQueryString back to a regular String. Converts percent-encoded sequences and '+'
signs back to their original characters. Returns none if the decoded bytes are not valid UTF-8.
This is almost the same code from System.Uri.UriEscape.decodeUri, but with Option instead.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Equations
Equations
- Std.Http.URI.instReprEncodedQueryString = { reprPrec := fun (es : Std.Http.URI.EncodedQueryString r) (n : Nat) => reprPrec (toString es) n }
Equations
- Std.Http.URI.instBEqEncodedQueryString = { beq := fun (x y : Std.Http.URI.EncodedQueryString r) => decide (x.toByteArray = y.toByteArray) }
Equations
- Std.Http.URI.instHashableEncodedQueryString = { hash := fun (x : Std.Http.URI.EncodedQueryString r) => hash x.toByteArray }
Equations
- One or more equations did not get rendered due to their size.
A percent-encoded URI path segment. Valid characters are pchar (unreserved, sub-delims, ':', '@').
Instances For
Encodes a raw string into an encoded path segment.
Instances For
Attempts to create an encoded path segment from raw bytes.
Instances For
Creates an encoded path segment from raw bytes, panicking on invalid encoding.
Instances For
Decodes an encoded path segment back to a UTF-8 string.
Equations
- segment.decode = Std.Http.URI.EncodedString.decode segment
Instances For
A percent-encoded URI fragment component. Valid characters are pchar / "/" / "?".
Equations
Instances For
Encodes a raw string into an encoded fragment component.
Instances For
Attempts to create an encoded fragment component from raw bytes.
Instances For
Creates an encoded fragment component from raw bytes, panicking on invalid encoding.
Instances For
Decodes an encoded fragment component back to a UTF-8 string.
Equations
- fragment.decode = Std.Http.URI.EncodedString.decode fragment
Instances For
A percent-encoded URI userinfo component. Valid characters are unreserved / sub-delims / ":".
Equations
Instances For
Encodes a raw string into an encoded userinfo component.
Instances For
Attempts to create an encoded userinfo component from raw bytes.
Instances For
Creates an encoded userinfo component from raw bytes, panicking on invalid encoding.
Instances For
Decodes an encoded userinfo component back to a UTF-8 string.
Equations
- userInfo.decode = Std.Http.URI.EncodedString.decode userInfo
Instances For
A percent-encoded URI query parameter. Valid characters are pchar / "/" / "?" with '+' for spaces.
Equations
Instances For
Encodes a raw string into an encoded query parameter.
Equations
Instances For
Attempts to create an encoded query parameter from raw bytes.
Equations
Instances For
Creates an encoded query parameter from raw bytes, panicking on invalid encoding.
Equations
Instances For
Attempts to create an encoded query parameter from an encoded string.
Equations
Instances For
Decodes an encoded query parameter back to a UTF-8 string.
Equations
- param.decode = Std.Http.URI.EncodedQueryString.decode param