Working on the manual...

Making better tests for error messages.
Changed a few names.
Moved gethostname to inet.c.
This commit is contained in:
Diego Nehab 2004-01-24 02:47:24 +00:00
parent 0c9f420a35
commit 62a4c505e4
21 changed files with 341 additions and 315 deletions

6
TODO
View File

@ -1,4 +1,5 @@
manual
check all occurences of it's
add shutdown
add gethostname
the need of a content-length header in the post method...
@ -22,6 +23,11 @@ tests
checar garbage collection
check for interrupts
close has to block...
fmt is not a good name
change wrap() to accept a number and default to "character"
move gethostname to dns table
get rid of _cb in name of functions?
trust character constants in mime.c? noooooo.
smtp.lua needs stuff filter

View File

@ -45,113 +45,72 @@ callback mechanism outlined below.
</p>
<p>
Instead of returning the entire contents of an entity
as strings to the Lua application, the library allows the user to
Instead of returning the received contents
as a big to the Lua application, the library allows the user to
provide a <em>receive callback</em> that will be called with successive
chunks of data, as the data becomes available. Conversely, the <em>send
callbacks</em> can be used when the application wants to incrementally
provide LuaSocket with the data to be sent.
callback</em> mechanism can be used when the application wants to incrementally provide LuaSocket with the data to be sent.
</p>
<!-- tohostname +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id=receive_cb>
ret, err_or_f = <b>receive_cb(</b>chunk, err<b>)</b>
<p class=name id=receive>
<b>receive_cb(</b>chunk, err<b>)</b>
</p>
<p class=description>
The callback provided by the user will be repeatedly called by the
library whenever new data is available. Each time it is called, the
callback receives successive chunks of downloaded data.
A receive callback will be repeatedly called by
LuaSocket wheneve new data is available. Each time it is called, the
callback receives successive chunks of downloaded data.
</p>
<p class=parameters>
<tt>Chunk</tt> contains the current chunk of data.
When the transmission is over, the function is called with an
empty string (i.e.&nbsp;<tt>""</tt>) as the <tt>chunk</tt>.
If an error occurs, the function receives <tt>nil</tt>
as <tt>chunk</tt> and an error message in <tt>err</tt>.
<tt>Chunk</tt> contains the latest downloaded chunk of data.
When the transmission is over, the function is called with an
empty string (i.e.&nbsp;<tt>""</tt>) in <tt>chunk</tt>.
If an error occurs, the function receives a <b><tt>nil</tt></b>
<tt>chunk</tt> and an error message in <tt>err</tt>.
</p>
<p class=return>
The callback can abort transmission by returning <tt>nil</tt> as its first
return value, and an optional error message as the
second return value. If the application wants to continue receiving
data, the function should return non-<tt>nil</tt> as it's first return
value. In this case, the function can optionally return a
new callback function, to replace itself, as the second return value.
</p>
<p class=note>
Note: The <tt>callback</tt> module provides several standard receive callbacks, including the following:
</p>
<pre class=example>
function receive.concat(concat)
concat = concat or socket.concat.create()
local callback = function(chunk, err)
-- if not finished, add chunk
if chunk and chunk ~= "" then
concat:addstring(chunk)
return 1
end
end
return callback, concat
end
</pre>
<p class=note>
This function creates a new receive callback that concatenates all
received chunks into a the same concat object, which can later be
queried for its contents.
The callback can abort transmission by returning <b><tt>nil</tt></b> as its first
return value, and an optional error message as the
second return value. To continue receiving
data, the function should return non-<b><tt>nil</tt></b> as its first return
value. Optionally, in this case, it may return a
second return value, with a new callback function to take its place.
</p>
<!-- send_cb ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name>
chunk, err_or_r = <b>send_cb()</b>
<p class=name id=send>
<b>send_cb()</b>
</p>
<p class=description>
The callback provided by the user will be repeatedly called whenever the
library needs more data to be sent.
A send callback will be called whenever LuaSocket
needs more data to be sent.
</p>
<p class=return>
Each time the callback is called, it should return the next chunk of data. It
can optionally return, as it's second return value, a new callback to replace
itself. The callback can abort the process at any time by returning
<tt>nil</tt> followed by an optional error message.
<b><tt>nil</tt></b> followed by an optional error message.
</p>
<p class=note>
Note: Below is the implementation of the <tt>callback.send.file</tt>
function. Given an open file handle, it returns a send callback that will send the contents of that file, chunk by chunk.
</p>
<pre class=example>
function send.file(file, io_err)
-- if successful, return the callback that reads from the file
if file then
return function()
-- send next block of data
return (file:read(BLOCKSIZE)) or ""
end
-- else, return a callback that just aborts the transfer
else return fail(io_err or "unable to open file") end
end
</pre>
<!-- predefined +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<h2 id=predefined>Predefined Callbacks</h2>
<p>
The module <tt>callback.lua</tt> provides several predefined callbacks to
The Callback module provides several predefined callbacks to
perform the most common input/output operations. Callbacks are provided
that send and receive contents of files and strings. Furthermore,
composition functions are provided to chain callbacks with filters, such as
the filters defined in the <tt>mime.lua</tt> module.
the filters defined in the <a href=mime.html>MIME</a> module.
Together, these two modules provide a powerful interface to send and
receive information.
</p>
<!-- send.file ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@ -161,15 +120,16 @@ the filters defined in the <tt>mime.lua</tt> module.
</p>
<p class=description>
This function creates a send callback that will return the contents
of a the file, until the file has been entirely sent. When done, the
callback closes the file.
This function creates a send callback that returns the contents
of a file, chunk by chunk, until the file has been entirely sent.
When done, the callback closes the file.
</p>
<p class=parameters>
<tt>File</tt> is a file open for reading. If <tt>file</tt> is
<tt>nil</tt>, <tt>io_err</tt> can contain an error message and the
returned callback just aborts transmission with the error message.
<tt>File</tt> is a file opened for reading. If <tt>file</tt> is
<b><tt>nil</tt></b>, <tt>io_err</tt> can contain an error message. In this
case, the function returns a callback that just aborts
transmission with the error message.
</p>
<p class=return>
@ -177,14 +137,14 @@ Returns a send callback for the file.
</p>
<p class=note>
Note: The function is designed so that it directly accepts the return
Note: This function is designed so that it directly accepts the return
values of the <tt>io.open</tt> function.
</p>
<!-- send.string ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id=send.string>
<b>send.string(</b>str<b>)</b>
<b>send.string(</b>str, err<b>)</b>
</p>
<p class=description>
@ -193,29 +153,33 @@ the contents of a string.
</p>
<p class=parameters>
<tt>Str</tt> is the string to be sent.
<tt>Str</tt> is the string to be sent.
<!--
If <tt>str</tt> is
<b><tt>nil</tt></b>, <tt>err</tt> can optionally contain an error message.
-->
</p>
<p class=return>
Returns a send callback for the string, or <tt>nil</tt> if the string is
<tt>nil</tt>.
Returns a send callback for the string, or <b><tt>nil</tt></b> if the string is
<b><tt>nil</tt></b>.
</p>
<p class=note>
Note: If any function in the LuaSocket API receives a <tt>nil</tt>
send callback, it assumes there is nothing to be sent.
Note: A <tt>nil</tt></b>
send callback is equivalent to a callback that returns the empty string.
</p>
<!-- send.chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<p class=name id=send.string>
<p class=name id=send.chain>
<b>send.chain(</b>send_cb, filter<b>)</b>
</p>
<p class=description>
This function creates a send callback that will send
all data that it gets from another callback,
after passing the data through a filter.
This function creates a send callback that will filter
all the data it receives from another send callback, before
sending it through.
</p>
<p class=parameters>
@ -226,7 +190,29 @@ after passing the data through a filter.
Returns a callback chained with the filter.
</p>
(write a note!)
<p class=note>
Note: Several filters are defined in the <a href=mime.html>MIME</a>
module. Below is an example that creates a send callback that sends
a file's contents encoded in the Base64 transfer content encoding.
</p>
<pre class=example>
send_cb = socket.callback.send.chain(
socket.callback.send.file(io.open("input.bin"))
socket.mime.chain(
socket.mime.encode("base64"),
socket.mime.wrap("base64")
)
)
</pre>
<p class=note>
The call to <a href=mime.html#chain><tt>socket.mime.chain</tt></a>
creates a chained filter that encodes it's input and then breaks it
into lines. The call to <tt>socket.callback.chain</tt> creates a chained
send callback that reads the file from disk and passes it through the
filter before sending it.
</p>
<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->

