aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-05-28 20:40:13 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-05-28 20:40:13 +0000
commitc23240726e3044e3eaa32a82a999b754c08bc183 (patch)
treeb04e29506e57795eda7ffdb6b3164801664a92b1
parent6c565dd7c06c96fd9b3394ade61f46da59590d30 (diff)
downloadluasocket-c23240726e3044e3eaa32a82a999b754c08bc183.tar.gz
luasocket-c23240726e3044e3eaa32a82a999b754c08bc183.tar.bz2
luasocket-c23240726e3044e3eaa32a82a999b754c08bc183.zip
Changed require"luasocket" to require"socket".
-rw-r--r--etc/dict.lua2
-rw-r--r--samples/daytimeclnt.lua2
-rw-r--r--samples/echoclnt.lua2
-rw-r--r--samples/echosrvr.lua2
-rw-r--r--src/ftp.lua2
-rw-r--r--src/http.lua2
-rw-r--r--src/smtp.lua2
-rw-r--r--src/socket.lua174
-rw-r--r--src/url.lua2
-rw-r--r--test/mimetest.lua2
-rw-r--r--test/testclnt.lua2
-rw-r--r--test/testmesg.lua2
-rw-r--r--test/testsrvr.lua2
13 files changed, 186 insertions, 12 deletions
diff --git a/etc/dict.lua b/etc/dict.lua
index 31359d9..e5d4740 100644
--- a/etc/dict.lua
+++ b/etc/dict.lua
@@ -4,7 +4,7 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"luasocket" 7require"socket"
8 8
9function get_status(sock, valid) 9function get_status(sock, valid)
10 local line, err = sock:receive() 10 local line, err = sock:receive()
diff --git a/samples/daytimeclnt.lua b/samples/daytimeclnt.lua
index ee7f652..b4075e9 100644
--- a/samples/daytimeclnt.lua
+++ b/samples/daytimeclnt.lua
@@ -4,7 +4,7 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"luasocket" 7require"socket"
8host = host or "127.0.0.1" 8host = host or "127.0.0.1"
9port = port or 13 9port = port or 13
10if arg then 10if arg then
diff --git a/samples/echoclnt.lua b/samples/echoclnt.lua
index a3d75f3..877e99b 100644
--- a/samples/echoclnt.lua
+++ b/samples/echoclnt.lua
@@ -4,7 +4,7 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"luasocket" 7require"socket"
8host = host or "localhost" 8host = host or "localhost"
9port = port or 7 9port = port or 7
10if arg then 10if arg then
diff --git a/samples/echosrvr.lua b/samples/echosrvr.lua
index 9d99506..79bff8f 100644
--- a/samples/echosrvr.lua
+++ b/samples/echosrvr.lua
@@ -4,7 +4,7 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"luasocket" 7require"socket"
8host = host or "127.0.0.1" 8host = host or "127.0.0.1"
9port = port or 7 9port = port or 7
10if arg then 10if arg then
diff --git a/src/ftp.lua b/src/ftp.lua
index f197018..72be695 100644
--- a/src/ftp.lua
+++ b/src/ftp.lua
@@ -6,7 +6,7 @@
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded 8-- make sure LuaSocket is loaded
9require("luasocket") 9require("socket")
10-- get LuaSocket namespace 10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME] 11local socket = _G[LUASOCKET_LIBNAME]
12 12
diff --git a/src/http.lua b/src/http.lua
index 7dee8e6..b372a2e 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -6,7 +6,7 @@
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded 8-- make sure LuaSocket is loaded
9require("luasocket") 9require("socket")
10-- get LuaSocket namespace 10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME] 11local socket = _G[LUASOCKET_LIBNAME]
12 12
diff --git a/src/smtp.lua b/src/smtp.lua
index 8b5e622..01babbe 100644
--- a/src/smtp.lua
+++ b/src/smtp.lua
@@ -6,7 +6,7 @@
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7----------------------------------------------------------------------------- 7-----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded 8-- make sure LuaSocket is loaded
9require"luasocket" 9require"socket"
10-- get LuaSocket namespace 10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME] 11local socket = _G[LUASOCKET_LIBNAME]
12 12
diff --git a/src/socket.lua b/src/socket.lua
new file mode 100644
index 0000000..e6e20f2
--- /dev/null
+++ b/src/socket.lua
@@ -0,0 +1,174 @@
1-----------------------------------------------------------------------------
2-- LuaSocket helper module
3-- Author: Diego Nehab
4-- RCS ID: $Id$
5-----------------------------------------------------------------------------
6
7-----------------------------------------------------------------------------
8-- Load LuaSocket from dynamic library
9-- Comment these lines if you are loading static
10-----------------------------------------------------------------------------
11open, err1, err2 = loadlib("luasocket", "luaopen_socket")
12if not open then error(err1) end
13open()
14if not LUASOCKET_LIBNAME then error("LuaSocket init failed") end
15
16-----------------------------------------------------------------------------
17-- Namespace independence
18-----------------------------------------------------------------------------
19local socket = _G[LUASOCKET_LIBNAME]
20if not socket then error('LuaSocket init failed') end
21
22-----------------------------------------------------------------------------
23-- Auxiliar functions
24-----------------------------------------------------------------------------
25function socket.connect(address, port, laddress, lport)
26 local sock, err = socket.tcp()
27 if not sock then return nil, err end
28 if laddress then
29 local res, err = sock:bind(laddress, lport, -1)
30 if not res then return nil, err end
31 end
32 local res, err = sock:connect(address, port)
33 if not res then return nil, err end
34 return sock
35end
36
37function socket.bind(host, port, backlog)
38 local sock, err = socket.tcp()
39 if not sock then return nil, err end
40 sock:setoption("reuseaddr", true)
41 local res, err = sock:bind(host, port)
42 if not res then return nil, err end
43 backlog = backlog or 1
44 res, err = sock:listen(backlog)
45 if not res then return nil, err end
46 return sock
47end
48
49function socket.choose(table)
50 return function(name, opt1, opt2)
51 if type(name) ~= "string" then
52 name, opt1, opt2 = "default", name, opt1
53 end
54 local f = table[name or "nil"]
55 if not f then error("unknown key (" .. tostring(name) .. ")", 3)
56 else return f(opt1, opt2) end
57 end
58end
59
60-----------------------------------------------------------------------------
61-- Socket sources and sinks, conforming to LTN12
62-----------------------------------------------------------------------------
63-- create namespaces inside LuaSocket namespace
64socket.sourcet = {}
65socket.sinkt = {}
66
67socket.BLOCKSIZE = 2048
68
69socket.sinkt["http-chunked"] = function(sock)
70 return setmetatable({
71 getfd = function() return sock:getfd() end,
72 dirty = function() return sock:dirty() end
73 }, {
74 __call = function(self, chunk, err)
75 if not chunk then return sock:send("0\r\n\r\n") end
76 local size = string.format("%X\r\n", string.len(chunk))
77 return sock:send(size, chunk, "\r\n")
78 end
79 })
80end
81
82socket.sinkt["close-when-done"] = function(sock)
83 return setmetatable({
84 getfd = function() return sock:getfd() end,
85 dirty = function() return sock:dirty() end
86 }, {
87 __call = function(self, chunk, err)
88 if not chunk then
89 sock:close()
90 return 1
91 else return sock:send(chunk) end
92 end
93 })
94end
95
96socket.sinkt["keep-open"] = function(sock)
97 return setmetatable({
98 getfd = function() return sock:getfd() end,
99 dirty = function() return sock:dirty() end
100 }, {
101 __call = function(self, chunk, err)
102 return sock:send(chunk)
103 end
104 })
105end
106
107socket.sinkt["default"] = socket.sinkt["keep-open"]
108
109socket.sink = socket.choose(socket.sinkt)
110
111socket.sourcet["by-length"] = function(sock, length)
112 return setmetatable({
113 getfd = function() return sock:getfd() end,
114 dirty = function() return sock:dirty() end
115 }, {
116 __call = function()
117 if length <= 0 then return nil end
118 local size = math.min(socket.BLOCKSIZE, length)
119 local chunk, err = sock:receive(size)
120 if err then return nil, err end
121 length = length - string.len(chunk)
122 return chunk
123 end
124 })
125end
126
127socket.sourcet["until-closed"] = function(sock)
128 return setmetatable({
129 getfd = function() return sock:getfd() end,
130 dirty = function() return sock:dirty() end
131 }, {
132 __call = ltn12.source.simplify(function()
133 local chunk, err, partial = sock:receive(socket.BLOCKSIZE)
134 if not err then return chunk
135 elseif err == "closed" then
136 sock:close()
137 return partial, ltn12.source.empty()
138 else return nil, err end
139 end)
140 })
141end
142
143socket.sourcet["http-chunked"] = function(sock)
144 return setmetatable({
145 getfd = function() return sock:getfd() end,
146 dirty = function() return sock:dirty() end
147 }, {
148 __call = function()
149 -- get chunk size, skip extention
150 local line, err = sock:receive()
151 if err then return nil, err end
152 local size = tonumber(string.gsub(line, ";.*", ""), 16)
153 if not size then return nil, "invalid chunk size" end
154 -- was it the last chunk?
155 if size <= 0 then
156 -- skip trailer headers, if any
157 local line, err = sock:receive()
158 while not err and line ~= "" do
159 line, err = sock:receive()
160 end
161 return nil, err
162 else
163 -- get chunk and skip terminating CRLF
164 local chunk, err = sock:receive(size)
165 if err or socket.skip(2, sock:receive()) then return nil, err
166 else return chunk end
167 end
168 end
169 })
170end
171
172socket.sourcet["default"] = socket.sourcet["until-closed"]
173
174socket.source = socket.choose(socket.sourcet)
diff --git a/src/url.lua b/src/url.lua
index b385d88..16b19e0 100644
--- a/src/url.lua
+++ b/src/url.lua
@@ -6,7 +6,7 @@
6-- RCS ID: $Id$ 6-- RCS ID: $Id$
7---------------------------------------------------------------------------- 7----------------------------------------------------------------------------
8-- make sure LuaSocket is loaded 8-- make sure LuaSocket is loaded
9require"luasocket" 9require("socket")
10-- get LuaSocket namespace 10-- get LuaSocket namespace
11local socket = _G[LUASOCKET_LIBNAME] 11local socket = _G[LUASOCKET_LIBNAME]
12-- create url namespace inside LuaSocket namespace 12-- create url namespace inside LuaSocket namespace
diff --git a/test/mimetest.lua b/test/mimetest.lua
index dea43d6..8433786 100644
--- a/test/mimetest.lua
+++ b/test/mimetest.lua
@@ -1,4 +1,4 @@
1require "luasocket" 1require "socket"
2require "ltn12" 2require "ltn12"
3require "mime" 3require "mime"
4 4
diff --git a/test/testclnt.lua b/test/testclnt.lua
index f5c0361..1825007 100644
--- a/test/testclnt.lua
+++ b/test/testclnt.lua
@@ -1,4 +1,4 @@
1require "luasocket" 1require"socket"
2 2
3host = host or "localhost" 3host = host or "localhost"
4port = port or "8080" 4port = port or "8080"
diff --git a/test/testmesg.lua b/test/testmesg.lua
index 8b33133..1e3ece2 100644
--- a/test/testmesg.lua
+++ b/test/testmesg.lua
@@ -43,7 +43,7 @@ mesgt = {
43 43
44-- sink = ltn12.sink.file(io.stdout) 44-- sink = ltn12.sink.file(io.stdout)
45-- source = ltn12.source.chain(socket.smtp.message(mesgt), socket.smtp.stuff()) 45-- source = ltn12.source.chain(socket.smtp.message(mesgt), socket.smtp.stuff())
46-- ltn12.pump(source, sink) 46-- ltn12.pump.all(source, sink)
47 47
48print(socket.smtp.send { 48print(socket.smtp.send {
49 rcpt = "<diego@cs.princeton.edu>", 49 rcpt = "<diego@cs.princeton.edu>",
diff --git a/test/testsrvr.lua b/test/testsrvr.lua
index 52f1f90..cf94b45 100644
--- a/test/testsrvr.lua
+++ b/test/testsrvr.lua
@@ -1,4 +1,4 @@
1require "luasocket" 1require"socket"
2 2
3host = host or "localhost" 3host = host or "localhost"
4port = port or "8080" 4port = port or "8080"