Blame doc/url.html

Packit b53373
Packit b53373
    "http://www.w3.org/TR/html4/strict.dtd">
Packit b53373
<html>
Packit b53373
Packit b53373
<head>
Packit b53373
<meta name="description" content="LuaSocket: URL manipulation">
Packit b53373
<meta name="keywords" content="Lua, LuaSocket, URL, Library, Link, Network, Support"> 
Packit b53373
<title>LuaSocket: URL support</title>
Packit b53373
<link rel="stylesheet" href="reference.css" type="text/css">
Packit b53373
</head>
Packit b53373
Packit b53373
<body>
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
<center>
Packit b53373
Packit b53373
Packit b53373
LuaSocket
Packit b53373
Packit b53373
Network support for the Lua language
Packit b53373
Packit b53373
Packit b53373

Packit b53373
home ·
Packit b53373
download ·
Packit b53373
installation ·
Packit b53373
introduction ·
Packit b53373
reference 
Packit b53373

Packit b53373
</center>
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

URL

Packit b53373
Packit b53373

Packit b53373
The  <tt>url</tt>  namespace provides  functions  to  parse,  protect,
Packit b53373
and build URLs, as well  as functions to compose  absolute URLs
Packit b53373
from base and relative URLs, according to 
Packit b53373
RFC 2396.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
To obtain the <tt>url</tt> namespace, run:
Packit b53373

Packit b53373
Packit b53373
Packit b53373
-- loads the URL module 
Packit b53373
local url = require("socket.url")
Packit b53373
Packit b53373
Packit b53373

Packit b53373
An URL is defined by the following grammar:
Packit b53373

