luasocket/docs/http.html

340 lines
12 KiB
HTML
Raw Permalink Normal View History

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2003-08-31 03:00:15 +02:00
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
2004-06-21 08:07:58 +02:00
<meta name="description" content="LuaSocket: HTTP support">
<meta name="keywords" content="Lua, HTTP, Library, WWW, Browser, Network, Support">
2004-06-21 08:07:58 +02:00
<title>LuaSocket: HTTP support</title>
2003-08-31 03:00:15 +02:00
<link rel="stylesheet" href="reference.css" type="text/css">
</head>
<body>
<!-- header ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<div class="header">
2003-08-31 03:00:15 +02:00
<hr>
<center>
<table summary="LuaSocket logo">
<tr><td align="center"><a href="http://www.lua.org">
<img width="128" height="128" border="0" alt="LuaSocket" src="luasocket.png">
2003-08-31 03:00:15 +02:00
</a></td></tr>
<tr><td align="center" valign="top">Network support for the Lua language
2003-08-31 03:00:15 +02:00
</td></tr>
</table>
<p class="bar">
2009-05-27 11:31:38 +02:00
<a href="index.html">home</a> &middot;
<a href="index.html#download">download</a> &middot;
2003-08-31 03:00:15 +02:00
<a href="introduction.html">introduction</a> &middot;
<a href="introduction.html">introduction</a> &middot;
<a href="reference.html">reference</a>
2003-08-31 03:00:15 +02:00
</p>
</center>
<hr>
</div>
<!-- http +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<h2 id="http">HTTP</h2>
2003-08-31 03:00:15 +02:00
<p>
HTTP (Hyper Text Transfer Protocol) is the protocol used to exchange
2004-06-16 06:28:21 +02:00
information between web-browsers and servers. The <tt>http</tt>
namespace offers full support for the client side of the HTTP
2004-06-16 06:28:21 +02:00
protocol (i.e.,
2003-08-31 03:00:15 +02:00
the facilities that would be used by a web-browser implementation). The
implementation conforms to the HTTP/1.1 standard,
<a href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a>.
2003-08-31 03:00:15 +02:00
</p>
<p>
The module exports functions that provide HTTP functionality in different
levels of abstraction. From the simple
2004-06-17 01:21:49 +02:00
string oriented requests, through generic
2024-03-15 12:55:02 +01:00
<a href="https://github.com/lunarmodules/luasocket/blob/master/ltn012.md">LTN12</a> based, down to even lower-level if you bother to look through the source code.
2003-08-31 03:00:15 +02:00
</p>
<p>
2004-06-16 22:41:03 +02:00
To obtain the <tt>http</tt> namespace, run:
2004-06-16 06:28:21 +02:00
</p>
<pre class="example">
2004-06-16 06:28:21 +02:00
-- loads the HTTP module and any libraries it requires
2005-02-08 11:01:01 +01:00
local http = require("socket.http")
2004-06-16 06:28:21 +02:00
</pre>
2003-08-31 03:00:15 +02:00
<p>
URLs must conform to
<a href="http://www.ietf.org/rfc/rfc1738.txt">RFC 1738</a>,
that is, an URL is a string in the form:
2003-08-31 03:00:15 +02:00
</p>
<blockquote>
<pre>
[http://][&lt;user&gt;[:&lt;password&gt;]@]&lt;host&gt;[:&lt;port&gt;][/&lt;path&gt;]
2003-08-31 03:00:15 +02:00
</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>
&nbsp;&nbsp;field-1-name = <i>field-1-value</i>,<br>
&nbsp;&nbsp;field-2-name = <i>field-2-value</i>,<br>
2004-06-16 22:41:03 +02:00
&nbsp;&nbsp;field-3-name = <i>field-3-value</i>,<br>
&nbsp;&nbsp;...<br>
2003-08-31 03:00:15 +02:00
&nbsp;&nbsp;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 (but see
<a href="socket.html#headers.canonic"><tt>socket.headers.canonic</tt></a>).
2003-08-31 03:00:15 +02:00
Field values are left unmodified.
</p>
<p class="note">
2003-08-31 03:00:15 +02:00
Note: MIME headers are independent of order. Therefore, there is no problem
in representing them in a Lua table.
2003-08-31 03:00:15 +02:00
</p>
2004-06-16 01:00:56 +02:00
<p>
2004-12-13 07:26:01 +01:00
The following constants can be set to control the default behavior of
the HTTP module:
2004-06-16 01:00:56 +02:00
</p>
<ul>
<li> <tt>PROXY</tt>: default proxy used for connections;</li>
<li> <tt>TIMEOUT</tt>: sets the timeout for all I/O operations;</li>
<li> <tt>USERAGENT</tt>: default user agent reported to server.</li>
2004-06-16 01:00:56 +02:00
</ul>
<p class="note">
Note: These constants are global. Changing them will also
change the behavior other code that might be using LuaSocket.
</p>
2003-08-31 03:00:15 +02:00
<!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class="name" id="request">
2004-06-17 01:21:49 +02:00
http.<b>request(</b>url [, body]<b>)</b><br>
2004-06-16 01:00:56 +02:00
http.<b>request{</b><br>
2003-08-31 03:00:15 +02:00
&nbsp;&nbsp;url = <i>string</i>,<br>
2004-06-16 22:41:03 +02:00
&nbsp;&nbsp;[sink = <i>LTN12 sink</i>,]<br>
2004-06-16 01:00:56 +02:00
&nbsp;&nbsp;[method = <i>string</i>,]<br>
&nbsp;&nbsp;[headers = <i>header-table</i>,]<br>
&nbsp;&nbsp;[source = <i>LTN12 source</i>],<br>
&nbsp;&nbsp;[step = <i>LTN12 pump step</i>,]<br>
&nbsp;&nbsp;[proxy = <i>string</i>,]<br>
2005-08-12 07:56:32 +02:00
&nbsp;&nbsp;[redirect = <i>boolean</i>,]<br>
&nbsp;&nbsp;[create = <i>function</i>,]<br>
&nbsp;&nbsp;[maxredirects = <i>number</i>]<br>
2003-08-31 03:00:15 +02:00
<b>}</b>
</p>
<p class="description">
The request function has two forms. The simple form downloads
a URL using the <tt>GET</tt> or <tt>POST</tt> method and is based
on strings. The generic form performs any HTTP method and is
2024-03-15 12:55:02 +01:00
<a href="https://github.com/lunarmodules/luasocket/blob/master/ltn012.md">LTN12</a> based.
2003-08-31 03:00:15 +02:00
</p>
<p class="parameters">
2004-06-17 01:21:49 +02:00
If the first argument of the <tt>request</tt> function is a string, it
should be an <tt>url</tt>. In that case, if a <tt>body</tt>
is provided as a string, the function will perform a <tt>POST</tt> method
in the <tt>url</tt>. Otherwise, it performs a <tt>GET</tt> in the
<tt>url</tt>
2004-06-17 01:21:49 +02:00
</p>
<p class="parameters">
If the first argument is instead a table, the most important fields are
2004-06-17 01:21:49 +02:00
the <tt>url</tt> and the <em>simple</em>
2024-03-15 12:55:02 +01:00
<a href="https://github.com/lunarmodules/luasocket/blob/master/ltn012.md">LTN12</a>
<tt>sink</tt> that will receive the downloaded content.
2004-06-16 06:28:21 +02:00
Any part of the <tt>url</tt> can be overridden by including
2004-06-16 01:00:56 +02:00
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>
<li><tt>headers</tt>: Any additional HTTP headers to send with the request;</li>
<li><tt>source</tt>: <em>simple</em>
2024-03-15 12:55:02 +01:00
<a href="https://github.com/lunarmodules/luasocket/blob/master/ltn012.md">LTN12</a>
2004-06-16 22:41:03 +02:00
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>
<li><tt>step</tt>:
2024-03-15 12:55:02 +01:00
<a href="https://github.com/lunarmodules/luasocket/blob/master/ltn012.md">LTN12</a>
pump step function used to move data.
Defaults to the LTN12 <tt>pump.step</tt> function.</li>
<li><tt>proxy</tt>: The URL of a proxy server to use. Defaults to no proxy;</li>
<li><tt>redirect</tt>: Set to <tt><b>false</b></tt> to prevent the
function from automatically following 301 or 302 server redirect messages;</li>
2005-10-05 05:16:17 +02:00
<li><tt>create</tt>: An optional function to be used instead of
<a href="tcp.html#socket.tcp"><tt>socket.tcp</tt></a> when the communications socket is created.</li>
<li><tt>maxredirects</tt>: An optional number specifying the maximum number of
redirects to follow. Defaults to <tt>5</tt> if not specified. A boolean
<tt>false</tt> value means no maximum (unlimited).</li>
2004-06-16 01:00:56 +02:00
</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 simple form returns the response
2004-06-17 01:21:49 +02:00
body as a string, followed by the response status code, the response
2006-04-03 06:45:42 +02:00
headers and the response status line. The generic function returns the same
2004-06-17 01:21:49 +02:00
information, except the first return value is just the number 1 (the body
goes to the <tt>sink</tt>).
2003-08-31 03:00:15 +02:00
</p>
<p class="return">
Even when the server fails to provide the contents of the requested URL (URL not found, for example),
2004-06-16 06:28:21 +02:00
it usually returns 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.ietf.org/rfc/rfc2616.txt">RFC 2616</a>.
2003-08-31 03:00:15 +02:00
</p>
<p class="description">
2004-06-17 01:21:49 +02:00
Here are a few examples with the simple interface:
</p>
<pre class="example">
2004-06-17 01:21:49 +02:00
-- load the http module
2005-11-20 09:56:19 +01:00
local io = require("io")
local http = require("socket.http")
local ltn12 = require("ltn12")
2004-06-17 01:21:49 +02:00
2005-11-20 09:56:19 +01:00
-- connect to server "www.cs.princeton.edu" and retrieves this manual
-- file from "~diego/professional/luasocket/http.html" and print it to stdout
http.request{
url = "http://www.cs.princeton.edu/~diego/professional/luasocket/http.html",
2005-11-20 09:56:19 +01:00
sink = ltn12.sink.file(io.stdout)
}
2004-06-17 01:21:49 +02:00
2004-06-21 00:19:54 +02:00
-- connect to server "www.example.com" and tries to retrieve
-- "/private/index.html". Fails because authentication is needed.
b, c, h = http.request("http://www.example.com/private/index.html")
-- b returns some useless page telling about the denied access,
2004-06-17 01:21:49 +02:00
-- 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.
r, e = http.request("http://wrong.host/")
-- r is nil, and e returns with value "host not found"
</pre>
<p class="description">
2004-06-17 01:21:49 +02:00
And here is an example using the generic interface:
</p>
<pre class="example">
2004-06-16 01:00:56 +02:00
-- load the http module
2005-02-08 11:01:01 +01:00
http = require("socket.http")
2004-06-16 01:00:56 +02:00
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-17 01:21:49 +02:00
r, c, h = http.request {
2003-08-31 03:00:15 +02:00
method = "HEAD",
url = "http://www.tecgraf.puc-rio.br/~diego"
}
2004-06-17 01:21:49 +02:00
-- r is 1, c is 200, and h would return the following headers:
-- h = {
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>
<p class="note" id="post">
2006-04-03 06:45:42 +02:00
Note: When sending a POST request, simple interface adds a
"<tt>Content-type: application/x-www-form-urlencoded</tt>"
header to the request. This is the type used by
HTML forms. If you need another type, use the generic
interface.
</p>
<p class="note" id="authentication">
2003-08-31 03:00:15 +02:00
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&nbsp;Authentication Required".
2003-08-31 03:00:15 +02:00
</p>
<p class="note">
2003-08-31 03:00:15 +02:00
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.ietf.org/rfc/rfc2068.txt">RFC 2068</a>.
2003-08-31 03:00:15 +02:00
</p>
<p class="note">The Basic Authentication Scheme sends
2003-08-31 03:00:15 +02:00
<tt>&lt;user&gt;</tt> and
<tt>&lt;password&gt;</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
2005-02-08 11:01:01 +01:00
http = require("socket.http")
2004-06-16 01:00:56 +02:00
mime = require("mime")
2004-06-21 00:19:54 +02:00
-- Connect to server "www.example.com" and tries to retrieve
-- "/private/index.html", using the provided name and password to
2003-08-31 03:00:15 +02:00
-- authenticate the request
2004-06-21 00:19:54 +02:00
b, c, h = http.request("http://fulano:silva@www.example.com/private/index.html")
2003-08-31 03:00:15 +02:00
-- Alternatively, one could fill the appropriate header and authenticate
-- the request directly.
2004-06-17 01:21:49 +02:00
r, c = http.request {
2004-06-21 00:19:54 +02:00
url = "http://www.example.com/private/index.html",
headers = { authorization = "Basic " .. (mime.b64("fulano:silva")) }
2003-08-31 03:00:15 +02:00
}
</pre>
<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<div class="footer">
2003-08-31 03:00:15 +02:00
<hr>
<center>
<p class="bar">
2009-05-27 11:31:38 +02:00
<a href="index.html">home</a> &middot;
<a href="index.html#download">download</a> &middot;
2004-12-13 07:26:01 +01:00
<a href="installation.html">installation</a> &middot;
2003-08-31 03:00:15 +02:00
<a href="introduction.html">introduction</a> &middot;
<a href="reference.html">reference</a>
2003-08-31 03:00:15 +02:00
</p>
<p>
<small>
Last modified by Eric Westbrook on <br>
Sat Feb 23 19:09:42 UTC 2019
2003-08-31 03:00:15 +02:00
</small>
</p>
</center>
</div>
</body>
</html>