Blame doc/ltn12.html

Packit b53373
Packit b53373
    "http://www.w3.org/TR/html4/strict.dtd">
Packit b53373
<html>
Packit b53373
Packit b53373
<head>
Packit b53373
<meta name="description" content="LuaSocket: LTN12 support">
Packit b53373
Packit b53373
Pump, Support, Library">
Packit b53373
<title>LuaSocket: LTN12 module</title>
Packit b53373
<link rel="stylesheet" href="reference.css" type="text/css">
Packit b53373
</head>
Packit b53373
Packit b53373
<body>
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
<center>
Packit b53373
Packit b53373
Packit b53373
LuaSocket
Packit b53373
Packit b53373
Network support for the Lua language
Packit b53373
Packit b53373
Packit b53373

Packit b53373
home ·
Packit b53373
download ·
Packit b53373
installation ·
Packit b53373
introduction ·
Packit b53373
reference 
Packit b53373

Packit b53373
</center>
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

LTN12

Packit b53373
Packit b53373

The <tt>ltn12</tt> namespace implements the ideas described in

Packit b53373
Packit b53373
LTN012, Filters sources and sinks. This manual simply describes the
Packit b53373
functions. Please refer to the LTN for a deeper explanation of the
Packit b53373
functionality provided by this module.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
To obtain the <tt>ltn12</tt> namespace, run:
Packit b53373

Packit b53373
Packit b53373
Packit b53373
-- loads the LTN21 module
Packit b53373
local ltn12 = require("ltn12")
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Filters

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.filter.chain(filter<sub>1</sub>, filter<sub>2</sub> 
Packit b53373
[, ... filter<sub>N</sub>])
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Returns a filter that passes all data it receives through each of a
Packit b53373
series of given filters. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Filter<sub>1</sub></tt> to <tt>filter<sub>N</sub></tt> are simple
Packit b53373
filters. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns the chained filter.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The nesting of filters can be arbitrary. For instance, the useless filter
Packit b53373
below doesn't do anything but return the data that was passed to it,
Packit b53373
unaltered.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
-- load required modules
Packit b53373
local ltn12 = require("ltn12")
Packit b53373
local mime = require("mime")
Packit b53373
Packit b53373
-- create a silly identity filter
Packit b53373
id = ltn12.filter.chain(
Packit b53373
  mime.encode("quoted-printable"),
Packit b53373
  mime.encode("base64"),
Packit b53373
  mime.decode("base64"),
Packit b53373
  mime.decode("quoted-printable")
Packit b53373
)
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.filter.cycle(low [, ctx, extra])
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Returns a high-level filter that cycles though a low-level filter by
Packit b53373
passing it each chunk and updating a context between calls. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Low</tt> is the low-level filter to be cycled, 
Packit b53373
<tt>ctx</tt> is the initial context and <tt>extra</tt> is any extra
Packit b53373
argument the low-level filter might take.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns the high-level filter. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
-- load the ltn12 module
Packit b53373
local ltn12 = require("ltn12")
Packit b53373
Packit b53373
-- the base64 mime filter factory
Packit b53373
encodet['base64'] = function()
Packit b53373
    return ltn12.filter.cycle(b64, "")
Packit b53373
end
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Pumps

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.pump.all(source, sink)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Pumps all data from a <tt>source</tt> to a <tt>sink</tt>. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
If successful, the function returns a value that evaluates to
Packit b53373
<tt>true</tt>. In case
Packit b53373
of error, the function returns a <tt>false</tt> value, followed by an error message.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.pump.step(source, sink)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Pumps one chunk of data from a <tt>source</tt> to a <tt>sink</tt>. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
If successful, the function returns a value that evaluates to
Packit b53373
<tt>true</tt>. In case
Packit b53373
of error, the function returns a <tt>false</tt> value, followed by an error message.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Sinks

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.sink.chain(filter, sink)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates and returns a new sink that passes data through a <tt>filter</tt> before sending it to a given <tt>sink</tt>. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.sink.error(message)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates and returns a sink that aborts transmission with the error
Packit b53373
<tt>message</tt>.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.sink.file(handle, message)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates a sink that sends data to a file. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Handle</tt> is a file handle. If <tt>handle</tt> is <tt>nil</tt>, 
Packit b53373
<tt>message</tt> should give the reason for failure. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns a sink that sends all data to the given <tt>handle</tt>
Packit b53373
and closes the file when done, or a sink that aborts the transmission with
Packit b53373
the error <tt>message</tt>
Packit b53373

