aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2005-11-20 08:56:19 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2005-11-20 08:56:19 +0000
commit5e09779c7f6b1710150d5a0f12d86ded7ede75c6 (patch)
treeffd2e2e7d918cc30015c89bd14122aa8cadb1546
parentf20f4889bfe5a02cd9b77868b90cc8042352176a (diff)
downloadluasocket-5e09779c7f6b1710150d5a0f12d86ded7ede75c6.tar.gz
luasocket-5e09779c7f6b1710150d5a0f12d86ded7ede75c6.tar.bz2
luasocket-5e09779c7f6b1710150d5a0f12d86ded7ede75c6.zip
In pre release mode!
-rw-r--r--NEW69
-rw-r--r--config20
-rw-r--r--doc/http.html13
-rw-r--r--doc/index.html7
-rw-r--r--doc/installation.html8
-rw-r--r--doc/tcp.html1
-rw-r--r--etc/README93
-rw-r--r--etc/check-memory.lua2
-rw-r--r--makefile.dist211
-rw-r--r--samples/README57
-rw-r--r--samples/forward.lua207
-rw-r--r--samples/lpr.lua2
-rw-r--r--test/httptest.lua4
13 files changed, 283 insertions, 411 deletions
diff --git a/NEW b/NEW
index b84b5fb..7c94368 100644
--- a/NEW
+++ b/NEW
@@ -1,21 +1,54 @@
1What's New 1What's New
2 2
3The big change for the 2.0 (beta3) release was the adoption of the Lua 3There is no big change for the 2.0 (final) release. It is
45.1 package proposal. There were several bug fixes too (a beta is a 4basically a bug fix release. The main improvement is in the
5beta, is a beta). 5non-blocking support.
6 6
7* New compat-5.1 distribution: 7 * New: sample module dispatch.lua implements a coroutine
8 - Instalation uses new directory structure; 8 based dispatcher;
9 - Namespace hierarchy is in now back in use (ex. socket.url instead of url); 9 * New: sample check-links.lua works both in blocking and
10 - All modules call require even for standard libraries; 10 non-blocking mode using coroutines (using the new
11* LTN12 avoids coroutines (so you can go wild on the C side); 11 dispatcher);
12* socket.select wasn't calling tm_markstart; 12 * New: sample forward.lua implements a coroutine based
13* Kludge on wsocket.c:sock_send for Windows timeout issue moved to 13 forward server (using the new dispatcher);
14 buffer.c:sendraw so it's not a kludge anymore; 14 * Improved: tcp:send(data, i, j) to return (i+sent-1). This
15* socket.protect only catches errors thrown by socket.try; 15 is great for non-blocking I/O, but might break some code;
16* Fixed udp:sendto to call sock_sendto instead of sock_send; 16 * Improved: HTTP, SMTP, and FTP functions to accept a new
17* close wasn't returning 1! 17 field create that overrides the function used to create
18* socket.gettime returns time since Unix Epoch 1/1/1970 (UTC) 18 socket objects;
19* socket.sleep is robust to interrupts; 19 * Improved: smtp.message now supports multipart/alternative
20* http.PROXY wasn't working. 20 (for the HTML messages we all love so much);
21* fixed some of the examples 21 * Fixed: smtp.send was hanging on errors returned by LTN12
22 sources;
23 * Fixed: url.absolute() to work when base_url is in parsed
24 form;
25 * Fixed: http.request() not to redirect when the location
26 header is empty (naughty servers...);
27 * Fixed: tcp{client}:shutdown() to check for class instead
28 of group;
29 * Fixed: The manual to stop using socket.try() in place of
30 assert(), since it can't;
31 * Improved: Got rid of package.loaded.base = _G kludge;
32 * Fixed: Parts of the manual referred to require("http")
33 instead of require("socket.http");
34 * Improved: Socket and MIME binaries are called 'core' each
35 inside their directory (ex. "socket/core.dll"). The 'l'
36 prefix was just a bad idea;
37 * Improved: Using bundles in Mac OS X, instead of dylibs;
38 * Fixed: luasocket.h to export luaopen_socket_core;
39 * Fixed: udp:setpeername() so you can "disconnect" an UDP
40 socket;
41 * Fixed: A weird bug in HTTP support that caused some
42 requests to fail (Florian Berger);
43 * Fixed: Bug in socket.select() that caused sockets with
44 descriptor 0 to be ignored (Renato Maia);
45 * Fixed: "Bug" that caused dns.toip() to crash under uLinux
46 (William Trenker);
47 * Fixed: "Bug" that caused gethostbyname to crash under VMS
48 (Renato Maia);
49 * Fixed: tcp:send("") to return 0 bytes sent (Alexander
50 Marinov);
51 * Improved: socket.DEBUG and socket.VERSION became
52 socket._DEBUGs and socket._VERSION for uniformity with other
53 libraries;
54 * Improved: socket.select now works on empty sets on Windows.
diff --git a/config b/config
index 175a885..255477b 100644
--- a/config
+++ b/config
@@ -37,20 +37,20 @@ INSTALL_EXEC=cp
37# Compiler and linker settings 37# Compiler and linker settings
38# for Mac OS X 38# for Mac OS X
39# 39#
40#CC=gcc 40CC=gcc
41#DEF=-DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN 41DEF=-DLUASOCKET_DEBUG -DUNIX_HAS_SUN_LEN
42#CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common 42CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fno-common
43#LDFLAGS=-bundle -undefined dynamic_lookup 43LDFLAGS=-bundle -undefined dynamic_lookup
44#LD=export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc 44LD=export MACOSX_DEPLOYMENT_TARGET="10.3"; gcc
45 45
46#------ 46#------
47# Compiler and linker settings 47# Compiler and linker settings
48# for Linux 48# for Linux
49CC=gcc 49#CC=gcc
50DEF=-DLUASOCKET_DEBUG 50#DEF=-DLUASOCKET_DEBUG
51CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fpic 51#CFLAGS= $(LUAINC) -I$(COMPAT) $(DEF) -pedantic -Wall -O2 -fpic
52LDFLAGS=-O -shared 52#LDFLAGS=-O -shared
53LD=gcc 53#LD=gcc
54 54
55#------ 55#------
56# End of makefile configuration 56# End of makefile configuration
diff --git a/doc/http.html b/doc/http.html
index 096ab2b..9d80c23 100644
--- a/doc/http.html
+++ b/doc/http.html
@@ -209,11 +209,16 @@ Here are a few examples with the simple interface:
209 209
210<pre class=example> 210<pre class=example>
211-- load the http module 211-- load the http module
212http = require("socket.http") 212local io = require("io")
213local http = require("socket.http")
214local ltn12 = require("ltn12")
213 215
214-- connect to server "www.tecgraf.puc-rio.br" and retrieves this manual 216-- connect to server "www.cs.princeton.edu" and retrieves this manual
215-- file from "/luasocket/http.html" 217-- file from "~diego/professional/luasocket/http.html" and print it to stdout
216b = http.request("http://www.tecgraf.puc-rio.br/luasocket/http.html") 218http.request{
219 url = "http://www.cs.princeton.edu/~diego/professional/luasocket/http.html",
220 sink = ltn12.sink.file(io.stdout)
221}
217 222
218-- connect to server "www.example.com" and tries to retrieve 223-- connect to server "www.example.com" and tries to retrieve
219-- "/private/index.html". Fails because authentication is needed. 224-- "/private/index.html". Fails because authentication is needed.
diff --git a/doc/index.html b/doc/index.html
index 3d5acb3..7ac0d4c 100644
--- a/doc/index.html
+++ b/doc/index.html
@@ -165,6 +165,13 @@ support.
165</p> 165</p>
166 166
167<ul> 167<ul>
168<li> New: sample module <tt>dispatch.lua</tt> implements a
169coroutine based dispatcher;
170<li> New: sample <tt>check-links.lua</tt> works
171both in blocking and non-blocking mode using coroutines
172(using the new dispatcher);
173<li> New: sample <tt>forward.lua</tt> implements a coroutine
174based forward server (using the new dispatcher);
168<li> Improved: <tt>tcp:send(data, i, j)</tt> to return <tt>(i+sent-1)</tt>. This is great for non-blocking I/O, but might break some code; 175<li> Improved: <tt>tcp:send(data, i, j)</tt> to return <tt>(i+sent-1)</tt>. This is great for non-blocking I/O, but might break some code;
169<li> Improved: HTTP, SMTP, and FTP functions to accept a new field 176<li> Improved: HTTP, SMTP, and FTP functions to accept a new field
170<tt>create</tt> that overrides the function used to create socket objects; 177<tt>create</tt> that overrides the function used to create socket objects;
diff --git a/doc/installation.html b/doc/installation.html
index 1b82ed3..4086f41 100644
--- a/doc/installation.html
+++ b/doc/installation.html
@@ -76,9 +76,9 @@ distribution directory structure:</p>
76<pre class=example> 76<pre class=example>
77&lt;SHARE&gt;/compat-5.1.lua 77&lt;SHARE&gt;/compat-5.1.lua
78&lt;SHARE&gt;/ltn12.lua 78&lt;SHARE&gt;/ltn12.lua
79&lt;SHARE&gt;/mime/init.lua (originally mime.lua) 79&lt;SHARE&gt;/mime/mime.lua
80&lt;LIB&gt;/mime/core.dll 80&lt;LIB&gt;/mime/core.dll
81&lt;SHARE&gt;/socket/init.lua (originally socket.lua) 81&lt;SHARE&gt;/socket/socket.lua
82&lt;LIB&gt;/socket/core.dll 82&lt;LIB&gt;/socket/core.dll
83&lt;SHARE&gt;/socket/http.lua 83&lt;SHARE&gt;/socket/http.lua
84&lt;SHARE&gt;/socket/tp.lua 84&lt;SHARE&gt;/socket/tp.lua
@@ -88,9 +88,7 @@ distribution directory structure:</p>
88</pre> 88</pre>
89 89
90<p> Naturally, on Unix systems, <tt>core.dll</tt> 90<p> Naturally, on Unix systems, <tt>core.dll</tt>
91would be replaced by <tt>core.so</tt>. Notice that in the instalation, 91would be replaced by <tt>core.so</tt>.
92<tt>socket.lua</tt> becomes <tt>socket/init.lua</tt>, and the same happens
93with <tt>mime.lua</tt>, which becomes <tt>mime/init.lua</tt>.
94</p> 92</p>
95 93
96<p> In order for the interpreter to find all LuaSocket components, three 94<p> In order for the interpreter to find all LuaSocket components, three
diff --git a/doc/tcp.html b/doc/tcp.html
index 5c810de..2c687c0 100644
--- a/doc/tcp.html
+++ b/doc/tcp.html
@@ -446,6 +446,7 @@ The method returns 1 in case of success and <tt><b>nil</b></tt> otherwise.
446<!-- settimeout +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 446<!-- settimeout +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
447 447
448<p class=name id=settimeout> 448<p class=name id=settimeout>
449master:<b>settimeout(</b>value [, mode]<b>)</b><br>
449client:<b>settimeout(</b>value [, mode]<b>)</b><br> 450client:<b>settimeout(</b>value [, mode]<b>)</b><br>
450server:<b>settimeout(</b>value [, mode]<b>)</b> 451server:<b>settimeout(</b>value [, mode]<b>)</b>
451</p> 452</p>
diff --git a/etc/README b/etc/README
index 6be740b..5a919cd 100644
--- a/etc/README
+++ b/etc/README
@@ -1,48 +1,89 @@
1This directory contains code that is more useful than the examples. This code 1This directory contains code that is more useful than the
2*is* supported. 2samples. This code *is* supported.
3 3
4 tftp.lua -- Trivial FTP client 4 tftp.lua -- Trivial FTP client
5 5
6This module implements file retrieval by the TFTP protocol. Its main use 6This module implements file retrieval by the TFTP protocol.
7was to test the UDP code, but since someone found it usefull, I turned it 7Its main use was to test the UDP code, but since someone
8into a module that is almost official (no uploads, yet). 8found it usefull, I turned it into a module that is almost
9official (no uploads, yet).
9 10
10 dict.lua -- Dict client 11 dict.lua -- Dict client
11 12
12The dict.lua module started with a cool simple client for the DICT 13The dict.lua module started with a cool simple client
13protocol, written by Luiz Henrique Figueiredo. This new version has been 14for the DICT protocol, written by Luiz Henrique Figueiredo.
14converted into a library, similar to the HTTP and FTP libraries, that can 15This new version has been converted into a library, similar
15be used from within any luasocket application. Take a look on the source 16to the HTTP and FTP libraries, that can be used from within
16code and you will be able to figure out how to use it. 17any luasocket application. Take a look on the source code
18and you will be able to figure out how to use it.
17 19
18 lp.lua -- LPD client 20 lp.lua -- LPD client library
19 21
20The lp.lua module implements the client part of the Line Printer Daemon 22The lp.lua module implements the client part of the Line
21protocol, used to print files on Unix machines. It is courtesy of David 23Printer Daemon protocol, used to print files on Unix
22Burgess and I heard he would be the one supporting it, even after I messed 24machines. It is courtesy of David Burgess! See the source
23with it. See the source code and the lpr.lua in the examples directory. 25code and the lpr.lua in the examples directory.
26
27 b64.lua
28 qp.lua
29 eol.lua
30
31These are tiny programs that perform Base64,
32Quoted-Printable and end-of-line marker conversions.
24 33
25 get.lua -- file retriever 34 get.lua -- file retriever
26 35
27This little program is a client that uses the FTP and HTTP code to 36This little program is a client that uses the FTP and
28implement a command line file graber. Just run 37HTTP code to implement a command line file graber. Just
38run
29 39
30 lua get.lua <remote-file> [<local-file>] 40 lua get.lua <remote-file> [<local-file>]
31 41
32to download a remote file (either ftp:// or http://) to the specified 42to download a remote file (either ftp:// or http://) to
33local file. The program also prints the download throughput, elapsed 43the specified local file. The program also prints the
34time, bytes already downloaded etc during download. 44download throughput, elapsed time, bytes already downloaded
45etc during download.
46
47 check-memory.lua -- checks memory consumption
48
49This is just to see how much memory each module uses.
50
51 dispatch.lua -- coroutine based dispatcher
52
53This is a first try at a coroutine based non-blocking
54dispatcher for LuaSocket. Take a look at 'check-links.lua'
55and at 'forward.lua' to see how to use it.
35 56
36 check-links.lua -- HTML link checker program 57 check-links.lua -- HTML link checker program
37 58
38This little program scans a HTML file and checks for broken links. It is 59This little program scans a HTML file and checks for broken
39similar to check-links.pl by Jamie Zawinski, but uses all facilities of 60links. It is similar to check-links.pl by Jamie Zawinski,
40the LuaSocket library and the Lua language. It has not been thoroughly 61but uses all facilities of the LuaSocket library and the Lua
41tested, but it should work. Just run 62language. It has not been thoroughly tested, but it should
63work. Just run
64
65 lua check-links.lua [-n] {<url>} > output
66
67and open the result to see a list of broken links. You can
68also use the '-n' switch to run the same program in
69non-blocking mode to see how much faster things can get.
70
71 forward.lua -- coroutine based forward server
72
73This is a forward server that can accept several connections
74and transfers simultaneously using non-blocking I/O and the
75coroutine-based dispatcher. You can run, for example
76
77 lua forward.lua 8080:proxy.com:3128
78
79to redirect all local conections to port 8080 to the host
80'proxy.com' at port 3128.
42 81
43 lua check-links.lua {<url>} > output 82 unix.c and unix.h
44 83
45and open the result to see a list of broken links. 84This is an implementation of Unix local domain sockets and
85demonstrates how to extend LuaSocket with a new type of
86transport. It has been tested on Linux and on Mac OS X.
46 87
47Good luck, 88Good luck,
48Diego. 89Diego.
diff --git a/etc/check-memory.lua b/etc/check-memory.lua
index 7bcdf67..7bd984d 100644
--- a/etc/check-memory.lua
+++ b/etc/check-memory.lua
@@ -7,7 +7,7 @@ function load(s)
7 print(s .. ":\t " .. (b-a) .. "k") 7 print(s .. ":\t " .. (b-a) .. "k")
8end 8end
9 9
10load("url") 10load("socket.url")
11load("ltn12") 11load("ltn12")
12load("socket") 12load("socket")
13load("mime") 13load("mime")
diff --git a/makefile.dist b/makefile.dist
index 8b79653..54a9751 100644
--- a/makefile.dist
+++ b/makefile.dist
@@ -4,125 +4,128 @@
4 4
5DIST = luasocket-2.0 5DIST = luasocket-2.0
6 6
7COMPAT = compat-5.1r4 7COMPAT = src/compat-5.1r4
8 8
9LUA = \ 9
10 ftp.lua \ 10TEST = \
11 http.lua \ 11 test/testclnt.lua \
12 ltn12.lua \ 12 test/testsrvr.lua \
13 mime.lua \ 13 test/testsupport.lua
14 smtp.lua \ 14
15 socket.lua \ 15SAMPLES = \
16 tp.lua \ 16 samples/README \
17 url.lua 17 samples/cddb.lua \
18 18 samples/daytimeclnt.lua \
19TESTS = \ 19 samples/echoclnt.lua \
20 testclnt.lua \ 20 samples/echosrvr.lua \
21 testsrvr.lua \ 21 samples/listener.lua \
22 testsupport.lua 22 samples/lpr.lua \
23 23 samples/talker.lua \
24EXAMPLES = \ 24 samples/tinyirc.lua
25 check-memory.lua \
26 b64.lua \
27 cddb.lua \
28 daytimeclnt.lua \
29 echoclnt.lua \
30 echosrvr.lua \
31 eol.lua \
32 listener.lua \
33 qp.lua \
34 lpr.lua \
35 talker.lua \
36 tinyirc.lua
37 25
38ETC = \ 26ETC = \
39 check-links.lua \ 27 etc/README \
40 check-links-nb.lua \ 28 etc/b64.lua \
41 dict.lua \ 29 etc/check-links.lua \
42 get.lua \ 30 etc/check-memory.lua \
43 unix.c \ 31 etc/dict.lua \
44 unix.h \ 32 etc/dispatch.lua \
45 lp.lua \ 33 etc/eol.lua \
46 tftp.lua 34 etc/forward.lua \
47 35 etc/get.lua \
48CORE = \ 36 etc/links \
49 auxiliar.c \ 37 etc/lp.lua \
50 auxiliar.h \ 38 etc/qp.lua \
51 buffer.c \ 39 etc/tftp.lua
52 buffer.h \ 40
53 except.c \ 41SRC = \
54 except.h \ 42 src/auxiliar.c \
55 inet.c \ 43 src/auxiliar.h \
56 inet.h \ 44 src/buffer.c \
57 io.c \ 45 src/buffer.h \
58 io.h \ 46 src/except.c \
59 luasocket.c \ 47 src/except.h \
60 luasocket.h \ 48 src/inet.c \
61 mime.c \ 49 src/inet.h \
62 mime.h \ 50 src/io.c \
63 options.c \ 51 src/io.h \
64 options.h \ 52 src/luasocket.c \
65 select.c \ 53 src/luasocket.h \
66 select.h \ 54 src/mime.c \
67 socket.h \ 55 src/mime.h \
68 tcp.c \ 56 src/options.c \
69 tcp.h \ 57 src/options.h \
70 timeout.c \ 58 src/select.c \
71 timeout.h \ 59 src/select.h \
72 udp.c \ 60 src/socket.h \
73 udp.h \ 61 src/tcp.c \
74 usocket.c \ 62 src/tcp.h \
75 usocket.h \ 63 src/timeout.c \
76 wsocket.c \ 64 src/timeout.h \
77 wsocket.h 65 src/udp.c \
66 src/udp.h \
67 src/unix.c \
68 src/unix.h \
69 src/usocket.c \
70 src/usocket.h \
71 src/wsocket.c \
72 src/wsocket.h \
73 src/ftp.lua \
74 src/http.lua \
75 src/ltn12.lua \
76 src/mime.lua \
77 src/smtp.lua \
78 src/socket.lua \
79 src/tp.lua \
80 src/url.lua
78 81
79MAKE = \ 82MAKE = \
80 makefile \ 83 makefile \
81 config \ 84 config \
82 luasocket.sln \ 85 luasocket.sln \
83 luasocket.vcproj \ 86 socket.vcproj \
84 mime.vcproj 87 mime.vcproj
85 88
86MANUAL = \ 89DOC = \
87 manual/dns.html \ 90 doc/dns.html \
88 manual/ftp.html \ 91 doc/ftp.html \
89 manual/home.html \ 92 doc/home.html \
90 manual/http.html \ 93 doc/http.html \
91 manual/introduction.html \ 94 doc/installation.html \
92 manual/ltn12.html \ 95 doc/introduction.html \
93 manual/luasocket.png \ 96 doc/ltn12.html \
94 manual/mime.html \ 97 doc/luasocket.png \
95 manual/installation.html \ 98 doc/mime.html \
96 manual/reference.css \ 99 doc/reference.css \
97 manual/reference.html \ 100 doc/reference.html \
98 manual/smtp.html \ 101 doc/smtp.html \
99 manual/socket.html \ 102 doc/socket.html \
100 manual/tcp.html \ 103 doc/tcp.html \
101 manual/udp.html \ 104 doc/udp.html \
102 manual/url.html 105 doc/url.html
103 106
104dist: 107dist:
105 mkdir -p $(DIST)/examples 108 mkdir -p $(DIST)
106 mkdir -p $(DIST)/tests
107 mkdir -p $(DIST)/etc
108 mkdir -p $(DIST)/lua
109 mkdir -p $(DIST)/manual
110 cp -vfr $(COMPAT) $(DIST)
111 cp -vf $(CORE) $(DIST)
112 cp -vf README $(DIST)
113 cp -vf NEW $(DIST) 109 cp -vf NEW $(DIST)
114 cp -vf LICENSE $(DIST) 110 cp -vf LICENSE $(DIST)
115 cp -vf $(MAKE) $(DIST) 111 cp -vf $(MAKE) $(DIST)
116 cp -vf make.README $(DIST)/INSTALL 112
117 cp -vf $(LUA) $(DIST)/lua 113 mkdir -p $(DIST)/etc
118 cp -vf lua.README $(DIST)/lua/README
119 cp -vf $(EXAMPLES) $(DIST)/examples
120 cp -vf examples.README $(DIST)/examples/README
121 cp -vf $(TESTS) $(DIST)/tests
122 cp -vf tests.README $(DIST)/tests/README
123 cp -vf $(ETC) $(DIST)/etc 114 cp -vf $(ETC) $(DIST)/etc
124 cp -vf etc.README $(DIST)/etc/README 115
125 cp -vf $(MANUAL) $(DIST)/manual 116 mkdir -p $(DIST)/src
117 cp -vf $(SRC) $(DIST)/src
118 cp -vfr $(COMPAT) $(DIST)/src
119
120 mkdir -p $(DIST)/doc
121 cp -vf $(DOC) $(DIST)/doc
122
123 mkdir -p $(DIST)/samples
124 cp -vf $(SAMPLES) $(DIST)/samples
125
126 mkdir -p $(DIST)/test
127 cp -vf $(TEST) $(DIST)/test
128
126 tar -zcvf $(DIST).tar.gz $(DIST) 129 tar -zcvf $(DIST).tar.gz $(DIST)
127 zip -r $(DIST).zip $(DIST) 130 zip -r $(DIST).zip $(DIST)
128 131
diff --git a/samples/README b/samples/README
index 0100a4a..e63a6f5 100644
--- a/samples/README
+++ b/samples/README
@@ -1,59 +1,50 @@
1This directory contains some sample programs using LuaSocket. This code 1This directory contains some sample programs using
2is not supported. 2LuaSocket. This code is not supported.
3 3
4 listener.lua -- socket to stdout 4 listener.lua -- socket to stdout
5 talker.lua -- stdin to socket 5 talker.lua -- stdin to socket
6 6
7listener.lua and talker.lua are about the simplest applications you can 7listener.lua and talker.lua are about the simplest
8write using LuaSocket. Run 8applications you can write using LuaSocket. Run
9 9
10 'lua listener.lua' and 'lua talker.lua' 10 'lua listener.lua' and 'lua talker.lua'
11 11
12on different terminals. Whatever you type on talk.lua will be 12on different terminals. Whatever you type on talk.lua will
13printed by listen.lua. 13be printed by listen.lua.
14
15 b64.lua
16 qp.lua
17 eol.lua
18
19These are tiny programs that perform Base64, Quoted-Printable and
20end-of-line marker conversions.
21 14
22 lpr.lua -- lpr client 15 lpr.lua -- lpr client
23 16
24This is a cool program written by David Burgess to print files using the 17This is a cool program written by David Burgess to print
25Line Printer Daemon protocol, widely used in Unix machines. It uses the 18files using the Line Printer Daemon protocol, widely used in
26lp.lua implementation, in the etc directory. Just run 19Unix machines. It uses the lp.lua implementation, in the
27'lua lpr.lua <filename> queue=<printername>' and the file will print! 20etc directory. Just run 'lua lpr.lua <filename>
21queue=<printername>' and the file will print!
28 22
29 cddb.lua -- CDDB client 23 cddb.lua -- CDDB client
30 24
31This is the first try on a simple CDDB client. Not really useful, but one 25This is the first try on a simple CDDB client. Not really
32day it might become a module. 26useful, but one day it might become a module.
33 27
34 daytimeclnt.lua -- day time client 28 daytimeclnt.lua -- day time client
35 29
36Just run the program to retrieve the hour and date in readable form from 30Just run the program to retrieve the hour and date in
37any server running an UDP daytime daemon. 31readable form from any server running an UDP daytime daemon.
38 32
39 echoclnt.lua -- UDP echo client 33 echoclnt.lua -- UDP echo client
40 echosrvr.lua -- UDP echo server 34 echosrvr.lua -- UDP echo server
41 35
42These are a UDP echo client/server pair. They work with other client and 36These are a UDP echo client/server pair. They work with
43servers as well. 37other client and servers as well.
44 38
45 tinyirc.lua -- irc like broadcast server 39 tinyirc.lua -- irc like broadcast server
46 40
47This is a simple server that waits simultaneously on two server sockets 41This is a simple server that waits simultaneously on two
48for telnet connections. Everything it receives from the telnet clients 42server sockets for telnet connections. Everything it
49is broadcasted to every other connected client. It tests the select 43receives from the telnet clients is broadcasted to every
50function and shows how to create a simple server whith LuaSocket. Just 44other connected client. It tests the select function and
51run tinyirc.lua and then open as many telnet connections as you want 45shows how to create a simple server whith LuaSocket. Just
52to ports 8080 and 8081. 46run tinyirc.lua and then open as many telnet connections
53 47as you want to ports 8080 and 8081.
54 check-memory.lua -- checks memory consumption
55
56This is just to see how much memory each module uses.
57 48
58Good luck, 49Good luck,
59Diego. 50Diego.
diff --git a/samples/forward.lua b/samples/forward.lua
deleted file mode 100644
index 548a753..0000000
--- a/samples/forward.lua
+++ /dev/null
@@ -1,207 +0,0 @@
1-- load our favourite library
2local socket = require"socket"
3
4-- creates a new set data structure
5function newset()
6 local reverse = {}
7 local set = {}
8 return setmetatable(set, {__index = {
9 insert = function(set, value)
10 if not reverse[value] then
11 table.insert(set, value)
12 reverse[value] = table.getn(set)
13 end
14 end,
15 remove = function(set, value)
16 local index = reverse[value]
17 if index then
18 reverse[value] = nil
19 local top = table.remove(set)
20 if top ~= value then
21 reverse[top] = index
22 set[index] = top
23 end
24 end
25 end
26 }})
27end
28
29-- timeout before an inactive thread is kicked
30local TIMEOUT = 10
31-- set of connections waiting to receive data
32local receiving = newset(1)
33-- set of sockets waiting to send data
34local sending = newset()
35-- context for connections and servers
36local context = {}
37
38function wait(who, what)
39 if what == "input" then receiving:insert(who)
40 else sending:insert(who) end
41 context[who].last = socket.gettime()
42 coroutine.yield()
43end
44
45-- initializes the forward server
46function init()
47 if table.getn(arg) < 1 then
48 print("Usage")
49 print(" lua forward.lua <iport:ohost:oport> ...")
50 os.exit(1)
51 end
52 -- for each tunnel, start a new server socket
53 for i, v in ipairs(arg) do
54 -- capture forwarding parameters
55 local iport, ohost, oport =
56 socket.skip(2, string.find(v, "([^:]+):([^:]+):([^:]+)"))
57 assert(iport, "invalid arguments")
58 -- create our server socket
59 local server = assert(socket.bind("*", iport))
60 server:settimeout(0) -- we don't want to be killed by bad luck
61 -- make sure server is tested for readability
62 receiving:insert(server)
63 -- add server context
64 context[server] = {
65 thread = coroutine.create(accept),
66 ohost = ohost,
67 oport = oport
68 }
69 end
70end
71
72-- starts a connection in a non-blocking way
73function connect(who, host, port)
74 who:settimeout(0)
75 local ret, err = who:connect(host, port)
76 if not ret and err == "timeout" then
77 wait(who, "output")
78 ret, err = who:connect(host, port)
79 if not ret and err ~= "already connected" then
80 kick(context[who].peer)
81 kick(who)
82 return
83 end
84 end
85 return forward(who)
86end
87
88-- gets rid of a client
89function kick(who)
90 if who then
91 sending:remove(who)
92 receiving:remove(who)
93 who:close()
94 context[who] = nil
95 end
96end
97
98-- loops accepting connections and creating new threads to deal with them
99function accept(server)
100 while true do
101 -- accept a new connection and start a new coroutine to deal with it
102 local client = server:accept()
103 if client then
104 -- create contexts for client and peer.
105 local peer, err = socket.tcp()
106 if peer then
107 context[client] = {
108 last = socket.gettime(),
109 -- client goes straight to forwarding loop
110 thread = coroutine.create(forward),
111 peer = peer,
112 }
113 context[peer] = {
114 last = socket.gettime(),
115 peer = client,
116 -- peer first tries to connect to forwarding address
117 thread = coroutine.create(connect),
118 last = socket.gettime()
119 }
120 -- resume peer and client so they can do their thing
121 local ohost = context[server].ohost
122 local oport = context[server].oport
123 coroutine.resume(context[peer].thread, peer, ohost, oport)
124 coroutine.resume(context[client].thread, client)
125 else
126 print(err)
127 client:close()
128 end
129 end
130 -- tell scheduler we are done for now
131 wait(server, "input")
132 end
133end
134
135-- forwards all data arriving to the appropriate peer
136function forward(who)
137 who:settimeout(0)
138 while true do
139 -- wait until we have something to read
140 wait(who, "input")
141 -- try to read as much as possible
142 local data, rec_err, partial = who:receive("*a")
143 -- if we had an error other than timeout, abort
144 if rec_err and rec_err ~= "timeout" then return kick(who) end
145 -- if we got a timeout, we probably have partial results to send
146 data = data or partial
147 -- forward what we got right away
148 local peer = context[who].peer
149 while true do
150 -- tell scheduler we need to wait until we can send something
151 wait(who, "output")
152 local ret, snd_err
153 local start = 0
154 ret, snd_err, start = peer:send(data, start+1)
155 if ret then break
156 elseif snd_err ~= "timeout" then return kick(who) end
157 end
158 -- if we are done receiving, we are done
159 if not rec_err then
160 kick(who)
161 kick(peer)
162 break
163 end
164 end
165end
166
167-- loop waiting until something happens, restarting the thread to deal with
168-- what happened, and routing it to wait until something else happens
169function go()
170 while true do
171 -- check which sockets are interesting and act on them
172 readable, writable = socket.select(receiving, sending)
173 -- for all readable connections, resume its thread
174 for _, who in ipairs(readable) do
175 if context[who] then
176 receiving:remove(who)
177 coroutine.resume(context[who].thread, who)
178 end
179 end
180 -- for all writable connections, do the same
181 for _, who in ipairs(writable) do
182 if context[who] then
183 sending:remove(who)
184 coroutine.resume(context[who].thread, who)
185 end
186 end
187 -- put all inactive threads in death row
188 local now = socket.gettime()
189 local deathrow
190 for who, data in pairs(context) do
191 if data.peer then
192 if now - data.last > TIMEOUT then
193 -- only create table if at least one is doomed
194 deathrow = deathrow or {}
195 deathrow[who] = true
196 end
197 end
198 end
199 -- finally kick everyone in deathrow
200 if deathrow then
201 for who in pairs(deathrow) do kick(who) end
202 end
203 end
204end
205
206init()
207go()
diff --git a/samples/lpr.lua b/samples/lpr.lua
index caab387..5ab4d69 100644
--- a/samples/lpr.lua
+++ b/samples/lpr.lua
@@ -1,7 +1,7 @@
1local lp = require("socket.lp") 1local lp = require("socket.lp")
2 2
3local function usage() 3local function usage()
4 print('\nUsage: lua lptest.lua [filename] [keyword=val...]\n') 4 print('\nUsage: lua lpr.lua [filename] [keyword=val...]\n')
5 print('Valid keywords are :') 5 print('Valid keywords are :')
6 print( 6 print(
7 ' host=remote host or IP address (default "localhost")\n' .. 7 ' host=remote host or IP address (default "localhost")\n' ..
diff --git a/test/httptest.lua b/test/httptest.lua
index 0951389..c05b4cc 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -22,8 +22,8 @@ http.TIMEOUT = 10
22 22
23local t = socket.gettime() 23local t = socket.gettime()
24 24
25host = host or "dell-diego" -- "diego.student.princeton.edu" 25host = host or "diego.student.princeton.edu"
26proxy = proxy or "http://dell-diego:3128" 26proxy = proxy or "http://localhost:3128"
27prefix = prefix or "/luasocket-test" 27prefix = prefix or "/luasocket-test"
28cgiprefix = cgiprefix or "/luasocket-test-cgi" 28cgiprefix = cgiprefix or "/luasocket-test-cgi"
29index_file = "index.html" 29index_file = "index.html"