url.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html>
  4. <head>
  5. <meta name="description" content="LuaSocket: URL manipulation">
  6. <meta name="keywords" content="Lua, LuaSocket, URL, Library, Link, Network, Support">
  7. <title>LuaSocket: URL support</title>
  8. <link rel="stylesheet" href="reference.css" type="text/css">
  9. </head>
  10. <body>
  11. <!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  12. <div class=header>
  13. <hr>
  14. <center>
  15. <table summary="LuaSocket logo">
  16. <tr><td align=center><a href="http://www.lua.org">
  17. <img width=128 height=128 border=0 alt="LuaSocket" src="luasocket.png">
  18. </a></td></tr>
  19. <tr><td align=center valign=top>Network support for the Lua language
  20. </td></tr>
  21. </table>
  22. <p class=bar>
  23. <a href="index.html">home</a> &middot;
  24. <a href="index.html#download">download</a> &middot;
  25. <a href="installation.html">installation</a> &middot;
  26. <a href="introduction.html">introduction</a> &middot;
  27. <a href="reference.html">reference</a>
  28. </p>
  29. </center>
  30. <hr>
  31. </div>
  32. <!-- url ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  33. <h2 id="url">URL</h2>
  34. <p>
  35. The <tt>url</tt> namespace provides functions to parse, protect,
  36. and build URLs, as well as functions to compose absolute URLs
  37. from base and relative URLs, according to
  38. <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>.
  39. </p>
  40. <p>
  41. To obtain the <tt>url</tt> namespace, run:
  42. </p>
  43. <pre class=example>
  44. -- loads the URL module
  45. local url = require("socket.url")
  46. </pre>
  47. <p>
  48. An URL is defined by the following grammar:
  49. </p>
  50. <blockquote>
  51. <tt>
  52. &lt;url&gt; ::= [&lt;scheme&gt;:][//&lt;authority&gt;][/&lt;path&gt;][;&lt;params&gt;][?&lt;query&gt;][#&lt;fragment&gt;]<br>
  53. &lt;authority&gt; ::= [&lt;userinfo&gt;@]&lt;host&gt;[:&lt;port&gt;]<br>
  54. &lt;userinfo&gt; ::= &lt;user&gt;[:&lt;password&gt;]<br>
  55. &lt;path&gt; ::= {&lt;segment&gt;/}&lt;segment&gt;<br>
  56. </tt>
  57. </blockquote>
  58. <!-- absolute +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  59. <p class=name id="absolute">
  60. url.<b>absolute(</b>base, relative<b>)</b>
  61. </p>
  62. <p class=description>
  63. Builds an absolute URL from a base URL and a relative URL.
  64. </p>
  65. <p class=parameters>
  66. <tt>Base</tt> is a string with the base URL or
  67. a parsed URL table. <tt>Relative</tt> is a
  68. string with the relative URL.
  69. </p>
  70. <p class=return>
  71. The function returns a string with the absolute URL.
  72. </p>
  73. <p class=note>
  74. Note: The rules that
  75. govern the composition are fairly complex, and are described in detail in
  76. <a href="http://www.ietf.org/rfc/rfc2396.txt">RFC 2396</a>.
  77. The example bellow should give an idea of what the rules are.
  78. </p>
  79. <pre class=example>
  80. http://a/b/c/d;p?q
  81. +
  82. g:h = g:h
  83. g = http://a/b/c/g
  84. ./g = http://a/b/c/g
  85. g/ = http://a/b/c/g/
  86. /g = http://a/g
  87. //g = http://g
  88. ?y = http://a/b/c/?y
  89. g?y = http://a/b/c/g?y
  90. #s = http://a/b/c/d;p?q#s
  91. g#s = http://a/b/c/g#s
  92. g?y#s = http://a/b/c/g?y#s
  93. ;x = http://a/b/c/;x
  94. g;x = http://a/b/c/g;x
  95. g;x?y#s = http://a/b/c/g;x?y#s
  96. . = http://a/b/c/
  97. ./ = http://a/b/c/
  98. .. = http://a/b/
  99. ../ = http://a/b/
  100. ../g = http://a/b/g
  101. ../.. = http://a/
  102. ../../ = http://a/
  103. ../../g = http://a/g
  104. </pre>
  105. <!-- build ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  106. <p class=name id="build">
  107. url.<b>build(</b>parsed_url<b>)</b>
  108. </p>
  109. <p class=description>
  110. Rebuilds an URL from its parts.
  111. </p>
  112. <p class=parameters>
  113. <tt>Parsed_url</tt> is a table with same components returned by
  114. <a href="#parse"><tt>parse</tt></a>.
  115. Lower level components, if specified,
  116. take precedence over high level components of the URL grammar.
  117. </p>
  118. <p class=return>
  119. The function returns a string with the built URL.
  120. </p>
  121. <!-- build_path +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  122. <p class=name id="build_path">
  123. url.<b>build_path(</b>segments, unsafe<b>)</b>
  124. </p>
  125. <p class=description>
  126. Builds a <tt>&lt;path&gt;</tt> component from a list of
  127. <tt>&lt;segment&gt;</tt> parts.
  128. Before composition, any reserved characters found in a segment are escaped into
  129. their protected form, so that the resulting path is a valid URL path
  130. component.
  131. </p>
  132. <p class=parameters>
  133. <tt>Segments</tt> is a list of strings with the <tt>&lt;segment&gt;</tt>
  134. parts. If <tt>unsafe</tt> is anything but <b><tt>nil</tt></b>, reserved
  135. characters are left untouched.
  136. </p>
  137. <p class=return>
  138. The function returns a string with the
  139. built <tt>&lt;path&gt;</tt> component.
  140. </p>
  141. <!-- escape +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  142. <p class=name id="escape">
  143. url.<b>escape(</b>content<b>)</b>
  144. </p>
  145. <p class=description>
  146. Applies the URL escaping content coding to a string
  147. Each byte is encoded as a percent character followed
  148. by the two byte hexadecimal representation of its integer
  149. value.
  150. </p>
  151. <p class=parameters>
  152. <tt>Content</tt> is the string to be encoded.
  153. </p>
  154. <p class=result>
  155. The function returns the encoded string.
  156. </p>
  157. <pre class=example>
  158. -- load url module
  159. url = require("socket.url")
  160. code = url.escape("/#?;")
  161. -- code = "%2f%23%3f%3b"
  162. </pre>
  163. <!-- parse ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  164. <p class=name id="parse">
  165. url.<b>parse(</b>url, default<b>)</b>
  166. </p>
  167. <p class=description>
  168. Parses an URL given as a string into a Lua table with its components.
  169. </p>
  170. <p class=parameters>
  171. <tt>Url</tt> is the URL to be parsed. If the <tt>default</tt> table is
  172. present, it is used to store the parsed fields. Only fields present in the
  173. URL are overwritten. Therefore, this table can be used to pass default
  174. values for each field.
  175. </p>
  176. <p class=return>
  177. The function returns a table with all the URL components:
  178. </p>
  179. <blockquote><tt>
  180. parsed_url = {<br>
  181. &nbsp;&nbsp;url = <i>string</i>,<br>
  182. &nbsp;&nbsp;scheme = <i>string</i>,<br>
  183. &nbsp;&nbsp;authority = <i>string</i>,<br>
  184. &nbsp;&nbsp;path = <i>string</i>,<br>
  185. &nbsp;&nbsp;params = <i>string</i>,<br>
  186. &nbsp;&nbsp;query = <i>string</i>,<br>
  187. &nbsp;&nbsp;fragment = <i>string</i>,<br>
  188. &nbsp;&nbsp;userinfo = <i>string</i>,<br>
  189. &nbsp;&nbsp;host = <i>string</i>,<br>
  190. &nbsp;&nbsp;port = <i>string</i>,<br>
  191. &nbsp;&nbsp;user = <i>string</i>,<br>
  192. &nbsp;&nbsp;password = <i>string</i><br>
  193. }
  194. </tt></blockquote>
  195. <pre class=example>
  196. -- load url module
  197. url = require("socket.url")
  198. parsed_url = url.parse("http://www.example.com/cgilua/index.lua?a=2#there")
  199. -- parsed_url = {
  200. -- scheme = "http",
  201. -- authority = "www.example.com",
  202. -- path = "/cgilua/index.lua"
  203. -- query = "a=2",
  204. -- fragment = "there",
  205. -- host = "www.puc-rio.br",
  206. -- }
  207. parsed_url = url.parse("ftp://root:[email protected]/pub/virus.exe;type=i")
  208. -- parsed_url = {
  209. -- scheme = "ftp",
  210. -- authority = "root:[email protected]",
  211. -- path = "/pub/virus.exe",
  212. -- params = "type=i",
  213. -- userinfo = "root:passwd",
  214. -- host = "unsafe.org",
  215. -- user = "root",
  216. -- password = "passwd",
  217. -- }
  218. </pre>
  219. <!-- parse_path +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  220. <p class=name id="parse_path">
  221. url.<b>parse_path(</b>path<b>)</b>
  222. </p>
  223. <p class=description>
  224. Breaks a <tt>&lt;path&gt;</tt> URL component into all its
  225. <tt>&lt;segment&gt;</tt> parts.
  226. </p>
  227. <p class=description>
  228. <tt>Path</tt> is a string with the path to be parsed.
  229. </p>
  230. <p class=return>
  231. Since some characters are reserved in URLs, they must be escaped
  232. whenever present in a <tt>&lt;path&gt;</tt> component. Therefore, before
  233. returning a list with all the parsed segments, the function removes
  234. escaping from all of them.
  235. </p>
  236. <!-- unescape +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  237. <p class=name id="unescape">
  238. url.<b>unescape(</b>content<b>)</b>
  239. </p>
  240. <p class=description>
  241. Removes the URL escaping content coding from a string.
  242. </p>
  243. <p class=parameters>
  244. <tt>Content</tt> is the string to be decoded.
  245. </p>
  246. <p class=return>
  247. The function returns the decoded string.
  248. </p>
  249. <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  250. <div class=footer>
  251. <hr>
  252. <center>
  253. <p class=bar>
  254. <a href="index.html">home</a> &middot;
  255. <a href="index.html#down">download</a> &middot;
  256. <a href="installation.html">installation</a> &middot;
  257. <a href="introduction.html">introduction</a> &middot;
  258. <a href="reference.html">reference</a>
  259. </p>
  260. <p>
  261. <small>
  262. Last modified by Diego Nehab on <br>
  263. Thu Apr 20 00:26:05 EDT 2006
  264. </small>
  265. </p>
  266. </center>
  267. </div>
  268. </body>
  269. </html>