aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-24 02:47:24 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-24 02:47:24 +0000
commit62a4c505e488c714e8795ea85564504562d30301 (patch)
treef270f53f26905b7275cea172e52ebc269a41c695
parent0c9f420a3549df3fb331bb24157b65a3301641d4 (diff)
downloadluasocket-62a4c505e488c714e8795ea85564504562d30301.tar.gz
luasocket-62a4c505e488c714e8795ea85564504562d30301.tar.bz2
luasocket-62a4c505e488c714e8795ea85564504562d30301.zip
Working on the manual...
Making better tests for error messages. Changed a few names. Moved gethostname to inet.c.
-rw-r--r--TODO6
-rw-r--r--doc/callback.html160
-rw-r--r--doc/code.html2
-rw-r--r--doc/dns.html4
-rw-r--r--doc/ftp.html4
-rw-r--r--doc/http.html4
-rw-r--r--doc/reference.css14
-rw-r--r--doc/reference.html298
-rw-r--r--doc/smtp.html2
-rw-r--r--doc/stream.html8
-rw-r--r--doc/tcp.html22
-rw-r--r--doc/udp.html18
-rw-r--r--doc/url.html2
-rw-r--r--etc/b64.lua11
-rw-r--r--src/inet.c21
-rw-r--r--src/luasocket.c27
-rw-r--r--src/mime.c16
-rw-r--r--src/mime.lua19
-rw-r--r--test/httptest.lua2
-rw-r--r--test/mimetest.lua10
-rw-r--r--test/testclnt.lua4
21 files changed, 340 insertions, 314 deletions
diff --git a/TODO b/TODO
index f6a67f1..4dd0766 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,5 @@
1manual 1manual
2 check all occurences of it's
2 add shutdown 3 add shutdown
3 add gethostname 4 add gethostname
4 the need of a content-length header in the post method... 5 the need of a content-length header in the post method...
@@ -22,6 +23,11 @@ tests
22 checar garbage collection 23 checar garbage collection
23 check for interrupts 24 check for interrupts
24 25
26close has to block...
27fmt is not a good name
28change wrap() to accept a number and default to "character"
29move gethostname to dns table
30get rid of _cb in name of functions?
25trust character constants in mime.c? noooooo. 31trust character constants in mime.c? noooooo.
26smtp.lua needs stuff filter 32smtp.lua needs stuff filter
27 33
diff --git a/doc/callback.html b/doc/callback.html
index 2c7ecd5..94af8ff 100644
--- a/doc/callback.html
+++ b/doc/callback.html
@@ -45,113 +45,72 @@ callback mechanism outlined below.
45</p> 45</p>
46 46
47<p> 47<p>
48Instead of returning the entire contents of an entity 48Instead of returning the received contents
49as strings to the Lua application, the library allows the user to 49as a big to the Lua application, the library allows the user to
50provide a <em>receive callback</em> that will be called with successive 50provide a <em>receive callback</em> that will be called with successive
51chunks of data, as the data becomes available. Conversely, the <em>send 51chunks of data, as the data becomes available. Conversely, the <em>send
52callbacks</em> can be used when the application wants to incrementally 52callback</em> mechanism can be used when the application wants to incrementally provide LuaSocket with the data to be sent.
53provide LuaSocket with the data to be sent.
54</p> 53</p>
55 54
56<!-- tohostname +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 55<!-- tohostname +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
57 56
58<p class=name id=receive_cb> 57<p class=name id=receive>
59ret, err_or_f = <b>receive_cb(</b>chunk, err<b>)</b> 58<b>receive_cb(</b>chunk, err<b>)</b>
60</p> 59</p>
61 60
62<p class=description> 61<p class=description>
63The callback provided by the user will be repeatedly called by the 62A receive callback will be repeatedly called by
64library whenever new data is available. Each time it is called, the 63LuaSocket wheneve new data is available. Each time it is called, the
65callback receives successive chunks of downloaded data. 64callback receives successive chunks of downloaded data.
66</p> 65</p>
67 66
68<p class=parameters> 67<p class=parameters>
69<tt>Chunk</tt> contains the current chunk of data. 68<tt>Chunk</tt> contains the latest downloaded chunk of data.
70When the transmission is over, the function is called with an 69When the transmission is over, the function is called with an
71empty string (i.e.&nbsp;<tt>""</tt>) as the <tt>chunk</tt>. 70empty string (i.e.&nbsp;<tt>""</tt>) in <tt>chunk</tt>.
72If an error occurs, the function receives <tt>nil</tt> 71If an error occurs, the function receives a <b><tt>nil</tt></b>
73as <tt>chunk</tt> and an error message in <tt>err</tt>. 72<tt>chunk</tt> and an error message in <tt>err</tt>.
74</p> 73</p>
75 74
76<p class=return> 75<p class=return>
77The callback can abort transmission by returning <tt>nil</tt> as its first 76The callback can abort transmission by returning <b><tt>nil</tt></b> as its first
78return value, and an optional error message as the 77return value, and an optional error message as the
79second return value. If the application wants to continue receiving 78second return value. To continue receiving
80data, the function should return non-<tt>nil</tt> as it's first return 79data, the function should return non-<b><tt>nil</tt></b> as its first return
81value. In this case, the function can optionally return a 80value. Optionally, in this case, it may return a
82new callback function, to replace itself, as the second return value. 81second return value, with a new callback function to take its place.
83</p>
84
85<p class=note>
86Note: The <tt>callback</tt> module provides several standard receive callbacks, including the following:
87</p>
88
89<pre class=example>
90function receive.concat(concat)
91 concat = concat or socket.concat.create()
92 local callback = function(chunk, err)
93 -- if not finished, add chunk
94 if chunk and chunk ~= "" then
95 concat:addstring(chunk)
96 return 1
97 end
98 end
99 return callback, concat
100end
101</pre>
102
103<p class=note>
104This function creates a new receive callback that concatenates all
105received chunks into a the same concat object, which can later be
106queried for its contents.
107</p> 82</p>
108 83
109<!-- send_cb ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 84<!-- send_cb ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
110 85
111<p class=name> 86<p class=name id=send>
112chunk, err_or_r = <b>send_cb()</b> 87<b>send_cb()</b>
113</p> 88</p>
114 89
115<p class=description> 90<p class=description>
116The callback provided by the user will be repeatedly called whenever the 91A send callback will be called whenever LuaSocket
117library needs more data to be sent. 92needs more data to be sent.
118</p> 93</p>
119 94
120<p class=return> 95<p class=return>
121Each time the callback is called, it should return the next chunk of data. It 96Each time the callback is called, it should return the next chunk of data. It
122can optionally return, as it's second return value, a new callback to replace 97can optionally return, as it's second return value, a new callback to replace
123itself. The callback can abort the process at any time by returning 98itself. The callback can abort the process at any time by returning
124<tt>nil</tt> followed by an optional error message. 99<b><tt>nil</tt></b> followed by an optional error message.
125</p>
126
127<p class=note>
128Note: Below is the implementation of the <tt>callback.send.file</tt>
129function. Given an open file handle, it returns a send callback that will send the contents of that file, chunk by chunk.
130</p> 100</p>
131 101
132<pre class=example>
133function send.file(file, io_err)
134 -- if successful, return the callback that reads from the file
135 if file then
136 return function()
137 -- send next block of data
138 return (file:read(BLOCKSIZE)) or ""
139 end
140 -- else, return a callback that just aborts the transfer
141 else return fail(io_err or "unable to open file") end
142end
143</pre>
144
145<!-- predefined +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 102<!-- predefined +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
146 103
147<h2 id=predefined>Predefined Callbacks</h2> 104<h2 id=predefined>Predefined Callbacks</h2>
148 105
149<p> 106<p>
150The module <tt>callback.lua</tt> provides several predefined callbacks to 107The Callback module provides several predefined callbacks to
151perform the most common input/output operations. Callbacks are provided 108perform the most common input/output operations. Callbacks are provided
152that send and receive contents of files and strings. Furthermore, 109that send and receive contents of files and strings. Furthermore,
153composition functions are provided to chain callbacks with filters, such as 110composition functions are provided to chain callbacks with filters, such as
154the filters defined in the <tt>mime.lua</tt> module. 111the filters defined in the <a href=mime.html>MIME</a> module.
112Together, these two modules provide a powerful interface to send and
113receive information.
155</p> 114</p>
156 115
157<!-- send.file ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 116<!-- send.file ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@@ -161,15 +120,16 @@ the filters defined in the <tt>mime.lua</tt> module.
161</p> 120</p>
162 121
163<p class=description> 122<p class=description>
164This function creates a send callback that will return the contents 123This function creates a send callback that returns the contents
165of a the file, until the file has been entirely sent. When done, the 124of a file, chunk by chunk, until the file has been entirely sent.
166callback closes the file. 125When done, the callback closes the file.
167</p> 126</p>
168 127
169<p class=parameters> 128<p class=parameters>
170<tt>File</tt> is a file open for reading. If <tt>file</tt> is 129<tt>File</tt> is a file opened for reading. If <tt>file</tt> is
171<tt>nil</tt>, <tt>io_err</tt> can contain an error message and the 130<b><tt>nil</tt></b>, <tt>io_err</tt> can contain an error message. In this
172returned callback just aborts transmission with the error message. 131case, the function returns a callback that just aborts
132transmission with the error message.
173</p> 133</p>
174 134
175<p class=return> 135<p class=return>
@@ -177,14 +137,14 @@ Returns a send callback for the file.
177</p> 137</p>
178 138
179<p class=note> 139<p class=note>
180Note: The function is designed so that it directly accepts the return 140Note: This function is designed so that it directly accepts the return
181values of the <tt>io.open</tt> function. 141values of the <tt>io.open</tt> function.
182</p> 142</p>
183 143
184<!-- send.string ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 144<!-- send.string ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
185 145
186<p class=name id=send.string> 146<p class=name id=send.string>
187<b>send.string(</b>str<b>)</b> 147<b>send.string(</b>str, err<b>)</b>
188</p> 148</p>
189 149
190<p class=description> 150<p class=description>
@@ -193,29 +153,33 @@ the contents of a string.
193</p> 153</p>
194 154
195<p class=parameters> 155<p class=parameters>
196<tt>Str</tt> is the string to be sent. 156<tt>Str</tt> is the string to be sent.
157<!--
158If <tt>str</tt> is
159<b><tt>nil</tt></b>, <tt>err</tt> can optionally contain an error message.
160-->
197</p> 161</p>
198 162
199<p class=return> 163<p class=return>
200Returns a send callback for the string, or <tt>nil</tt> if the string is 164Returns a send callback for the string, or <b><tt>nil</tt></b> if the string is
201<tt>nil</tt>. 165<b><tt>nil</tt></b>.
202</p> 166</p>
203 167
204<p class=note> 168<p class=note>
205Note: If any function in the LuaSocket API receives a <tt>nil</tt> 169Note: A <tt>nil</tt></b>
206send callback, it assumes there is nothing to be sent. 170send callback is equivalent to a callback that returns the empty string.
207</p> 171</p>
208 172
209<!-- send.chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 173<!-- send.chain ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
210 174
211<p class=name id=send.string> 175<p class=name id=send.chain>
212<b>send.chain(</b>send_cb, filter<b>)</b> 176<b>send.chain(</b>send_cb, filter<b>)</b>
213</p> 177</p>
214 178
215<p class=description> 179<p class=description>
216This function creates a send callback that will send 180This function creates a send callback that will filter
217all data that it gets from another callback, 181all the data it receives from another send callback, before
218after passing the data through a filter. 182sending it through.
219</p> 183</p>
220 184
221<p class=parameters> 185<p class=parameters>
@@ -226,7 +190,29 @@ after passing the data through a filter.
226Returns a callback chained with the filter. 190Returns a callback chained with the filter.
227</p> 191</p>
228 192
229(write a note!) 193<p class=note>
194Note: Several filters are defined in the <a href=mime.html>MIME</a>
195module. Below is an example that creates a send callback that sends
196a file's contents encoded in the Base64 transfer content encoding.
197</p>
198
199<pre class=example>
200send_cb = socket.callback.send.chain(
201 socket.callback.send.file(io.open("input.bin"))
202 socket.mime.chain(
203 socket.mime.encode("base64"),
204 socket.mime.wrap("base64")
205 )
206)
207</pre>
208
209<p class=note>
210The call to <a href=mime.html#chain><tt>socket.mime.chain</tt></a>
211creates a chained filter that encodes it's input and then breaks it
212into lines. The call to <tt>socket.callback.chain</tt> creates a chained
213send callback that reads the file from disk and passes it through the
214filter before sending it.
215</p>
230 216
231<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 217<!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
232 218
diff --git a/doc/code.html b/doc/code.html
index 45fd21a..4668b7a 100644
--- a/doc/code.html
+++ b/doc/code.html
@@ -59,7 +59,7 @@ Applies the Base 64 content coding to a string.
59<p class=parameters> 59<p class=parameters>
60<tt>Content</tt> is the string to be encoded. 60<tt>Content</tt> is the string to be encoded.
61If <tt>single</tt> is set to anything 61If <tt>single</tt> is set to anything
62but <tt>nil</tt>, the output is returned as a single 62but <b><tt>nil</tt></b>, the output is returned as a single
63line, otherwise the function splits the content into 76 character long 63line, otherwise the function splits the content into 76 character long
64lines after encoding. 64lines after encoding.
65</p> 65</p>
diff --git a/doc/dns.html b/doc/dns.html
index 6abf341..17cee45 100644
--- a/doc/dns.html
+++ b/doc/dns.html
@@ -70,7 +70,7 @@ Converts from IP address to host name.
70<p class=return> 70<p class=return>
71The function a string with the canonic host name of the given 71The function a string with the canonic host name of the given
72<tt>address</tt>, followed by a table with all information returned by 72<tt>address</tt>, followed by a table with all information returned by
73the resolver. In case of error, the function returns <tt>nil</tt> 73the resolver. In case of error, the function returns <b><tt>nil</tt></b>
74followed by an error message. 74followed by an error message.
75</p> 75</p>
76 76
@@ -92,7 +92,7 @@ Converts from host name to IP address.
92<p class=return> 92<p class=return>
93Returns a string with the first IP address found for <tt>address</tt>, 93Returns a string with the first IP address found for <tt>address</tt>,
94followed by a table with all information returned by the resolver. 94followed by a table with all information returned by the resolver.
95In case of error, the function returns <tt>nil</tt> followed by an error 95In case of error, the function returns <b><tt>nil</tt></b> followed by an error
96message. 96message.
97</p> 97</p>
98 98
diff --git a/doc/ftp.html b/doc/ftp.html
index 8072afe..6776a17 100644
--- a/doc/ftp.html
+++ b/doc/ftp.html
@@ -88,7 +88,7 @@ determines the transfer type. If <tt>&lt;path&gt;</tt> ends with a
88<p class=return> 88<p class=return>
89If successful, the function returns 89If successful, the function returns
90the file content as a string. In case of error, the function returns 90the file content as a string. In case of error, the function returns
91<tt>nil</tt> and an error message describing the error. 91<b><tt>nil</tt></b> and an error message describing the error.
92</p> 92</p>
93 93
94<pre class=example> 94<pre class=example>
@@ -165,7 +165,7 @@ function tries to log in as '<tt>anonymous</tt>'.
165 165
166<p class=return> 166<p class=return>
167If successful, the function returns 1. In case of error, the 167If successful, the function returns 1. In case of error, the
168function returns <tt>nil</tt> followed by a string describing the error. 168function returns <b><tt>nil</tt></b> followed by a string describing the error.
169</p> 169</p>
170 170
171<pre class=example> 171<pre class=example>
diff --git a/doc/http.html b/doc/http.html
index b7469a4..f977ea9 100644
--- a/doc/http.html
+++ b/doc/http.html
@@ -203,7 +203,7 @@ request message. If authentication information is provided, the function
203uses the Basic Authentication Scheme (see <a href="#authentication">note</a>) 203uses the Basic Authentication Scheme (see <a href="#authentication">note</a>)
204to retrieve the document. <tt>User</tt> and <tt>password</tt> provided 204to retrieve the document. <tt>User</tt> and <tt>password</tt> provided
205explicitly override those given by the <tt>url</tt>. The <tt>stay</tt> 205explicitly override those given by the <tt>url</tt>. The <tt>stay</tt>
206parameter, when set to anything but <tt>nil</tt>, prevents the function 206parameter, when set to anything but <b><tt>nil</tt></b>, prevents the function
207from automatically following 301 or 302 server redirect messages. 207from automatically following 301 or 302 server redirect messages.
208</p> 208</p>
209 209
@@ -333,7 +333,7 @@ Authentication Scheme (see <a href="#authentication">note</a>) to
333retrieve the document. <tt>Request.user</tt> and 333retrieve the document. <tt>Request.user</tt> and
334<tt>request.password</tt> override those given by the 334<tt>request.password</tt> override those given by the
335<tt>request.url</tt>. The <tt>request.stay</tt> parameter, when set to 335<tt>request.url</tt>. The <tt>request.stay</tt> parameter, when set to
336anything but <tt>nil</tt>, prevents the function from automatically 336anything but <b><tt>nil</tt></b>, prevents the function from automatically
337following 301 or 302 server redirect messages. 337following 301 or 302 server redirect messages.
338</p> 338</p>
339 339
diff --git a/doc/reference.css b/doc/reference.css
index 2a8987e..607ac4f 100644
--- a/doc/reference.css
+++ b/doc/reference.css
@@ -1,11 +1,19 @@
1body { margin-left: 1em; margin-right: 1em; } 1body {
2 margin-left: 1em;
3 margin-right: 1em;
4 font-family: "Verdana";
5}
6
7tt {
8 font-family: "Andale Mono", monospace;
9}
2 10
3h1, h2, h3, h4 { margin-left: 0em; } 11h1, h2, h3, h4 { margin-left: 0em; }
4 12
5p { margin-left: 1em; } 13p { margin-left: 1em; }
6 14
7p.name { 15p.name {
8 font-family: monospace; 16 font-family: "Andale Mono", monospace;
9 padding-top: 1em; 17 padding-top: 1em;
10 margin-left: 0em; 18 margin-left: 0em;
11} 19}
@@ -18,6 +26,8 @@ pre.example {
18 background: #ccc; 26 background: #ccc;
19 padding: 1em; 27 padding: 1em;
20 margin-left: 1em; 28 margin-left: 1em;
29 font-family: "Andale Mono", monospace;
30 font-size: small;
21} 31}
22 32
23hr { 33hr {
diff --git a/doc/reference.html b/doc/reference.html
index 99b1ea7..0bfd378 100644
--- a/doc/reference.html
+++ b/doc/reference.html
@@ -35,155 +35,173 @@
35 35
36<h2>Reference</h2> 36<h2>Reference</h2>
37 37
38<!-- tcp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 38<!-- callback +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
39 39
40<table summary="TCP Index" class=index width=100%> 40<blockquote>
41<colgroup> <col width="50%"> <col width="50%"> </colgroup> 41<a href="callback.html">Callbacks (socket.callback)</a>
42<tr> 42<blockquote>
43<td><ul> 43<a href="callback.html#send">send</a>:
44<li><a href="tcp.html">TCP (socket.tcp)</a> 44<a href="callback.html#send.chain">chain</a>,
45 <ul> 45<a href="callback.html#send.file">file</a>,
46 <li><a href="tcp.html#accept">accept</a> 46<a href="callback.html#send.string">string</a>.
47 <li><a href="tcp.html#bind">bind</a> 47</blockquote>
48 <li><a href="tcp.html#close">close</a> 48<blockquote>
49 <li><a href="tcp.html#connect">connect</a> 49<a href="callback.html#receive">receive</a>:
50 <li><a href="tcp.html#getpeername">getpeername</a> 50<a href="callback.html#receive.chain">chain</a>,
51 </ul> 51<a href="callback.html#receive.file">file</a>,
52</ul></td> 52<a href="callback.html#receive.concat">concat</a>.
53<td valign=top><ul> 53</blockquote>
54 <li><a href="tcp.html#getsockname">getsockname</a> 54</blockquote>
55 <li><a href="tcp.html#receive">receive</a>
56 <li><a href="tcp.html#send">send</a>
57 <li><a href="tcp.html#setoption">setoption</a>
58 <li><a href="tcp.html#settimeout">settimeout</a>
59</ul></td>
60</tr>
61</table> 55</table>
62 56
63<hr> 57<!-- dns ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
64 58
65<!-- udp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 59<blockquote>
66 60<a href="dns.html">DNS services (socket.dns)</a>
67<table summary="UDP Index" class=index width=100%> 61<blockquote>
68<colgroup> <col width="50%"> <col width="50%"> </colgroup> 62<a href="dns.html#toip">toip</a>,
69<tr> 63<a href="dns.html#tohostname">tohostname</a>,
70<td><ul> 64<a href="dns.html#gethostname">gethostname</a>.
71<li><a href="udp.html">UDP (socket.udp)</a> 65</blockquote>
72 <ul> 66</blockquote>
73 <li><a href="udp.html#close">close</a> 67
74 <li><a href="udp.html#getpeername">getpeername</a> 68<!-- ftp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
75 <li><a href="udp.html#getsockname">getsockname</a> 69
76 <li><a href="udp.html#receive">receive</a> 70<blockquote>
77 <li><a href="udp.html#receivefrom">receivefrom</a> 71<a href="ftp.html">FTP (socket.ftp)</a>
78 </ul> 72<blockquote>
79</ul></td> 73<a href="ftp.html#get">get</a>,
80<td><ul> 74<a href="ftp.html#get_cb">get_cb</a>,
81 <li><a href="udp.html#send">send</a> 75<a href="ftp.html#put">put</a>,
82 <li><a href="udp.html#sendto">sendto</a> 76<a href="ftp.html#put_cb">put_cb</a>.
83 <li><a href="udp.html#setpeername">setpeername</a> 77</blockquote>
84 <li><a href="udp.html#setsockname">setsockname</a> 78</blockquote>
85 <li><a href="udp.html#setoption">setoption</a> 79
86 <li><a href="udp.html#settimeout">settimeout</a> 80<!-- misc ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
87</ul></td> 81
88</tr> 82<blockquote>
89</table> 83<a href="global.html">Globals (socket)</a>
90 84<blockquote>
91<hr> 85<a href="global.html#bind">bind</a>,
92 86<a href="global.html#callback">callback</a>,
93<!-- http & ftp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 87<a href="global.html#concat">concat</a>,
94 88<a href="global.html#connect">connect</a>,
95<table summary="HTTP and FTP Index" class=index width=100%> 89<a href="global.html#debug">debug</a>,
96<colgroup> <col width="50%"> <col width="50%"> </colgroup> 90<a href="global.html#dns">dns</a>,
97<tr> 91<a href="global.html#ftp">ftp</a>,
98<td valign=top><ul> 92<a href="global.html#http">http</a>,
99<li><a href="http.html">HTTP (socket.http)</a> 93<a href="global.html#mime">mime</a>,
100 <ul> 94<a href="global.html#select">select</a>,
101 <li><a href="http.html#get">get</a> 95<a href="global.html#sleep">sleep</a>,
102 <li><a href="http.html#post">post</a> 96<a href="global.html#smtp">smtp</a>,
103 <li><a href="http.html#request">request</a> 97<a href="global.html#time">time</a>,
104 <li><a href="http.html#request_cb">request_cb</a> 98<a href="global.html#tcp">tcp</a>.
105 </ul> 99<a href="global.html#udp">udp</a>,
106</ul></td> 100<a href="global.html#url">url</a>,
107<td valign=top><ul> 101<a href="global.html#version">version</a>.
108<li><a href="ftp.html">FTP (socket.ftp)</a> 102</blockquote>
109 <ul> 103</blockquote>
110 <li><a href="ftp.html#get">get</a>
111 <li><a href="ftp.html#get_cb">get_cb</a>
112 <li><a href="ftp.html#put">put</a>
113 <li><a href="ftp.html#put_cb">put_cb</a>
114 </ul>
115</ul></td>
116</tr>
117</table> 104</table>
118 105
119<hr>
120 106
121<!-- http & ftp ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 107<!-- http +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
122 108
123<table summary="Streaming Index" class=index width=100%> 109<blockquote>
124<colgroup> <col width="50%"> <col width="50%"> </colgroup> 110<a href="http.html">HTTP (socket.http)</a>
125<tr><td valign=top><ul> 111<blockquote>
126<li><a href="stream.html">Streaming with Callbacks</a> 112<a href="http.html#get">get</a>,
127 <ul> 113<a href="http.html#post">post</a>,
128 <li><a href="stream.html#receive_cb">receive_cb</a> 114<a href="http.html#request">request</a>,
129 <li><a href="stream.html#send_cb">send_cb</a> 115<a href="http.html#request_cb">request_cb</a>.
130 </ul> 116</blockquote>
131</ul></td></tr> 117</blockquote>
132</table> 118
119<!-- mime +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
120
121<blockquote>
122<a href="mime.html">MIME (socket.mime) </a>
123<blockquote>
124<a href="mime.html#filters">filters</a>:
125<a href="mime.html#decode">canonic</a>,
126<a href="mime.html#chain">chain</a>,
127<a href="mime.html#decode">decode</a>,
128<a href="mime.html#encode">encode</a>,
129<a href="mime.html#wrap">wrap</a>.
130</blockquote>
131<blockquote>
132<a href="mime.html#low-level">low-level</a>:
133<a href="mime.html#b64">b64</a>,
134<a href="mime.html#unb64">unb64</a>,
135<a href="mime.html#eol">eol</a>,
136<a href="mime.html#qp">qp</a>,
137<a href="mime.html#unqp">unqp</a>,
138<a href="mime.html#wrp">wrp</a>,
139<a href="mime.html#qpwrp">qpwrp</a>.
140</blockquote>
141</blockquote>
142
143<!-- smtp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
144
145<blockquote>
146<a href="smtp.html">SMTP (socket.smtp)</a>
147<blockquote>
148<a href="smtp.html#mail">mail</a>
149</blockquote>
150</blockquote>
133 151
134<hr> 152<!-- tcp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
135 153
136<!-- smtp & dns ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 154<blockquote>
137 155<a href="tcp.html">TCP (socket.tcp)</a>
138<table summary="SMTP and DNS Index" class=index width=100%> 156<blockquote>
139<colgroup> <col width="50%"> <col width="50%"> </colgroup> 157<a href="tcp.html#accept">accept</a>,
140<tr> 158<a href="tcp.html#bind">bind</a>,
141<td><ul> 159<a href="tcp.html#close">close</a>,
142<li><a href="smtp.html">SMTP (socket.smtp)</a> 160<a href="tcp.html#connect">connect</a>,
143 <ul> 161<a href="tcp.html#getpeername">getpeername</a>,
144 <li> <a href="smtp.html#mail">mail</a> 162<a href="tcp.html#getsockname">getsockname</a>,
145 </ul> 163<a href="tcp.html#receive">receive</a>,
146</ul></td> 164<a href="tcp.html#send">send</a>,
147<td><ul> 165<a href="tcp.html#setoption">setoption</a>,
148<li><a href="dns.html">DNS services (socket.dns)</a> 166<a href="tcp.html#settimeout">settimeout</a>,
149 <ul> 167<a href="tcp.html#shutdown">shutdown</a>.
150 <li><a href="dns.html#toip">toip</a> 168</blockquote>
151 <li><a href="dns.html#tohostname">tohostname</a> 169</blockquote>
152 </ul>
153</ul></td>
154</tr>
155</table>
156 170
157<hr> 171<!-- udp +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
158 172
159<!-- url & code ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 173<blockquote>
160 174<a href="udp.html">UDP (socket.udp)</a>
161<table summary="URL and Code Index" class=index width=100%> 175<blockquote>
162<colgroup> <col width="50%"> <col width="50%"> </colgroup> 176<a href="udp.html#close">close</a>,
163<tr> 177<a href="udp.html#getpeername">getpeername</a>,
164<td valign=top><ul> 178<a href="udp.html#getsockname">getsockname</a>,
165<li><a href="url.html">URL (socket.url) </a> 179<a href="udp.html#receive">receive</a>,
166 <ul> 180<a href="udp.html#receivefrom">receivefrom</a>,
167 <li> <a href="url.html#absolute">absolute</a> 181<a href="udp.html#send">send</a>,
168 <li> <a href="url.html#build">build</a> 182<a href="udp.html#sendto">sendto</a>,
169 <li> <a href="url.html#build_path">build_path</a> 183<a href="udp.html#setpeername">setpeername</a>,
170 <li> <a href="url.html#parse">parse</a> 184<a href="udp.html#setsockname">setsockname</a>,
171 <li> <a href="url.html#parse_path">parse_path</a> 185<a href="udp.html#setoption">setoption</a>,
172 </ul> 186<a href="udp.html#settimeout">settimeout</a>,
173</ul></td> 187<a href="udp.html#settimeout">shutdown</a>.
174<td valign=top><ul> 188</blockquote>
175<li> <a href="code.html">Code (socket.code) </a> 189</blockquote>
176 <ul> 190
177 <li> <a href="code.html#base64">base64</a> 191<!-- url ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
178 <li> <a href="code.html#unbase64">unbase64</a> 192
179 <li> <a href="code.html#escape">escape</a> 193<blockquote>
180 <li> <a href="code.html#unescape">unescape</a> 194<a href="url.html">URL (socket.url) </a>
181 <li> <a href="code.html#hexa">hexa</a> 195<blockquote>
182 <li> <a href="code.html#unhexa">unhexa</a> 196<a href="url.html#absolute">absolute</a>,
183 </ul> 197<a href="url.html#build">build</a>,
184</ul></td> 198<a href="url.html#build_path">build_path</a>,
185</tr> 199<a href="url.html#quote">quote</a>,
186</table> 200<a href="url.html#parse">parse</a>,
201<a href="url.html#parse_path">parse_path</a>,
202<a href="url.html#quote">unquote</a>.
203</blockquote>
204</blockquote>
187 205
188<!-- footer ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 206<!-- footer ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
189 207
diff --git a/doc/smtp.html b/doc/smtp.html
index aa92149..0862de0 100644
--- a/doc/smtp.html
+++ b/doc/smtp.html
@@ -105,7 +105,7 @@ and text <tt>body</tt>. The message is sent using the server
105 105
106<p class=return> 106<p class=return>
107If successful, the function returns 1. Otherwise, the function returns 107If successful, the function returns 1. Otherwise, the function returns
108<tt>nil</tt> followed by an error message. 108<b><tt>nil</tt></b> followed by an error message.
109</p> 109</p>
110 110
111<p class=note> 111<p class=note>
diff --git a/doc/stream.html b/doc/stream.html
index b88cbb5..585ad18 100644
--- a/doc/stream.html
+++ b/doc/stream.html
@@ -69,15 +69,15 @@ callback receives successive chunks of downloaded data.
69<tt>Chunk</tt> contains the current chunk of data. 69<tt>Chunk</tt> contains the current chunk of data.
70When the transmission is over, the function is called with an 70When the transmission is over, the function is called with an
71empty string (i.e.&nbsp;<tt>""</tt>) as the <tt>chunk</tt>. 71empty string (i.e.&nbsp;<tt>""</tt>) as the <tt>chunk</tt>.
72If an error occurs, the function receives <tt>nil</tt> 72If an error occurs, the function receives <b><tt>nil</tt></b>
73as <tt>chunk</tt> and an error message in <tt>err</tt>. 73as <tt>chunk</tt> and an error message in <tt>err</tt>.
74</p> 74</p>
75 75
76<p class=return> 76<p class=return>
77The callback can abort transmission by returning <tt>nil</tt> as its first 77The callback can abort transmission by returning <b><tt>nil</tt></b> as its first
78return value, and an optional error message as the 78return value, and an optional error message as the
79second return value. If the application wants to continue receiving 79second return value. If the application wants to continue receiving
80data, the function should return non-<tt>nil</tt> as it's first return 80data, the function should return non-<b><tt>nil</tt></b> as it's first return
81value. In this case, the function can optionally return a 81value. In this case, the function can optionally return a
82new callback function, to replace itself, as the second return value. 82new callback function, to replace itself, as the second return value.
83</p> 83</p>
@@ -121,7 +121,7 @@ library needs more data to be sent.
121Each time the callback is called, it should return the next chunk of data. It 121Each time the callback is called, it should return the next chunk of data. It
122can optionally return, as it's second return value, a new callback to replace 122can optionally return, as it's second return value, a new callback to replace
123itself. The callback can abort the process at any time by returning 123itself. The callback can abort the process at any time by returning
124<tt>nil</tt> followed by an optional error message. 124<b><tt>nil</tt></b> followed by an optional error message.
125</p> 125</p>
126 126
127<p class=note> 127<p class=note>
diff --git a/doc/tcp.html b/doc/tcp.html
index 0be535a..eb4cdfa 100644
--- a/doc/tcp.html
+++ b/doc/tcp.html
@@ -51,7 +51,7 @@ method.</p>
51 51
52<p class=return> 52<p class=return>
53In case of success, a new master object is returned. In case of error, 53In case of success, a new master object is returned. In case of error,
54<tt>nil</tt> is returned, followed by an error message. 54<b><tt>nil</tt></b> is returned, followed by an error message.
55</p> 55</p>
56 56
57<!-- accept +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 57<!-- accept +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@@ -67,7 +67,7 @@ object and returns a client object representing that connection.
67 67
68<p class=return> 68<p class=return>
69If a connection is successfully initiated, a client object is returned. 69If a connection is successfully initiated, a client object is returned.
70If a timeout condition is met, the method returns <tt>nil</tt> followed 70If a timeout condition is met, the method returns <b><tt>nil</tt></b> followed
71by the error string '<tt>timeout</tt>'. 71by the error string '<tt>timeout</tt>'.
72</p> 72</p>
73 73
@@ -111,7 +111,7 @@ attempts connection, the connection is refused.
111 111
112<p class=return> 112<p class=return>
113In case of success, the method returns 1. In case of error, the 113In case of success, the method returns 1. In case of error, the
114method returns <tt>nil</tt> followed by an error message. 114method returns <b><tt>nil</tt></b> followed by an error message.
115</p> 115</p>
116 116
117<p class=note> 117<p class=note>
@@ -165,7 +165,7 @@ and <a href=#close><tt>close</tt></a>.
165</p> 165</p>
166 166
167<p class=return> 167<p class=return>
168In case of error, the method returns <tt>nil</tt> followed by a string 168In case of error, the method returns <b><tt>nil</tt></b> followed by a string
169describing the error. In case of success, the method returns 1. 169describing the error. In case of success, the method returns 1.
170</p> 170</p>
171 171
@@ -187,7 +187,7 @@ Returns information about the remote side of a connected client object.
187<p class=return> 187<p class=return>
188Returns a string with the IP address of the peer, followed by the 188Returns a string with the IP address of the peer, followed by the
189port number that peer is using for the connection. 189port number that peer is using for the connection.
190In case of error, the method returns <tt>nil</tt>. 190In case of error, the method returns <b><tt>nil</tt></b>.
191</p> 191</p>
192 192
193<p class=note> 193<p class=note>
@@ -207,7 +207,7 @@ Returns the local address information associated to the object.
207 207
208<p class=return> 208<p class=return>
209The method returns a string with local IP address and a number with 209The method returns a string with local IP address and a number with
210the port. In case of error, the method returns <tt>nil</tt>. 210the port. In case of error, the method returns <b><tt>nil</tt></b>.
211</p> 211</p>
212 212
213<p class=note> 213<p class=note>
@@ -248,7 +248,7 @@ bytes from the socket.
248 248
249<p class=return> 249<p class=return>
250The method returns one value for each pattern, followed by a single 250The method returns one value for each pattern, followed by a single
251error code that can be <tt>nil</tt> in case of success, the string 251error code that can be <b><tt>nil</tt></b> in case of success, the string
252'<tt>closed</tt>' in case the connection was closed before the 252'<tt>closed</tt>' in case the connection was closed before the
253transmission was completed or the string '<tt>timeout</tt>' in case 253transmission was completed or the string '<tt>timeout</tt>' in case
254there was a timeout during the operation. 254there was a timeout during the operation.
@@ -278,7 +278,7 @@ result to LuaSocket instead of passing several independent strings.
278 278
279<p class=return> 279<p class=return>
280The method returns the number of bytes accepted by the transport layer, 280The method returns the number of bytes accepted by the transport layer,
281followed by an error code. The error code is <tt>nil</tt> if the operation 281followed by an error code. The error code is <b><tt>nil</tt></b> if the operation
282completed with no errors, the string '<tt>closed</tt>' in case 282completed with no errors, the string '<tt>closed</tt>' in case
283the connection was closed before the transmission was completed or the 283the connection was closed before the transmission was completed or the
284string '<tt>timeout</tt>' in case there was a timeout during the 284string '<tt>timeout</tt>' in case there was a timeout during the
@@ -288,7 +288,7 @@ operation.
288<p class=note> 288<p class=note>
289Note: The return values for the <tt>send</tt> method have been changed in 289Note: The return values for the <tt>send</tt> method have been changed in
290LuaSocket 2.0! In previous versions, the method returned only the 290LuaSocket 2.0! In previous versions, the method returned only the
291error message. Since returning <tt>nil</tt> in case of success goes 291error message. Since returning <b><tt>nil</tt></b> in case of success goes
292against all other LuaSocket methods and functions, the 292against all other LuaSocket methods and functions, the
293<tt>send</tt> method been changed for the sake of uniformity. 293<tt>send</tt> method been changed for the sake of uniformity.
294</p> 294</p>
@@ -330,7 +330,7 @@ considered broken and processes using the socket are notified.
330</ul> 330</ul>
331 331
332<p class=return> 332<p class=return>
333The method returns 1 in case of success, or <tt>nil</tt> otherwise. 333The method returns 1 in case of success, or <b><tt>nil</tt></b> otherwise.
334</p> 334</p>
335 335
336<p class=note> 336<p class=note>
@@ -374,7 +374,7 @@ a call.</li>
374</ul> 374</ul>
375 375
376<p class=parameters> 376<p class=parameters>
377The <tt>nil</tt> timeout <tt>value</tt> allows operations to block 377The <b><tt>nil</tt></b> timeout <tt>value</tt> allows operations to block
378indefinitely. Negative timeout values have the same effect. 378indefinitely. Negative timeout values have the same effect.
379</p> 379</p>
380 380
diff --git a/doc/udp.html b/doc/udp.html
index 7f2ecce..9f5cf8f 100644
--- a/doc/udp.html
+++ b/doc/udp.html
@@ -52,7 +52,7 @@ is used to connect the object.
52 52
53<p class="return"> 53<p class="return">
54In case of success, a new unconnected UDP object 54In case of success, a new unconnected UDP object
55returned. In case of error, <tt>nil</tt> is returned, followed by 55returned. In case of error, <b><tt>nil</tt></b> is returned, followed by
56an error message. 56an error message.
57</p> 57</p>
58 58
@@ -112,7 +112,7 @@ Returns the local address information associated to the object.
112<p class="return"> 112<p class="return">
113The method returns a string with local IP 113The method returns a string with local IP
114address and a number with the port. In case of error, the method 114address and a number with the port. In case of error, the method
115returns <tt>nil</tt>. 115returns <b><tt>nil</tt></b>.
116</p> 116</p>
117 117
118<p class="note"> 118<p class="note">
@@ -150,7 +150,7 @@ maximum datagram size is used.
150<p class="return"> 150<p class="return">
151In case of success, the method return the 151In case of success, the method return the
152received datagram. In case of timeout, the method returns 152received datagram. In case of timeout, the method returns
153<tt>nil</tt> followed by the string '<tt>timeout</tt>'. 153<b><tt>nil</tt></b> followed by the string '<tt>timeout</tt>'.
154</p> 154</p>
155 155
156<!-- receivefrom +++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 156<!-- receivefrom +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
@@ -183,7 +183,7 @@ Beware that the maximum datagram size for UDP is 576 bytes.
183 183
184<p class="return"> 184<p class="return">
185If successful, the method returns 1. In case of 185If successful, the method returns 1. In case of
186error, the method returns <tt>nil</tt> followed by the 186error, the method returns <b><tt>nil</tt></b> followed by the
187'<tt>refused</tt>' message. 187'<tt>refused</tt>' message.
188</p> 188</p>
189 189
@@ -214,7 +214,7 @@ names are <em>not</em> allowed for performance reasons.
214 214
215<p class="return"> 215<p class="return">
216If successful, the method returns 1. In case of 216If successful, the method returns 1. In case of
217error, the method returns <tt>nil</tt> followed by the 217error, the method returns <b><tt>nil</tt></b> followed by the
218'<tt>refused</tt>' message. 218'<tt>refused</tt>' message.
219</p> 219</p>
220 220
@@ -258,7 +258,7 @@ case, the <tt>port</tt> argument is ignored.
258 258
259<p class="return"> 259<p class="return">
260In case of error the method returns 260In case of error the method returns
261<tt>nil</tt> followed by an error message. In case of success, the 261<b><tt>nil</tt></b> followed by an error message. In case of success, the
262method returns 1. 262method returns 1.
263</p> 263</p>
264 264
@@ -288,7 +288,7 @@ all local interfaces using the constant <tt>INADDR_ANY</tt>. If
288 288
289<p class="return"> 289<p class="return">
290If successful, the method returns 1. In case of 290If successful, the method returns 1. In case of
291error, the method returns <tt>nil</tt> followed by an error 291error, the method returns <b><tt>nil</tt></b> followed by an error
292message. 292message.
293</p> 293</p>
294 294
@@ -328,7 +328,7 @@ socket.</li>
328 328
329<p class="return"> 329<p class="return">
330The method returns 1 in case of success, or 330The method returns 1 in case of success, or
331<tt>nil</tt> followed by an error message otherwise. 331<b><tt>nil</tt></b> followed by an error message otherwise.
332</p> 332</p>
333 333
334<p class="note"> 334<p class="note">
@@ -356,7 +356,7 @@ give up and fail with an error code.
356 356
357<p class="parameters"> 357<p class="parameters">
358The amount of time to wait is specified as 358The amount of time to wait is specified as
359the <tt>value</tt> parameter, in seconds. The <tt>nil</tt> timeout 359the <tt>value</tt> parameter, in seconds. The <b><tt>nil</tt></b> timeout
360<tt>value</tt> allows operations to block indefinitely. Negative 360<tt>value</tt> allows operations to block indefinitely. Negative
361timeout values have the same effect. 361timeout values have the same effect.
362</p> 362</p>
diff --git a/doc/url.html b/doc/url.html
index e67ea13..0eafafa 100644
--- a/doc/url.html
+++ b/doc/url.html
@@ -148,7 +148,7 @@ component.
148 148
149<p class=parameters> 149<p class=parameters>
150<tt>Segments</tt> is a list of strings with the <tt>&lt;segment&gt;</tt> 150<tt>Segments</tt> is a list of strings with the <tt>&lt;segment&gt;</tt>
151parts. If <tt>unsafe</tt> is anything but <tt>nil</tt>, reserved 151parts. If <tt>unsafe</tt> is anything but <b><tt>nil</tt></b>, reserved
152characters are left untouched. 152characters are left untouched.
153</p> 153</p>
154 154
diff --git a/etc/b64.lua b/etc/b64.lua
index 7a4607d..de83578 100644
--- a/etc/b64.lua
+++ b/etc/b64.lua
@@ -1,6 +1,11 @@
1local base64 = socket.mime.base64.encode() 1local convert
2local split = socket.mime.split() 2if arg and arg[1] == '-d' then
3local convert = socket.mime.chain(base64, split) 3 convert = socket.mime.decode("base64")
4else
5 local base64 = socket.mime.encode("base64")
6 local wrap = socket.mime.wrap()
7 convert = socket.mime.chain(base64, wrap)
8end
4while 1 do 9while 1 do
5 local chunk = io.read(4096) 10 local chunk = io.read(4096)
6 io.write(convert(chunk)) 11 io.write(convert(chunk))
diff --git a/src/inet.c b/src/inet.c
index dff4bf2..d626b86 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -19,11 +19,13 @@
19static int inet_global_toip(lua_State *L); 19static int inet_global_toip(lua_State *L);
20static int inet_global_tohostname(lua_State *L); 20static int inet_global_tohostname(lua_State *L);
21static void inet_pushresolved(lua_State *L, struct hostent *hp); 21static void inet_pushresolved(lua_State *L, struct hostent *hp);
22static int inet_global_gethostname(lua_State *L);
22 23
23/* DNS functions */ 24/* DNS functions */
24static luaL_reg func[] = { 25static luaL_reg func[] = {
25 { "toip", inet_global_toip }, 26 { "toip", inet_global_toip },
26 { "tohostname", inet_global_tohostname }, 27 { "tohostname", inet_global_tohostname },
28 { "gethostname", inet_global_gethostname},
27 { NULL, NULL} 29 { NULL, NULL}
28}; 30};
29 31
@@ -101,6 +103,25 @@ static int inet_global_tohostname(lua_State *L)
101 return 2; 103 return 2;
102} 104}
103 105
106/*-------------------------------------------------------------------------*\
107* Gets the host name
108\*-------------------------------------------------------------------------*/
109static int inet_global_gethostname(lua_State *L)
110{
111 char name[257];
112 name[256] = '\0';
113 if (gethostname(name, 256) < 0) {
114 lua_pushnil(L);
115 lua_pushstring(L, "gethostname failed");
116 return 2;
117 } else {
118 lua_pushstring(L, name);
119 return 1;
120 }
121}
122
123
124
104/*=========================================================================*\ 125/*=========================================================================*\
105* Lua methods 126* Lua methods
106\*=========================================================================*/ 127\*=========================================================================*/
diff --git a/src/luasocket.c b/src/luasocket.c
index 73583a8..bfe71c2 100644
--- a/src/luasocket.c
+++ b/src/luasocket.c
@@ -38,15 +38,8 @@
38/*=========================================================================*\ 38/*=========================================================================*\
39* Declarations 39* Declarations
40\*=========================================================================*/ 40\*=========================================================================*/
41static int global_gethostname(lua_State *L);
42static int base_open(lua_State *L); 41static int base_open(lua_State *L);
43 42
44/* functions in library namespace */
45static luaL_reg func[] = {
46 {"gethostname", global_gethostname},
47 {NULL, NULL}
48};
49
50/*-------------------------------------------------------------------------*\ 43/*-------------------------------------------------------------------------*\
51* Setup basic stuff. 44* Setup basic stuff.
52\*-------------------------------------------------------------------------*/ 45\*-------------------------------------------------------------------------*/
@@ -70,30 +63,10 @@ static int base_open(lua_State *L)
70 lua_pushstring(L, "LUASOCKET_LIBNAME"); 63 lua_pushstring(L, "LUASOCKET_LIBNAME");
71 lua_pushstring(L, LUASOCKET_LIBNAME); 64 lua_pushstring(L, LUASOCKET_LIBNAME);
72 lua_settable(L, LUA_GLOBALSINDEX); 65 lua_settable(L, LUA_GLOBALSINDEX);
73 /* define library functions */
74 luaL_openlib(L, LUASOCKET_LIBNAME, func, 0);
75 lua_pop(L, 1);
76 return 0; 66 return 0;
77} 67}
78 68
79/*-------------------------------------------------------------------------*\ 69/*-------------------------------------------------------------------------*\
80* Gets the host name
81\*-------------------------------------------------------------------------*/
82static int global_gethostname(lua_State *L)
83{
84 char name[257];
85 name[256] = '\0';
86 if (gethostname(name, 256) < 0) {
87 lua_pushnil(L);
88 lua_pushstring(L, "gethostname failed");
89 return 2;
90 } else {
91 lua_pushstring(L, name);
92 return 1;
93 }
94}
95
96/*-------------------------------------------------------------------------*\
97* Initializes all library modules. 70* Initializes all library modules.
98\*-------------------------------------------------------------------------*/ 71\*-------------------------------------------------------------------------*/
99LUASOCKET_API int luaopen_socket(lua_State *L) 72LUASOCKET_API int luaopen_socket(lua_State *L)
diff --git a/src/mime.c b/src/mime.c
index 8e97f8b..95cd400 100644
--- a/src/mime.c
+++ b/src/mime.c
@@ -27,12 +27,12 @@ static const char EQCRLF[3] = {'=', CR, LF};
27/*=========================================================================*\ 27/*=========================================================================*\
28* Internal function prototypes. 28* Internal function prototypes.
29\*=========================================================================*/ 29\*=========================================================================*/
30static int mime_global_fmt(lua_State *L); 30static int mime_global_wrp(lua_State *L);
31static int mime_global_b64(lua_State *L); 31static int mime_global_b64(lua_State *L);
32static int mime_global_unb64(lua_State *L); 32static int mime_global_unb64(lua_State *L);
33static int mime_global_qp(lua_State *L); 33static int mime_global_qp(lua_State *L);
34static int mime_global_unqp(lua_State *L); 34static int mime_global_unqp(lua_State *L);
35static int mime_global_qpfmt(lua_State *L); 35static int mime_global_qpwrp(lua_State *L);
36static int mime_global_eol(lua_State *L); 36static int mime_global_eol(lua_State *L);
37 37
38static void b64setup(UC *b64unbase); 38static void b64setup(UC *b64unbase);
@@ -54,10 +54,10 @@ static luaL_reg func[] = {
54 { "eol", mime_global_eol }, 54 { "eol", mime_global_eol },
55 { "qp", mime_global_qp }, 55 { "qp", mime_global_qp },
56 { "unqp", mime_global_unqp }, 56 { "unqp", mime_global_unqp },
57 { "qpfmt", mime_global_qpfmt }, 57 { "qpwrp", mime_global_qpwrp },
58 { "b64", mime_global_b64 }, 58 { "b64", mime_global_b64 },
59 { "unb64", mime_global_unb64 }, 59 { "unb64", mime_global_unb64 },
60 { "fmt", mime_global_fmt }, 60 { "wrp", mime_global_wrp },
61 { NULL, NULL } 61 { NULL, NULL }
62}; 62};
63 63
@@ -127,13 +127,13 @@ static const char *optlstring(lua_State *L, int n, const char *v, size_t *l)
127\*=========================================================================*/ 127\*=========================================================================*/
128/*-------------------------------------------------------------------------*\ 128/*-------------------------------------------------------------------------*\
129* Incrementaly breaks a string into lines 129* Incrementaly breaks a string into lines
130* A, n = fmt(l, B, length, marker) 130* A, n = wrp(l, B, length, marker)
131* A is a copy of B, broken into lines of at most 'length' bytes. 131* A is a copy of B, broken into lines of at most 'length' bytes.
132* 'l' is how many bytes are left for the first line of B. 132* 'l' is how many bytes are left for the first line of B.
133* 'n' is the number of bytes left in the last line of A. 133* 'n' is the number of bytes left in the last line of A.
134* Marker is the end-of-line marker. 134* Marker is the end-of-line marker.
135\*-------------------------------------------------------------------------*/ 135\*-------------------------------------------------------------------------*/
136static int mime_global_fmt(lua_State *L) 136static int mime_global_wrp(lua_State *L)
137{ 137{
138 size_t size = 0; 138 size_t size = 0;
139 int left = (int) luaL_checknumber(L, 1); 139 int left = (int) luaL_checknumber(L, 1);
@@ -526,14 +526,14 @@ static int mime_global_unqp(lua_State *L)
526 526
527/*-------------------------------------------------------------------------*\ 527/*-------------------------------------------------------------------------*\
528* Incrementally breaks a quoted-printed string into lines 528* Incrementally breaks a quoted-printed string into lines
529* A, n = qpfmt(l, B, length) 529* A, n = qpwrp(l, B, length)
530* A is a copy of B, broken into lines of at most 'length' bytes. 530* A is a copy of B, broken into lines of at most 'length' bytes.
531* 'l' is how many bytes are left for the first line of B. 531* 'l' is how many bytes are left for the first line of B.
532* 'n' is the number of bytes left in the last line of A. 532* 'n' is the number of bytes left in the last line of A.
533* There are two complications: lines can't be broken in the middle 533* There are two complications: lines can't be broken in the middle
534* of an encoded =XX, and there might be line breaks already 534* of an encoded =XX, and there might be line breaks already
535\*-------------------------------------------------------------------------*/ 535\*-------------------------------------------------------------------------*/
536static int mime_global_qpfmt(lua_State *L) 536static int mime_global_qpwrp(lua_State *L)
537{ 537{
538 size_t size = 0; 538 size_t size = 0;
539 int left = (int) luaL_checknumber(L, 1); 539 int left = (int) luaL_checknumber(L, 1);
diff --git a/src/mime.lua b/src/mime.lua
index 0251f6e..30c6b38 100644
--- a/src/mime.lua
+++ b/src/mime.lua
@@ -19,7 +19,7 @@ local wt = {}
19local function choose(table) 19local function choose(table)
20 return function(method, ...) 20 return function(method, ...)
21 local f = table[method or "nil"] 21 local f = table[method or "nil"]
22 if not f then return nil, "unknown method (" .. tostring(method) .. ")" 22 if not f then error("unknown method (" .. tostring(method) .. ")", 3)
23 else return f(unpack(arg)) end 23 else return f(unpack(arg)) end
24 end 24 end
25end 25end
@@ -37,7 +37,15 @@ end
37-- function that choose the encoding, decoding or wrap algorithm 37-- function that choose the encoding, decoding or wrap algorithm
38encode = choose(et) 38encode = choose(et)
39decode = choose(dt) 39decode = choose(dt)
40wrap = choose(wt) 40
41-- the wrap filter has default parameters
42local cwt = choose(wt)
43function wrap(...)
44 if not arg[1] or type(arg[1]) ~= "string" then
45 table.insert(arg, 1, "base64")
46 end
47 return cwt(unpack(arg))
48end
41 49
42-- define the encoding algorithms 50-- define the encoding algorithms
43et['base64'] = function() 51et['base64'] = function()
@@ -58,15 +66,14 @@ dt['quoted-printable'] = function()
58end 66end
59 67
60-- define the wrap algorithms 68-- define the wrap algorithms
61wt['character'] = function(length) 69wt['base64'] = function(length, marker)
62 length = length or 76 70 length = length or 76
63 return cicle(fmt, length, length) 71 return cicle(wrp, length, length, marker)
64end 72end
65wt['base64'] = wt['character']
66 73
67wt['quoted-printable'] = function(length) 74wt['quoted-printable'] = function(length)
68 length = length or 76 75 length = length or 76
69 return cicle(qpfmt, length, length) 76 return cicle(qpwrp, length, length)
70end 77end
71 78
72-- define the end-of-line translation function 79-- define the end-of-line translation function
diff --git a/test/httptest.lua b/test/httptest.lua
index 8bf3980..c9a74a8 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -12,7 +12,7 @@ socket.http.TIMEOUT = 5
12 12
13local t = socket.time() 13local t = socket.time()
14 14
15host = host or "diego.student.dyn.cs.princeton.edu" 15host = host or "diego-interface2.student.dyn.CS.Princeton.EDU"
16proxy = proxy or "http://localhost:3128" 16proxy = proxy or "http://localhost:3128"
17prefix = prefix or "/luasocket-test" 17prefix = prefix or "/luasocket-test"
18cgiprefix = cgiprefix or "/luasocket-test-cgi" 18cgiprefix = cgiprefix or "/luasocket-test-cgi"
diff --git a/test/mimetest.lua b/test/mimetest.lua
index aa2772c..81814de 100644
--- a/test/mimetest.lua
+++ b/test/mimetest.lua
@@ -4,7 +4,7 @@ local qptest = "qptest.bin"
4local eqptest = "qptest.bin2" 4local eqptest = "qptest.bin2"
5local dqptest = "qptest.bin3" 5local dqptest = "qptest.bin3"
6 6
7local b64test = "luasocket.exe" 7local b64test = "luasocket"
8local eb64test = "b64test.bin" 8local eb64test = "b64test.bin"
9local db64test = "b64test.bin2" 9local db64test = "b64test.bin2"
10 10
@@ -155,10 +155,10 @@ local function encode_b64test()
155 local e2 = socket.mime.encode("base64") 155 local e2 = socket.mime.encode("base64")
156 local e3 = socket.mime.encode("base64") 156 local e3 = socket.mime.encode("base64")
157 local e4 = socket.mime.encode("base64") 157 local e4 = socket.mime.encode("base64")
158 local sp4 = socket.mime.wrap("character") 158 local sp4 = socket.mime.wrap()
159 local sp3 = socket.mime.wrap("character", 59) 159 local sp3 = socket.mime.wrap(59)
160 local sp2 = socket.mime.wrap("character", 30) 160 local sp2 = socket.mime.wrap("base64", 30)
161 local sp1 = socket.mime.wrap("character", 27) 161 local sp1 = socket.mime.wrap(27)
162 local chain = socket.mime.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4) 162 local chain = socket.mime.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4)
163 transform(b64test, eb64test, chain) 163 transform(b64test, eb64test, chain)
164end 164end
diff --git a/test/testclnt.lua b/test/testclnt.lua
index beb0157..5f366b2 100644
--- a/test/testclnt.lua
+++ b/test/testclnt.lua
@@ -378,7 +378,7 @@ end
378 378
379------------------------------------------------------------------------ 379------------------------------------------------------------------------
380function accept_errors() 380function accept_errors()
381 io.write("not listenning: ") 381 io.write("not listening: ")
382 local d, e = socket.bind("*", 0) 382 local d, e = socket.bind("*", 0)
383 assert(d, e); 383 assert(d, e);
384 local c, e = socket.tcp(); 384 local c, e = socket.tcp();
@@ -392,7 +392,7 @@ function accept_errors()
392 assert(c, e); 392 assert(c, e);
393 d:setfd(c:getfd()) 393 d:setfd(c:getfd())
394 local r, e = d:accept() 394 local r, e = d:accept()
395 assert(not r and e == "not supported", e) 395 assert(not r and e == "not supported" or e == "not listening", e)
396 print("ok") 396 print("ok")
397end 397end
398 398