View File

@ -59,7 +59,7 @@ Applies the Base 64 content coding to a string.
<p class=parameters>
<tt>Content</tt> is the string to be encoded.
If <tt>single</tt> is set to anything
but <tt>nil</tt>, the output is returned as a single
but <b><tt>nil</tt></b>, the output is returned as a single
line, otherwise the function splits the content into 76 character long
lines after encoding.
</p>

View File

@ -70,7 +70,7 @@ Converts from IP address to host name.
<p class=return>
The function a string with the canonic host name of the given
<tt>address</tt>, followed by a table with all information returned by
the resolver. In case of error, the function returns <tt>nil</tt>
the resolver. In case of error, the function returns <b><tt>nil</tt></b>
followed by an error message.
</p>
@ -92,7 +92,7 @@ Converts from host name to IP address.
<p class=return>
Returns a string with the first IP address found for <tt>address</tt>,
followed by a table with all information returned by the resolver.
In case of error, the function returns <tt>nil</tt> followed by an error
In case of error, the function returns <b><tt>nil</tt></b> followed by an error
message.
</p>

View File

@ -88,7 +88,7 @@ determines the transfer type. If <tt>&lt;path&gt;</tt> ends with a
<p class=return>
If successful, the function returns
the file content as a string. In case of error, the function returns
<tt>nil</tt> and an error message describing the error.
<b><tt>nil</tt></b> and an error message describing the error.
</p>
<pre class=example>
@ -165,7 +165,7 @@ function tries to log in as '<tt>anonymous</tt>'.
<p class=return>
If successful, the function returns 1. In case of error, the
function returns <tt>nil</tt> followed by a string describing the error.
function returns <b><tt>nil</tt></b> followed by a string describing the error.
</p>
<pre class=example>