Packit b53373
Packit b53373
Packit b53373
<tt>
Packit b53373
<url> ::= [<scheme>:][//<authority>][/<path>][;<params>][?<query>][#<fragment>]
Packit b53373
<authority> ::= [<userinfo>@]<host>[:<port>]
Packit b53373
<userinfo> ::= <user>[:<password>]
Packit b53373
<path> ::= {<segment>/}<segment>
Packit b53373
</tt>
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
url.absolute(base, relative)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Builds an absolute URL from a base URL and a relative URL. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Base</tt> is a string with the base URL or
Packit b53373
a parsed URL table.  <tt>Relative</tt> is a
Packit b53373
string with the relative URL.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns a string with the absolute URL.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Note: The rules that
Packit b53373
govern the composition are fairly complex, and are described in detail in
Packit b53373
RFC 2396.
Packit b53373
The example bellow should give an idea of what the rules are.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
http://a/b/c/d;p?q
Packit b53373
Packit b53373
+
Packit b53373
Packit b53373
g:h      =  g:h
Packit b53373
g        =  http://a/b/c/g
Packit b53373
./g      =  http://a/b/c/g
Packit b53373
g/       =  http://a/b/c/g/
Packit b53373
/g       =  http://a/g
Packit b53373
//g      =  http://g
Packit b53373
?y       =  http://a/b/c/?y
Packit b53373
g?y      =  http://a/b/c/g?y
Packit b53373
#s       =  http://a/b/c/d;p?q#s
Packit b53373
g#s      =  http://a/b/c/g#s
Packit b53373
g?y#s    =  http://a/b/c/g?y#s
Packit b53373
;x       =  http://a/b/c/;x
Packit b53373
g;x      =  http://a/b/c/g;x
Packit b53373
g;x?y#s  =  http://a/b/c/g;x?y#s
Packit b53373
.        =  http://a/b/c/
Packit b53373
./       =  http://a/b/c/
Packit b53373
..       =  http://a/b/
Packit b53373
../      =  http://a/b/
Packit b53373
../g     =  http://a/b/g
Packit b53373
../..    =  http://a/
Packit b53373
../../   =  http://a/
Packit b53373
../../g  =  http://a/g
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
url.build(parsed_url)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Rebuilds an URL from its parts. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Parsed_url</tt> is a table with same components returned by
Packit b53373
<tt>parse</tt>.
Packit b53373
Lower level components, if specified,
Packit b53373
take  precedence over  high level  components of  the URL grammar. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns a string with the built URL.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
url.build_path(segments, unsafe)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Builds a <tt><path></tt> component from  a list of
Packit b53373
<tt><segment></tt> parts. 
Packit b53373
Before composition, any reserved characters found in a segment are escaped into
Packit b53373
their protected  form, so that  the resulting path  is a valid  URL path
Packit b53373
component.  
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Segments</tt> is a list of strings with the <tt><segment></tt>
Packit b53373
parts. If <tt>unsafe</tt>  is anything  but <tt>nil</tt>,  reserved
Packit b53373
characters are left untouched.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The  function  returns   a string with the  
Packit b53373
built <tt><path></tt> component. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
url.escape(content)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Applies the URL escaping content coding to a string
Packit b53373
Each byte is encoded as a percent character followed
Packit b53373
by the two byte hexadecimal representation of its integer 
Packit b53373
value.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Content</tt> is the string to be encoded.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns the encoded string.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
-- load url module
Packit b53373
url = require("socket.url")
Packit b53373
Packit b53373
code = url.escape("/#?;")
Packit b53373
-- code = "%2f%23%3f%3b"
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
url.parse(url, default)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Parses an URL given as a string into a Lua table with its components.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Url</tt> is the URL to be parsed. If the <tt>default</tt> table is
Packit b53373
present, it is used to store the parsed fields. Only fields present in the
Packit b53373
URL are overwritten. Therefore, this table can be used to pass default
Packit b53373
values for each field.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns a table with all the URL components:
Packit b53373

Packit b53373
Packit b53373
<tt>
Packit b53373
parsed_url = {
Packit b53373
  url = string,
Packit b53373
  scheme = string,
Packit b53373
  authority = string,
Packit b53373
  path = string,
Packit b53373
  params = string,
Packit b53373
  query = string,
Packit b53373
  fragment = string,
Packit b53373
  userinfo = string,
Packit b53373
  host = string,
Packit b53373
  port = string,
Packit b53373
  user = string,
Packit b53373
  password = string
Packit b53373
}
Packit b53373
</tt>
Packit b53373
Packit b53373
Packit b53373
-- load url module
Packit b53373
url = require("socket.url")
Packit b53373
Packit b53373
parsed_url = url.parse("http://www.example.com/cgilua/index.lua?a=2#there")
Packit b53373
-- parsed_url = {
Packit b53373
--   scheme = "http",
Packit b53373
--   authority = "www.example.com",
Packit b53373
--   path = "/cgilua/index.lua"
Packit b53373
--   query = "a=2",
Packit b53373
--   fragment = "there",
Packit b53373
--   host = "www.puc-rio.br",
Packit b53373
-- }
Packit b53373
Packit b53373
parsed_url = url.parse("ftp://root:passwd@unsafe.org/pub/virus.exe;type=i")
Packit b53373
-- parsed_url = {
Packit b53373
--   scheme = "ftp",
Packit b53373
--   authority = "root:passwd@unsafe.org",
Packit b53373
--   path = "/pub/virus.exe",
Packit b53373
--   params = "type=i",
Packit b53373
--   userinfo = "root:passwd",
Packit b53373
--   host = "unsafe.org",
Packit b53373
--   user = "root",
Packit b53373
--   password = "passwd",
Packit b53373
-- }
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
url.parse_path(path)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Breaks a <tt><path></tt> URL component into all its 
Packit b53373
<tt><segment></tt> parts. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Path</tt> is a string with the path to be parsed.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Since  some characters   are   reserved   in   URLs,  they must be escaped
Packit b53373
whenever present in a <tt><path></tt> component. Therefore, before
Packit b53373
returning a list with all the parsed segments, the function removes
Packit b53373
escaping from all of them. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
url.unescape(content)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Removes  the   URL   escaping    content   coding   from   a string.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Content</tt> is the string to be decoded.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns the decoded string. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
<center>
Packit b53373

Packit b53373
home ·
Packit b53373
download ·
Packit b53373
installation ·
Packit b53373
introduction ·
Packit b53373
reference
Packit b53373

Packit b53373

Packit b53373
<small>
Packit b53373
Last modified by Diego Nehab on 
Packit b53373
Thu Apr 20 00:26:05 EDT 2006
Packit b53373
</small>
Packit b53373

Packit b53373
</center>
Packit b53373
Packit b53373
Packit b53373
</body>
Packit b53373
</html>