diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-08-31 01:00:15 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-08-31 01:00:15 +0000 |
| commit | 982781f1464c9b7a8133130433f83dbf1f59a2c0 (patch) | |
| tree | 8f96f9e9fa1e6bef8b8356037986ddc18673cade /doc/http.html | |
| parent | 6789b83ff5c15296267f880d3b98cf8a1800c30a (diff) | |
| download | luasocket-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.html | 388 |
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> · | ||
| 26 | <a href="home.html#download">download</a> · | ||
| 27 | <a href="introduction.html">introduction</a> · | ||
| 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> | ||
| 39 | HTTP (Hyper Text Transfer Protocol) is the protocol used to exchange | ||
| 40 | information between web-browsers and servers. The <tt>http.lua</tt> | ||
| 41 | module offers support for the client side of the HTTP protocol (i.e., | ||
| 42 | the facilities that would be used by a web-browser implementation). The | ||
| 43 | implementation conforms to the HTTP/1.1 standard, | ||
| 44 | <a href="http://www.cs.princeton.edu/~diego/rfc/rfc2616.txt">RFC | ||
| 45 | 2616</a>. | ||
| 46 | </p> | ||
| 47 | |||
| 48 | <p> | ||
| 49 | The module exports functions that provide HTTP functionality in different | ||
| 50 | levels of abstraction, from a simple <a | ||
| 51 | href="#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> | ||
| 56 | URLs must conform to | ||
| 57 | <a href="http://www.cs.princeton.edu/~diego/rfc/rfc1738.txt">RFC | ||
| 58 | 1738</a>, | ||
| 59 | that is, an URL is a string in the form: | ||
| 60 | </p> | ||
| 61 | |||
| 62 | <blockquote> | ||
| 63 | <pre> | ||
| 64 | [http://][<user>[:<password>]@]<host>[:<port>][/<path>] | ||
| 65 | </pre> | ||
| 66 | </blockquote> | ||
| 67 | |||
| 68 | <p> | ||
| 69 | MIME 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> | ||
| 75 | headers = {<br> | ||
| 76 | field-1-name = <i>field-1-value</i>,<br> | ||
| 77 | field-2-name = <i>field-2-value</i>,<br> | ||
| 78 | field-3-name = <i>field-3-value</i>, | ||
| 79 | </tt></td></tr> | ||
| 80 | <tr><td align=center><tt> | ||
| 81 | ... | ||
| 82 | </tt></td></tr> | ||
| 83 | <tr><td><tt> | ||
| 84 | field-n-name = <i>field-n-value</i><br> | ||
| 85 | } | ||
| 86 | </tt></td></tr> | ||
| 87 | </table> | ||
| 88 | </blockquote> | ||
| 89 | |||
| 90 | <p> | ||
| 91 | Field names are case insensitive (as specified by the standard) and all | ||
| 92 | functions work with lowercase field names. | ||
| 93 | Field values are left unmodified. | ||
| 94 | </p> | ||
| 95 | |||
| 96 | <p class=note> | ||
| 97 | Note: MIME headers are independent of order. Therefore, there is no problem | ||
| 98 | in representing them in a Lua table. | ||
| 99 | </p> | ||
| 100 | |||
| 101 | <!-- http.get +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 102 | |||
| 103 | <p class=name id=get> | ||
| 104 | socket.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> | ||
| 113 | |||
| 114 | <p class=description> | ||
| 115 | Performs the HTTP method <tt>GET</tt>. | ||
| 116 | </p> | ||
| 117 | |||
| 118 | <p class=parameters> | ||
| 119 | The function can be | ||
| 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> | ||
| 126 | |||
| 127 | <p class=return> | ||
| 128 | The function returns the response message body, the mime headers, the | ||
| 129 | status code and an error message (if any). In case of failure, the | ||
| 130 | function returns all information it managed to gather. | ||
| 131 | </p> | ||
| 132 | |||
| 133 | <p class=note> | ||
| 134 | Note: 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" | ||
| 141 | b, 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. | ||
| 145 | b, 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. | ||
| 152 | b, 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> | ||
| 159 | socket.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> | ||
| 169 | |||
| 170 | <p class=description> | ||
| 171 | Same as <a href="#get"><tt>get</tt></a>, except | ||
| 172 | that the <tt>POST</tt> method is used and the request | ||
| 173 | message <tt>body</tt> is sent along with the request. | ||
| 174 | </p> | ||
| 175 | |||
| 176 | <p class=note> | ||
| 177 | Note: 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> | ||
| 184 | socket.http.<b>request{</b><br> | ||
| 185 | method = <i>string</i>,<br> | ||
| 186 | url = <i>string</i>,<br> | ||
| 187 | headers = <i>header-table</i>,<br> | ||
| 188 | body = <i>string</i>,<br> | ||
| 189 | user = <i>string</i>,<br> | ||
| 190 | password = <i>string</i>,<br> | ||
| 191 | stay = <i>string</i>,<br> | ||
| 192 | <b>}</b> | ||
| 193 | </p> | ||
| 194 | |||
| 195 | <p class=description> | ||
| 196 | Performs the generic HTTP request using. | ||
| 197 | </p> | ||
| 198 | |||
| 199 | <p class=parameters> | ||
| 200 | The request uses <tt>method</tt> on <tt>url</tt> | ||
| 201 | sending the request <tt>headers</tt> and request <tt>body</tt> in the | ||
| 202 | request message. If authentication information is provided, the function | ||
| 203 | uses the Basic Authentication Scheme (see <a href="#authentication">note</a>) | ||
| 204 | to retrieve the document. <tt>User</tt> and <tt>password</tt> provided | ||
| 205 | explicitly override those given by the <tt>url</tt>. The <tt>stay</tt> | ||
| 206 | parameter, when set to anything but <tt>nil</tt>, prevents the function | ||
| 207 | from automatically following 301 or 302 server redirect messages. | ||
| 208 | </p> | ||
| 209 | |||
| 210 | <p class=return> | ||
| 211 | The function returns a table with all components of the response message | ||
| 212 | it managed to retrieve. The response table has the following form: | ||
| 213 | </p> | ||
| 214 | |||
| 215 | <blockquote><tt> | ||
| 216 | response = {<br> | ||
| 217 | body = <i>string</i>,<br> | ||
| 218 | headers = <i>header-table</i>,<br> | ||
| 219 | status = <i>string</i>,<br> | ||
| 220 | code = <i>number</i>,<br> | ||
| 221 | error = <i>string</i><br> | ||
| 222 | } | ||
| 223 | </tt></blockquote> | ||
| 224 | |||
| 225 | <p class=return> | ||
| 226 | Even when there was failure (URL not found, for example), the | ||
| 227 | function may succeed retrieving a message body (a web page informing the | ||
| 228 | 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 | ||
| 230 | a list of the possible values and their meanings, refer to <a | ||
| 231 | href="http://www.cs.princeton.edu/~diego/rfc/rfc2616.txt">RFC | ||
| 232 | 2616</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 | ||
| 239 | response = 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> | ||
| 256 | Note: Some URLs are protected by their | ||
| 257 | servers from anonymous download. For those URLs, the server must receive | ||
| 258 | some sort of authentication along with the request or it will deny | ||
| 259 | download and return status "401 Authentication Required". | ||
| 260 | </p> | ||
| 261 | |||
| 262 | <p class=note> | ||
| 263 | The HTTP/1.1 standard defines two authentication methods: the Basic | ||
| 264 | Authentication Scheme and the Digest Authentication Scheme, both | ||
| 265 | explained 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><user></tt> and | ||
| 271 | <tt><password></tt> unencrypted to the server and is therefore | ||
| 272 | considered unsafe. Unfortunately, by the time of this implementation, | ||
| 273 | the wide majority of servers and browsers support the Basic Scheme only. | ||
| 274 | Therefore, this is the method used by the toolkit whenever | ||
| 275 | authentication 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 | ||
| 282 | response = 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. | ||
| 290 | headers = { | ||
| 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", | ||
| 295 | headers = headers | ||
| 296 | } | ||
| 297 | </pre> | ||
| 298 | |||
| 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 <tt>nil</tt>, 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 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 368 | |||
| 369 | <div class=footer> | ||
| 370 | <hr> | ||
| 371 | <center> | ||
| 372 | <p class=bar> | ||
| 373 | <a href="home.html">home</a> · | ||
| 374 | <a href="home.html#download">download</a> · | ||
| 375 | <a href="introduction.html">introduction</a> · | ||
| 376 | <a href="reference.html">reference</a> | ||
| 377 | </p> | ||
| 378 | <p> | ||
| 379 | <small> | ||
| 380 | Last modified by Diego Nehab on <br> | ||
| 381 | Sat Aug 9 01:00:41 PDT 2003 | ||
| 382 | </small> | ||
| 383 | </p> | ||
| 384 | </center> | ||
| 385 | </div> | ||
| 386 | |||
| 387 | </body> | ||
| 388 | </html> | ||
