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