build-an-http-server / lesson-16.md
Lesson 16 · Parsing requests

Percent-decode the path

URLs escape spaces and special characters as %XX hex sequences, so the path on the wire is not the path on disk. Today you decode those escapes back into real bytes.

The goal

Decode percent-escaped sequences in a path back into their literal characters.

Start here - the target
TO DO
Scenario: Decoding a percent-encoded path
Giventhe path "/a%20b/%7Euser"
Whenit is decoded
Thenthe result is "/a b/~user"
Anda path with no escapes like "/plain" is returned unchanged
Background

A URL may only contain a limited set of characters, so anything outside it — a space, a tilde, a slash you want treated as data — is written as % followed by two hex digits giving the byte’s value. %20 is a space, %7E is ~. The path you route on and read files from must be the decoded form, or /a%20b will never match the file a b.

Walk the string, copying ordinary characters straight through; when you hit %, read the next two characters as a hex byte and emit it. A path with no % comes out identical, which is the common case and a good check that you are not corrupting anything.

Make it work
// walk the string; on '%' read the next two hex digits as one byte
b, err := strconv.ParseUint(path[i+1:i+3], 16, 8)
// 0x20 -> ' ', 0x7E -> '~'
CheckpointDONE
Percent-escapes in the path decode back to real bytes. Commit.