|
1 | | -URI_Parsing |
2 | | -=========== |
| 1 | +URI Parsing |
| 2 | +===== |
3 | 3 |
|
4 | 4 | JavaScript functions for validating, parsing, and normalizing URIs and email addresses |
| 5 | + |
| 6 | +These do not support IPvFuture literal address formats. |
| 7 | + |
| 8 | +===== |
| 9 | + |
| 10 | +**Normalizing functions for parts of a URI** |
| 11 | + |
| 12 | +- **`normalizeHost(host)`** |
| 13 | + Converts an obscured host to a more readable one. |
| 14 | + Returns `null` if it's not a valid host. |
| 15 | + |
| 16 | +- **`normalizeDNSHost(host[, requireMultipleLabels])`** |
| 17 | + Converts an obscured host to a more readable one; only accepts IP addresses and DNS domain names as valid. |
| 18 | + |
| 19 | + - `requireMultipleLabels` (optional) |
| 20 | + Specify whether a domain must consist of multiple labels (e.g., if true, "localhost" would be considered invalid). |
| 21 | + |
| 22 | + Returns `null` if it's not a valid host. |
| 23 | + |
| 24 | +- **`normalizeIPv4(ip)`** |
| 25 | + Converts an obscured IPv4 to a more readable one. |
| 26 | + Returns `null` if it's not a valid IPv4. |
| 27 | + |
| 28 | +- **`normalizeIPv6(ip)`** |
| 29 | + Converts an obscured IPv6 to a more readable one. |
| 30 | + Returns `null` if it's not a valid IPv6. |
| 31 | + |
| 32 | +- **`normalizePath(path)`** |
| 33 | + Converts an obscured path to a more readable one. |
| 34 | + |
| 35 | +===== |
| 36 | + |
| 37 | +**URI and email parsing functions** |
| 38 | + |
| 39 | +Output of all parsing functions is normalized |
| 40 | + |
| 41 | +- **`parseURI(uri)`** |
| 42 | + Splits a URI into its parts. |
| 43 | + Returns an object including the following, or `null` if the URI is not valid. |
| 44 | + - `uri` entire normalized URI |
| 45 | + - `scheme` |
| 46 | + - `authority` entire authority (empty string if there isn't one) |
| 47 | + - `userinfo` |
| 48 | + - `host` |
| 49 | + - `port` |
| 50 | + - `path` |
| 51 | + - `query` |
| 52 | + - `fragment` |
| 53 | + |
| 54 | +- **`parseHttp(uri[, requireMultipleLabels])`** |
| 55 | + Splits an http or https scheme URI into its parts. |
| 56 | + |
| 57 | + - `requireMultipleLabels` (optional) |
| 58 | + Specify whether a domain must consist of multiple labels (e.g., if true, "localhost" would be considered invalid). |
| 59 | + |
| 60 | + Returns an object including the following, or `null` if the URI is not valid. |
| 61 | + - `uri` entire normalized URI |
| 62 | + - `scheme` |
| 63 | + - `authority` entire authority |
| 64 | + - `userinfo` |
| 65 | + - `host` |
| 66 | + - `port` |
| 67 | + - `path` |
| 68 | + - `query` |
| 69 | + - `fragment` |
| 70 | + |
| 71 | +- **`parseMailto(uri)`** |
| 72 | + Splits a mailto scheme URI into its parts. Invalid email addresses are discarded. |
| 73 | + Returns an object including the following, or `null` if the URI is not valid or if there is no valid destination. |
| 74 | + - `uri` entire normalized URI |
| 75 | + - `scheme` *mailto* |
| 76 | + - `to` array of valid email addresses |
| 77 | + - `cc` array of valid email addresses |
| 78 | + - `bcc` array of valid email addresses |
| 79 | + - `subject` |
| 80 | + - `body` |
| 81 | + - `headers` array of other headers besides the above (each header is an object `{name, value}`) |
| 82 | + |
| 83 | +- **`parseQuery(queryString[, name])`** |
| 84 | + Parses a query string as a sequence of name/value pairs. |
| 85 | + If `name` is not specified, returns an array of name/value pairs (each pair is an object `{name, value}`). |
| 86 | + If `name` is specified, returns an array of values with that name. |
| 87 | + |
| 88 | +- **`parseEmailAddress(address)`** |
| 89 | + Splits a single email address into its parts. Unfolds whitespace and removes any comments. Obsolete syntax is not supported. |
| 90 | + Returns an object including the following, or `null` if the address is not valid. |
| 91 | + - `display` display name |
| 92 | + - `local` local part of the address |
| 93 | + - `domain` domain part of the address |
| 94 | + - `unrecognizedDomain` true if the domain is something other than a DNS domain, IPv4, IPv4 literal, or IPv6 literal |
| 95 | + - `simple` *local@domain* |
| 96 | + - `full` *"display name" \<local@domain\>* if there is a display name, or *local@domain* if not |
| 97 | + - `stripped` same address that was passed to the function, but unfolded and without any comments |
| 98 | + |
| 99 | +===== |
| 100 | + |
| 101 | +**Helper functions** |
| 102 | + |
| 103 | +- **`fixHyperlink(str[, allowedSchemes[, domain]])`** |
| 104 | + Attempts to fix a URI (if needed) and normalizes it. If the string does not have a scheme, it will be assumed that it's meant to be that of the current page (e.g., if `str` is a relative URL). |
| 105 | + |
| 106 | + - `allowedSchemes` (optional) |
| 107 | + A string or array of strings listing accepted schemes; *http*, *https*, and *mailto* by default if none are specified. |
| 108 | + - `domain` (optional) |
| 109 | + Host name (and optionally port) to use if an http/https URI is relative; current page's domain by default. |
| 110 | + |
| 111 | + Returns `null` if it can't be fixed or if `allowedSchemes` is invalid. |
| 112 | + |
| 113 | +===== |
| 114 | + |
| 115 | +**General references** |
| 116 | + |
| 117 | +- RFC 3986 "Uniform Resource Identifier (URI): Generic Syntax" http://tools.ietf.org/html/rfc3986 |
| 118 | +- How to Obscure Any URL http://www.pc-help.org/obscure.htm |
| 119 | +- RFC 6068 "The 'mailto' URI Scheme" http://tools.ietf.org/html/rfc6068 |
0 commit comments