diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-16 23:21:49 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-06-16 23:21:49 +0000 |
| commit | 27c8ae30aaef25d537669062d5f7f929eec18032 (patch) | |
| tree | 1a141b842e1b5ed49c0bc4385cabdae95c204e83 /doc/http.html | |
| parent | 9fc682a106f13901b60a665619fc61d9dae49fb0 (diff) | |
| download | luasocket-27c8ae30aaef25d537669062d5f7f929eec18032.tar.gz luasocket-27c8ae30aaef25d537669062d5f7f929eec18032.tar.bz2 luasocket-27c8ae30aaef25d537669062d5f7f929eec18032.zip | |
Updated the HTTP manual. Finaly done.
Diffstat (limited to 'doc/http.html')
| -rw-r--r-- | doc/http.html | 148 |
1 files changed, 58 insertions, 90 deletions
diff --git a/doc/http.html b/doc/http.html index a621ec7..9125df7 100644 --- a/doc/http.html +++ b/doc/http.html | |||
| @@ -48,11 +48,9 @@ implementation conforms to the HTTP/1.1 standard, | |||
| 48 | 48 | ||
| 49 | <p> | 49 | <p> |
| 50 | The module exports functions that provide HTTP functionality in different | 50 | The module exports functions that provide HTTP functionality in different |
| 51 | levels of abstraction, from the simple | 51 | levels of abstraction. From the simple |
| 52 | <a href="#get"><tt>get</tt></a> function, through the generic | 52 | string oriented requests, through generic |
| 53 | <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> based | 53 | <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. |
| 54 | <a href="#request"><tt>request</tt></a> function, down to | ||
| 55 | even lower-level if you bother to look through the source code. | ||
| 56 | </p> | 54 | </p> |
| 57 | 55 | ||
| 58 | <p> | 56 | <p> |
| @@ -118,72 +116,10 @@ the HTTP module: | |||
| 118 | <li> <tt>USERAGENT</tt>: default user agent reported to server. | 116 | <li> <tt>USERAGENT</tt>: default user agent reported to server. |
| 119 | </ul> | 117 | </ul> |
| 120 | 118 | ||
| 121 | <!-- http.get +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 122 | |||
| 123 | <p class=name id=get> | ||
| 124 | http.<b>get(</b>url<b>)</b><br> | ||
| 125 | </p> | ||
| 126 | |||
| 127 | <p class=description> | ||
| 128 | Performs the HTTP method <tt>GET</tt>. | ||
| 129 | </p> | ||
| 130 | |||
| 131 | <p class=parameters> | ||
| 132 | <tt>Url</tt> identifies the entity to retrieve. | ||
| 133 | </p> | ||
| 134 | |||
| 135 | <p class=return> | ||
| 136 | If successful, the function returns the response message body, the mime | ||
| 137 | headers, and the status code. In case of failure, the | ||
| 138 | function returns <tt><b>nil</b></tt> followed by an error message. | ||
| 139 | </p> | ||
| 140 | |||
| 141 | <p class=note> | ||
| 142 | Note: The function is trivially implemented with the use of the | ||
| 143 | <a href="#request"><tt>request</tt></a> function. | ||
| 144 | </p> | ||
| 145 | |||
| 146 | <pre class=example> | ||
| 147 | -- load the http module | ||
| 148 | http = require("http") | ||
| 149 | |||
| 150 | -- connect to server "www.tecgraf.puc-rio.br" and retrieves this manual | ||
| 151 | -- file from "/luasocket/http.html" | ||
| 152 | b, h, c = http.get("http://www.tecgraf.puc-rio.br/luasocket/http.html") | ||
| 153 | |||
| 154 | -- connect to server "www.tecgraf.puc-rio.br" and tries to retrieve | ||
| 155 | -- "~diego/auth/index.html". Fails because authentication is needed. | ||
| 156 | b, h, c = http.get("http://www.tecgraf.puc-rio.br/~diego/auth/index.html") | ||
| 157 | -- b returns some useless page telling about the denied access, | ||
| 158 | -- h returns authentication information | ||
| 159 | -- and c returns with value 401 (Authentication Required) | ||
| 160 | |||
| 161 | -- tries to connect to server "wrong.host" to retrieve "/" | ||
| 162 | -- and fails because the host does not exist. | ||
| 163 | r, e = http.get("http://wrong.host/") | ||
| 164 | -- r is nil, and e returns with value "host not found" | ||
| 165 | </pre> | ||
| 166 | |||
| 167 | <!-- http.post ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
| 168 | |||
| 169 | <p class=name id=post> | ||
| 170 | http.<b>post(</b>url, body<b>)</b><br> | ||
| 171 | </p> | ||
| 172 | |||
| 173 | <p class=description> | ||
| 174 | Same as <a href="#get"><tt>get</tt></a>, except | ||
| 175 | that the <tt>POST</tt> method is used and the request | ||
| 176 | message <tt>body</tt> is sent along with the request. | ||
| 177 | </p> | ||
| 178 | |||
| 179 | <p class=note> | ||
| 180 | Note: This function is also trivially implemented with the use of the | ||
| 181 | <a href="#request"><tt>request</tt></a> function. | ||
| 182 | </p> | ||
| 183 | |||
| 184 | <!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 119 | <!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
| 185 | 120 | ||
| 186 | <p class=name id=request> | 121 | <p class=name id=request> |
| 122 | http.<b>request(</b>url [, body]<b>)</b><br> | ||
| 187 | http.<b>request{</b><br> | 123 | http.<b>request{</b><br> |
| 188 | url = <i>string</i>,<br> | 124 | url = <i>string</i>,<br> |
| 189 | [sink = <i>LTN12 sink</i>,]<br> | 125 | [sink = <i>LTN12 sink</i>,]<br> |
| @@ -197,11 +133,23 @@ http.<b>request{</b><br> | |||
| 197 | </p> | 133 | </p> |
| 198 | 134 | ||
| 199 | <p class=description> | 135 | <p class=description> |
| 200 | Performs the generic HTTP request, controlled by a request table. | 136 | The request function has two forms. The simple form downloads |
| 137 | a URL using the <tt>GET</tt> or <tt>POST</tt> method and is based | ||
| 138 | on strings. The generic form performs any HTTP method and is | ||
| 139 | <a href=http://lua-users.org/wiki/FiltersSourcesAndSinks>LTN12</a> based. | ||
| 201 | </p> | 140 | </p> |
| 202 | 141 | ||
| 203 | <p class=parameters> | 142 | <p class=parameters> |
| 204 | The most important parameters are the <tt>url</tt> and the <em>simple</em> | 143 | If the first argument of the <tt>request</tt> function is a string, it |
| 144 | should be an <tt>url</tt>. In that case, if a <tt>body</tt> | ||
| 145 | is provided as a string, the function will perform a <tt>POST</tt> method | ||
| 146 | in the <tt>url</tt>. Otherwise, it performs a <tt>GET</tt> in the | ||
| 147 | <tt>url</tt> | ||
| 148 | </p> | ||
| 149 | |||
| 150 | <p class=parameters> | ||
| 151 | If the first argument is instead a table, the most important fields are | ||
| 152 | the <tt>url</tt> and the <em>simple</em> | ||
| 205 | <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> | 153 | <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> |
| 206 | <tt>sink</tt> that will receive the downloaded content. | 154 | <tt>sink</tt> that will receive the downloaded content. |
| 207 | Any part of the <tt>url</tt> can be overridden by including | 155 | Any part of the <tt>url</tt> can be overridden by including |
| @@ -232,20 +180,15 @@ function from automatically following 301 or 302 server redirect messages. | |||
| 232 | 180 | ||
| 233 | <p class=return> | 181 | <p class=return> |
| 234 | In case of failure, the function returns <tt><b>nil</b></tt> followed by an | 182 | In case of failure, the function returns <tt><b>nil</b></tt> followed by an |
| 235 | error message. If successful, the function returns a table | 183 | error message. If successful, the simple form returns the response |
| 236 | with all components of the response message. The response table has the following form: | 184 | body as a string, followed by the response status code, the response |
| 185 | headers and the response status line. The complex function returns the same | ||
| 186 | information, except the first return value is just the number 1 (the body | ||
| 187 | goes to the <tt>sink</tt>). | ||
| 237 | </p> | 188 | </p> |
| 238 | 189 | ||
| 239 | <blockquote><tt> | ||
| 240 | respt = {<br> | ||
| 241 | headers = <i>header-table</i>,<br> | ||
| 242 | status = <i>string</i>,<br> | ||
| 243 | code = <i>number</i>,<br> | ||
| 244 | } | ||
| 245 | </tt></blockquote> | ||
| 246 | |||
| 247 | <p class=return> | 190 | <p class=return> |
| 248 | Even when the server fails to provide the contents of the requested URL (URL not found, for example), the | 191 | Even when the server fails to provide the contents of the requested URL (URL not found, for example), |
| 249 | it usually returns a message body (a web page informing the | 192 | it usually returns a message body (a web page informing the |
| 250 | URL was not found or some other useless page). To make sure the | 193 | URL was not found or some other useless page). To make sure the |
| 251 | operation was successful, check the returned status <tt>code</tt>. For | 194 | operation was successful, check the returned status <tt>code</tt>. For |
| @@ -254,6 +197,35 @@ href="http://www.cs.princeton.edu/~diego/rfc/rfc2616.txt">RFC | |||
| 254 | 2616</a>. | 197 | 2616</a>. |
| 255 | </p> | 198 | </p> |
| 256 | 199 | ||
| 200 | <p class=description> | ||
| 201 | Here are a few examples with the simple interface: | ||
| 202 | </p> | ||
| 203 | |||
| 204 | <pre class=example> | ||
| 205 | -- load the http module | ||
| 206 | http = require("http") | ||
| 207 | |||
| 208 | -- connect to server "www.tecgraf.puc-rio.br" and retrieves this manual | ||
| 209 | -- file from "/luasocket/http.html" | ||
| 210 | b = http.request("http://www.tecgraf.puc-rio.br/luasocket/http.html") | ||
| 211 | |||
| 212 | -- connect to server "www.tecgraf.puc-rio.br" and tries to retrieve | ||
| 213 | -- "~diego/auth/index.html". Fails because authentication is needed. | ||
| 214 | b, c, h = http.request("http://www.tecgraf.puc-rio.br/~diego/auth/index.html") | ||
| 215 | -- b returns some useless page telling about the denied access, | ||
| 216 | -- h returns authentication information | ||
| 217 | -- and c returns with value 401 (Authentication Required) | ||
| 218 | |||
| 219 | -- tries to connect to server "wrong.host" to retrieve "/" | ||
| 220 | -- and fails because the host does not exist. | ||
| 221 | r, e = http.request("http://wrong.host/") | ||
| 222 | -- r is nil, and e returns with value "host not found" | ||
| 223 | </pre> | ||
| 224 | |||
| 225 | <p class=description> | ||
| 226 | And here is an example using the generic interface: | ||
| 227 | </p> | ||
| 228 | |||
| 257 | <pre class=example> | 229 | <pre class=example> |
| 258 | -- load the http module | 230 | -- load the http module |
| 259 | http = require("http") | 231 | http = require("http") |
| @@ -261,12 +233,12 @@ http = require("http") | |||
| 261 | -- Requests information about a document, without downloading it. | 233 | -- Requests information about a document, without downloading it. |
| 262 | -- Useful, for example, if you want to display a download gauge and need | 234 | -- Useful, for example, if you want to display a download gauge and need |
| 263 | -- to know the size of the document in advance | 235 | -- to know the size of the document in advance |
| 264 | respt = http.request { | 236 | r, c, h = http.request { |
| 265 | method = "HEAD", | 237 | method = "HEAD", |
| 266 | url = "http://www.tecgraf.puc-rio.br/~diego" | 238 | url = "http://www.tecgraf.puc-rio.br/~diego" |
| 267 | } | 239 | } |
| 268 | -- Would return the following headers: | 240 | -- r is 1, c is 200, and h would return the following headers: |
| 269 | -- respt.headers = { | 241 | -- h = { |
| 270 | -- date = "Tue, 18 Sep 2001 20:42:21 GMT", | 242 | -- date = "Tue, 18 Sep 2001 20:42:21 GMT", |
| 271 | -- server = "Apache/1.3.12 (Unix) (Red Hat/Linux)", | 243 | -- server = "Apache/1.3.12 (Unix) (Red Hat/Linux)", |
| 272 | -- ["last-modified"] = "Wed, 05 Sep 2001 06:11:20 GMT", | 244 | -- ["last-modified"] = "Wed, 05 Sep 2001 06:11:20 GMT", |
| @@ -307,15 +279,11 @@ mime = require("mime") | |||
| 307 | -- Connect to server "www.tecgraf.puc-rio.br" and tries to retrieve | 279 | -- Connect to server "www.tecgraf.puc-rio.br" and tries to retrieve |
| 308 | -- "~diego/auth/index.html", using the provided name and password to | 280 | -- "~diego/auth/index.html", using the provided name and password to |
| 309 | -- authenticate the request | 281 | -- authenticate the request |
| 310 | respt = http.request{ | 282 | b, c, h = http.request("http://diego:password@www.tecgraf.puc-rio.br/~diego/auth/index.html") |
| 311 | url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html", | ||
| 312 | user = "diego", | ||
| 313 | password = "password" | ||
| 314 | } | ||
| 315 | 283 | ||
| 316 | -- Alternatively, one could fill the appropriate header and authenticate | 284 | -- Alternatively, one could fill the appropriate header and authenticate |
| 317 | -- the request directly. | 285 | -- the request directly. |
| 318 | respt = http.request { | 286 | r, c = http.request { |
| 319 | url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html", | 287 | url = "http://www.tecgraf.puc-rio.br/~diego/auth/index.html", |
| 320 | headers = { authentication = "Basic " .. (mime.b64("diego:password")) } | 288 | headers = { authentication = "Basic " .. (mime.b64("diego:password")) } |
| 321 | } | 289 | } |
