2003-08-31 03:00:15 +02:00
|
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
|
|
|
|
"http://www.w3.org/TR/html4/strict.dtd">
|
|
|
|
<html>
|
|
|
|
|
|
|
|
<head>
|
|
|
|
<title>LuaSocket: Network support for the Lua language</title>
|
|
|
|
<link rel="stylesheet" href="reference.css" type="text/css">
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<!-- header ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
|
|
|
|
|
|
|
<div class=header>
|
|
|
|
<hr>
|
|
|
|
<center>
|
|
|
|
<table summary="LuaSocket logo">
|
|
|
|
<tr><td align=center><a href="http://www.lua.org">
|
|
|
|
<img border=0 alt="LuaSocket" src="luasocket.png">
|
|
|
|
</a></td></tr>
|
|
|
|
<tr><td align=center valign=top>Network support for the Lua language
|
|
|
|
</td></tr>
|
|
|
|
</table>
|
|
|
|
<p class=bar>
|
|
|
|
<a href="home.html">home</a> ·
|
|
|
|
<a href="home.html#download">download</a> ·
|
|
|
|
<a href="introduction.html">introduction</a> ·
|
|
|
|
<a href="reference.html">reference</a>
|
|
|
|
</p>
|
|
|
|
</center>
|
|
|
|
<hr>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- http +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
|
|
|
|
|
|
|
<h2 id=http>HTTP</h2>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
HTTP (Hyper Text Transfer Protocol) is the protocol used to exchange
|
|
|
|
information between web-browsers and servers. The <tt>http.lua</tt>
|
|
|
|
module offers support for the client side of the HTTP protocol (i.e.,
|
|
|
|
the facilities that would be used by a web-browser implementation). The
|
|
|
|
implementation conforms to the HTTP/1.1 standard,
|
|
|
|
<a href="http://www.cs.princeton.edu/~diego/rfc/rfc2616.txt">RFC
|
|
|
|
2616</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
The module exports functions that provide HTTP functionality in different
|
2004-06-15 22:05:54 +02:00
|
|
|
levels of abstraction, from the simple <a
|
|
|
|
href="#get"><tt>get</tt></a> function to the generic, LTN12 based <a
|
|
|
|
href="#request"><tt>request</tt></a> function.
|
2003-08-31 03:00:15 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
URLs must conform to
|
|
|
|
<a href="http://www.cs.princeton.edu/~diego/rfc/rfc1738.txt">RFC
|
|
|
|
1738</a>,
|
|
|
|
that is, an URL is a string in the form:
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<blockquote>
|
|
|
|
<pre>
|
|
|
|
[http://][<user>[:<password>]@]<host>[:<port>][/<path>]
|
|
|
|
</pre>
|
|
|
|
</blockquote>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
MIME headers are represented as a Lua table in the form:
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<blockquote>
|
|
|
|
<table summary="MIME headers in Lua table">
|
|
|
|
<tr><td><tt>
|
|
|
|
headers = {<br>
|
|
|
|
field-1-name = <i>field-1-value</i>,<br>
|
|
|
|
field-2-name = <i>field-2-value</i>,<br>
|
|
|
|
field-3-name = <i>field-3-value</i>,
|
|
|
|
</tt></td></tr>
|
|
|
|
<tr><td align=center><tt>
|
|
|
|
...
|
|
|
|
</tt></td></tr>
|
|
|
|
<tr><td><tt>
|
|
|
|
field-n-name = <i>field-n-value</i><br>
|
|
|
|
}
|
|
|
|
</tt></td></tr>
|
|
|
|
</table>
|
|
|
|
</blockquote>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
Field names are case insensitive (as specified by the standard) and all
|
|
|
|
functions work with lowercase field names.
|
|
|
|
Field values are left unmodified.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=note>
|
|
|
|
Note: MIME headers are independent of order. Therefore, there is no problem
|
|
|
|
in representing them in a Lua table.
|
|
|
|
</p>
|
|
|
|
|
2004-06-16 01:00:56 +02:00
|
|
|
<p>
|
|
|
|
The following constants can be set to control the default behaviour of
|
|
|
|
the HTTP module:
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
<li> <tt>PORT</tt>: default port used for connections;
|
|
|
|
<li> <tt>PROXY</tt>: default proxy used for connections;
|
|
|
|
<li> <tt>TIMEOUT</tt>: sets the timeout for all I/O operations;
|
|
|
|
<li> <tt>USERAGENT</tt>: default user agent reported to server.
|
|
|
|
</ul>
|
|
|
|
|
2003-08-31 03:00:15 +02:00
|
|
|
<!-- http.get +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
|
|
|
|
|
|
|
<p class=name id=get>
|
2004-06-16 01:00:56 +02:00
|
|
|
http.<b>get(</b>url<b>)</b><br>
|
2003-08-31 03:00:15 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=description>
|
2004-06-16 01:00:56 +02:00
|
|
|
Performs the HTTP method <tt>GET</tt>.
|
2003-08-31 03:00:15 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=parameters>
|
2004-06-16 01:00:56 +02:00
|
|
|
<tt>Url</tt> identifies the entity to retrieve.
|
2003-08-31 03:00:15 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=return>
|
2004-06-16 01:00:56 +02:00
|
|
|
If successful, the function returns the response message body, the mime
|
|
|
|
headers, and the status code. In case of failure, the
|
|
|
|
function returns <tt><b>nil</b></tt> followed by an error message.
|
2003-08-31 03:00:15 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=note>
|
|
|
|
Note: The function is trivially implemented with the use of the
|
|
|
|
<a href="#request"><tt>request</tt></a> function.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<pre class=example>
|
2004-06-16 01:00:56 +02:00
|
|
|
-- load the http module
|
|
|
|
http = require("http")
|
|
|
|
|
2003-08-31 03:00:15 +02:00
|
|
|
-- connect to server "www.tecgraf.puc-rio.br" and retrieves this manual
|
|
|
|
-- file from "/luasocket/http.html"
|
2004-06-16 01:00:56 +02:00
|
|
|
b, h, c = http.get("http://www.tecgraf.puc-rio.br/luasocket/http.html")
|
2003-08-31 03:00:15 +02:00
|
|
|
|
|
|
|
-- connect to server "www.tecgraf.puc-rio.br" and tries to retrieve
|
|
|
|
-- "~diego/auth/index.html". Fails because authentication is needed.
|
2004-06-16 01:00:56 +02:00
|
|
|
b, h, c = http.get("http://www.tecgraf.puc-rio.br/~diego/auth/index.html")
|
2003-08-31 03:00:15 +02:00
|
|
|
-- b returns some useless page telling about the denied access,
|
|
|
|
-- h returns authentication information
|
|
|
|
-- and c returns with value 401 (Authentication Required)
|
|
|
|
|
|
|
|
-- tries to connect to server "wrong.host" to retrieve "/"
|
|
|
|
-- and fails because the host does not exist.
|
2004-06-16 01:00:56 +02:00
|
|
|
r, e = http.get("http://wrong.host/")
|
|
|
|
-- r is nil, and e returns with value "host not found"
|
2003-08-31 03:00:15 +02:00
|
|
|
</pre>
|
|
|
|
|
|
|
|
<!-- http.post ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
|
|
|
|
|
|
|
<p class=name id=post>
|
2004-06-16 01:00:56 +02:00
|
|
|
http.<b>post(</b>url, body<b>)</b><br>
|
2003-08-31 03:00:15 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=description>
|
|
|
|
Same as <a href="#get"><tt>get</tt></a>, except
|
|
|
|
that the <tt>POST</tt> method is used and the request
|
|
|
|
message <tt>body</tt> is sent along with the request.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=note>
|
|
|
|
Note: This function is also trivially implemented with the use of the
|
|
|
|
<a href="#request"><tt>request</tt></a> function.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
|
|
|
|
|
|
|
<p class=name id=request>
|
2004-06-16 01:00:56 +02:00
|
|
|
http.<b>request{</b><br>
|
2003-08-31 03:00:15 +02:00
|
|
|
url = <i>string</i>,<br>
|
2004-06-16 01:00:56 +02:00
|
|
|
[sink = <i>LTN12 sink</i>],]<br>
|
|
|
|
[method = <i>string</i>,]<br>
|
|
|
|
[headers = <i>header-table</i>,]<br>
|
|
|
|
[source = <i>LTN12 source</i>],<br>
|
|
|
|
[step = <i>LTN12 pump step</i>,]<br>
|
|
|
|
[proxy = <i>string</i>,]<br>
|
|
|
|
[redirect = <i>boolean</i>]<br>
|
2003-08-31 03:00:15 +02:00
|
|
|
<b>}</b>
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=description>
|
2004-06-16 01:00:56 +02:00
|
|
|
Performs the generic HTTP request, controled by a request table.
|
2003-08-31 03:00:15 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=parameters>
|
2004-06-16 03:02:14 +02:00
|
|
|
The most important parameters are the <tt>url</tt> and the <em>simple</em> LTN12 <tt>sink</tt> that will receive the downloaded content.
|
2004-06-16 01:00:56 +02:00
|
|
|
Any part of the <tt>url</tt> can be overriden by including
|
|
|
|
the appropriate field in the request table.
|
|
|
|
If authentication information is provided, the function
|
2003-08-31 03:00:15 +02:00
|
|
|
uses the Basic Authentication Scheme (see <a href="#authentication">note</a>)
|
2004-06-16 01:00:56 +02:00
|
|
|
to retrieve the document. If <tt>sink</tt> is <tt><b>nil</b></tt>, the
|
|
|
|
function discards the downloaded data. The optional parameters are the
|
|
|
|
following:
|
|
|
|
</p>
|
|
|
|
<ul>
|
|
|
|
<li><tt>method</tt>: The HTTP request method. Defaults to "GET";
|
|
|
|
<li><tt>headers</tt>: Any additional HTTP headers to send with the request;
|
2004-06-16 03:02:14 +02:00
|
|
|
<li><tt>source</tt>: <em>simple</em> LTN12 source to provide the request body. If there
|
2004-06-16 01:00:56 +02:00
|
|
|
is a body, you need to provide an appropriate "<tt>content-length</tt>"
|
|
|
|
request header field, or the function will attempt to send the body as
|
|
|
|
"<tt>chunked</tt>" (something few servers support). Defaults to the empty source;
|
|
|
|
<li><tt>step</tt>: LTN12 pump step function used to move data.
|
|
|
|
Defaults to the LTN12 <tt>pump.step</tt> function.
|
|
|
|
<li><tt>proxy</tt>: The URL of a proxy server to use. Defaults to no proxy;
|
|
|
|
<li><tt>redirect</tt>: Set to <tt><b>false</b></tt> to prevent the
|
|
|
|
function from automatically following 301 or 302 server redirect messages.
|
|
|
|
</ul>
|
2003-08-31 03:00:15 +02:00
|
|
|
|
|
|
|
<p class=return>
|
2004-06-16 01:00:56 +02:00
|
|
|
In case of failure, the function returns <tt><b>nil</b></tt> followed by an
|
|
|
|
error message. If successful, the function returns a table
|
|
|
|
with all components of the response message. The response table has the following form:
|
2003-08-31 03:00:15 +02:00
|
|
|
</p>
|
|
|
|
|
|
|
|
<blockquote><tt>
|
2004-06-16 01:00:56 +02:00
|
|
|
respt = {<br>
|
2003-08-31 03:00:15 +02:00
|
|
|
headers = <i>header-table</i>,<br>
|
|
|
|
status = <i>string</i>,<br>
|
|
|
|
code = <i>number</i>,<br>
|
|
|
|
}
|
|
|
|
</tt></blockquote>
|
|
|
|
|
|
|
|
<p class=return>
|
|
|
|
Even when there was failure (URL not found, for example), the
|
2004-06-16 01:00:56 +02:00
|
|
|
function usually succeeds retrieving a message body (a web page informing the
|
2003-08-31 03:00:15 +02:00
|
|
|
URL was not found or some other useless page). To make sure the
|
|
|
|
operation was successful, check the returned status <tt>code</tt>. For
|
|
|
|
a list of the possible values and their meanings, refer to <a
|
|
|
|
href="http://www.cs.princeton.edu/~diego/rfc/rfc2616.txt">RFC
|
|
|
|
2616</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<pre class=example>
|
2004-06-16 01:00:56 +02:00
|
|
|
-- load the http module
|
|
|
|
http = require("http")
|
|
|
|
|
2003-08-31 03:00:15 +02:00
|
|
|
-- Requests information about a document, without downloading it.
|
|
|
|
-- Useful, for example, if you want to display a download gauge and need
|
|
|
|
-- to know the size of the document in advance
|
2004-06-16 01:00:56 +02:00
|
|
|
respt = http.request {
|
2003-08-31 03:00:15 +02:00
|
|
|
method = "HEAD",
|
|
|
|
url = "http://www.tecgraf.puc-rio.br/~diego"
|
|
|
|
}
|
|
|
|
-- Would return the following headers:
|
2004-06-16 01:00:56 +02:00
|
|
|
-- respt.headers = {
|
2003-08-31 03:00:15 +02:00
|
|
|
-- date = "Tue, 18 Sep 2001 20:42:21 GMT",
|
|
|
|
-- server = "Apache/1.3.12 (Unix) (Red Hat/Linux)",
|
|
|
|
-- ["last-modified"] = "Wed, 05 Sep 2001 06:11:20 GMT",
|
|
|
|
-- ["content-length"] = 15652,
|
|
|
|
-- ["connection"] = "close",
|
|
|
|
-- ["content-Type"] = "text/html"
|
|
|
|
-- }
|
|
|
|
</pre>
|
|
|
|
</blockquote>
|
|
|
|
|
|
|
|
<p class=note id=authentication>
|
|
|
|
Note: Some URLs are protected by their
|
|
|
|
servers from anonymous download. For those URLs, the server must receive
|
|
|
|
some sort of authentication along with the request or it will deny
|
|
|
|
download and return status "401 Authentication Required".
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=note>
|
|
|
|
The HTTP/1.1 standard defines two authentication methods: the Basic
|
|
|
|
Authentication Scheme and the Digest Authentication Scheme, both
|
|
|
|
explained in detail in
|
|
|
|
<a href="http://www.cs.princeton.edu/~diego/rfc/rfc2068.txt">RFC 2068</a>.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<p class=note>The Basic Authentication Scheme sends
|
|
|
|
<tt><user></tt> and
|
|
|
|
<tt><password></tt> unencrypted to the server and is therefore
|
|
|
|
considered unsafe. Unfortunately, by the time of this implementation,
|
|
|
|
the wide majority of servers and browsers support the Basic Scheme only.
|
|
|
|
Therefore, this is the method used by the toolkit whenever
|
|
|
|
authentication is required.
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<pre class=example>
|
2004-06-16 01:00:56 +02:00
|
|
|
-- load required modules
|
|
|
|
http = require("http")
|
|
|
|
mime = require("mime")
|
|
|
|
|
2003-08-31 03:00:15 +02:00
|
|
|
-- Connect to server "www.tecgraf.puc-rio.br" and tries to retrieve
|
|
|
|
-- "~diego/auth/index.html", using the provided name and password to
|
|
|
|
-- authenticate the request
|
2004-06-16 01:00:56 +02:00
|
|
|
respt = http.request{
|
2003-08-31 03:00:15 +02:00
|
|
|
url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html",
|
|
|
|
user = "diego",
|
|
|
|
password = "password"
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Alternatively, one could fill the appropriate header and authenticate
|
|
|
|
-- the request directly.
|
2004-06-16 01:00:56 +02:00
|
|
|
respt = http.request {
|
2003-08-31 03:00:15 +02:00
|
|
|
url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html",
|
2004-06-16 01:00:56 +02:00
|
|
|
headers = { authentication = "Basic " .. (mime.b64("diego:password")) }
|
2003-08-31 03:00:15 +02:00
|
|
|
}
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
|
|
|
|
|
|
|
|
<div class=footer>
|
|
|
|
<hr>
|
|
|
|
<center>
|
|
|
|
<p class=bar>
|
|
|
|
<a href="home.html">home</a> ·
|
|
|
|
<a href="home.html#download">download</a> ·
|
|
|
|
<a href="introduction.html">introduction</a> ·
|
|
|
|
<a href="reference.html">reference</a>
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<small>
|
|
|
|
Last modified by Diego Nehab on <br>
|
|
|
|
Sat Aug 9 01:00:41 PDT 2003
|
|
|
|
</small>
|
|
|
|
</p>
|
|
|
|
</center>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</body>
|
|
|
|
</html>
|