1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
<!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
levels of abstraction, from a simple <a
href="#get"><tt>get</tt></a>, to the generic, stream oriented
<a href="#request_cb"><tt>request_cb</tt></a>.
</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>
<!-- http.get +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id=get>
socket.http.<b>get(</b>url<b>)</b><br>
socket.http.<b>get{</b><br>
url = <i>string</i>,<br>
headers = <i>header-table</i>,<br>
user = <i>string</i>,<br>
password = <i>string</i>,<br>
stay = <i>bool</i>,<br>
<b>}</b>
</p>
<p class=description>
Performs the HTTP method <tt>GET</tt>.
</p>
<p class=parameters>
The function can be
called either directly with a <tt>url</tt> or with a <em>request table</em>.
The use of a request table allows complete control over the components of
the request. Values passed explicitly as fields of the request table
override those given by the <tt>url</tt>. For a description of the fields,
see the <a href=#request><tt>request</tt></a> function.
</p>
<p class=return>
The function returns the response message body, the mime headers, the
status code and an error message (if any). In case of failure, the
function returns all information it managed to gather.
</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>
-- connect to server "www.tecgraf.puc-rio.br" and retrieves this manual
-- file from "/luasocket/http.html"
b, h, c, e = socket.http.get("http://www.tecgraf.puc-rio.br/luasocket/http.html")
-- connect to server "www.tecgraf.puc-rio.br" and tries to retrieve
-- "~diego/auth/index.html". Fails because authentication is needed.
b, h, c, e = socket.http.get("http://www.tecgraf.puc-rio.br/~diego/auth/index.html")
-- 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.
b, h, c, e = socket.http.get("http://wrong.host/")
-- b, h, c are nil, and e returns with value "host not found"
</pre>
<!-- http.post ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id=post>
socket.http.<b>post(</b>url, body<b>)</b><br>
socket.http.<b>post{</b><br>
url = <i>string</i>,<br>
headers = <i>header-table</i>,<br>
body = <i>string</i>,<br>
user = <i>string</i>,<br>
password = <i>string</i>,<br>
stay = <i>bool</i>,<br>
<b>}</b>
</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>
socket.http.<b>request{</b><br>
method = <i>string</i>,<br>
url = <i>string</i>,<br>
headers = <i>header-table</i>,<br>
body = <i>string</i>,<br>
user = <i>string</i>,<br>
password = <i>string</i>,<br>
stay = <i>string</i>,<br>
<b>}</b>
</p>
<p class=description>
Performs the generic HTTP request using.
</p>
<p class=parameters>
The request uses <tt>method</tt> on <tt>url</tt>
sending the request <tt>headers</tt> and request <tt>body</tt> in the
request message. If authentication information is provided, the function
uses the Basic Authentication Scheme (see <a href="#authentication">note</a>)
to retrieve the document. <tt>User</tt> and <tt>password</tt> provided
explicitly override those given by the <tt>url</tt>. The <tt>stay</tt>
parameter, when set to anything but <b><tt>nil</tt></b>, prevents the function
from automatically following 301 or 302 server redirect messages.
</p>
<p class=return>
The function returns a table with all components of the response message
it managed to retrieve. The response table has the following form:
</p>
<blockquote><tt>
response = {<br>
body = <i>string</i>,<br>
headers = <i>header-table</i>,<br>
status = <i>string</i>,<br>
code = <i>number</i>,<br>
error = <i>string</i><br>
}
</tt></blockquote>
<p class=return>
Even when there was failure (URL not found, for example), the
function may succeed retrieving a message body (a web page informing the
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>
-- 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
response = socket.http.request {
method = "HEAD",
url = "http://www.tecgraf.puc-rio.br/~diego"
}
-- Would return the following headers:
-- response.headers = {
-- 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>
-- 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
response = socket.http.request{
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.
headers = {
authentication = "Basic " .. socket.code.base64("diego:password")
}
response = socket.http.request {
url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html",
headers = headers
}
</pre>
<!-- request_cb +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id=request_cb>
socket.http.<b>request_cb(</b>request, response<b>)</b>
</p>
<p class=description>
Performs the generic HTTP request.
</p>
<p class=parameters>
The function receives two tables as parameters. The <tt>request</tt> table
provides information about the request:
</p>
<blockquote><tt>
request = {<br>
method = <i>string</i>,<br>
url = <i>string</i>,<br>
headers = <i>header-table</i>,<br>
body_cb = <i>send-callback</i>,<br>
user = <i>string</i>,<br>
password = <i>string</i>,<br>
stay = <i>string</i>,<br>
}</tt>
</blockquote>
<p class=parameters>
The function uses the HTTP method specified in
<tt>request.method</tt> on the URL <tt>request.url</tt>,
sending <tt>request.headers</tt> along with the request. The request
message body is sent via the send callback <tt>request.body_cb</tt>.
If authentication information is provided, the function uses the Basic
Authentication Scheme (see <a href="#authentication">note</a>) to
retrieve the document. <tt>Request.user</tt> and
<tt>request.password</tt> override those given by the
<tt>request.url</tt>. The <tt>request.stay</tt> parameter, when set to
anything but <b><tt>nil</tt></b>, prevents the function from automatically
following 301 or 302 server redirect messages.
</p>
<p class=parameters>
The <tt>response</tt> table specifies information about the desired
response:
</p>
<blockquote><tt>
response = {<br>
body_cb = <i>receive-callback</i><br>
}</tt>
</blockquote>
<p class=return>
The function returns the same response table as that returned by the
<tt>socket.http.request</tt> function, except the response message body is
returned to the receive callback given by the
<tt>response.body_cb</tt> field.
</p>
<p class=note>
Note: For more information on callbacks, please refer to
<a href="stream.html#stream">Streaming with callbacks</a>.
</p>
<p class=note>
Note: Method names are case <em>sensitive</em>
</p>
<!-- 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>
|