diff options
| author | Caleb Maclennan <caleb@alerque.com> | 2026-08-31 11:08:19 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-31 11:08:19 +0300 |
| commit | 8e00f02f24d8618fcbff42c93a7ee512f694cc2f (patch) | |
| tree | afb9d251d53bf2bdba571b91d9609efa477fbf72 /docs | |
| parent | 01162f05408ac4206be15842ef91048c63c23869 (diff) | |
| parent | 630a3b7f4fb0c786170e3903175e964bbc1bc77f (diff) | |
| download | luasocket-8e00f02f24d8618fcbff42c93a7ee512f694cc2f.tar.gz luasocket-8e00f02f24d8618fcbff42c93a7ee512f694cc2f.tar.bz2 luasocket-8e00f02f24d8618fcbff42c93a7ee512f694cc2f.zip | |
Merge pull request #472 from lunarmodules/feat/url-hosttype
feat(url): classify host as name/ipv4/ipv6 in parse and build
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/url.html | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/docs/url.html b/docs/url.html index e03b094..788f409 100644 --- a/docs/url.html +++ b/docs/url.html | |||
| @@ -140,6 +140,14 @@ Lower level components, if specified, | |||
| 140 | take precedence over high level components of the URL grammar. | 140 | take precedence over high level components of the URL grammar. |
| 141 | </p> | 141 | </p> |
| 142 | 142 | ||
| 143 | <p class=parameters> | ||
| 144 | If <tt>host</tt> is not set, the host is taken from whichever single | ||
| 145 | one of <tt>hostname</tt>, <tt>ipv4</tt> or <tt>ipv6</tt> is set | ||
| 146 | (<tt>hosttype</tt> is ignored when building). It is an error to set more | ||
| 147 | than one of <tt>hostname</tt>, <tt>ipv4</tt> or <tt>ipv6</tt> while | ||
| 148 | <tt>host</tt> is absent. | ||
| 149 | </p> | ||
| 150 | |||
| 143 | <p class=return> | 151 | <p class=return> |
| 144 | The function returns a string with the built URL. | 152 | The function returns a string with the built URL. |
| 145 | </p> | 153 | </p> |
| @@ -169,6 +177,62 @@ The function returns a string with the | |||
| 169 | built <tt><path></tt> component. | 177 | built <tt><path></tt> component. |
| 170 | </p> | 178 | </p> |
| 171 | 179 | ||
| 180 | <!-- classify_host ++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 181 | |||
| 182 | <p class=name id="classify_host"> | ||
| 183 | url.<b>classify_host(</b>host<b>)</b> | ||
| 184 | </p> | ||
| 185 | |||
| 186 | <p class=description> | ||
| 187 | Classifies a raw <tt><host></tt> string into <tt>"name"</tt>, | ||
| 188 | <tt>"ipv4"</tt> or <tt>"ipv6"</tt>. This is the same classification | ||
| 189 | <a href="#parse"><tt>parse</tt></a> uses to fill in <tt>hosttype</tt>, | ||
| 190 | exposed for callers that have a host string to classify without a full | ||
| 191 | URL to parse. | ||
| 192 | </p> | ||
| 193 | |||
| 194 | <p class=parameters> | ||
| 195 | <tt>Host</tt> is a host string as found in a URL's authority. Any | ||
| 196 | <tt>:</tt> in <tt>host</tt> is taken as a sign of an IPv6 literal, so | ||
| 197 | the enclosing <tt>[...]</tt> brackets URLs normally require for one are | ||
| 198 | optional here: they are stripped when present, but classification does | ||
| 199 | not depend on them. | ||
| 200 | </p> | ||
| 201 | |||
| 202 | <p class=return> | ||
| 203 | The function returns two values: <tt>hosttype</tt>, one of | ||
| 204 | <tt>"name"</tt>, <tt>"ipv4"</tt> or <tt>"ipv6"</tt>; and <tt>host</tt>, | ||
| 205 | the input with any enclosing <tt>[...]</tt> brackets stripped, if | ||
| 206 | present. | ||
| 207 | </p> | ||
| 208 | |||
| 209 | <p class=note> | ||
| 210 | Note: classification is done by exclusion of shape, not by validating | ||
| 211 | the address. A host is <tt>"ipv4"</tt> if it merely has the shape of | ||
| 212 | four dot-separated digit groups — octet ranges are not checked, | ||
| 213 | so <tt>"999.1.1.1"</tt> classifies as <tt>"ipv4"</tt> — and | ||
| 214 | <tt>"ipv6"</tt> if it merely contains a <tt>:</tt>, whether or not that | ||
| 215 | is a well-formed IPv6 address. Anything left over is <tt>"name"</tt>, | ||
| 216 | whether or not it is actually a valid hostname. | ||
| 217 | </p> | ||
| 218 | |||
| 219 | <pre class=example> | ||
| 220 | -- load url module | ||
| 221 | url = require("socket.url") | ||
| 222 | |||
| 223 | print(url.classify_host("example.com")) | ||
| 224 | -- name example.com | ||
| 225 | |||
| 226 | print(url.classify_host("192.168.1.1")) | ||
| 227 | -- ipv4 192.168.1.1 | ||
| 228 | |||
| 229 | print(url.classify_host("[::1]")) | ||
| 230 | -- ipv6 ::1 | ||
| 231 | |||
| 232 | print(url.classify_host("::1")) | ||
| 233 | -- ipv6 ::1 | ||
| 234 | </pre> | ||
| 235 | |||
| 172 | <!-- escape +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 236 | <!-- escape +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
| 173 | 237 | ||
| 174 | <p class=name id="escape"> | 238 | <p class=name id="escape"> |
| @@ -230,12 +294,28 @@ parsed_url = {<br> | |||
| 230 | fragment = <i>string</i>,<br> | 294 | fragment = <i>string</i>,<br> |
| 231 | userinfo = <i>string</i>,<br> | 295 | userinfo = <i>string</i>,<br> |
| 232 | host = <i>string</i>,<br> | 296 | host = <i>string</i>,<br> |
| 297 | hosttype = <i>string</i>,<br> | ||
| 298 | hostname = <i>string</i>,<br> | ||
| 299 | ipv4 = <i>string</i>,<br> | ||
| 300 | ipv6 = <i>string</i>,<br> | ||
| 233 | port = <i>string</i>,<br> | 301 | port = <i>string</i>,<br> |
| 234 | user = <i>string</i>,<br> | 302 | user = <i>string</i>,<br> |
| 235 | password = <i>string</i><br> | 303 | password = <i>string</i><br> |
| 236 | } | 304 | } |
| 237 | </tt></blockquote> | 305 | </tt></blockquote> |
| 238 | 306 | ||
| 307 | <p class=parameters> | ||
| 308 | When a host is present, <tt>hosttype</tt> is set to one of | ||
| 309 | <tt>"name"</tt>, <tt>"ipv4"</tt> or <tt>"ipv6"</tt>, describing the | ||
| 310 | syntax of <tt>host</tt>. Exactly one of <tt>hostname</tt>, <tt>ipv4</tt> | ||
| 311 | or <tt>ipv6</tt> is then also set to the same value as <tt>host</tt>, | ||
| 312 | matching <tt>hosttype</tt> — the other two are left <b><tt>nil</tt></b>. | ||
| 313 | In case of an ipv6 address the brackets are stripped, both in <tt>host</tt> and <tt>ipv6</tt>. | ||
| 314 | This classification is done by | ||
| 315 | <a href="#classify_host"><tt>classify_host</tt></a>, by exclusion of | ||
| 316 | shape rather than by validating the address (see its notes). | ||
| 317 | </p> | ||
| 318 | |||
| 239 | <pre class=example> | 319 | <pre class=example> |
| 240 | -- load url module | 320 | -- load url module |
| 241 | url = require("socket.url") | 321 | url = require("socket.url") |
| @@ -248,6 +328,8 @@ parsed_url = url.parse("http://www.example.com/cgilua/index.lua?a=2#there") | |||
| 248 | -- query = "a=2", | 328 | -- query = "a=2", |
| 249 | -- fragment = "there", | 329 | -- fragment = "there", |
| 250 | -- host = "www.puc-rio.br", | 330 | -- host = "www.puc-rio.br", |
| 331 | -- hosttype = "name", | ||
| 332 | -- hostname = "www.puc-rio.br", | ||
| 251 | -- } | 333 | -- } |
| 252 | 334 | ||
| 253 | parsed_url = url.parse("ftp://root:passwd@unsafe.org/pub/virus.exe;type=i") | 335 | parsed_url = url.parse("ftp://root:passwd@unsafe.org/pub/virus.exe;type=i") |