View File

@ -203,7 +203,7 @@ request message. If authentication information is provided, the function
uses the Basic Authentication Scheme (see <a href="#authentication">note</a>)
to retrieve the document. <tt>User</tt> and <tt>password</tt> provided
explicitly override those given by the <tt>url</tt>. The <tt>stay</tt>
parameter, when set to anything but <tt>nil</tt>, prevents the function
parameter, when set to anything but <b><tt>nil</tt></b>, prevents the function
from automatically following 301 or 302 server redirect messages.
</p>
@ -333,7 +333,7 @@ Authentication Scheme (see <a href="#authentication">note</a>) to
retrieve the document. <tt>Request.user</tt> and
<tt>request.password</tt> override those given by the
<tt>request.url</tt>. The <tt>request.stay</tt> parameter, when set to
anything but <tt>nil</tt>, prevents the function from automatically
anything but <b><tt>nil</tt></b>, prevents the function from automatically
following 301 or 302 server redirect messages.
</p>

View File

@ -1,11 +1,19 @@
body { margin-left: 1em; margin-right: 1em; }
body {
margin-left: 1em;
margin-right: 1em;
font-family: "Verdana";
}
tt {
font-family: "Andale Mono", monospace;
}
h1, h2, h3, h4 { margin-left: 0em; }
p { margin-left: 1em; }
p.name {
font-family: monospace;
font-family: "Andale Mono", monospace;
padding-top: 1em;
margin-left: 0em;
}
@ -18,6 +26,8 @@ pre.example {
background: #ccc;
padding: 1em;
margin-left: 1em;
font-family: "Andale Mono", monospace;
font-size: small;
}
hr {

View File

@ -35,155 +35,173 @@
<h2>Reference</h2>
<!-- tcp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- callback +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table summary="TCP Index" class=index width=100%>
<colgroup> <col width="50%"> <col width="50%"> </colgroup>
<tr>
<td><ul>
<li><a href="tcp.html">TCP (socket.tcp)</a>
<ul>
<li><a href="tcp.html#accept">accept</a>
<li><a href="tcp.html#bind">bind</a>
<li><a href="tcp.html#close">close</a>
<li><a href="tcp.html#connect">connect</a>
<li><a href="tcp.html#getpeername">getpeername</a>
</ul>
</ul></td>
<td valign=top><ul>
<li><a href="tcp.html#getsockname">getsockname</a>
<li><a href="tcp.html#receive">receive</a>
<li><a href="tcp.html#send">send</a>
<li><a href="tcp.html#setoption">setoption</a>
<li><a href="tcp.html#settimeout">settimeout</a>
</ul></td>
</tr>
<blockquote>
<a href="callback.html">Callbacks (socket.callback)</a>
<blockquote>
<a href="callback.html#send">send</a>:
<a href="callback.html#send.chain">chain</a>,
<a href="callback.html#send.file">file</a>,
<a href="callback.html#send.string">string</a>.
</blockquote>
<blockquote>
<a href="callback.html#receive">receive</a>:
<a href="callback.html#receive.chain">chain</a>,
<a href="callback.html#receive.file">file</a>,
<a href="callback.html#receive.concat">concat</a>.
</blockquote>
</blockquote>
</table>
<hr>
<!-- dns ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<blockquote>
<a href="dns.html">DNS services (socket.dns)</a>
<blockquote>
<a href="dns.html#toip">toip</a>,
<a href="dns.html#tohostname">tohostname</a>,
<a href="dns.html#gethostname">gethostname</a>.
</blockquote>
</blockquote>
<!-- ftp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<blockquote>
<a href="ftp.html">FTP (socket.ftp)</a>
<blockquote>
<a href="ftp.html#get">get</a>,
<a href="ftp.html#get_cb">get_cb</a>,
<a href="ftp.html#put">put</a>,
<a href="ftp.html#put_cb">put_cb</a>.
</blockquote>
</blockquote>
<!-- misc ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<blockquote>
<a href="global.html">Globals (socket)</a>
<blockquote>
<a href="global.html#bind">bind</a>,
<a href="global.html#callback">callback</a>,
<a href="global.html#concat">concat</a>,
<a href="global.html#connect">connect</a>,
<a href="global.html#debug">debug</a>,
<a href="global.html#dns">dns</a>,
<a href="global.html#ftp">ftp</a>,
<a href="global.html#http">http</a>,
<a href="global.html#mime">mime</a>,
<a href="global.html#select">select</a>,
<a href="global.html#sleep">sleep</a>,
<a href="global.html#smtp">smtp</a>,
<a href="global.html#time">time</a>,
<a href="global.html#tcp">tcp</a>.
<a href="global.html#udp">udp</a>,
<a href="global.html#url">url</a>,
<a href="global.html#version">version</a>.
</blockquote>
</blockquote>
</table>
<!-- http +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<blockquote>
<a href="http.html">HTTP (socket.http)</a>
<blockquote>
<a href="http.html#get">get</a>,
<a href="http.html#post">post</a>,
<a href="http.html#request">request</a>,
<a href="http.html#request_cb">request_cb</a>.
</blockquote>
</blockquote>
<!-- mime +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<blockquote>
<a href="mime.html">MIME (socket.mime) </a>
<blockquote>
<a href="mime.html#filters">filters</a>:
<a href="mime.html#decode">canonic</a>,
<a href="mime.html#chain">chain</a>,
<a href="mime.html#decode">decode</a>,
<a href="mime.html#encode">encode</a>,
<a href="mime.html#wrap">wrap</a>.
</blockquote>
<blockquote>
<a href="mime.html#low-level">low-level</a>:
<a href="mime.html#b64">b64</a>,
<a href="mime.html#unb64">unb64</a>,
<a href="mime.html#eol">eol</a>,
<a href="mime.html#qp">qp</a>,
<a href="mime.html#unqp">unqp</a>,
<a href="mime.html#wrp">wrp</a>,
<a href="mime.html#qpwrp">qpwrp</a>.
</blockquote>
</blockquote>
<!-- smtp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<blockquote>
<a href="smtp.html">SMTP (socket.smtp)</a>
<blockquote>
<a href="smtp.html#mail">mail</a>
</blockquote>
</blockquote>
<!-- tcp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<blockquote>
<a href="tcp.html">TCP (socket.tcp)</a>
<blockquote>
<a href="tcp.html#accept">accept</a>,
<a href="tcp.html#bind">bind</a>,
<a href="tcp.html#close">close</a>,
<a href="tcp.html#connect">connect</a>,
<a href="tcp.html#getpeername">getpeername</a>,
<a href="tcp.html#getsockname">getsockname</a>,
<a href="tcp.html#receive">receive</a>,
<a href="tcp.html#send">send</a>,
<a href="tcp.html#setoption">setoption</a>,
<a href="tcp.html#settimeout">settimeout</a>,
<a href="tcp.html#shutdown">shutdown</a>.
</blockquote>
</blockquote>
<!-- udp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table summary="UDP Index" class=index width=100%>
<colgroup> <col width="50%"> <col width="50%"> </colgroup>
<tr>
<td><ul>
<li><a href="udp.html">UDP (socket.udp)</a>
<ul>
<li><a href="udp.html#close">close</a>
<li><a href="udp.html#getpeername">getpeername</a>
<li><a href="udp.html#getsockname">getsockname</a>
<li><a href="udp.html#receive">receive</a>
<li><a href="udp.html#receivefrom">receivefrom</a>
</ul>
</ul></td>
<td><ul>
<li><a href="udp.html#send">send</a>
<li><a href="udp.html#sendto">sendto</a>
<li><a href="udp.html#setpeername">setpeername</a>
<li><a href="udp.html#setsockname">setsockname</a>
<li><a href="udp.html#setoption">setoption</a>
<li><a href="udp.html#settimeout">settimeout</a>
</ul></td>
</tr>
</table>
<blockquote>
<a href="udp.html">UDP (socket.udp)</a>
<blockquote>
<a href="udp.html#close">close</a>,
<a href="udp.html#getpeername">getpeername</a>,
<a href="udp.html#getsockname">getsockname</a>,
<a href="udp.html#receive">receive</a>,
<a href="udp.html#receivefrom">receivefrom</a>,
<a href="udp.html#send">send</a>,
<a href="udp.html#sendto">sendto</a>,
<a href="udp.html#setpeername">setpeername</a>,
<a href="udp.html#setsockname">setsockname</a>,
<a href="udp.html#setoption">setoption</a>,
<a href="udp.html#settimeout">settimeout</a>,
<a href="udp.html#settimeout">shutdown</a>.
</blockquote>
</blockquote>
<hr>
<!-- url ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- http & ftp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table summary="HTTP and FTP Index" class=index width=100%>
<colgroup> <col width="50%"> <col width="50%"> </colgroup>
<tr>
<td valign=top><ul>
<li><a href="http.html">HTTP (socket.http)</a>
<ul>
<li><a href="http.html#get">get</a>
<li><a href="http.html#post">post</a>
<li><a href="http.html#request">request</a>
<li><a href="http.html#request_cb">request_cb</a>
</ul>
</ul></td>
<td valign=top><ul>
<li><a href="ftp.html">FTP (socket.ftp)</a>
<ul>
<li><a href="ftp.html#get">get</a>
<li><a href="ftp.html#get_cb">get_cb</a>
<li><a href="ftp.html#put">put</a>
<li><a href="ftp.html#put_cb">put_cb</a>
</ul>
</ul></td>
</tr>
</table>
<hr>
<!-- http & ftp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table summary="Streaming Index" class=index width=100%>
<colgroup> <col width="50%"> <col width="50%"> </colgroup>
<tr><td valign=top><ul>
<li><a href="stream.html">Streaming with Callbacks</a>
<ul>
<li><a href="stream.html#receive_cb">receive_cb</a>
<li><a href="stream.html#send_cb">send_cb</a>
</ul>
</ul></td></tr>
</table>
<hr>
<!-- smtp & dns ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table summary="SMTP and DNS Index" class=index width=100%>
<colgroup> <col width="50%"> <col width="50%"> </colgroup>
<tr>
<td><ul>
<li><a href="smtp.html">SMTP (socket.smtp)</a>
<ul>
<li> <a href="smtp.html#mail">mail</a>
</ul>
</ul></td>
<td><ul>
<li><a href="dns.html">DNS services (socket.dns)</a>
<ul>
<li><a href="dns.html#toip">toip</a>
<li><a href="dns.html#tohostname">tohostname</a>
</ul>
</ul></td>
</tr>
</table>
<hr>
<!-- url & code ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<table summary="URL and Code Index" class=index width=100%>
<colgroup> <col width="50%"> <col width="50%"> </colgroup>
<tr>
<td valign=top><ul>
<li><a href="url.html">URL (socket.url) </a>
<ul>
<li> <a href="url.html#absolute">absolute</a>
<li> <a href="url.html#build">build</a>
<li> <a href="url.html#build_path">build_path</a>
<li> <a href="url.html#parse">parse</a>
<li> <a href="url.html#parse_path">parse_path</a>
</ul>
</ul></td>
<td valign=top><ul>
<li> <a href="code.html">Code (socket.code) </a>
<ul>
<li> <a href="code.html#base64">base64</a>
<li> <a href="code.html#unbase64">unbase64</a>
<li> <a href="code.html#escape">escape</a>
<li> <a href="code.html#unescape">unescape</a>
<li> <a href="code.html#hexa">hexa</a>
<li> <a href="code.html#unhexa">unhexa</a>
</ul>
</ul></td>
</tr>
</table>
<blockquote>
<a href="url.html">URL (socket.url) </a>
<blockquote>
<a href="url.html#absolute">absolute</a>,
<a href="url.html#build">build</a>,
<a href="url.html#build_path">build_path</a>,
<a href="url.html#quote">quote</a>,
<a href="url.html#parse">parse</a>,
<a href="url.html#parse_path">parse_path</a>,
<a href="url.html#quote">unquote</a>.
</blockquote>
</blockquote>
<!-- footer ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->

View File

@ -105,7 +105,7 @@ and text <tt>body</tt>. The message is sent using the server
<p class=return>
If successful, the function returns 1. Otherwise, the function returns
<tt>nil</tt> followed by an error message.
<b><tt>nil</tt></b> followed by an error message.
</p>
<p class=note>

View File

@ -69,15 +69,15 @@ callback receives successive chunks of downloaded data.
<tt>Chunk</tt> contains the current chunk of data.
When the transmission is over, the function is called with an
empty string (i.e.&nbsp;<tt>""</tt>) as the <tt>chunk</tt>.
If an error occurs, the function receives <tt>nil</tt>
If an error occurs, the function receives <b><tt>nil</tt></b>
as <tt>chunk</tt> and an error message in <tt>err</tt>.
</p>
<p class=return>
The callback can abort transmission by returning <tt>nil</tt> as its first
The callback can abort transmission by returning <b><tt>nil</tt></b> as its first
return value, and an optional error message as the
second return value. If the application wants to continue receiving
data, the function should return non-<tt>nil</tt> as it's first return
data, the function should return non-<b><tt>nil</tt></b> as it's first return
value. In this case, the function can optionally return a
new callback function, to replace itself, as the second return value.
</p>
@ -121,7 +121,7 @@ library needs more data to be sent.
Each time the callback is called, it should return the next chunk of data. It
can optionally return, as it's second return value, a new callback to replace
itself. The callback can abort the process at any time by returning
<tt>nil</tt> followed by an optional error message.
<b><tt>nil</tt></b> followed by an optional error message.
</p>
<p class=note>

View File

@ -51,7 +51,7 @@ method.</p>
<p class=return>
In case of success, a new master object is returned. In case of error,
<tt>nil</tt> is returned, followed by an error message.
<b><tt>nil</tt></b> is returned, followed by an error message.
</p>
<!-- accept +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@ -67,7 +67,7 @@ object and returns a client object representing that connection.
<p class=return>
If a connection is successfully initiated, a client object is returned.
If a timeout condition is met, the method returns <tt>nil</tt> followed
If a timeout condition is met, the method returns <b><tt>nil</tt></b> followed
by the error string '<tt>timeout</tt>'.
</p>
@ -111,7 +111,7 @@ attempts connection, the connection is refused.
<p class=return>
In case of success, the method returns 1. In case of error, the
method returns <tt>nil</tt> followed by an error message.
method returns <b><tt>nil</tt></b> followed by an error message.
</p>
<p class=note>
@ -165,7 +165,7 @@ and <a href=#close><tt>close</tt></a>.
</p>
<p class=return>
In case of error, the method returns <tt>nil</tt> followed by a string
In case of error, the method returns <b><tt>nil</tt></b> followed by a string
describing the error. In case of success, the method returns 1.
</p>
@ -187,7 +187,7 @@ Returns information about the remote side of a connected client object.
<p class=return>
Returns a string with the IP address of the peer, followed by the
port number that peer is using for the connection.
In case of error, the method returns <tt>nil</tt>.
In case of error, the method returns <b><tt>nil</tt></b>.
</p>
<p class=note>
@ -207,7 +207,7 @@ Returns the local address information associated to the object.
<p class=return>
The method returns a string with local IP address and a number with
the port. In case of error, the method returns <tt>nil</tt>.
the port. In case of error, the method returns <b><tt>nil</tt></b>.
</p>
<p class=note>
@ -248,7 +248,7 @@ bytes from the socket.
<p class=return>
The method returns one value for each pattern, followed by a single
error code that can be <tt>nil</tt> in case of success, the string
error code that can be <b><tt>nil</tt></b> in case of success, the string
'<tt>closed</tt>' in case the connection was closed before the
transmission was completed or the string '<tt>timeout</tt>' in case
there was a timeout during the operation.
@ -278,7 +278,7 @@ result to LuaSocket instead of passing several independent strings.
<p class=return>
The method returns the number of bytes accepted by the transport layer,
followed by an error code. The error code is <tt>nil</tt> if the operation
followed by an error code. The error code is <b><tt>nil</tt></b> if the operation
completed with no errors, the string '<tt>closed</tt>' in case
the connection was closed before the transmission was completed or the
string '<tt>timeout</tt>' in case there was a timeout during the
@ -288,7 +288,7 @@ operation.
<p class=note>
Note: The return values for the <tt>send</tt> method have been changed in
LuaSocket 2.0! In previous versions, the method returned only the
error message. Since returning <tt>nil</tt> in case of success goes
error message. Since returning <b><tt>nil</tt></b> in case of success goes
against all other LuaSocket methods and functions, the
<tt>send</tt> method been changed for the sake of uniformity.
</p>
@ -330,7 +330,7 @@ considered broken and processes using the socket are notified.
</ul>
<p class=return>
The method returns 1 in case of success, or <tt>nil</tt> otherwise.
The method returns 1 in case of success, or <b><tt>nil</tt></b> otherwise.
</p>
<p class=note>
@ -374,7 +374,7 @@ a call.</li>
</ul>
<p class=parameters>
The <tt>nil</tt> timeout <tt>value</tt> allows operations to block
The <b><tt>nil</tt></b> timeout <tt>value</tt> allows operations to block
indefinitely. Negative timeout values have the same effect.
</p>

View File

@ -52,7 +52,7 @@ is used to connect the object.
<p class="return">
In case of success, a new unconnected UDP object
returned. In case of error, <tt>nil</tt> is returned, followed by
returned. In case of error, <b><tt>nil</tt></b> is returned, followed by
an error message.
</p>
@ -112,7 +112,7 @@ Returns the local address information associated to the object.
<p class="return">
The method returns a string with local IP
address and a number with the port. In case of error, the method
returns <tt>nil</tt>.
returns <b><tt>nil</tt></b>.
</p>
<p class="note">
@ -150,7 +150,7 @@ maximum datagram size is used.
<p class="return">
In case of success, the method return the
received datagram. In case of timeout, the method returns
<tt>nil</tt> followed by the string '<tt>timeout</tt>'.
<b><tt>nil</tt></b> followed by the string '<tt>timeout</tt>'.
</p>
<!-- receivefrom +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@ -183,7 +183,7 @@ Beware that the maximum datagram size for UDP is 576 bytes.
<p class="return">
If successful, the method returns 1. In case of
error, the method returns <tt>nil</tt> followed by the
error, the method returns <b><tt>nil</tt></b> followed by the
'<tt>refused</tt>' message.
</p>
@ -214,7 +214,7 @@ names are <em>not</em> allowed for performance reasons.
<p class="return">
If successful, the method returns 1. In case of
error, the method returns <tt>nil</tt> followed by the
error, the method returns <b><tt>nil</tt></b> followed by the
'<tt>refused</tt>' message.
</p>
@ -258,7 +258,7 @@ case, the <tt>port</tt> argument is ignored.
<p class="return">
In case of error the method returns
<tt>nil</tt> followed by an error message. In case of success, the
<b><tt>nil</tt></b> followed by an error message. In case of success, the
method returns 1.
</p>
@ -288,7 +288,7 @@ all local interfaces using the constant <tt>INADDR_ANY</tt>. If
<p class="return">
If successful, the method returns 1. In case of
error, the method returns <tt>nil</tt> followed by an error
error, the method returns <b><tt>nil</tt></b> followed by an error
message.
</p>
@ -328,7 +328,7 @@ socket.</li>
<p class="return">
The method returns 1 in case of success, or
<tt>nil</tt> followed by an error message otherwise.
<b><tt>nil</tt></b> followed by an error message otherwise.
</p>
<p class="note">
@ -356,7 +356,7 @@ give up and fail with an error code.
<p class="parameters">
The amount of time to wait is specified as
the <tt>value</tt> parameter, in seconds. The <tt>nil</tt> timeout
the <tt>value</tt> parameter, in seconds. The <b><tt>nil</tt></b> timeout
<tt>value</tt> allows operations to block indefinitely. Negative
timeout values have the same effect.
</p>

View File

@ -148,7 +148,7 @@ component.
<p class=parameters>
<tt>Segments</tt> is a list of strings with the <tt>&lt;segment&gt;</tt>
parts. If <tt>unsafe</tt> is anything but <tt>nil</tt>, reserved
parts. If <tt>unsafe</tt> is anything but <b><tt>nil</tt></b>, reserved
characters are left untouched.
</p>

View File

@ -1,6 +1,11 @@
local base64 = socket.mime.base64.encode()
local split = socket.mime.split()
local convert = socket.mime.chain(base64, split)
local convert
if arg and arg[1] == '-d' then
convert = socket.mime.decode("base64")
else
local base64 = socket.mime.encode("base64")
local wrap = socket.mime.wrap()
convert = socket.mime.chain(base64, wrap)
end
while 1 do
local chunk = io.read(4096)
io.write(convert(chunk))

View File

@ -19,11 +19,13 @@
static int inet_global_toip(lua_State *L);
static int inet_global_tohostname(lua_State *L);
static void inet_pushresolved(lua_State *L, struct hostent *hp);
static int inet_global_gethostname(lua_State *L);
/* DNS functions */
static luaL_reg func[] = {
{ "toip", inet_global_toip },
{ "tohostname", inet_global_tohostname },
{ "gethostname", inet_global_gethostname},
{ NULL, NULL}
};
@ -101,6 +103,25 @@ static int inet_global_tohostname(lua_State *L)
return 2;
}
/*-------------------------------------------------------------------------*\
* Gets the host name
\*-------------------------------------------------------------------------*/
static int inet_global_gethostname(lua_State *L)
{
char name[257];
name[256] = '\0';
if (gethostname(name, 256) < 0) {
lua_pushnil(L);
lua_pushstring(L, "gethostname failed");
return 2;
} else {
lua_pushstring(L, name);
return 1;
}
}
/*=========================================================================*\
* Lua methods
\*=========================================================================*/