Packit b53373
Packit b53373

Packit b53373
In the following example, notice how the prototype is designed to 
Packit b53373
fit nicely with the <tt>io.open</tt> function.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
-- load the ltn12 module
Packit b53373
local ltn12 = require("ltn12")
Packit b53373
Packit b53373
-- copy a file
Packit b53373
ltn12.pump.all(
Packit b53373
  ltn12.source.file(io.open("original.png", "rb")),
Packit b53373
  ltn12.sink.file(io.open("copy.png", "wb"))
Packit b53373
)
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.sink.null()
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Returns a sink that ignores all data it receives. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.sink.simplify(sink)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates and returns a simple sink given a fancy <tt>sink</tt>. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.sink.table([table])
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates a sink that stores all chunks in a table. The chunks can later be
Packit b53373
efficiently concatenated into a single string.
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Table</tt> is used to hold the chunks. If
Packit b53373
<tt>nil</tt>, the function creates its own table. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns the sink and the table used to store the chunks. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
-- load needed modules
Packit b53373
local http = require("socket.http")
Packit b53373
local ltn12 = require("ltn12")
Packit b53373
Packit b53373
-- a simplified http.get function
Packit b53373
function http.get(u)
Packit b53373
  local t = {}
Packit b53373
  local respt = request{
Packit b53373
    url = u,
Packit b53373
    sink = ltn12.sink.table(t)
Packit b53373
  }
Packit b53373
  return table.concat(t), respt.headers, respt.code
Packit b53373
end
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Sources

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.source.cat(source<sub>1</sub> [, source<sub>2</sub>, ...,
Packit b53373
source<sub>N</sub>])
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates a new source that produces the concatenation of the data produced
Packit b53373
by a number of sources. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Source<sub>1</sub></tt> to <tt>source<sub>N</sub></tt> are the original
Packit b53373
sources. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns the new source.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.source.chain(source, filter)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates a new <tt>source</tt> that passes data through a <tt>filter</tt> 
Packit b53373
before returning it. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns the new source.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.source.empty()
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates and returns an empty source. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.source.error(message)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates and returns a source that aborts transmission with the error
Packit b53373
<tt>message</tt>.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.source.file(handle, message)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates a source that produces the contents of a file. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
<tt>Handle</tt> is a file handle. If <tt>handle</tt> is <tt>nil</tt>, 
Packit b53373
<tt>message</tt> should give the reason for failure. 
Packit b53373

Packit b53373
Packit b53373

Packit b53373
The function returns a source that reads chunks of data from 
Packit b53373
given <tt>handle</tt> and returns it to the user,
Packit b53373
closing the file when done, or a source that aborts the transmission with
Packit b53373
the error <tt>message</tt>
Packit b53373

Packit b53373
Packit b53373

Packit b53373
In the following example, notice how the prototype is designed to 
Packit b53373
fit nicely with the <tt>io.open</tt> function.
Packit b53373

Packit b53373
Packit b53373
Packit b53373
-- load the ltn12 module
Packit b53373
local ltn12 = require("ltn12")
Packit b53373
Packit b53373
-- copy a file
Packit b53373
ltn12.pump.all(
Packit b53373
  ltn12.source.file(io.open("original.png", "rb")),
Packit b53373
  ltn12.sink.file(io.open("copy.png", "wb"))
Packit b53373
)
Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.source.simplify(source)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates and returns a simple source given a fancy <tt>source</tt>. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
ltn12.source.string(string)
Packit b53373

Packit b53373
Packit b53373

Packit b53373
Creates and returns a source that produces the contents of a
Packit b53373
<tt>string</tt>, chunk by chunk. 
Packit b53373

Packit b53373
Packit b53373
Packit b53373
Packit b53373
Packit b53373

Packit b53373
<center>
Packit b53373

Packit b53373
home ·
Packit b53373
download ·
Packit b53373
installation ·
Packit b53373
introduction ·
Packit b53373
reference
Packit b53373

Packit b53373

Packit b53373
<small>
Packit b53373
Last modified by Diego Nehab on 
Packit b53373
Thu Apr 20 00:25:41 EDT 2006
Packit b53373
</small>
Packit b53373

Packit b53373
</center>
Packit b53373
Packit b53373
Packit b53373
</body>
Packit b53373
</html>