1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
6 <meta name=
"description" content=
"LuaSocket: URL manipulation">
7 <meta name=
"keywords" content=
"Lua, LuaSocket, URL, Library, Link, Network, Support">
8 <title>LuaSocket: URL support
</title>
9 <link rel=
"stylesheet" href=
"reference.css" type=
"text/css">
14 <!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
19 <table summary=
"LuaSocket logo">
20 <tr><td align=center
><a href=
"http://www.lua.org">
21 <img width=
128 height=
128 border=
0 alt=
"LuaSocket" src=
"luasocket.png">
23 <tr><td align=center valign=top
>Network support for the Lua language
27 <a href=
"index.html">home
</a> ·
28 <a href=
"index.html#download">download
</a> ·
29 <a href=
"installation.html">installation
</a> ·
30 <a href=
"introduction.html">introduction
</a> ·
31 <a href=
"reference.html">reference
</a>
37 <!-- url ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
42 The
<tt>url
</tt> namespace provides functions to parse, protect,
43 and build URLs, as well as functions to compose absolute URLs
44 from base and relative URLs, according to
45 <a href=
"http://www.ietf.org/rfc/rfc2396.txt">RFC
2396</a>.
49 To obtain the
<tt>url
</tt> namespace, run:
53 -- loads the URL module
54 local url = require(
"socket.url")
58 An URL is defined by the following grammar:
63 <url
> ::= [
<scheme
>:][//
<authority
>][/
<path
>][;
<params
>][?
<query
>][#
<fragment
>]
<br>
64 <authority
> ::= [
<userinfo
>@]
<host
>[:
<port
>]
<br>
65 <userinfo
> ::=
<user
>[:
<password
>]
<br>
66 <path
> ::= {
<segment
>/}
<segment
><br>
70 <!-- absolute +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
72 <p class=name
id=
"absolute">
73 url.
<b>absolute(
</b>base, relative
<b>)
</b>
77 Builds an absolute URL from a base URL and a relative URL.
81 <tt>Base
</tt> is a string with the base URL or
82 a parsed URL table.
<tt>Relative
</tt> is a
83 string with the relative URL.
87 The function returns a string with the absolute URL.
92 govern the composition are fairly complex, and are described in detail in
93 <a href=
"http://www.ietf.org/rfc/rfc2396.txt">RFC
2396</a>.
94 The example bellow should give an idea of what the rules are.
109 g?y = http://a/b/c/g?y
110 #s = http://a/b/c/d;p?q#s
111 g#s = http://a/b/c/g#s
112 g?y#s = http://a/b/c/g?y#s
114 g;x = http://a/b/c/g;x
115 g;x?y#s = http://a/b/c/g;x?y#s
126 <!-- build ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
128 <p class=name
id=
"build">
129 url.
<b>build(
</b>parsed_url
<b>)
</b>
132 <p class=description
>
133 Rebuilds an URL from its parts.
137 <tt>Parsed_url
</tt> is a table with same components returned by
138 <a href=
"#parse"><tt>parse
</tt></a>.
139 Lower level components, if specified,
140 take precedence over high level components of the URL grammar.
144 The function returns a string with the built URL.
147 <!-- build_path +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
149 <p class=name
id=
"build_path">
150 url.
<b>build_path(
</b>segments, unsafe
<b>)
</b>
153 <p class=description
>
154 Builds a
<tt><path
></tt> component from a list of
155 <tt><segment
></tt> parts.
156 Before composition, any reserved characters found in a segment are escaped into
157 their protected form, so that the resulting path is a valid URL path
162 <tt>Segments
</tt> is a list of strings with the
<tt><segment
></tt>
163 parts. If
<tt>unsafe
</tt> is anything but
<b><tt>nil
</tt></b>, reserved
164 characters are left untouched.
168 The function returns a string with the
169 built
<tt><path
></tt> component.
172 <!-- escape +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
174 <p class=name
id=
"escape">
175 url.
<b>escape(
</b>content
<b>)
</b>
178 <p class=description
>
179 Applies the URL escaping content coding to a string
180 Each byte is encoded as a percent character followed
181 by the two byte hexadecimal representation of its integer
186 <tt>Content
</tt> is the string to be encoded.
190 The function returns the encoded string.
195 url = require(
"socket.url")
197 code = url.escape(
"/#?;")
198 -- code =
"%2f%23%3f%3b"
201 <!-- parse ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
203 <p class=name
id=
"parse">
204 url.
<b>parse(
</b>url, default
<b>)
</b>
207 <p class=description
>
208 Parses an URL given as a string into a Lua table with its components.
212 <tt>Url
</tt> is the URL to be parsed. If the
<tt>default
</tt> table is
213 present, it is used to store the parsed fields. Only fields present in the
214 URL are overwritten. Therefore, this table can be used to pass default
215 values for each field.
219 The function returns a table with all the URL components:
224 url =
<i>string
</i>,
<br>
225 scheme =
<i>string
</i>,
<br>
226 authority =
<i>string
</i>,
<br>
227 path =
<i>string
</i>,
<br>
228 params =
<i>string
</i>,
<br>
229 query =
<i>string
</i>,
<br>
230 fragment =
<i>string
</i>,
<br>
231 userinfo =
<i>string
</i>,
<br>
232 host =
<i>string
</i>,
<br>
233 port =
<i>string
</i>,
<br>
234 user =
<i>string
</i>,
<br>
235 password =
<i>string
</i><br>
241 url = require(
"socket.url")
243 parsed_url = url.parse(
"http://www.example.com/cgilua/index.lua?a=2#there")
246 -- authority =
"www.example.com",
247 -- path =
"/cgilua/index.lua"
249 -- fragment =
"there",
250 -- host =
"www.puc-rio.br",
253 parsed_url = url.parse(
"ftp://root:passwd@unsafe.org/pub/virus.exe;type=i")
256 -- authority =
"root:passwd@unsafe.org",
257 -- path =
"/pub/virus.exe",
258 -- params =
"type=i",
259 -- userinfo =
"root:passwd",
260 -- host =
"unsafe.org",
262 -- password =
"passwd",
266 <!-- parse_path +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
268 <p class=name
id=
"parse_path">
269 url.
<b>parse_path(
</b>path
<b>)
</b>
272 <p class=description
>
273 Breaks a
<tt><path
></tt> URL component into all its
274 <tt><segment
></tt> parts.
277 <p class=description
>
278 <tt>Path
</tt> is a string with the path to be parsed.
282 Since some characters are reserved in URLs, they must be escaped
283 whenever present in a
<tt><path
></tt> component. Therefore, before
284 returning a list with all the parsed segments, the function removes
285 escaping from all of them.
288 <!-- unescape +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
290 <p class=name
id=
"unescape">
291 url.
<b>unescape(
</b>content
<b>)
</b>
294 <p class=description
>
295 Removes the URL escaping content coding from a string.
299 <tt>Content
</tt> is the string to be decoded.
303 The function returns the decoded string.
306 <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
312 <a href=
"index.html">home
</a> ·
313 <a href=
"index.html#down">download
</a> ·
314 <a href=
"installation.html">installation
</a> ·
315 <a href=
"introduction.html">introduction
</a> ·
316 <a href=
"reference.html">reference
</a>
320 Last modified by Diego Nehab on
<br>
321 Thu Apr
20 00:
26:
05 EDT
2006