aboutsummaryrefslogtreecommitdiff
path: root/doc/http.html
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2003-08-31 01:00:15 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2003-08-31 01:00:15 +0000
commit982781f1464c9b7a8133130433f83dbf1f59a2c0 (patch)
tree8f96f9e9fa1e6bef8b8356037986ddc18673cade /doc/http.html
parent6789b83ff5c15296267f880d3b98cf8a1800c30a (diff)
downloadluasocket-982781f1464c9b7a8133130433f83dbf1f59a2c0.tar.gz
luasocket-982781f1464c9b7a8133130433f83dbf1f59a2c0.tar.bz2
luasocket-982781f1464c9b7a8133130433f83dbf1f59a2c0.zip
LuaSocket 2.0 User's Manual.
Diffstat (limited to 'doc/http.html')
-rw-r--r--doc/http.html388
1 files changed, 388 insertions, 0 deletions
diff --git a/doc/http.html b/doc/http.html
new file mode 100644
index 0000000..b7469a4
--- /dev/null
+++ b/doc/http.html
@@ -0,0 +1,388 @@
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<title>LuaSocket: Network support for the Lua language</title>
7<link rel="stylesheet" href="reference.css" type="text/css">
8</head>
9
10<body>
11
12<!-- header ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
13
14<div class=header>
15<hr>
16<center>
17<table summary="LuaSocket logo">
18<tr><td align=center><a href="http://www.lua.org">
19<img border=0 alt="LuaSocket" src="luasocket.png">
20</a></td></tr>
21<tr><td align=center valign=top>Network support for the Lua language
22</td></tr>
23</table>
24<p class=bar>
25<a href="home.html">home</a> &middot;
26<a href="home.html#download">download</a> &middot;
27<a href="introduction.html">introduction</a> &middot;
28<a href="reference.html">reference</a>
29</p>
30</center>
31<hr>
32</div>
33
34<!-- http +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
35
36<h2 id=http>HTTP</h2>
37
38<p>
39HTTP (Hyper Text Transfer Protocol) is the protocol used to exchange
40information between web-browsers and servers. The <tt>http.lua</tt>
41module offers support for the client side of the HTTP protocol (i.e.,
42the facilities that would be used by a web-browser implementation). The
43implementation conforms to the HTTP/1.1 standard,
44<a href="http://www.cs.princeton.edu/~diego/rfc/rfc2616.txt">RFC
452616</a>.
46</p>
47
48<p>
49The module exports functions that provide HTTP functionality in different
50levels of abstraction, from a simple <a
51href="#get"><tt>get</tt></a>, to the generic, stream oriented
52<a href="#request_cb"> <tt>request_cb</tt></a>.
53</p>
54
55<p>
56URLs must conform to
57<a href="http://www.cs.princeton.edu/~diego/rfc/rfc1738.txt">RFC
581738</a>,
59that is, an URL is a string in the form:
60</p>
61
62<blockquote>
63<pre>
64[http://][&lt;user&gt;[:&lt;password&gt;]@]&lt;host&gt;[:&lt;port&gt;][/&lt;path&gt;]
65</pre>
66</blockquote>
67
68<p>
69MIME headers are represented as a Lua table in the form:
70</p>
71
72<blockquote>
73<table summary="MIME headers in Lua table">
74<tr><td><tt>
75headers = {<br>
76&nbsp;&nbsp;field-1-name = <i>field-1-value</i>,<br>
77&nbsp;&nbsp;field-2-name = <i>field-2-value</i>,<br>
78&nbsp;&nbsp;field-3-name = <i>field-3-value</i>,
79</tt></td></tr>
80<tr><td align=center><tt>
81&nbsp;&nbsp;...
82</tt></td></tr>
83<tr><td><tt>
84&nbsp;&nbsp;field-n-name = <i>field-n-value</i><br>
85}
86</tt></td></tr>
87</table>
88</blockquote>
89
90<p>
91Field names are case insensitive (as specified by the standard) and all
92functions work with lowercase field names.
93Field values are left unmodified.
94</p>
95
96<p class=note>
97Note: MIME headers are independent of order. Therefore, there is no problem
98in representing them in a Lua table.
99</p>
100
101<!-- http.get +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
102
103<p class=name id=get>
104socket.http.<b>get(</b>url<b>)</b><br>
105socket.http.<b>get{</b><br>
106&nbsp;&nbsp;url = <i>string</i>,<br>
107&nbsp;&nbsp;headers = <i>header-table</i>,<br>
108&nbsp;&nbsp;user = <i>string</i>,<br>
109&nbsp;&nbsp;password = <i>string</i>,<br>
110&nbsp;&nbsp;stay = <i>bool</i>,<br>
111<b>}</b>
112</p>
113
114<p class=description>
115Performs the HTTP method <tt>GET</tt>.
116</p>
117
118<p class=parameters>
119The function can be
120called either directly with a <tt>url</tt> or with a <em>request table</em>.
121The use of a request table allows complete control over the components of
122the request. Values passed explicitly as fields of the request table
123override those given by the <tt>url</tt>. For a description of the fields,
124see the <a href=#request><tt>request</tt></a> function.
125</p>
126
127<p class=return>
128The function returns the response message body, the mime headers, the
129status code and an error message (if any). In case of failure, the
130function returns all information it managed to gather.
131</p>
132
133<p class=note>
134Note: The function is trivially implemented with the use of the
135<a href="#request"><tt>request</tt></a> function.
136</p>
137
138<pre class=example>
139-- connect to server "www.tecgraf.puc-rio.br" and retrieves this manual
140-- file from "/luasocket/http.html"
141b, h, c, e = socket.http.get("http://www.tecgraf.puc-rio.br/luasocket/http.html")
142
143-- connect to server "www.tecgraf.puc-rio.br" and tries to retrieve
144-- "~diego/auth/index.html". Fails because authentication is needed.
145b, h, c, e = socket.http.get("http://www.tecgraf.puc-rio.br/~diego/auth/index.html")
146-- b returns some useless page telling about the denied access,
147-- h returns authentication information
148-- and c returns with value 401 (Authentication Required)
149
150-- tries to connect to server "wrong.host" to retrieve "/"
151-- and fails because the host does not exist.
152b, h, c, e = socket.http.get("http://wrong.host/")
153-- b, h, c are nil, and e returns with value "host not found"
154</pre>
155
156<!-- http.post ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
157
158<p class=name id=post>
159socket.http.<b>post(</b>url, body<b>)</b><br>
160socket.http.<b>post{</b><br>
161&nbsp;&nbsp; url = <i>string</i>,<br>
162&nbsp;&nbsp; headers = <i>header-table</i>,<br>
163&nbsp;&nbsp; body = <i>string</i>,<br>
164&nbsp;&nbsp; user = <i>string</i>,<br>
165&nbsp;&nbsp; password = <i>string</i>,<br>
166&nbsp;&nbsp; stay = <i>bool</i>,<br>
167<b>}</b>
168</p>
169
170<p class=description>
171Same as <a href="#get"><tt>get</tt></a>, except
172that the <tt>POST</tt> method is used and the request
173message <tt>body</tt> is sent along with the request.
174</p>
175
176<p class=note>
177Note: This function is also trivially implemented with the use of the
178<a href="#request"><tt>request</tt></a> function.
179</p>
180
181<!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
182
183<p class=name id=request>
184socket.http.<b>request{</b><br>
185&nbsp;&nbsp;method = <i>string</i>,<br>
186&nbsp;&nbsp;url = <i>string</i>,<br>
187&nbsp;&nbsp;headers = <i>header-table</i>,<br>
188&nbsp;&nbsp;body = <i>string</i>,<br>
189&nbsp;&nbsp;user = <i>string</i>,<br>
190&nbsp;&nbsp;password = <i>string</i>,<br>
191&nbsp;&nbsp;stay = <i>string</i>,<br>
192<b>}</b>
193</p>
194
195<p class=description>
196Performs the generic HTTP request using.
197</p>
198
199<p class=parameters>
200The request uses <tt>method</tt> on <tt>url</tt>
201sending the request <tt>headers</tt> and request <tt>body</tt> in the
202request message. If authentication information is provided, the function
203uses the Basic Authentication Scheme (see <a href="#authentication">note</a>)
204to retrieve the document. <tt>User</tt> and <tt>password</tt> provided
205explicitly override those given by the <tt>url</tt>. The <tt>stay</tt>
206parameter, when set to anything but <tt>nil</tt>, prevents the function
207from automatically following 301 or 302 server redirect messages.
208</p>
209
210<p class=return>
211The function returns a table with all components of the response message
212it managed to retrieve. The response table has the following form:
213</p>
214
215<blockquote><tt>
216response = {<br>
217&nbsp;&nbsp;body = <i>string</i>,<br>
218&nbsp;&nbsp;headers = <i>header-table</i>,<br>
219&nbsp;&nbsp;status = <i>string</i>,<br>
220&nbsp;&nbsp;code = <i>number</i>,<br>
221&nbsp;&nbsp;error = <i>string</i><br>
222}
223</tt></blockquote>
224
225<p class=return>
226Even when there was failure (URL not found, for example), the
227function may succeed retrieving a message body (a web page informing the
228URL was not found or some other useless page). To make sure the
229operation was successful, check the returned status <tt>code</tt>. For
230a list of the possible values and their meanings, refer to <a
231href="http://www.cs.princeton.edu/~diego/rfc/rfc2616.txt">RFC
2322616</a>.
233</p>
234
235<pre class=example>
236-- Requests information about a document, without downloading it.
237-- Useful, for example, if you want to display a download gauge and need
238-- to know the size of the document in advance
239response = socket.http.request {
240 method = "HEAD",
241 url = "http://www.tecgraf.puc-rio.br/~diego"
242}
243-- Would return the following headers:
244-- response.headers = {
245-- date = "Tue, 18 Sep 2001 20:42:21 GMT",
246-- server = "Apache/1.3.12 (Unix) (Red Hat/Linux)",
247-- ["last-modified"] = "Wed, 05 Sep 2001 06:11:20 GMT",
248-- ["content-length"] = 15652,
249-- ["connection"] = "close",
250-- ["content-Type"] = "text/html"
251-- }
252</pre>
253</blockquote>
254
255<p class=note id=authentication>
256Note: Some URLs are protected by their
257servers from anonymous download. For those URLs, the server must receive
258some sort of authentication along with the request or it will deny
259download and return status "401&nbsp;Authentication Required".
260</p>
261
262<p class=note>
263The HTTP/1.1 standard defines two authentication methods: the Basic
264Authentication Scheme and the Digest Authentication Scheme, both
265explained in detail in
266<a href="http://www.cs.princeton.edu/~diego/rfc/rfc2068.txt">RFC 2068</a>.
267</p>
268
269<p class=note>The Basic Authentication Scheme sends
270<tt>&lt;user&gt;</tt> and
271<tt>&lt;password&gt;</tt> unencrypted to the server and is therefore
272considered unsafe. Unfortunately, by the time of this implementation,
273the wide majority of servers and browsers support the Basic Scheme only.
274Therefore, this is the method used by the toolkit whenever
275authentication is required.
276</p>
277
278<pre class=example>
279-- Connect to server "www.tecgraf.puc-rio.br" and tries to retrieve
280-- "~diego/auth/index.html", using the provided name and password to
281-- authenticate the request
282response = socket.http.request{
283 url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html",
284 user = "diego",
285 password = "password"
286}
287
288-- Alternatively, one could fill the appropriate header and authenticate
289-- the request directly.
290headers = {
291 authentication = "Basic " .. socket.code.base64("diego:password")
292}
293response = socket.http.request {
294 url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html",
295 headers = headers
296}
297</pre>
298
299<!-- request_cb +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
300
301<p class=name id=request_cb>
302socket.http.<b>request_cb(</b>request, response<b>)</b>
303</p>
304
305<p class=description>
306Performs the generic HTTP request.
307</p>
308
309<p class=parameters>
310The function receives two tables as parameters. The <tt>request</tt> table
311provides information about the request:
312</p>
313
314<blockquote><tt>
315request = {<br>
316&nbsp;&nbsp;method = <i>string</i>,<br>
317&nbsp;&nbsp;url = <i>string</i>,<br>
318&nbsp;&nbsp;headers = <i>header-table</i>,<br>
319&nbsp;&nbsp;body_cb = <i>send-callback</i>,<br>
320&nbsp;&nbsp;user = <i>string</i>,<br>
321&nbsp;&nbsp;password = <i>string</i>,<br>
322&nbsp;&nbsp;stay = <i>string</i>,<br>
323}</tt>
324</blockquote>
325
326<p class=parameters>
327The function uses the HTTP method specified in
328<tt>request.method</tt> on the URL <tt>request.url</tt>,
329sending <tt>request.headers</tt> along with the request. The request
330message body is sent via the send callback <tt>request.body_cb</tt>.
331If authentication information is provided, the function uses the Basic
332Authentication Scheme (see <a href="#authentication">note</a>) to
333retrieve the document. <tt>Request.user</tt> and
334<tt>request.password</tt> override those given by the
335<tt>request.url</tt>. The <tt>request.stay</tt> parameter, when set to
336anything but <tt>nil</tt>, prevents the function from automatically
337following 301 or 302 server redirect messages.
338</p>
339
340<p class=parameters>
341The <tt>response</tt> table specifies information about the desired
342response:
343</p>
344
345<blockquote><tt>
346response = {<br>
347&nbsp;&nbsp;body_cb = <i>receive-callback</i><br>
348}</tt>
349</blockquote>
350
351<p class=return>
352The function returns the same response table as that returned by the
353<tt>socket.http.request</tt> function, except the response message body is
354returned to the receive callback given by the
355<tt>response.body_cb</tt> field.
356</p>
357
358<p class=note>
359Note: For more information on callbacks, please refer to
360<a href="stream.html#stream">Streaming with callbacks</a>.
361</p>
362
363<p class=note>
364Note: Method names are case <em>sensitive</em>
365</p>
366
367<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
368
369<div class=footer>
370<hr>
371<center>
372<p class=bar>
373<a href="home.html">home</a> &middot;
374<a href="home.html#download">download</a> &middot;
375<a href="introduction.html">introduction</a> &middot;
376<a href="reference.html">reference</a>
377</p>
378<p>
379<small>
380Last modified by Diego Nehab on <br>
381Sat Aug 9 01:00:41 PDT 2003
382</small>
383</p>
384</center>
385</div>
386
387</body>
388</html>