View File

@ -38,15 +38,8 @@
/*=========================================================================*\
* Declarations
\*=========================================================================*/
static int global_gethostname(lua_State *L);
static int base_open(lua_State *L);
/* functions in library namespace */
static luaL_reg func[] = {
{"gethostname", global_gethostname},
{NULL, NULL}
};
/*-------------------------------------------------------------------------*\
* Setup basic stuff.
\*-------------------------------------------------------------------------*/
@ -70,29 +63,9 @@ static int base_open(lua_State *L)
lua_pushstring(L, "LUASOCKET_LIBNAME");
lua_pushstring(L, LUASOCKET_LIBNAME);
lua_settable(L, LUA_GLOBALSINDEX);
/* define library functions */
luaL_openlib(L, LUASOCKET_LIBNAME, func, 0);
lua_pop(L, 1);
return 0;
}
/*-------------------------------------------------------------------------*\
* Gets the host name
\*-------------------------------------------------------------------------*/
static int global_gethostname(lua_State *L)
{
char name[257];
name[256] = '\0';
if (gethostname(name, 256) < 0) {
lua_pushnil(L);
lua_pushstring(L, "gethostname failed");
return 2;
} else {
lua_pushstring(L, name);
return 1;
}
}
/*-------------------------------------------------------------------------*\
* Initializes all library modules.
\*-------------------------------------------------------------------------*/

