diff options
Diffstat (limited to '')
-rw-r--r-- | docs/http.html (renamed from doc/http.html) | 151 |
1 files changed, 79 insertions, 72 deletions
diff --git a/doc/http.html b/docs/http.html index cd41c0d..52b8a30 100644 --- a/doc/http.html +++ b/docs/http.html | |||
@@ -1,10 +1,10 @@ | |||
1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
2 | "http://www.w3.org/TR/html4/strict.dtd"> | 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
3 | <html> | 3 | <html> |
4 | 4 | ||
5 | <head> | 5 | <head> |
6 | <meta name="description" content="LuaSocket: HTTP support"> | 6 | <meta name="description" content="LuaSocket: HTTP support"> |
7 | <meta name="keywords" content="Lua, HTTP, Library, WWW, Browser, Network, Support"> | 7 | <meta name="keywords" content="Lua, HTTP, Library, WWW, Browser, Network, Support"> |
8 | <title>LuaSocket: HTTP support</title> | 8 | <title>LuaSocket: HTTP support</title> |
9 | <link rel="stylesheet" href="reference.css" type="text/css"> | 9 | <link rel="stylesheet" href="reference.css" type="text/css"> |
10 | </head> | 10 | </head> |
@@ -13,22 +13,22 @@ | |||
13 | 13 | ||
14 | <!-- header ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 14 | <!-- header ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
15 | 15 | ||
16 | <div class=header> | 16 | <div class="header"> |
17 | <hr> | 17 | <hr> |
18 | <center> | 18 | <center> |
19 | <table summary="LuaSocket logo"> | 19 | <table summary="LuaSocket logo"> |
20 | <tr><td align=center><a href="http://www.lua.org"> | 20 | <tr><td align="center"><a href="http://www.lua.org"> |
21 | <img width=128 height=128 border=0 alt="LuaSocket" src="luasocket.png"> | 21 | <img width="128" height="128" border="0" alt="LuaSocket" src="luasocket.png"> |
22 | </a></td></tr> | 22 | </a></td></tr> |
23 | <tr><td align=center valign=top>Network support for the Lua language | 23 | <tr><td align="center" valign="top">Network support for the Lua language |
24 | </td></tr> | 24 | </td></tr> |
25 | </table> | 25 | </table> |
26 | <p class=bar> | 26 | <p class="bar"> |
27 | <a href="index.html">home</a> · | 27 | <a href="index.html">home</a> · |
28 | <a href="index.html#download">download</a> · | 28 | <a href="index.html#download">download</a> · |
29 | <a href="introduction.html">introduction</a> · | 29 | <a href="introduction.html">introduction</a> · |
30 | <a href="introduction.html">introduction</a> · | 30 | <a href="introduction.html">introduction</a> · |
31 | <a href="reference.html">reference</a> | 31 | <a href="reference.html">reference</a> |
32 | </p> | 32 | </p> |
33 | </center> | 33 | </center> |
34 | <hr> | 34 | <hr> |
@@ -36,12 +36,12 @@ | |||
36 | 36 | ||
37 | <!-- http +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 37 | <!-- http +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
38 | 38 | ||
39 | <h2 id="http">HTTP</h2> | 39 | <h2 id="http">HTTP</h2> |
40 | 40 | ||
41 | <p> | 41 | <p> |
42 | HTTP (Hyper Text Transfer Protocol) is the protocol used to exchange | 42 | HTTP (Hyper Text Transfer Protocol) is the protocol used to exchange |
43 | information between web-browsers and servers. The <tt>http</tt> | 43 | information between web-browsers and servers. The <tt>http</tt> |
44 | namespace offers full support for the client side of the HTTP | 44 | namespace offers full support for the client side of the HTTP |
45 | protocol (i.e., | 45 | protocol (i.e., |
46 | the facilities that would be used by a web-browser implementation). The | 46 | the facilities that would be used by a web-browser implementation). The |
47 | implementation conforms to the HTTP/1.1 standard, | 47 | implementation conforms to the HTTP/1.1 standard, |
@@ -50,16 +50,16 @@ implementation conforms to the HTTP/1.1 standard, | |||
50 | 50 | ||
51 | <p> | 51 | <p> |
52 | The module exports functions that provide HTTP functionality in different | 52 | The module exports functions that provide HTTP functionality in different |
53 | levels of abstraction. From the simple | 53 | levels of abstraction. From the simple |
54 | string oriented requests, through generic | 54 | string 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. | 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> | 56 | </p> |
57 | 57 | ||
58 | <p> | 58 | <p> |
59 | To obtain the <tt>http</tt> namespace, run: | 59 | To obtain the <tt>http</tt> namespace, run: |
60 | </p> | 60 | </p> |
61 | 61 | ||
62 | <pre class=example> | 62 | <pre class="example"> |
63 | -- loads the HTTP module and any libraries it requires | 63 | -- loads the HTTP module and any libraries it requires |
64 | local http = require("socket.http") | 64 | local http = require("socket.http") |
65 | </pre> | 65 | </pre> |
@@ -67,12 +67,12 @@ local http = require("socket.http") | |||
67 | <p> | 67 | <p> |
68 | URLs must conform to | 68 | URLs must conform to |
69 | <a href="http://www.ietf.org/rfc/rfc1738.txt">RFC 1738</a>, | 69 | <a href="http://www.ietf.org/rfc/rfc1738.txt">RFC 1738</a>, |
70 | that is, an URL is a string in the form: | 70 | that is, an URL is a string in the form: |
71 | </p> | 71 | </p> |
72 | 72 | ||
73 | <blockquote> | 73 | <blockquote> |
74 | <pre> | 74 | <pre> |
75 | [http://][<user>[:<password>]@]<host>[:<port>][/<path>] | 75 | [http://][<user>[:<password>]@]<host>[:<port>][/<path>] |
76 | </pre> | 76 | </pre> |
77 | </blockquote> | 77 | </blockquote> |
78 | 78 | ||
@@ -97,31 +97,34 @@ headers = {<br> | |||
97 | <p> | 97 | <p> |
98 | Field names are case insensitive (as specified by the standard) and all | 98 | Field names are case insensitive (as specified by the standard) and all |
99 | functions work with lowercase field names (but see | 99 | functions work with lowercase field names (but see |
100 | <a href=socket.html#headers.canonic><tt>socket.headers.canonic</tt></a>). | 100 | <a href="socket.html#headers.canonic"><tt>socket.headers.canonic</tt></a>). |
101 | Field values are left unmodified. | 101 | Field values are left unmodified. |
102 | </p> | 102 | </p> |
103 | 103 | ||
104 | <p class=note> | 104 | <p class="note"> |
105 | Note: MIME headers are independent of order. Therefore, there is no problem | 105 | Note: MIME headers are independent of order. Therefore, there is no problem |
106 | in representing them in a Lua table. | 106 | in representing them in a Lua table. |
107 | </p> | 107 | </p> |
108 | 108 | ||
109 | <p> | 109 | <p> |
110 | The following constants can be set to control the default behavior of | 110 | The following constants can be set to control the default behavior of |
111 | the HTTP module: | 111 | the HTTP module: |
112 | </p> | 112 | </p> |
113 | 113 | ||
114 | <ul> | 114 | <ul> |
115 | <li> <tt>PORT</tt>: default port used for connections; | 115 | <li> <tt>PROXY</tt>: default proxy used for connections;</li> |
116 | <li> <tt>PROXY</tt>: default proxy used for connections; | 116 | <li> <tt>TIMEOUT</tt>: sets the timeout for all I/O operations;</li> |
117 | <li> <tt>TIMEOUT</tt>: sets the timeout for all I/O operations; | 117 | <li> <tt>USERAGENT</tt>: default user agent reported to server.</li> |
118 | <li> <tt>USERAGENT</tt>: default user agent reported to server. | ||
119 | </ul> | 118 | </ul> |
120 | 119 | ||
120 | <p class="note"> | ||
121 | Note: These constants are global. Changing them will also | ||
122 | change the behavior other code that might be using LuaSocket. | ||
123 | </p> | ||
121 | 124 | ||
122 | <!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 125 | <!-- http.request ++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
123 | 126 | ||
124 | <p class=name id="request"> | 127 | <p class="name" id="request"> |
125 | http.<b>request(</b>url [, body]<b>)</b><br> | 128 | http.<b>request(</b>url [, body]<b>)</b><br> |
126 | http.<b>request{</b><br> | 129 | http.<b>request{</b><br> |
127 | url = <i>string</i>,<br> | 130 | url = <i>string</i>,<br> |
@@ -132,30 +135,31 @@ http.<b>request{</b><br> | |||
132 | [step = <i>LTN12 pump step</i>,]<br> | 135 | [step = <i>LTN12 pump step</i>,]<br> |
133 | [proxy = <i>string</i>,]<br> | 136 | [proxy = <i>string</i>,]<br> |
134 | [redirect = <i>boolean</i>,]<br> | 137 | [redirect = <i>boolean</i>,]<br> |
135 | [create = <i>function</i>]<br> | 138 | [create = <i>function</i>,]<br> |
139 | [maxredirects = <i>number</i>]<br> | ||
136 | <b>}</b> | 140 | <b>}</b> |
137 | </p> | 141 | </p> |
138 | 142 | ||
139 | <p class=description> | 143 | <p class="description"> |
140 | The request function has two forms. The simple form downloads | 144 | The request function has two forms. The simple form downloads |
141 | a URL using the <tt>GET</tt> or <tt>POST</tt> method and is based | 145 | a URL using the <tt>GET</tt> or <tt>POST</tt> method and is based |
142 | on strings. The generic form performs any HTTP method and is | 146 | on strings. The generic form performs any HTTP method and is |
143 | <a href=http://lua-users.org/wiki/FiltersSourcesAndSinks>LTN12</a> based. | 147 | <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> based. |
144 | </p> | 148 | </p> |
145 | 149 | ||
146 | <p class=parameters> | 150 | <p class="parameters"> |
147 | If the first argument of the <tt>request</tt> function is a string, it | 151 | If the first argument of the <tt>request</tt> function is a string, it |
148 | should be an <tt>url</tt>. In that case, if a <tt>body</tt> | 152 | should be an <tt>url</tt>. In that case, if a <tt>body</tt> |
149 | is provided as a string, the function will perform a <tt>POST</tt> method | 153 | is provided as a string, the function will perform a <tt>POST</tt> method |
150 | in the <tt>url</tt>. Otherwise, it performs a <tt>GET</tt> in the | 154 | in the <tt>url</tt>. Otherwise, it performs a <tt>GET</tt> in the |
151 | <tt>url</tt> | 155 | <tt>url</tt> |
152 | </p> | 156 | </p> |
153 | 157 | ||
154 | <p class=parameters> | 158 | <p class="parameters"> |
155 | If the first argument is instead a table, the most important fields are | 159 | If the first argument is instead a table, the most important fields are |
156 | the <tt>url</tt> and the <em>simple</em> | 160 | the <tt>url</tt> and the <em>simple</em> |
157 | <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> | 161 | <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> |
158 | <tt>sink</tt> that will receive the downloaded content. | 162 | <tt>sink</tt> that will receive the downloaded content. |
159 | Any part of the <tt>url</tt> can be overridden by including | 163 | Any part of the <tt>url</tt> can be overridden by including |
160 | the appropriate field in the request table. | 164 | the appropriate field in the request table. |
161 | If authentication information is provided, the function | 165 | If authentication information is provided, the function |
@@ -165,48 +169,51 @@ function discards the downloaded data. The optional parameters are the | |||
165 | following: | 169 | following: |
166 | </p> | 170 | </p> |
167 | <ul> | 171 | <ul> |
168 | <li><tt>method</tt>: The HTTP request method. Defaults to "GET"; | 172 | <li><tt>method</tt>: The HTTP request method. Defaults to "GET";</li> |
169 | <li><tt>headers</tt>: Any additional HTTP headers to send with the request; | 173 | <li><tt>headers</tt>: Any additional HTTP headers to send with the request;</li> |
170 | <li><tt>source</tt>: <em>simple</em> | 174 | <li><tt>source</tt>: <em>simple</em> |
171 | <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> | 175 | <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> |
172 | source to provide the request body. If there | 176 | source to provide the request body. If there |
173 | is a body, you need to provide an appropriate "<tt>content-length</tt>" | 177 | is a body, you need to provide an appropriate "<tt>content-length</tt>" |
174 | request header field, or the function will attempt to send the body as | 178 | request header field, or the function will attempt to send the body as |
175 | "<tt>chunked</tt>" (something few servers support). Defaults to the empty source; | 179 | "<tt>chunked</tt>" (something few servers support). Defaults to the empty source;</li> |
176 | <li><tt>step</tt>: | 180 | <li><tt>step</tt>: |
177 | <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> | 181 | <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">LTN12</a> |
178 | pump step function used to move data. | 182 | pump step function used to move data. |
179 | Defaults to the LTN12 <tt>pump.step</tt> function. | 183 | Defaults to the LTN12 <tt>pump.step</tt> function.</li> |
180 | <li><tt>proxy</tt>: The URL of a proxy server to use. Defaults to no proxy; | 184 | <li><tt>proxy</tt>: The URL of a proxy server to use. Defaults to no proxy;</li> |
181 | <li><tt>redirect</tt>: Set to <tt><b>false</b></tt> to prevent the | 185 | <li><tt>redirect</tt>: Set to <tt><b>false</b></tt> to prevent the |
182 | function from automatically following 301 or 302 server redirect messages; | 186 | function from automatically following 301 or 302 server redirect messages;</li> |
183 | <li><tt>create</tt>: An optional function to be used instead of | 187 | <li><tt>create</tt>: An optional function to be used instead of |
184 | <a href=tcp.html#socket.tcp><tt>socket.tcp</tt></a> when the communications socket is created. | 188 | <a href="tcp.html#socket.tcp"><tt>socket.tcp</tt></a> when the communications socket is created.</li> |
189 | <li><tt>maxredirects</tt>: An optional number specifying the maximum number of | ||
190 | redirects to follow. Defaults to <tt>5</tt> if not specified. A boolean | ||
191 | <tt>false</tt> value means no maximum (unlimited).</li> | ||
185 | </ul> | 192 | </ul> |
186 | 193 | ||
187 | <p class=return> | 194 | <p class="return"> |
188 | In case of failure, the function returns <tt><b>nil</b></tt> followed by an | 195 | In case of failure, the function returns <tt><b>nil</b></tt> followed by an |
189 | error message. If successful, the simple form returns the response | 196 | error message. If successful, the simple form returns the response |
190 | body as a string, followed by the response status code, the response | 197 | body as a string, followed by the response status code, the response |
191 | headers and the response status line. The generic function returns the same | 198 | headers and the response status line. The generic function returns the same |
192 | information, except the first return value is just the number 1 (the body | 199 | information, except the first return value is just the number 1 (the body |
193 | goes to the <tt>sink</tt>). | 200 | goes to the <tt>sink</tt>). |
194 | </p> | 201 | </p> |
195 | 202 | ||
196 | <p class=return> | 203 | <p class="return"> |
197 | Even when the server fails to provide the contents of the requested URL (URL not found, for example), | 204 | Even when the server fails to provide the contents of the requested URL (URL not found, for example), |
198 | it usually returns a message body (a web page informing the | 205 | it usually returns a message body (a web page informing the |
199 | URL was not found or some other useless page). To make sure the | 206 | URL was not found or some other useless page). To make sure the |
200 | operation was successful, check the returned status <tt>code</tt>. For | 207 | operation was successful, check the returned status <tt>code</tt>. For |
201 | a list of the possible values and their meanings, refer to <a | 208 | a list of the possible values and their meanings, refer to <a |
202 | href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a>. | 209 | href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a>. |
203 | </p> | 210 | </p> |
204 | 211 | ||
205 | <p class=description> | 212 | <p class="description"> |
206 | Here are a few examples with the simple interface: | 213 | Here are a few examples with the simple interface: |
207 | </p> | 214 | </p> |
208 | 215 | ||
209 | <pre class=example> | 216 | <pre class="example"> |
210 | -- load the http module | 217 | -- load the http module |
211 | local io = require("io") | 218 | local io = require("io") |
212 | local http = require("socket.http") | 219 | local http = require("socket.http") |
@@ -214,15 +221,15 @@ local ltn12 = require("ltn12") | |||
214 | 221 | ||
215 | -- connect to server "www.cs.princeton.edu" and retrieves this manual | 222 | -- connect to server "www.cs.princeton.edu" and retrieves this manual |
216 | -- file from "~diego/professional/luasocket/http.html" and print it to stdout | 223 | -- file from "~diego/professional/luasocket/http.html" and print it to stdout |
217 | http.request{ | 224 | http.request{ |
218 | url = "http://www.cs.princeton.edu/~diego/professional/luasocket/http.html", | 225 | url = "http://www.cs.princeton.edu/~diego/professional/luasocket/http.html", |
219 | sink = ltn12.sink.file(io.stdout) | 226 | sink = ltn12.sink.file(io.stdout) |
220 | } | 227 | } |
221 | 228 | ||
222 | -- connect to server "www.example.com" and tries to retrieve | 229 | -- connect to server "www.example.com" and tries to retrieve |
223 | -- "/private/index.html". Fails because authentication is needed. | 230 | -- "/private/index.html". Fails because authentication is needed. |
224 | b, c, h = http.request("http://www.example.com/private/index.html") | 231 | b, c, h = http.request("http://www.example.com/private/index.html") |
225 | -- b returns some useless page telling about the denied access, | 232 | -- b returns some useless page telling about the denied access, |
226 | -- h returns authentication information | 233 | -- h returns authentication information |
227 | -- and c returns with value 401 (Authentication Required) | 234 | -- and c returns with value 401 (Authentication Required) |
228 | 235 | ||
@@ -232,11 +239,11 @@ r, e = http.request("http://wrong.host/") | |||
232 | -- r is nil, and e returns with value "host not found" | 239 | -- r is nil, and e returns with value "host not found" |
233 | </pre> | 240 | </pre> |
234 | 241 | ||
235 | <p class=description> | 242 | <p class="description"> |
236 | And here is an example using the generic interface: | 243 | And here is an example using the generic interface: |
237 | </p> | 244 | </p> |
238 | 245 | ||
239 | <pre class=example> | 246 | <pre class="example"> |
240 | -- load the http module | 247 | -- load the http module |
241 | http = require("socket.http") | 248 | http = require("socket.http") |
242 | 249 | ||
@@ -258,7 +265,7 @@ r, c, h = http.request { | |||
258 | -- } | 265 | -- } |
259 | </pre> | 266 | </pre> |
260 | 267 | ||
261 | <p class=note id="post"> | 268 | <p class="note" id="post"> |
262 | Note: When sending a POST request, simple interface adds a | 269 | Note: When sending a POST request, simple interface adds a |
263 | "<tt>Content-type: application/x-www-form-urlencoded</tt>" | 270 | "<tt>Content-type: application/x-www-form-urlencoded</tt>" |
264 | header to the request. This is the type used by | 271 | header to the request. This is the type used by |
@@ -266,21 +273,21 @@ HTML forms. If you need another type, use the generic | |||
266 | interface. | 273 | interface. |
267 | </p> | 274 | </p> |
268 | 275 | ||
269 | <p class=note id="authentication"> | 276 | <p class="note" id="authentication"> |
270 | Note: Some URLs are protected by their | 277 | Note: Some URLs are protected by their |
271 | servers from anonymous download. For those URLs, the server must receive | 278 | servers from anonymous download. For those URLs, the server must receive |
272 | some sort of authentication along with the request or it will deny | 279 | some sort of authentication along with the request or it will deny |
273 | download and return status "401 Authentication Required". | 280 | download and return status "401 Authentication Required". |
274 | </p> | 281 | </p> |
275 | 282 | ||
276 | <p class=note> | 283 | <p class="note"> |
277 | The HTTP/1.1 standard defines two authentication methods: the Basic | 284 | The HTTP/1.1 standard defines two authentication methods: the Basic |
278 | Authentication Scheme and the Digest Authentication Scheme, both | 285 | Authentication Scheme and the Digest Authentication Scheme, both |
279 | explained in detail in | 286 | explained in detail in |
280 | <a href="http://www.ietf.org/rfc/rfc2068.txt">RFC 2068</a>. | 287 | <a href="http://www.ietf.org/rfc/rfc2068.txt">RFC 2068</a>. |
281 | </p> | 288 | </p> |
282 | 289 | ||
283 | <p class=note>The Basic Authentication Scheme sends | 290 | <p class="note">The Basic Authentication Scheme sends |
284 | <tt><user></tt> and | 291 | <tt><user></tt> and |
285 | <tt><password></tt> unencrypted to the server and is therefore | 292 | <tt><password></tt> unencrypted to the server and is therefore |
286 | considered unsafe. Unfortunately, by the time of this implementation, | 293 | considered unsafe. Unfortunately, by the time of this implementation, |
@@ -289,7 +296,7 @@ Therefore, this is the method used by the toolkit whenever | |||
289 | authentication is required. | 296 | authentication is required. |
290 | </p> | 297 | </p> |
291 | 298 | ||
292 | <pre class=example> | 299 | <pre class="example"> |
293 | -- load required modules | 300 | -- load required modules |
294 | http = require("socket.http") | 301 | http = require("socket.http") |
295 | mime = require("mime") | 302 | mime = require("mime") |
@@ -309,20 +316,20 @@ r, c = http.request { | |||
309 | 316 | ||
310 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 317 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
311 | 318 | ||
312 | <div class=footer> | 319 | <div class="footer"> |
313 | <hr> | 320 | <hr> |
314 | <center> | 321 | <center> |
315 | <p class=bar> | 322 | <p class="bar"> |
316 | <a href="index.html">home</a> · | 323 | <a href="index.html">home</a> · |
317 | <a href="index.html#download">download</a> · | 324 | <a href="index.html#download">download</a> · |
318 | <a href="installation.html">installation</a> · | 325 | <a href="installation.html">installation</a> · |
319 | <a href="introduction.html">introduction</a> · | 326 | <a href="introduction.html">introduction</a> · |
320 | <a href="reference.html">reference</a> | 327 | <a href="reference.html">reference</a> |
321 | </p> | 328 | </p> |
322 | <p> | 329 | <p> |
323 | <small> | 330 | <small> |
324 | Last modified by Diego Nehab on <br> | 331 | Last modified by Eric Westbrook on <br> |
325 | Thu Apr 20 00:25:26 EDT 2006 | 332 | Sat Feb 23 19:09:42 UTC 2019 |
326 | </small> | 333 | </small> |
327 | </p> | 334 | </p> |
328 | </center> | 335 | </center> |