diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-15 23:00:56 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-15 23:00:56 +0000 |
commit | 843a431ef98fd541d98fd3898463985d9bfcde28 (patch) | |
tree | 2de4cd78a58ee17aa029528ede8ff5989b819019 /doc/http.html | |
parent | cb03a0e95429cc0d498e77fd50241c87fd2bf3ea (diff) | |
download | luasocket-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.html | 203 |
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 | |||
98 | in representing them in a Lua table. | 98 | in representing them in a Lua table. |
99 | </p> | 99 | </p> |
100 | 100 | ||
101 | <p> | ||
102 | The following constants can be set to control the default behaviour of | ||
103 | the 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> |
104 | socket.http.<b>get(</b>url<b>)</b><br> | 116 | http.<b>get(</b>url<b>)</b><br> |
105 | socket.http.<b>get{</b><br> | ||
106 | url = <i>string</i>,<br> | ||
107 | headers = <i>header-table</i>,<br> | ||
108 | user = <i>string</i>,<br> | ||
109 | password = <i>string</i>,<br> | ||
110 | stay = <i>bool</i>,<br> | ||
111 | <b>}</b> | ||
112 | </p> | 117 | </p> |
113 | 118 | ||
114 | <p class=description> | 119 | <p class=description> |
115 | Performs the HTTP method <tt>GET</tt>. | 120 | Performs the HTTP method <tt>GET</tt>. |
116 | </p> | 121 | </p> |
117 | 122 | ||
118 | <p class=parameters> | 123 | <p class=parameters> |
119 | The function can be | 124 | <tt>Url</tt> identifies the entity to retrieve. |
120 | called either directly with a <tt>url</tt> or with a <em>request table</em>. | ||
121 | The use of a request table allows complete control over the components of | ||
122 | the request. Values passed explicitly as fields of the request table | ||
123 | override those given by the <tt>url</tt>. For a description of the fields, | ||
124 | see the <a href=#request><tt>request</tt></a> function. | ||
125 | </p> | 125 | </p> |
126 | 126 | ||
127 | <p class=return> | 127 | <p class=return> |
128 | The function returns the response message body, the mime headers, the | 128 | If successful, the function returns the response message body, the mime |
129 | status code and an error message (if any). In case of failure, the | 129 | headers, and the status code. In case of failure, the |
130 | function returns all information it managed to gather. | 130 | function 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 | ||
140 | http = 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" |
141 | b, h, c, e = socket.http.get("http://www.tecgraf.puc-rio.br/luasocket/http.html") | 144 | b, 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. |
145 | b, h, c, e = socket.http.get("http://www.tecgraf.puc-rio.br/~diego/auth/index.html") | 148 | b, 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. |
152 | b, h, c, e = socket.http.get("http://wrong.host/") | 155 | r, 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> |
159 | socket.http.<b>post(</b>url, body<b>)</b><br> | 162 | http.<b>post(</b>url, body<b>)</b><br> |
160 | socket.http.<b>post{</b><br> | ||
161 | url = <i>string</i>,<br> | ||
162 | headers = <i>header-table</i>,<br> | ||
163 | body = <i>string</i>,<br> | ||
164 | user = <i>string</i>,<br> | ||
165 | password = <i>string</i>,<br> | ||
166 | 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> |
184 | socket.http.<b>request{</b><br> | 179 | http.<b>request{</b><br> |
185 | method = <i>string</i>,<br> | ||
186 | url = <i>string</i>,<br> | 180 | url = <i>string</i>,<br> |
187 | headers = <i>header-table</i>,<br> | 181 | [sink = <i>LTN12 sink</i>],]<br> |
188 | body = <i>string</i>,<br> | 182 | [method = <i>string</i>,]<br> |
189 | user = <i>string</i>,<br> | 183 | [headers = <i>header-table</i>,]<br> |
190 | password = <i>string</i>,<br> | 184 | [source = <i>LTN12 source</i>],<br> |
191 | stay = <i>string</i>,<br> | 185 | [step = <i>LTN12 pump step</i>,]<br> |
186 | [proxy = <i>string</i>,]<br> | ||
187 | [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> |
196 | Performs the generic HTTP request using. | 192 | Performs the generic HTTP request, controled by a request table. |
197 | </p> | 193 | </p> |
198 | 194 | ||
199 | <p class=parameters> | 195 | <p class=parameters> |
200 | The request uses <tt>method</tt> on <tt>url</tt> | 196 | The most important parameters are the <tt>url</tt> and the LTN12 |
201 | sending the request <tt>headers</tt> and request <tt>body</tt> in the | 197 | <tt>sink</tt> that will receive the downloaded content. |
202 | request message. If authentication information is provided, the function | 198 | Any part of the <tt>url</tt> can be overriden by including |
199 | the appropriate field in the request table. | ||
200 | If authentication information is provided, the function | ||
203 | uses the Basic Authentication Scheme (see <a href="#authentication">note</a>) | 201 | uses the Basic Authentication Scheme (see <a href="#authentication">note</a>) |
204 | to retrieve the document. <tt>User</tt> and <tt>password</tt> provided | 202 | to retrieve the document. If <tt>sink</tt> is <tt><b>nil</b></tt>, the |
205 | explicitly override those given by the <tt>url</tt>. The <tt>stay</tt> | 203 | function discards the downloaded data. The optional parameters are the |
206 | parameter, when set to anything but <b><tt>nil</tt></b>, prevents the function | 204 | following: |
207 | from 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 | ||
210 | is a body, you need to provide an appropriate "<tt>content-length</tt>" | ||
211 | request 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. | ||
214 | Defaults 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 | ||
217 | function from automatically following 301 or 302 server redirect messages. | ||
218 | </ul> | ||
209 | 219 | ||
210 | <p class=return> | 220 | <p class=return> |
211 | The function returns a table with all components of the response message | 221 | In case of failure, the function returns <tt><b>nil</b></tt> followed by an |
212 | it managed to retrieve. The response table has the following form: | 222 | error message. If successful, the function returns a table |
223 | with 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> |
216 | response = {<br> | 227 | respt = {<br> |
217 | body = <i>string</i>,<br> | ||
218 | headers = <i>header-table</i>,<br> | 228 | headers = <i>header-table</i>,<br> |
219 | status = <i>string</i>,<br> | 229 | status = <i>string</i>,<br> |
220 | code = <i>number</i>,<br> | 230 | code = <i>number</i>,<br> |
221 | error = <i>string</i><br> | ||
222 | } | 231 | } |
223 | </tt></blockquote> | 232 | </tt></blockquote> |
224 | 233 | ||
225 | <p class=return> | 234 | <p class=return> |
226 | Even when there was failure (URL not found, for example), the | 235 | Even when there was failure (URL not found, for example), the |
227 | function may succeed retrieving a message body (a web page informing the | 236 | function usually succeeds retrieving a message body (a web page informing the |
228 | URL was not found or some other useless page). To make sure the | 237 | URL was not found or some other useless page). To make sure the |
229 | operation was successful, check the returned status <tt>code</tt>. For | 238 | operation was successful, check the returned status <tt>code</tt>. For |
230 | a list of the possible values and their meanings, refer to <a | 239 | a 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 | ||
246 | http = 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 |
239 | response = socket.http.request { | 251 | respt = 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 | ||
292 | http = require("http") | ||
293 | mime = 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 |
282 | response = socket.http.request{ | 298 | respt = 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. |
290 | headers = { | 306 | respt = http.request { |
291 | authentication = "Basic " .. socket.code.base64("diego:password") | ||
292 | } | ||
293 | response = 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> | ||
302 | socket.http.<b>request_cb(</b>request, response<b>)</b> | ||
303 | </p> | ||
304 | |||
305 | <p class=description> | ||
306 | Performs the generic HTTP request. | ||
307 | </p> | ||
308 | |||
309 | <p class=parameters> | ||
310 | The function receives two tables as parameters. The <tt>request</tt> table | ||
311 | provides information about the request: | ||
312 | </p> | ||
313 | |||
314 | <blockquote><tt> | ||
315 | request = {<br> | ||
316 | method = <i>string</i>,<br> | ||
317 | url = <i>string</i>,<br> | ||
318 | headers = <i>header-table</i>,<br> | ||
319 | body_cb = <i>send-callback</i>,<br> | ||
320 | user = <i>string</i>,<br> | ||
321 | password = <i>string</i>,<br> | ||
322 | stay = <i>string</i>,<br> | ||
323 | }</tt> | ||
324 | </blockquote> | ||
325 | |||
326 | <p class=parameters> | ||
327 | The function uses the HTTP method specified in | ||
328 | <tt>request.method</tt> on the URL <tt>request.url</tt>, | ||
329 | sending <tt>request.headers</tt> along with the request. The request | ||
330 | message body is sent via the send callback <tt>request.body_cb</tt>. | ||
331 | If authentication information is provided, the function uses the Basic | ||
332 | Authentication Scheme (see <a href="#authentication">note</a>) to | ||
333 | retrieve 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 | ||
336 | anything but <b><tt>nil</tt></b>, prevents the function from automatically | ||
337 | following 301 or 302 server redirect messages. | ||
338 | </p> | ||
339 | |||
340 | <p class=parameters> | ||
341 | The <tt>response</tt> table specifies information about the desired | ||
342 | response: | ||
343 | </p> | ||
344 | |||
345 | <blockquote><tt> | ||
346 | response = {<br> | ||
347 | body_cb = <i>receive-callback</i><br> | ||
348 | }</tt> | ||
349 | </blockquote> | ||
350 | |||
351 | <p class=return> | ||
352 | The function returns the same response table as that returned by the | ||
353 | <tt>socket.http.request</tt> function, except the response message body is | ||
354 | returned to the receive callback given by the | ||
355 | <tt>response.body_cb</tt> field. | ||
356 | </p> | ||
357 | |||
358 | <p class=note> | ||
359 | Note: 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> | ||
364 | Note: Method names are case <em>sensitive</em> | ||
365 | </p> | ||
366 | |||
367 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 312 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
368 | 313 | ||
369 | <div class=footer> | 314 | <div class=footer> |