1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
6 <meta name=
"description" content=
"LuaSocket: LTN12 support">
7 <meta name=
"keywords" content=
"Lua, LuaSocket, Filters, Source, Sink,
8 Pump, Support, Library">
9 <title>LuaSocket: LTN12 module
</title>
10 <link rel=
"stylesheet" href=
"reference.css" type=
"text/css">
15 <!-- header +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
20 <table summary=
"LuaSocket logo">
21 <tr><td align=center
><a href=
"http://www.lua.org">
22 <img width=
128 height=
128 border=
0 alt=
"LuaSocket" src=
"luasocket.png">
24 <tr><td align=center valign=top
>Network support for the Lua language
28 <a href=
"index.html">home
</a> ·
29 <a href=
"index.html#download">download
</a> ·
30 <a href=
"installation.html">installation
</a> ·
31 <a href=
"introduction.html">introduction
</a> ·
32 <a href=
"reference.html">reference
</a>
38 <!-- ltn12 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
40 <h2 id=ltn12
>LTN12
</h2>
42 <p> The
<tt>ltn12
</tt> namespace implements the ideas described in
43 <a href=
"http://lua-users.org/wiki/FiltersSourcesAndSinks">
44 LTN012, Filters sources and sinks
</a>. This manual simply describes the
45 functions. Please refer to the LTN for a deeper explanation of the
46 functionality provided by this module.
50 To obtain the
<tt>ltn12
</tt> namespace, run:
54 -- loads the LTN21 module
55 local ltn12 = require(
"ltn12")
58 <!-- filters ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
60 <h3 id=
"filter">Filters
</h3>
62 <!-- chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
64 <p class=name
id=
"filter.chain">
65 ltn12.filter.
<b>chain(
</b>filter
<sub>1</sub>, filter
<sub>2</sub>
66 [, ... filter
<sub>N
</sub>]
<b>)
</b>
70 Returns a filter that passes all data it receives through each of a
71 series of given filters.
75 <tt>Filter
<sub>1</sub></tt> to
<tt>filter
<sub>N
</sub></tt> are simple
80 The function returns the chained filter.
84 The nesting of filters can be arbitrary. For instance, the useless filter
85 below doesn't do anything but return the data that was passed to it,
90 -- load required modules
91 local ltn12 = require(
"ltn12")
92 local mime = require(
"mime")
94 -- create a silly identity filter
95 id = ltn12.filter.chain(
96 mime.encode(
"quoted-printable"),
97 mime.encode(
"base64"),
98 mime.decode(
"base64"),
99 mime.decode(
"quoted-printable")
103 <!-- cycle ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
105 <p class=name
id=
"filter.cycle">
106 ltn12.filter.
<b>cycle(
</b>low [, ctx, extra]
<b>)
</b>
109 <p class=description
>
110 Returns a high-level filter that cycles though a low-level filter by
111 passing it each chunk and updating a context between calls.
115 <tt>Low
</tt> is the low-level filter to be cycled,
116 <tt>ctx
</tt> is the initial context and
<tt>extra
</tt> is any extra
117 argument the low-level filter might take.
121 The function returns the high-level filter.
125 -- load the ltn12 module
126 local ltn12 = require(
"ltn12")
128 -- the base64 mime filter factory
129 encodet['base64'] = function()
130 return ltn12.filter.cycle(b64,
"")
134 <!-- pumps ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
136 <h3 id=
"pump">Pumps
</h3>
138 <!-- all ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
140 <p class=name
id=
"pump.all">
141 ltn12.pump.
<b>all(
</b>source, sink
<b>)
</b>
144 <p class=description
>
145 Pumps
<em>all
</em> data from a
<tt>source
</tt> to a
<tt>sink
</tt>.
149 If successful, the function returns a value that evaluates to
150 <b><tt>true
</tt></b>. In case
151 of error, the function returns a
<b><tt>false
</tt></b> value, followed by an error message.
154 <!-- step +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
156 <p class=name
id=
"pump.step">
157 ltn12.pump.
<b>step(
</b>source, sink
<b>)
</b>
160 <p class=description
>
161 Pumps
<em>one
</em> chunk of data from a
<tt>source
</tt> to a
<tt>sink
</tt>.
165 If successful, the function returns a value that evaluates to
166 <b><tt>true
</tt></b>. In case
167 of error, the function returns a
<b><tt>false
</tt></b> value, followed by an error message.
170 <!-- sinks ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
172 <h3 id=
"sink">Sinks
</h3>
174 <!-- chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
176 <p class=name
id=
"sink.chain">
177 ltn12.sink.
<b>chain(
</b>filter, sink
<b>)
</b>
180 <p class=description
>
181 Creates and returns a new sink that passes data through a
<tt>filter
</tt> before sending it to a given
<tt>sink
</tt>.
184 <!-- error ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
186 <p class=name
id=
"sink.error">
187 ltn12.sink.
<b>error(
</b>message
<b>)
</b>
190 <p class=description
>
191 Creates and returns a sink that aborts transmission with the error
195 <!-- file +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
197 <p class=name
id=
"sink.file">
198 ltn12.sink.
<b>file(
</b>handle, message
<b>)
</b>
201 <p class=description
>
202 Creates a sink that sends data to a file.
206 <tt>Handle
</tt> is a file handle. If
<tt>handle
</tt> is
<tt><b>nil
</b></tt>,
207 <tt>message
</tt> should give the reason for failure.
211 The function returns a sink that sends all data to the given
<tt>handle
</tt>
212 and closes the file when done, or a sink that aborts the transmission with
213 the error
<tt>message
</tt>
217 In the following example, notice how the prototype is designed to
218 fit nicely with the
<tt>io.open
</tt> function.
222 -- load the ltn12 module
223 local ltn12 = require(
"ltn12")
227 ltn12.source.file(io.open(
"original.png")),
228 ltn12.sink.file(io.open(
"copy.png"))
232 <!-- null +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
234 <p class=name
id=
"sink.null">
235 ltn12.sink.
<b>null()
</b>
238 <p class=description
>
239 Returns a sink that ignores all data it receives.
242 <!-- simplify +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
244 <p class=name
id=
"sink.simplify">
245 ltn12.sink.
<b>simplify(
</b>sink
<b>)
</b>
248 <p class=description
>
249 Creates and returns a simple sink given a fancy
<tt>sink
</tt>.
252 <!-- table ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
254 <p class=name
id=
"sink.table">
255 ltn12.sink.
<b>table(
</b>[table]
<b>)
</b>
258 <p class=description
>
259 Creates a sink that stores all chunks in a table. The chunks can later be
260 efficiently concatenated into a single string.
264 <tt>Table
</tt> is used to hold the chunks. If
265 <tt><b>nil
</b></tt>, the function creates its own table.
269 The function returns the sink and the table used to store the chunks.
273 -- load needed modules
274 local http = require(
"socket.http")
275 local ltn12 = require(
"ltn12")
277 -- a simplified http.get function
280 local respt = request{
282 sink = ltn12.sink.table(t)
284 return table.concat(t), respt.headers, respt.code
288 <!-- sinks ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
290 <h3 id=
"source">Sources
</h3>
292 <!-- cat ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
294 <p class=name
id=
"source.cat">
295 ltn12.source.
<b>cat(
</b>source
<sub>1</sub> [, source
<sub>2</sub>, ...,
296 source
<sub>N
</sub>]
<b>)
</b>
299 <p class=description
>
300 Creates a new source that produces the concatenation of the data produced
301 by a number of sources.
305 <tt>Source
<sub>1</sub></tt> to
<tt>source
<sub>N
</sub></tt> are the original
310 The function returns the new source.
313 <!-- chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
315 <p class=name
id=
"source.chain">
316 ltn12.source.
<b>chain(
</b>source, filter
<b>)
</b>
319 <p class=description
>
320 Creates a new
<tt>source
</tt> that passes data through a
<tt>filter
</tt>
325 The function returns the new source.
328 <!-- empty ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
330 <p class=name
id=
"source.empty">
331 ltn12.source.
<b>empty()
</b>
334 <p class=description
>
335 Creates and returns an empty source.
338 <!-- error ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
340 <p class=name
id=
"source.error">
341 ltn12.source.
<b>error(
</b>message
<b>)
</b>
344 <p class=description
>
345 Creates and returns a source that aborts transmission with the error
349 <!-- file +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
351 <p class=name
id=
"source.file">
352 ltn12.source.
<b>file(
</b>handle, message
<b>)
</b>
355 <p class=description
>
356 Creates a source that produces the contents of a file.
360 <tt>Handle
</tt> is a file handle. If
<tt>handle
</tt> is
<tt><b>nil
</b></tt>,
361 <tt>message
</tt> should give the reason for failure.
365 The function returns a source that reads chunks of data from
366 given
<tt>handle
</tt> and returns it to the user,
367 closing the file when done, or a source that aborts the transmission with
368 the error
<tt>message
</tt>
372 In the following example, notice how the prototype is designed to
373 fit nicely with the
<tt>io.open
</tt> function.
377 -- load the ltn12 module
378 local ltn12 = require(
"ltn12")
382 ltn12.source.file(io.open(
"original.png")),
383 ltn12.sink.file(io.open(
"copy.png"))
387 <!-- simplify +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
389 <p class=name
id=
"source.simplify">
390 ltn12.source.
<b>simplify(
</b>source
<b>)
</b>
393 <p class=description
>
394 Creates and returns a simple source given a fancy
<tt>source
</tt>.
397 <!-- string +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
399 <p class=name
id=
"source.string">
400 ltn12.source.
<b>string(
</b>string
<b>)
</b>
403 <p class=description
>
404 Creates and returns a source that produces the contents of a
405 <tt>string
</tt>, chunk by chunk.
408 <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
414 <a href=
"index.html">home
</a> ·
415 <a href=
"index.html#down">download
</a> ·
416 <a href=
"installation.html">installation
</a> ·
417 <a href=
"introduction.html">introduction
</a> ·
418 <a href=
"reference.html">reference
</a>
422 Last modified by Diego Nehab on
<br>
423 Thu Apr
20 00:
25:
41 EDT
2006