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-urlencodeddata — 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. Useparse_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 corruptsGoogle+,C++, and a leading+on a phone number. Usedecode.
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:
%XXescapes 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
%XXescape of its UTF-8 encoding. - parse_
form_ query - Parse an
application/x-www-form-urlencodedquery string into its key/value pairs, decoding%XXescapes and+as a space.