Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

URL Encoding

Also Percent-Encoding · URL Decoding

URL encoding (percent-encoding) escapes every character that is not allowed or has a special meaning in a URL by the bytes of its encoding (by default UTF-8), each written as % followed by two hex digits; in form data a space becomes +.

Know these firstCharacter Encoding

Formula#

For a character cc with code point uu:

  1. If cc is allowed and not used as a delimiter, keep it.
  2. Otherwise compute the UTF-8 bytes b1…bkb_1 \dots b_k of uu (k∈{1,2,3,4}k \in \{1,2,3,4\}) and write %hex(b1)\mathrm{hex}(b_1) … %hex(bk)\mathrm{hex}(b_k).
  3. In form data (application/x-www-form-urlencoded, i.e. what an HTML Form sends) a space is written as +; elsewhere as %20.

UTF-8 Encoding of Non-ASCII Characters#

For code points >127> 127, UTF-8 uses multi-byte encoding; the payload bits of the code point fill the x positions from the right:

Code point rangeUTF-8 bytesByte pattern
U+0000–U+007F ([0,127][0, 127])10xxxxxxx
U+0080–U+07FF ([128,2047][128, 2047])2110xxxxx 10xxxxxx
U+0800–U+FFFF ([2048,65535][2048, 65535])31110xxxx 10xxxxxx 10xxxxxx
U+10000–U+1FFFFF ([65536,221−1][65536, 2^{21}-1])411110xxx 10xxxxxx 10xxxxxx 10xxxxxx

UTF-8 itself (code points, validity, decoding) belongs to Encodings and Compression.

Properties#

  • Space: + in query strings (forms), %20 in general.
  • Reserved characters (?, &, =, #, …) are encoded when used as data, not as delimiters: the query a&b is sent as ?q=a%26b.
  • Non-ASCII characters are first converted to their UTF-8 byte sequence, then each byte is percent-encoded: ä → %C3%A4. Under ISO-8859-1 the same ä would be the single byte %E4; UTF-8 is the default of browsers and fetch.
  • The page’s encoding is declared in its head, <meta charset="utf-8">.
  • Every URL-encoded byte of a multi-byte character is ≥\geq 0x80, so a % code ≥\geq %80 is always part of a multi-byte UTF-8 sequence.
  • Server side: replace + by a space (Sheet 9), then turn each %XY into the byte 0xXY (e.g. Python bytes.fromhex("C3A4")) and decode the resulting bytes as UTF-8 (Sheet 10). Decoding byte by byte into characters is wrong: %C3%A4 would become ä.
  • URL encoding hides content from the human eye but not from the browser: %3C%73%63%72%69%70%74 is <script (Code Injection Protection).

Example#

Sources#

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only