View File

@ -27,12 +27,12 @@ static const char EQCRLF[3] = {'=', CR, LF};
/*=========================================================================*\
* Internal function prototypes.
\*=========================================================================*/
static int mime_global_fmt(lua_State *L);
static int mime_global_wrp(lua_State *L);
static int mime_global_b64(lua_State *L);
static int mime_global_unb64(lua_State *L);
static int mime_global_qp(lua_State *L);
static int mime_global_unqp(lua_State *L);
static int mime_global_qpfmt(lua_State *L);
static int mime_global_qpwrp(lua_State *L);
static int mime_global_eol(lua_State *L);
static void b64setup(UC *b64unbase);
@ -54,10 +54,10 @@ static luaL_reg func[] = {
{ "eol", mime_global_eol },
{ "qp", mime_global_qp },
{ "unqp", mime_global_unqp },
{ "qpfmt", mime_global_qpfmt },
{ "qpwrp", mime_global_qpwrp },
{ "b64", mime_global_b64 },
{ "unb64", mime_global_unb64 },
{ "fmt", mime_global_fmt },
{ "wrp", mime_global_wrp },
{ NULL, NULL }
};
@ -127,13 +127,13 @@ static const char *optlstring(lua_State *L, int n, const char *v, size_t *l)
\*=========================================================================*/
/*-------------------------------------------------------------------------*\
* Incrementaly breaks a string into lines
* A, n = fmt(l, B, length, marker)
* A, n = wrp(l, B, length, marker)
* A is a copy of B, broken into lines of at most 'length' bytes.
* 'l' is how many bytes are left for the first line of B.
* 'n' is the number of bytes left in the last line of A.
* Marker is the end-of-line marker.
\*-------------------------------------------------------------------------*/
static int mime_global_fmt(lua_State *L)
static int mime_global_wrp(lua_State *L)
{
size_t size = 0;
int left = (int) luaL_checknumber(L, 1);
@ -526,14 +526,14 @@ static int mime_global_unqp(lua_State *L)
/*-------------------------------------------------------------------------*\
* Incrementally breaks a quoted-printed string into lines
* A, n = qpfmt(l, B, length)
* A, n = qpwrp(l, B, length)
* A is a copy of B, broken into lines of at most 'length' bytes.
* 'l' is how many bytes are left for the first line of B.
* 'n' is the number of bytes left in the last line of A.
* There are two complications: lines can't be broken in the middle
* of an encoded =XX, and there might be line breaks already
\*-------------------------------------------------------------------------*/
static int mime_global_qpfmt(lua_State *L)
static int mime_global_qpwrp(lua_State *L)
{
size_t size = 0;
int left = (int) luaL_checknumber(L, 1);

View File

@ -19,7 +19,7 @@ local wt = {}
local function choose(table)
return function(method, ...)
local f = table[method or "nil"]
if not f then return nil, "unknown method (" .. tostring(method) .. ")"
if not f then error("unknown method (" .. tostring(method) .. ")", 3)
else return f(unpack(arg)) end
end
end
@ -37,7 +37,15 @@ end
-- function that choose the encoding, decoding or wrap algorithm
encode = choose(et)
decode = choose(dt)
wrap = choose(wt)
-- the wrap filter has default parameters
local cwt = choose(wt)
function wrap(...)
if not arg[1] or type(arg[1]) ~= "string" then
table.insert(arg, 1, "base64")
end
return cwt(unpack(arg))
end
-- define the encoding algorithms
et['base64'] = function()
@ -58,15 +66,14 @@ dt['quoted-printable'] = function()
end
-- define the wrap algorithms
wt['character'] = function(length)
wt['base64'] = function(length, marker)
length = length or 76
return cicle(fmt, length, length)
return cicle(wrp, length, length, marker)
end
wt['base64'] = wt['character']
wt['quoted-printable'] = function(length)
length = length or 76
return cicle(qpfmt, length, length)
return cicle(qpwrp, length, length)
end
-- define the end-of-line translation function

View File

@ -12,7 +12,7 @@ socket.http.TIMEOUT = 5
local t = socket.time()
host = host or "diego.student.dyn.cs.princeton.edu"
host = host or "diego-interface2.student.dyn.CS.Princeton.EDU"
proxy = proxy or "http://localhost:3128"
prefix = prefix or "/luasocket-test"
cgiprefix = cgiprefix or "/luasocket-test-cgi"

View File

@ -4,7 +4,7 @@ local qptest = "qptest.bin"
local eqptest = "qptest.bin2"
local dqptest = "qptest.bin3"
local b64test = "luasocket.exe"
local b64test = "luasocket"
local eb64test = "b64test.bin"
local db64test = "b64test.bin2"
@ -155,10 +155,10 @@ local function encode_b64test()
local e2 = socket.mime.encode("base64")
local e3 = socket.mime.encode("base64")
local e4 = socket.mime.encode("base64")
local sp4 = socket.mime.wrap("character")
local sp3 = socket.mime.wrap("character", 59)
local sp2 = socket.mime.wrap("character", 30)
local sp1 = socket.mime.wrap("character", 27)
local sp4 = socket.mime.wrap()
local sp3 = socket.mime.wrap(59)
local sp2 = socket.mime.wrap("base64", 30)
local sp1 = socket.mime.wrap(27)
local chain = socket.mime.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4)
transform(b64test, eb64test, chain)
end

View File

@ -378,7 +378,7 @@ end
------------------------------------------------------------------------
function accept_errors()
io.write("not listenning: ")
io.write("not listening: ")
local d, e = socket.bind("*", 0)
assert(d, e);
local c, e = socket.tcp();
@ -392,7 +392,7 @@ function accept_errors()
assert(c, e);
d:setfd(c:getfd())
local r, e = d:accept()
assert(not r and e == "not supported", e)
assert(not r and e == "not supported" or e == "not listening", e)
print("ok")
end