http.html 11 KB

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