aboutsummaryrefslogtreecommitdiff
path: root/doc/http.html
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-15 23:00:56 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-15 23:00:56 +0000
commit843a431ef98fd541d98fd3898463985d9bfcde28 (patch)
tree2de4cd78a58ee17aa029528ede8ff5989b819019 /doc/http.html
parentcb03a0e95429cc0d498e77fd50241c87fd2bf3ea (diff)
downloadluasocket-843a431ef98fd541d98fd3898463985d9bfcde28.tar.gz
luasocket-843a431ef98fd541d98fd3898463985d9bfcde28.tar.bz2
luasocket-843a431ef98fd541d98fd3898463985d9bfcde28.zip
Almost done with manual...
Diffstat (limited to 'doc/http.html')
-rw-r--r--doc/http.html203
1 files changed, 74 insertions, 129 deletions
diff --git a/doc/http.html b/doc/http.html
index 16595f7..39eb2e4 100644
--- a/doc/http.html
+++ b/doc/http.html
@@ -98,36 +98,36 @@ Note: MIME headers are independent of order. Therefore, there is no problem
98in representing them in a Lua table. 98in representing them in a Lua table.
99</p> 99</p>
100 100
101<p>
102The following constants can be set to control the default behaviour of
103the HTTP module:
104</p>
105
106<ul>
107<li> <tt>PORT</tt>: default port used for connections;
108<li> <tt>PROXY</tt>: default proxy used for connections;
109<li> <tt>TIMEOUT</tt>: sets the timeout for all I/O operations;
110<li> <tt>USERAGENT</tt>: default user agent reported to server.
111</ul>
112
101<!-- http.get +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 113<!-- http.get +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
102 114
103<p class=name id=get> 115<p class=name id=get>
104socket.http.<b>get(</b>url<b>)</b><br> 116http.<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> 117</p>
113 118
114<p class=description> 119<p class=description>
115Performs the HTTP method <tt>GET</tt>. 120Performs the HTTP method <tt>GET</tt>.
116</p> 121</p>
117 122
118<p class=parameters> 123<p class=parameters>
119The function can be 124<tt>Url</tt> identifies the entity to retrieve.
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> 125</p>
126 126
127<p class=return> 127<p class=return>
128The function returns the response message body, the mime headers, the 128If successful, the function returns the response message body, the mime
129status code and an error message (if any). In case of failure, the 129headers, and the status code. In case of failure, the
130function returns all information it managed to gather. 130function returns <tt><b>nil</b></tt> followed by an error message.
131</p> 131</p>
132 132
133<p class=note> 133<p class=note>
@@ -136,35 +136,30 @@ Note: The function is trivially implemented with the use of the
136</p> 136</p>
137 137
138<pre class=example> 138<pre class=example>
139-- load the http module
140http = require("http")
141
139-- connect to server "www.tecgraf.puc-rio.br" and retrieves this manual 142-- connect to server "www.tecgraf.puc-rio.br" and retrieves this manual
140-- file from "/luasocket/http.html" 143-- file from "/luasocket/http.html"
141b, h, c, e = socket.http.get("http://www.tecgraf.puc-rio.br/luasocket/http.html") 144b, h, c = http.get("http://www.tecgraf.puc-rio.br/luasocket/http.html")
142 145
143-- connect to server "www.tecgraf.puc-rio.br" and tries to retrieve 146-- connect to server "www.tecgraf.puc-rio.br" and tries to retrieve
144-- "~diego/auth/index.html". Fails because authentication is needed. 147-- "~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") 148b, h, c = http.get("http://www.tecgraf.puc-rio.br/~diego/auth/index.html")
146-- b returns some useless page telling about the denied access, 149-- b returns some useless page telling about the denied access,
147-- h returns authentication information 150-- h returns authentication information
148-- and c returns with value 401 (Authentication Required) 151-- and c returns with value 401 (Authentication Required)
149 152
150-- tries to connect to server "wrong.host" to retrieve "/" 153-- tries to connect to server "wrong.host" to retrieve "/"
151-- and fails because the host does not exist. 154-- and fails because the host does not exist.
152b, h, c, e = socket.http.get("http://wrong.host/") 155r, e = http.get("http://wrong.host/")
153-- b, h, c are nil, and e returns with value "host not found" 156-- r is nil, and e returns with value "host not found"
154</pre> 157</pre>
155 158
156<!-- http.post ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 159<!-- http.post ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
157 160
158<p class=name id=post> 161<p class=name id=post>
159socket.http.<b>post(</b>url, body<b>)</b><br> 162http.<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> 163</p>
169 164
170<p class=description> 165<p class=description>
@@ -181,50 +176,64 @@ Note: This function is also trivially implemented with the use of the
181<!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 176<!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
182 177
183<p class=name id=request> 178<p class=name id=request>
184socket.http.<b>request{</b><br> 179http.<b>request{</b><br>
185&nbsp;&nbsp;method = <i>string</i>,<br>
186&nbsp;&nbsp;url = <i>string</i>,<br> 180&nbsp;&nbsp;url = <i>string</i>,<br>
187&nbsp;&nbsp;headers = <i>header-table</i>,<br> 181&nbsp;&nbsp;[sink = <i>LTN12 sink</i>],]<br>
188&nbsp;&nbsp;body = <i>string</i>,<br> 182&nbsp;&nbsp;[method = <i>string</i>,]<br>
189&nbsp;&nbsp;user = <i>string</i>,<br> 183&nbsp;&nbsp;[headers = <i>header-table</i>,]<br>
190&nbsp;&nbsp;password = <i>string</i>,<br> 184&nbsp;&nbsp;[source = <i>LTN12 source</i>],<br>
191&nbsp;&nbsp;stay = <i>string</i>,<br> 185&nbsp;&nbsp;[step = <i>LTN12 pump step</i>,]<br>
186&nbsp;&nbsp;[proxy = <i>string</i>,]<br>
187&nbsp;&nbsp;[redirect = <i>boolean</i>]<br>
192<b>}</b> 188<b>}</b>
193</p> 189</p>
194 190
195<p class=description> 191<p class=description>
196Performs the generic HTTP request using. 192Performs the generic HTTP request, controled by a request table.
197</p> 193</p>
198 194
199<p class=parameters> 195<p class=parameters>
200The request uses <tt>method</tt> on <tt>url</tt> 196The most important parameters are the <tt>url</tt> and the LTN12
201sending the request <tt>headers</tt> and request <tt>body</tt> in the 197<tt>sink</tt> that will receive the downloaded content.
202request message. If authentication information is provided, the function 198Any part of the <tt>url</tt> can be overriden by including
199the appropriate field in the request table.
200If authentication information is provided, the function
203uses the Basic Authentication Scheme (see <a href="#authentication">note</a>) 201uses the Basic Authentication Scheme (see <a href="#authentication">note</a>)
204to retrieve the document. <tt>User</tt> and <tt>password</tt> provided 202to retrieve the document. If <tt>sink</tt> is <tt><b>nil</b></tt>, the
205explicitly override those given by the <tt>url</tt>. The <tt>stay</tt> 203function discards the downloaded data. The optional parameters are the
206parameter, when set to anything but <b><tt>nil</tt></b>, prevents the function 204following:
207from automatically following 301 or 302 server redirect messages. 205</p>
208</p> 206<ul>
207<li><tt>method</tt>: The HTTP request method. Defaults to "GET";
208<li><tt>headers</tt>: Any additional HTTP headers to send with the request;
209<li><tt>source</tt>: LTN12 source to provide the request body. If there
210is a body, you need to provide an appropriate "<tt>content-length</tt>"
211request header field, or the function will attempt to send the body as
212"<tt>chunked</tt>" (something few servers support). Defaults to the empty source;
213<li><tt>step</tt>: LTN12 pump step function used to move data.
214Defaults to the LTN12 <tt>pump.step</tt> function.
215<li><tt>proxy</tt>: The URL of a proxy server to use. Defaults to no proxy;
216<li><tt>redirect</tt>: Set to <tt><b>false</b></tt> to prevent the
217function from automatically following 301 or 302 server redirect messages.
218</ul>
209 219
210<p class=return> 220<p class=return>
211The function returns a table with all components of the response message 221In case of failure, the function returns <tt><b>nil</b></tt> followed by an
212it managed to retrieve. The response table has the following form: 222error message. If successful, the function returns a table
223with all components of the response message. The response table has the following form:
213</p> 224</p>
214 225
215<blockquote><tt> 226<blockquote><tt>
216response = {<br> 227respt = {<br>
217&nbsp;&nbsp;body = <i>string</i>,<br>
218&nbsp;&nbsp;headers = <i>header-table</i>,<br> 228&nbsp;&nbsp;headers = <i>header-table</i>,<br>
219&nbsp;&nbsp;status = <i>string</i>,<br> 229&nbsp;&nbsp;status = <i>string</i>,<br>
220&nbsp;&nbsp;code = <i>number</i>,<br> 230&nbsp;&nbsp;code = <i>number</i>,<br>
221&nbsp;&nbsp;error = <i>string</i><br>
222} 231}
223</tt></blockquote> 232</tt></blockquote>
224 233
225<p class=return> 234<p class=return>
226Even when there was failure (URL not found, for example), the 235Even when there was failure (URL not found, for example), the
227function may succeed retrieving a message body (a web page informing the 236function usually succeeds retrieving a message body (a web page informing the
228URL was not found or some other useless page). To make sure the 237URL was not found or some other useless page). To make sure the
229operation was successful, check the returned status <tt>code</tt>. For 238operation was successful, check the returned status <tt>code</tt>. For
230a list of the possible values and their meanings, refer to <a 239a list of the possible values and their meanings, refer to <a
@@ -233,15 +242,18 @@ href="http://www.cs.princeton.edu/~diego/rfc/rfc2616.txt">RFC
233</p> 242</p>
234 243
235<pre class=example> 244<pre class=example>
245-- load the http module
246http = require("http")
247
236-- Requests information about a document, without downloading it. 248-- Requests information about a document, without downloading it.
237-- Useful, for example, if you want to display a download gauge and need 249-- Useful, for example, if you want to display a download gauge and need
238-- to know the size of the document in advance 250-- to know the size of the document in advance
239response = socket.http.request { 251respt = http.request {
240 method = "HEAD", 252 method = "HEAD",
241 url = "http://www.tecgraf.puc-rio.br/~diego" 253 url = "http://www.tecgraf.puc-rio.br/~diego"
242} 254}
243-- Would return the following headers: 255-- Would return the following headers:
244-- response.headers = { 256-- respt.headers = {
245-- date = "Tue, 18 Sep 2001 20:42:21 GMT", 257-- date = "Tue, 18 Sep 2001 20:42:21 GMT",
246-- server = "Apache/1.3.12 (Unix) (Red Hat/Linux)", 258-- server = "Apache/1.3.12 (Unix) (Red Hat/Linux)",
247-- ["last-modified"] = "Wed, 05 Sep 2001 06:11:20 GMT", 259-- ["last-modified"] = "Wed, 05 Sep 2001 06:11:20 GMT",
@@ -276,10 +288,14 @@ authentication is required.
276</p> 288</p>
277 289
278<pre class=example> 290<pre class=example>
291-- load required modules
292http = require("http")
293mime = require("mime")
294
279-- Connect to server "www.tecgraf.puc-rio.br" and tries to retrieve 295-- Connect to server "www.tecgraf.puc-rio.br" and tries to retrieve
280-- "~diego/auth/index.html", using the provided name and password to 296-- "~diego/auth/index.html", using the provided name and password to
281-- authenticate the request 297-- authenticate the request
282response = socket.http.request{ 298respt = http.request{
283 url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html", 299 url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html",
284 user = "diego", 300 user = "diego",
285 password = "password" 301 password = "password"
@@ -287,83 +303,12 @@ response = socket.http.request{
287 303
288-- Alternatively, one could fill the appropriate header and authenticate 304-- Alternatively, one could fill the appropriate header and authenticate
289-- the request directly. 305-- the request directly.
290headers = { 306respt = http.request {
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", 307 url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html",
295 headers = headers 308 headers = { authentication = "Basic " .. (mime.b64("diego:password")) }
296} 309}
297</pre> 310</pre>
298 311
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 <b><tt>nil</tt></b>, 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 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 312<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
368 313
369<div class=footer> 314<div class=footer>