url.normalize
Available inall subroutines.
Normalizes a URL by downcasing the scheme and hostname, removing default ports, decoding unnecessary encodings, and normalizing path segments.
The input can be either a complete URL with scheme and authority, or only the path, query string and fragment portion of a URL, such as req.url.
If the input URL is of a scheme other than http or https, or its syntax is invalid, the URL is returned unchanged.
This function implements URL normalization according to RFC 9110 and RFC 3986. In particular, the following steps are taken:
- The scheme is normalized to lowercase.
- The hostname is normalized to lowercase. IPv6 address hostnames are normalized to lowercase hexadecimal, but not otherwise modified.
- The port, if present, is removed if it matches the default for the scheme (80 for
httpand 443 forhttps). - An empty path following the scheme and hostname is normalized to
/. - Unnecessarily percent-encoded characters in the path are decoded, and encoded characters are normalized to uppercase hexadecimal. The set of characters which do not require encoding are the alphanumeric characters and
-._~. - The path segments
/./and/../are interpreted and removed. - Any query string, if present, is preserved unchanged.
- Any fragment, if present, is discarded.
Example
# Example req.url: /foo/../foo%3dbarset req.url = url.normalize(req.url);# req.url is now /foo%3Dbar
# Construct a complete normalized URL for the requestdeclare local var.url STRING;set var.url = url.normalize(req.protocol "://" req.http.host req.url);