Skip to main content

Module uri

Module uri 

Expand description

Percent-decoding for URIs, with the + question answered once.

Both answers are right — for different inputs, which is exactly why a single blanket rule would have been wrong:

  • In application/x-www-form-urlencoded data — HTML form submissions, and the query strings browsers and OAuth providers build from them — + is an encoded space. That encoding is defined by the URL living standard, not by RFC 3986. Use parse_form_query.

  • Everywhere else in a URI — path segments, and query strings that are not form data — RFC 3986 gives + no special meaning: it is a literal plus. Decoding it as a space corrupts Google+, C++, and a leading + on a phone number. Use decode.

Pick per call site by asking what produced the string, not by what it looks like.

Encoding has one answer: encode_component escapes everything but the RFC 3986 unreserved set, so its output is a valid path segment, query key or query value, and a space always becomes %20 (never +), which both decoders read back as a space.

Functions§

decode
Percent-decode a URI component per RFC 3986: %XX escapes are decoded and + is left as a literal plus.
encode_component
Percent-encode a URI component: every byte outside the RFC 3986 unreserved set becomes an upper-case %XX escape of its UTF-8 encoding.
parse_form_query
Parse an application/x-www-form-urlencoded query string into its key/value pairs, decoding %XX escapes and + as a space.