ltn12.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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: LTN12 support">
  6. <meta name="keywords" content="Lua, LuaSocket, Filters, Source, Sink,
  7. Pump, Support, Library">
  8. <title>LuaSocket: LTN12 module</title>
  9. <link rel="stylesheet" href="reference.css" type="text/css">
  10. </head>
  11. <body>
  12. <!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  13. <div class=header>
  14. <hr>
  15. <center>
  16. <table summary="LuaSocket logo">
  17. <tr><td align=center><a href="http://www.lua.org">
  18. <img width=128 height=128 border=0 alt="LuaSocket" src="luasocket.png">
  19. </a></td></tr>
  20. <tr><td align=center valign=top>Network support for the Lua language
  21. </td></tr>
  22. </table>
  23. <p class=bar>
  24. <a href="index.html">home</a> &middot;
  25. <a href="index.html#download">download</a> &middot;
  26. <a href="installation.html">installation</a> &middot;
  27. <a href="introduction.html">introduction</a> &middot;
  28. <a href="reference.html">reference</a>
  29. </p>
  30. </center>
  31. <hr>
  32. </div>
  33. <!-- ltn12 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  34. <h2 id=ltn12>LTN12</h2>
  35. <p> The <tt>ltn12</tt> namespace implements the ideas described in
  36. <a href="http://lua-users.org/wiki/FiltersSourcesAndSinks">
  37. LTN012, Filters sources and sinks</a>. This manual simply describes the
  38. functions. Please refer to the LTN for a deeper explanation of the
  39. functionality provided by this module.
  40. </p>
  41. <p>
  42. To obtain the <tt>ltn12</tt> namespace, run:
  43. </p>
  44. <pre class=example>
  45. -- loads the LTN21 module
  46. local ltn12 = require("ltn12")
  47. </pre>
  48. <!-- filters ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  49. <h3 id="filter">Filters</h3>
  50. <!-- chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  51. <p class=name id="filter.chain">
  52. ltn12.filter.<b>chain(</b>filter<sub>1</sub>, filter<sub>2</sub>
  53. [, ... filter<sub>N</sub>]<b>)</b>
  54. </p>
  55. <p class=description>
  56. Returns a filter that passes all data it receives through each of a
  57. series of given filters.
  58. </p>
  59. <p class=parameters>
  60. <tt>Filter<sub>1</sub></tt> to <tt>filter<sub>N</sub></tt> are simple
  61. filters.
  62. </p>
  63. <p class=return>
  64. The function returns the chained filter.
  65. </p>
  66. <p class=note>
  67. The nesting of filters can be arbitrary. For instance, the useless filter
  68. below doesn't do anything but return the data that was passed to it,
  69. unaltered.
  70. </p>
  71. <pre class=example>
  72. -- load required modules
  73. local ltn12 = require("ltn12")
  74. local mime = require("mime")
  75. -- create a silly identity filter
  76. id = ltn12.filter.chain(
  77. mime.encode("quoted-printable"),
  78. mime.encode("base64"),
  79. mime.decode("base64"),
  80. mime.decode("quoted-printable")
  81. )
  82. </pre>
  83. <!-- cycle ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  84. <p class=name id="filter.cycle">
  85. ltn12.filter.<b>cycle(</b>low [, ctx, extra]<b>)</b>
  86. </p>
  87. <p class=description>
  88. Returns a high-level filter that cycles though a low-level filter by
  89. passing it each chunk and updating a context between calls.
  90. </p>
  91. <p class=parameters>
  92. <tt>Low</tt> is the low-level filter to be cycled,
  93. <tt>ctx</tt> is the initial context and <tt>extra</tt> is any extra
  94. argument the low-level filter might take.
  95. </p>
  96. <p class=return>
  97. The function returns the high-level filter.
  98. </p>
  99. <pre class=example>
  100. -- load the ltn12 module
  101. local ltn12 = require("ltn12")
  102. -- the base64 mime filter factory
  103. encodet['base64'] = function()
  104. return ltn12.filter.cycle(b64, "")
  105. end
  106. </pre>
  107. <!-- pumps ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  108. <h3 id="pump">Pumps</h3>
  109. <!-- all ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  110. <p class=name id="pump.all">
  111. ltn12.pump.<b>all(</b>source, sink<b>)</b>
  112. </p>
  113. <p class=description>
  114. Pumps <em>all</em> data from a <tt>source</tt> to a <tt>sink</tt>.
  115. </p>
  116. <p class=return>
  117. If successful, the function returns a value that evaluates to
  118. <b><tt>true</tt></b>. In case
  119. of error, the function returns a <b><tt>false</tt></b> value, followed by an error message.
  120. </p>
  121. <!-- step +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  122. <p class=name id="pump.step">
  123. ltn12.pump.<b>step(</b>source, sink<b>)</b>
  124. </p>
  125. <p class=description>
  126. Pumps <em>one</em> chunk of data from a <tt>source</tt> to a <tt>sink</tt>.
  127. </p>
  128. <p class=return>
  129. If successful, the function returns a value that evaluates to
  130. <b><tt>true</tt></b>. In case
  131. of error, the function returns a <b><tt>false</tt></b> value, followed by an error message.
  132. </p>
  133. <!-- sinks ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  134. <h3 id="sink">Sinks</h3>
  135. <!-- chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  136. <p class=name id="sink.chain">
  137. ltn12.sink.<b>chain(</b>filter, sink<b>)</b>
  138. </p>
  139. <p class=description>
  140. Creates and returns a new sink that passes data through a <tt>filter</tt> before sending it to a given <tt>sink</tt>.
  141. </p>
  142. <!-- error ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  143. <p class=name id="sink.error">
  144. ltn12.sink.<b>error(</b>message<b>)</b>
  145. </p>
  146. <p class=description>
  147. Creates and returns a sink that aborts transmission with the error
  148. <tt>message</tt>.
  149. </p>
  150. <!-- file +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  151. <p class=name id="sink.file">
  152. ltn12.sink.<b>file(</b>handle, message<b>)</b>
  153. </p>
  154. <p class=description>
  155. Creates a sink that sends data to a file.
  156. </p>
  157. <p class=parameters>
  158. <tt>Handle</tt> is a file handle. If <tt>handle</tt> is <tt><b>nil</b></tt>,
  159. <tt>message</tt> should give the reason for failure.
  160. </p>
  161. <p class=return>
  162. The function returns a sink that sends all data to the given <tt>handle</tt>
  163. and closes the file when done, or a sink that aborts the transmission with
  164. the error <tt>message</tt>
  165. </p>
  166. <p class=note>
  167. In the following example, notice how the prototype is designed to
  168. fit nicely with the <tt>io.open</tt> function.
  169. </p>
  170. <pre class=example>
  171. -- load the ltn12 module
  172. local ltn12 = require("ltn12")
  173. -- copy a file
  174. ltn12.pump.all(
  175. ltn12.source.file(io.open("original.png", "rb")),
  176. ltn12.sink.file(io.open("copy.png", "wb"))
  177. )
  178. </pre>
  179. <!-- null +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  180. <p class=name id="sink.null">
  181. ltn12.sink.<b>null()</b>
  182. </p>
  183. <p class=description>
  184. Returns a sink that ignores all data it receives.
  185. </p>
  186. <!-- simplify +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  187. <p class=name id="sink.simplify">
  188. ltn12.sink.<b>simplify(</b>sink<b>)</b>
  189. </p>
  190. <p class=description>
  191. Creates and returns a simple sink given a fancy <tt>sink</tt>.
  192. </p>
  193. <!-- table ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  194. <p class=name id="sink.table">
  195. ltn12.sink.<b>table(</b>[table]<b>)</b>
  196. </p>
  197. <p class=description>
  198. Creates a sink that stores all chunks in a table. The chunks can later be
  199. efficiently concatenated into a single string.
  200. </p>
  201. <p class=parameters>
  202. <tt>Table</tt> is used to hold the chunks. If
  203. <tt><b>nil</b></tt>, the function creates its own table.
  204. </p>
  205. <p class=return>
  206. The function returns the sink and the table used to store the chunks.
  207. </p>
  208. <pre class=example>
  209. -- load needed modules
  210. local http = require("socket.http")
  211. local ltn12 = require("ltn12")
  212. -- a simplified http.get function
  213. function http.get(u)
  214. local t = {}
  215. local respt = request{
  216. url = u,
  217. sink = ltn12.sink.table(t)
  218. }
  219. return table.concat(t), respt.headers, respt.code
  220. end
  221. </pre>
  222. <!-- sinks ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  223. <h3 id="source">Sources</h3>
  224. <!-- cat ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  225. <p class=name id="source.cat">
  226. ltn12.source.<b>cat(</b>source<sub>1</sub> [, source<sub>2</sub>, ...,
  227. source<sub>N</sub>]<b>)</b>
  228. </p>
  229. <p class=description>
  230. Creates a new source that produces the concatenation of the data produced
  231. by a number of sources.
  232. </p>
  233. <p class=parameters>
  234. <tt>Source<sub>1</sub></tt> to <tt>source<sub>N</sub></tt> are the original
  235. sources.
  236. </p>
  237. <p class=return>
  238. The function returns the new source.
  239. </p>
  240. <!-- chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  241. <p class=name id="source.chain">
  242. ltn12.source.<b>chain(</b>source, filter<b>)</b>
  243. </p>
  244. <p class=description>
  245. Creates a new <tt>source</tt> that passes data through a <tt>filter</tt>
  246. before returning it.
  247. </p>
  248. <p class=return>
  249. The function returns the new source.
  250. </p>
  251. <!-- empty ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  252. <p class=name id="source.empty">
  253. ltn12.source.<b>empty()</b>
  254. </p>
  255. <p class=description>
  256. Creates and returns an empty source.
  257. </p>
  258. <!-- error ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  259. <p class=name id="source.error">
  260. ltn12.source.<b>error(</b>message<b>)</b>
  261. </p>
  262. <p class=description>
  263. Creates and returns a source that aborts transmission with the error
  264. <tt>message</tt>.
  265. </p>
  266. <!-- file +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  267. <p class=name id="source.file">
  268. ltn12.source.<b>file(</b>handle, message<b>)</b>
  269. </p>
  270. <p class=description>
  271. Creates a source that produces the contents of a file.
  272. </p>
  273. <p class=parameters>
  274. <tt>Handle</tt> is a file handle. If <tt>handle</tt> is <tt><b>nil</b></tt>,
  275. <tt>message</tt> should give the reason for failure.
  276. </p>
  277. <p class=return>
  278. The function returns a source that reads chunks of data from
  279. given <tt>handle</tt> and returns it to the user,
  280. closing the file when done, or a source that aborts the transmission with
  281. the error <tt>message</tt>
  282. </p>
  283. <p class=note>
  284. In the following example, notice how the prototype is designed to
  285. fit nicely with the <tt>io.open</tt> function.
  286. </p>
  287. <pre class=example>
  288. -- load the ltn12 module
  289. local ltn12 = require("ltn12")
  290. -- copy a file
  291. ltn12.pump.all(
  292. ltn12.source.file(io.open("original.png", "rb")),
  293. ltn12.sink.file(io.open("copy.png", "wb"))
  294. )
  295. </pre>
  296. <!-- simplify +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  297. <p class=name id="source.simplify">
  298. ltn12.source.<b>simplify(</b>source<b>)</b>
  299. </p>
  300. <p class=description>
  301. Creates and returns a simple source given a fancy <tt>source</tt>.
  302. </p>
  303. <!-- string +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  304. <p class=name id="source.string">
  305. ltn12.source.<b>string(</b>string<b>)</b>
  306. </p>
  307. <p class=description>
  308. Creates and returns a source that produces the contents of a
  309. <tt>string</tt>, chunk by chunk.
  310. </p>
  311. <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
  312. <div class=footer>
  313. <hr>
  314. <center>
  315. <p class=bar>
  316. <a href="index.html">home</a> &middot;
  317. <a href="index.html#down">download</a> &middot;
  318. <a href="installation.html">installation</a> &middot;
  319. <a href="introduction.html">introduction</a> &middot;
  320. <a href="reference.html">reference</a>
  321. </p>
  322. <p>
  323. <small>
  324. Last modified by Diego Nehab on <br>
  325. Thu Apr 20 00:25:41 EDT 2006
  326. </small>
  327. </p>
  328. </center>
  329. </div>
  330. </body>
  331. </html>