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 with code point :
- If is allowed and not used as a delimiter, keep it.
- Otherwise compute the UTF-8 bytes of () and write
%…%. - 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 , UTF-8 uses multi-byte encoding; the payload bits of the code point fill the x positions from the right:
| Code point range | UTF-8 bytes | Byte pattern |
|---|---|---|
| U+0000–U+007F () | 1 | 0xxxxxxx |
| U+0080–U+07FF () | 2 | 110xxxxx 10xxxxxx |
| U+0800–U+FFFF () | 3 | 1110xxxx 10xxxxxx 10xxxxxx |
| U+10000–U+1FFFFF () | 4 | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
UTF-8 itself (code points, validity, decoding) belongs to Encodings and Compression.
Properties#
- Space:
+in query strings (forms),%20in general. - Reserved characters (
?,&,=,#, …) are encoded when used as data, not as delimiters: the querya&bis 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 andfetch. - The page’s encoding is declared in its head,
<meta charset="utf-8">. - Every URL-encoded byte of a multi-byte character is
0x80, so a%code%80is always part of a multi-byte UTF-8 sequence. - Server side: replace
+by a space (Sheet 9), then turn each%XYinto the byte0xXY(e.g. Pythonbytes.fromhex("C3A4")) and decode the resulting bytes as UTF-8 (Sheet 10). Decoding byte by byte into characters is wrong:%C3%A4would becomeä. - URL encoding hides content from the human eye but not from the browser:
%3C%73%63%72%69%70%74is<script(Code Injection Protection).
Example#
Sources#
- Slides 09, p. 20 (allowed characters,
+,%C3%A4) - Slides 10, p. 28 (UTF-8 patterns), pp. 31–32 (URL encoding and decoding), p. 19 (URL-encoded
<script>) - DBIS_VL09 > URL encoding and decoding, DBIS_VL10 > UTF-8 in detail, DBIS_VL10 > URL encoding and decoding, DBIS_VL10 > Code injection
- Sheet 10, Exercise 1.4
- DBIS Exam WS1516 (not published) T3.4, DBIS Exam WS1617 (not published) T3.3, DBIS Exam WS2324-2 (not published) T4.2