| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 |
"http://www.w3.org/TR/html4/strict.dtd"> |
| 3 |
<html> |
| 4 |
|
| 5 |
<head> |
| 6 |
<meta name="description" content="LuaSocket: HTTP support"> |
| 7 |
<meta name="keywords" content="Lua, HTTP, Library, WWW, Browser, Network, Support"> |
| 8 |
<title>LuaSocket: HTTP support</title> |
| 9 |
<link rel="stylesheet" href="reference.css" type="text/css"> |
| 10 |
</head> |
| 11 |
|
| 12 |
<body> |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
<div class=header> |
| 17 |
<hr> |
| 18 |
<center> |
| 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"> |
| 22 |
</a></td></tr> |
| 23 |
<tr><td align=center valign=top>Network support for the Lua language |
| 24 |
</td></tr> |
| 25 |
</table> |
| 26 |
<p class=bar> |
| 27 |
<a href="index.html">home</a> · |
| 28 |
<a href="index.html#download">download</a> · |
| 29 |
<a href="introduction.html">introduction</a> · |
| 30 |
<a href="introduction.html">introduction</a> · |
| 31 |
<a href="reference.html">reference</a> |
| 32 |
</p> |
| 33 |
</center> |
| 34 |
<hr> |
| 35 |
</div> |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
<h2 id="http">HTTP</h2> |
| 40 |
|
| 41 |
<p> |
| 42 |
HTTP (Hyper Text Transfer Protocol) is the protocol used to exchange |
| 43 |
information between web-browsers and servers. The <tt>http</tt> |
| 44 |
namespace offers full support for the client side of the HTTP |
| 45 |
protocol (i.e., |
| 46 |
the facilities that would be used by a web-browser implementation). The |
| 47 |
implementation conforms to the HTTP/1.1 standard, |
| 48 |
<a href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a>. |
| 49 |
</p> |
| 50 |
|
| 51 |
<p> |
| 52 |
The module exports functions that provide HTTP functionality in different |
| 53 |
levels of abstraction. From the simple |
| 54 |
string oriented requests, through generic |
| 55 |
<a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> based, down to even lower-level if you bother to look through the source code. |
| 56 |
</p> |
| 57 |
|
| 58 |
<p> |
| 59 |
To obtain the <tt>http</tt> namespace, run: |
| 60 |
</p> |
| 61 |
|
| 62 |
<pre class=example> |
| 63 |
-- loads the HTTP module and any libraries it requires |
| 64 |
local http = require("socket.http") |
| 65 |
</pre> |
| 66 |
|
| 67 |
<p> |
| 68 |
URLs must conform to |
| 69 |
<a href="http://www.ietf.org/rfc/rfc1738.txt">RFC 1738</a>, |
| 70 |
that is, an URL is a string in the form: |
| 71 |
</p> |
| 72 |
|
| 73 |
<blockquote> |
| 74 |
<pre> |
| 75 |
[http://][<user>[:<password>]@]<host>[:<port>][/<path>] |
| 76 |
</pre> |
| 77 |
</blockquote> |
| 78 |
|
| 79 |
<p> |
| 80 |
MIME headers are represented as a Lua table in the form: |
| 81 |
</p> |
| 82 |
|
| 83 |
<blockquote> |
| 84 |
<table summary="MIME headers in Lua table"> |
| 85 |
<tr><td><tt> |
| 86 |
headers = {<br> |
| 87 |
field-1-name = <i>field-1-value</i>,<br> |
| 88 |
field-2-name = <i>field-2-value</i>,<br> |
| 89 |
field-3-name = <i>field-3-value</i>,<br> |
| 90 |
...<br> |
| 91 |
field-n-name = <i>field-n-value</i><br> |
| 92 |
} |
| 93 |
</tt></td></tr> |
| 94 |
</table> |
| 95 |
</blockquote> |
| 96 |
|
| 97 |
<p> |
| 98 |
Field names are case insensitive (as specified by the standard) and all |
| 99 |
functions work with lowercase field names (but see |
| 100 |
<a href=socket.html#headers.canonic><tt>socket.headers.canonic</tt></a>). |
| 101 |
Field values are left unmodified. |
| 102 |
</p> |
| 103 |
|
| 104 |
<p class=note> |
| 105 |
Note: MIME headers are independent of order. Therefore, there is no problem |
| 106 |
in representing them in a Lua table. |
| 107 |
</p> |
| 108 |
|
| 109 |
<p> |
| 110 |
The following constants can be set to control the default behavior of |
| 111 |
the HTTP module: |
| 112 |
</p> |
| 113 |
|
| 114 |
<ul> |
| 115 |
<li> <tt>PROXY</tt>: default proxy used for connections; |
| 116 |
<li> <tt>TIMEOUT</tt>: sets the timeout for all I/O operations; |
| 117 |
<li> <tt>USERAGENT</tt>: default user agent reported to server. |
| 118 |
</ul> |
| 119 |
|
| 120 |
<p class=note id="post"> |
| 121 |
Note: These constants are global. Changing them will also |
| 122 |
change the behavior other code that might be using LuaSocket. |
| 123 |
</p> |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
<p class=name id="request"> |
| 128 |
http.<b>request(</b>url [, body]<b>)</b><br> |
| 129 |
http.<b>request{</b><br> |
| 130 |
url = <i>string</i>,<br> |
| 131 |
[sink = <i>LTN12 sink</i>,]<br> |
| 132 |
[method = <i>string</i>,]<br> |
| 133 |
[headers = <i>header-table</i>,]<br> |
| 134 |
[source = <i>LTN12 source</i>],<br> |
| 135 |
[step = <i>LTN12 pump step</i>,]<br> |
| 136 |
[proxy = <i>string</i>,]<br> |
| 137 |
[redirect = <i>boolean</i>,]<br> |
| 138 |
[create = <i>function</i>]<br> |
| 139 |
<b>}</b> |
| 140 |
</p> |
| 141 |
|
| 142 |
<p class=description> |
| 143 |
The request function has two forms. The simple form downloads |
| 144 |
a URL using the <tt>GET</tt> or <tt>POST</tt> method and is based |
| 145 |
on strings. The generic form performs any HTTP method and is |
| 146 |
<a href=http://lua-users.org/wiki/FiltersSourcesAndSinks>LTN12</a> based. |
| 147 |
</p> |
| 148 |
|
| 149 |
<p class=parameters> |
| 150 |
If the first argument of the <tt>request</tt> function is a string, it |
| 151 |
should be an <tt>url</tt>. In that case, if a <tt>body</tt> |
| 152 |
is provided as a string, the function will perform a <tt>POST</tt> method |
| 153 |
in the <tt>url</tt>. Otherwise, it performs a <tt>GET</tt> in the |
| 154 |
<tt>url</tt> |
| 155 |
</p> |
| 156 |
|
| 157 |
<p class=parameters> |
| 158 |
If the first argument is instead a table, the most important fields are |
| 159 |
the <tt>url</tt> and the <em>simple</em> |
| 160 |
<a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> |
| 161 |
<tt>sink</tt> that will receive the downloaded content. |
| 162 |
Any part of the <tt>url</tt> can be overridden by including |
| 163 |
the appropriate field in the request table. |
| 164 |
If authentication information is provided, the function |
| 165 |
uses the Basic Authentication Scheme (see <a href="#authentication">note</a>) |
| 166 |
to retrieve the document. If <tt>sink</tt> is <tt><b>nil</b></tt>, the |
| 167 |
function discards the downloaded data. The optional parameters are the |
| 168 |
following: |
| 169 |
</p> |
| 170 |
<ul> |
| 171 |
<li><tt>method</tt>: The HTTP request method. Defaults to "GET"; |
| 172 |
<li><tt>headers</tt>: Any additional HTTP headers to send with the request; |
| 173 |
<li><tt>source</tt>: <em>simple</em> |
| 174 |
<a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> |
| 175 |
source to provide the request body. If there |
| 176 |
is a body, you need to provide an appropriate "<tt>content-length</tt>" |
| 177 |
request header field, or the function will attempt to send the body as |
| 178 |
"<tt>chunked</tt>" (something few servers support). Defaults to the empty source; |
| 179 |
<li><tt>step</tt>: |
| 180 |
<a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> |
| 181 |
pump step function used to move data. |
| 182 |
Defaults to the LTN12 <tt>pump.step</tt> function. |
| 183 |
<li><tt>proxy</tt>: The URL of a proxy server to use. Defaults to no proxy; |
| 184 |
<li><tt>redirect</tt>: Set to <tt><b>false</b></tt> to prevent the |
| 185 |
function from automatically following 301 or 302 server redirect messages; |
| 186 |
<li><tt>create</tt>: An optional function to be used instead of |
| 187 |
<a href=tcp.html#socket.tcp><tt>socket.tcp</tt></a> when the communications socket is created. |
| 188 |
</ul> |
| 189 |
|
| 190 |
<p class=return> |
| 191 |
In case of failure, the function returns <tt><b>nil</b></tt> followed by an |
| 192 |
error message. If successful, the simple form returns the response |
| 193 |
body as a string, followed by the response status code, the response |
| 194 |
headers and the response status line. The generic function returns the same |
| 195 |
information, except the first return value is just the number 1 (the body |
| 196 |
goes to the <tt>sink</tt>). |
| 197 |
</p> |
| 198 |
|
| 199 |
<p class=return> |
| 200 |
Even when the server fails to provide the contents of the requested URL (URL not found, for example), |
| 201 |
it usually returns a message body (a web page informing the |
| 202 |
URL was not found or some other useless page). To make sure the |
| 203 |
operation was successful, check the returned status <tt>code</tt>. For |
| 204 |
a list of the possible values and their meanings, refer to <a |
| 205 |
href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a>. |
| 206 |
</p> |
| 207 |
|
| 208 |
<p class=description> |
| 209 |
Here are a few examples with the simple interface: |
| 210 |
</p> |
| 211 |
|
| 212 |
<pre class=example> |
| 213 |
-- load the http module |
| 214 |
local io = require("io") |
| 215 |
local http = require("socket.http") |
| 216 |
local ltn12 = require("ltn12") |
| 217 |
|
| 218 |
-- connect to server "www.cs.princeton.edu" and retrieves this manual |
| 219 |
-- file from "~diego/professional/luasocket/http.html" and print it to stdout |
| 220 |
http.request{ |
| 221 |
url = "http://www.cs.princeton.edu/~diego/professional/luasocket/http.html", |
| 222 |
sink = ltn12.sink.file(io.stdout) |
| 223 |
} |
| 224 |
|
| 225 |
-- connect to server "www.example.com" and tries to retrieve |
| 226 |
-- "/private/index.html". Fails because authentication is needed. |
| 227 |
b, c, h = http.request("http://www.example.com/private/index.html") |
| 228 |
-- b returns some useless page telling about the denied access, |
| 229 |
-- h returns authentication information |
| 230 |
-- and c returns with value 401 (Authentication Required) |
| 231 |
|
| 232 |
-- tries to connect to server "wrong.host" to retrieve "/" |
| 233 |
-- and fails because the host does not exist. |
| 234 |
r, e = http.request("http://wrong.host/") |
| 235 |
-- r is nil, and e returns with value "host not found" |
| 236 |
</pre> |
| 237 |
|
| 238 |
<p class=description> |
| 239 |
And here is an example using the generic interface: |
| 240 |
</p> |
| 241 |
|
| 242 |
<pre class=example> |
| 243 |
-- load the http module |
| 244 |
http = require("socket.http") |
| 245 |
|
| 246 |
-- Requests information about a document, without downloading it. |
| 247 |
-- Useful, for example, if you want to display a download gauge and need |
| 248 |
-- to know the size of the document in advance |
| 249 |
r, c, h = http.request { |
| 250 |
method = "HEAD", |
| 251 |
url = "http://www.tecgraf.puc-rio.br/~diego" |
| 252 |
} |
| 253 |
-- r is 1, c is 200, and h would return the following headers: |
| 254 |
-- h = { |
| 255 |
-- date = "Tue, 18 Sep 2001 20:42:21 GMT", |
| 256 |
-- server = "Apache/1.3.12 (Unix) (Red Hat/Linux)", |
| 257 |
-- ["last-modified"] = "Wed, 05 Sep 2001 06:11:20 GMT", |
| 258 |
-- ["content-length"] = 15652, |
| 259 |
-- ["connection"] = "close", |
| 260 |
-- ["content-Type"] = "text/html" |
| 261 |
-- } |
| 262 |
</pre> |
| 263 |
|
| 264 |
<p class=note id="post"> |
| 265 |
Note: When sending a POST request, simple interface adds a |
| 266 |
"<tt>Content-type: application/x-www-form-urlencoded</tt>" |
| 267 |
header to the request. This is the type used by |
| 268 |
HTML forms. If you need another type, use the generic |
| 269 |
interface. |
| 270 |
</p> |
| 271 |
|
| 272 |
<p class=note id="authentication"> |
| 273 |
Note: Some URLs are protected by their |
| 274 |
servers from anonymous download. For those URLs, the server must receive |
| 275 |
some sort of authentication along with the request or it will deny |
| 276 |
download and return status "401 Authentication Required". |
| 277 |
</p> |
| 278 |
|
| 279 |
<p class=note> |
| 280 |
The HTTP/1.1 standard defines two authentication methods: the Basic |
| 281 |
Authentication Scheme and the Digest Authentication Scheme, both |
| 282 |
explained in detail in |
| 283 |
<a href="http://www.ietf.org/rfc/rfc2068.txt">RFC 2068</a>. |
| 284 |
</p> |
| 285 |
|
| 286 |
<p class=note>The Basic Authentication Scheme sends |
| 287 |
<tt><user></tt> and |
| 288 |
<tt><password></tt> unencrypted to the server and is therefore |
| 289 |
considered unsafe. Unfortunately, by the time of this implementation, |
| 290 |
the wide majority of servers and browsers support the Basic Scheme only. |
| 291 |
Therefore, this is the method used by the toolkit whenever |
| 292 |
authentication is required. |
| 293 |
</p> |
| 294 |
|
| 295 |
<pre class=example> |
| 296 |
-- load required modules |
| 297 |
http = require("socket.http") |
| 298 |
mime = require("mime") |
| 299 |
|
| 300 |
-- Connect to server "www.example.com" and tries to retrieve |
| 301 |
-- "/private/index.html", using the provided name and password to |
| 302 |
-- authenticate the request |
| 303 |
b, c, h = http.request("http://fulano:[email protected]/private/index.html") |
| 304 |
|
| 305 |
-- Alternatively, one could fill the appropriate header and authenticate |
| 306 |
-- the request directly. |
| 307 |
r, c = http.request { |
| 308 |
url = "http://www.example.com/private/index.html", |
| 309 |
headers = { authorization = "Basic " .. (mime.b64("fulano:silva")) } |
| 310 |
} |
| 311 |
</pre> |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
<div class=footer> |
| 316 |
<hr> |
| 317 |
<center> |
| 318 |
<p class=bar> |
| 319 |
<a href="index.html">home</a> · |
| 320 |
<a href="index.html#download">download</a> · |
| 321 |
<a href="installation.html">installation</a> · |
| 322 |
<a href="introduction.html">introduction</a> · |
| 323 |
<a href="reference.html">reference</a> |
| 324 |
</p> |
| 325 |
<p> |
| 326 |
<small> |
| 327 |
Last modified by Diego Nehab on <br> |
| 328 |
Thu Apr 20 00:25:26 EDT 2006 |
| 329 |
</small> |
| 330 |
</p> |
| 331 |
</center> |
| 332 |
</div> |
| 333 |
|
| 334 |
</body> |
| 335 |
</html> |