Documentation

Std.Internal.Http.Data.URI.Encoding

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
    Instances For
      @[reducible, inline]

      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
      Instances For
        @[reducible, inline]

        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
        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:

          • %20 is valid (percent followed by two hex digits)
          • % is invalid (percent with no following digits)
          • %2 is invalid (percent followed by only one digit)
          • %GG is invalid (percent followed by non-hex characters)
          Equations
          Instances For

            Converts a nibble (4-bit value, 0-15) to its hexadecimal digit representation. Returns '0'-'9' for values 0-9, and 'A'-'F' for values 10-15.

            Equations
            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
                theorem Std.Http.URI.all_of_all_of_imp {p q : UInt8Bool} {b : ByteArray} (h : b.data.all p = true) (imp : ∀ (c : UInt8), p c = trueq c = true) :

                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.

                • Proof that all characters in the byte array are valid encoded characters.

                Instances For

                  Creates an empty encoded string.

                  Equations
                  Instances For

                    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.

                          Equations
                          Instances For

                            Creates an EncodedString from a String, panicking if the string is not a valid percent-encoded string.

                            Equations
                            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
                              Instances For
                                @[implicit_reducible]
                                Equations

                                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
                                  @[implicit_reducible]
                                  Equations

                                  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".

                                  Instances For

                                    Creates an empty encoded query string.

                                    Equations
                                    Instances For

                                      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 ByteArray with compile-time proofs. Use this when you have proofs that the byte array is valid.

                                            Equations
                                            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
                                                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
                                                    @[implicit_reducible]
                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    @[reducible, inline]

                                                    A percent-encoded URI path segment. Valid characters are pchar (unreserved, sub-delims, ':', '@').

                                                    Equations
                                                    Instances For

                                                      Encodes a raw string into an encoded path segment.

                                                      Equations
                                                      Instances For

                                                        Creates an encoded path segment from raw bytes, panicking on invalid encoding.

                                                        Equations
                                                        Instances For

                                                          Decodes an encoded path segment back to a UTF-8 string.

                                                          Equations
                                                          Instances For
                                                            @[reducible, inline]

                                                            A percent-encoded URI fragment component. Valid characters are pchar / "/" / "?".

                                                            Equations
                                                            Instances For

                                                              Encodes a raw string into an encoded fragment component.

                                                              Equations
                                                              Instances For

                                                                Attempts to create an encoded fragment component from raw bytes.

                                                                Equations
                                                                Instances For

                                                                  Creates an encoded fragment component from raw bytes, panicking on invalid encoding.

                                                                  Equations
                                                                  Instances For

                                                                    Decodes an encoded fragment component back to a UTF-8 string.

                                                                    Equations
                                                                    Instances For
                                                                      @[reducible, inline]

                                                                      A percent-encoded URI userinfo component. Valid characters are unreserved / sub-delims / ":".

                                                                      Equations
                                                                      Instances For

                                                                        Encodes a raw string into an encoded userinfo component.

                                                                        Equations
                                                                        Instances For

                                                                          Attempts to create an encoded userinfo component from raw bytes.

                                                                          Equations
                                                                          Instances For

                                                                            Creates an encoded userinfo component from raw bytes, panicking on invalid encoding.

                                                                            Equations
                                                                            Instances For

                                                                              Decodes an encoded userinfo component back to a UTF-8 string.

                                                                              Equations
                                                                              Instances For
                                                                                @[reducible, inline]

                                                                                A percent-encoded URI query parameter. Valid characters are pchar / "/" / "?" with '+' for spaces.

                                                                                Equations
                                                                                Instances For

                                                                                  Decodes an encoded query parameter back to a UTF-8 string.

                                                                                  Equations
                                                                                  Instances For