From c23240726e3044e3eaa32a82a999b754c08bc183 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Fri, 28 May 2004 20:40:13 +0000 Subject: [PATCH] Changed require"luasocket" to require"socket". --- etc/dict.lua | 2 +- samples/daytimeclnt.lua | 2 +- samples/echoclnt.lua | 2 +- samples/echosrvr.lua | 2 +- src/ftp.lua | 2 +- src/http.lua | 2 +- src/smtp.lua | 2 +- src/socket.lua | 174 ++++++++++++++++++++++++++++++++++++++++ src/url.lua | 2 +- test/mimetest.lua | 2 +- test/testclnt.lua | 2 +- test/testmesg.lua | 2 +- test/testsrvr.lua | 2 +- 13 files changed, 186 insertions(+), 12 deletions(-) create mode 100644 src/socket.lua 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 @@ -- Author: Diego Nehab -- RCS ID: $Id$ ----------------------------------------------------------------------------- -require"luasocket" +require"socket" function get_status(sock, valid) 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 @@ -- Author: Diego Nehab -- RCS ID: $Id$ ----------------------------------------------------------------------------- -require"luasocket" +require"socket" host = host or "127.0.0.1" port = port or 13 if 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 @@ -- Author: Diego Nehab -- RCS ID: $Id$ ----------------------------------------------------------------------------- -require"luasocket" +require"socket" host = host or "localhost" port = port or 7 if 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 @@ -- Author: Diego Nehab -- RCS ID: $Id$ ----------------------------------------------------------------------------- -require"luasocket" +require"socket" host = host or "127.0.0.1" port = port or 7 if 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 @@ -- RCS ID: $Id$ ----------------------------------------------------------------------------- -- make sure LuaSocket is loaded -require("luasocket") +require("socket") -- get LuaSocket namespace local socket = _G[LUASOCKET_LIBNAME] 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 @@ -- RCS ID: $Id$ ----------------------------------------------------------------------------- -- make sure LuaSocket is loaded -require("luasocket") +require("socket") -- get LuaSocket namespace local socket = _G[LUASOCKET_LIBNAME] 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 @@ -- RCS ID: $Id$ ----------------------------------------------------------------------------- -- make sure LuaSocket is loaded -require"luasocket" +require"socket" -- get LuaSocket namespace local socket = _G[LUASOCKET_LIBNAME] 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 @@ +----------------------------------------------------------------------------- +-- LuaSocket helper module +-- Author: Diego Nehab +-- RCS ID: $Id$ +----------------------------------------------------------------------------- + +----------------------------------------------------------------------------- +-- Load LuaSocket from dynamic library +-- Comment these lines if you are loading static +----------------------------------------------------------------------------- +open, err1, err2 = loadlib("luasocket", "luaopen_socket") +if not open then error(err1) end +open() +if not LUASOCKET_LIBNAME then error("LuaSocket init failed") end + +----------------------------------------------------------------------------- +-- Namespace independence +----------------------------------------------------------------------------- +local socket = _G[LUASOCKET_LIBNAME] +if not socket then error('LuaSocket init failed') end + +----------------------------------------------------------------------------- +-- Auxiliar functions +----------------------------------------------------------------------------- +function socket.connect(address, port, laddress, lport) + local sock, err = socket.tcp() + if not sock then return nil, err end + if laddress then + local res, err = sock:bind(laddress, lport, -1) + if not res then return nil, err end + end + local res, err = sock:connect(address, port) + if not res then return nil, err end + return sock +end + +function socket.bind(host, port, backlog) + local sock, err = socket.tcp() + if not sock then return nil, err end + sock:setoption("reuseaddr", true) + local res, err = sock:bind(host, port) + if not res then return nil, err end + backlog = backlog or 1 + res, err = sock:listen(backlog) + if not res then return nil, err end + return sock +end + +function socket.choose(table) + return function(name, opt1, opt2) + if type(name) ~= "string" then + name, opt1, opt2 = "default", name, opt1 + end + local f = table[name or "nil"] + if not f then error("unknown key (" .. tostring(name) .. ")", 3) + else return f(opt1, opt2) end + end +end + +----------------------------------------------------------------------------- +-- Socket sources and sinks, conforming to LTN12 +----------------------------------------------------------------------------- +-- create namespaces inside LuaSocket namespace +socket.sourcet = {} +socket.sinkt = {} + +socket.BLOCKSIZE = 2048 + +socket.sinkt["http-chunked"] = function(sock) + return setmetatable({ + getfd = function() return sock:getfd() end, + dirty = function() return sock:dirty() end + }, { + __call = function(self, chunk, err) + if not chunk then return sock:send("0\r\n\r\n") end + local size = string.format("%X\r\n", string.len(chunk)) + return sock:send(size, chunk, "\r\n") + end + }) +end + +socket.sinkt["close-when-done"] = function(sock) + return setmetatable({ + getfd = function() return sock:getfd() end, + dirty = function() return sock:dirty() end + }, { + __call = function(self, chunk, err) + if not chunk then + sock:close() + return 1 + else return sock:send(chunk) end + end + }) +end + +socket.sinkt["keep-open"] = function(sock) + return setmetatable({ + getfd = function() return sock:getfd() end, + dirty = function() return sock:dirty() end + }, { + __call = function(self, chunk, err) + return sock:send(chunk) + end + }) +end + +socket.sinkt["default"] = socket.sinkt["keep-open"] + +socket.sink = socket.choose(socket.sinkt) + +socket.sourcet["by-length"] = function(sock, length) + return setmetatable({ + getfd = function() return sock:getfd() end, + dirty = function() return sock:dirty() end + }, { + __call = function() + if length <= 0 then return nil end + local size = math.min(socket.BLOCKSIZE, length) + local chunk, err = sock:receive(size) + if err then return nil, err end + length = length - string.len(chunk) + return chunk + end + }) +end + +socket.sourcet["until-closed"] = function(sock) + return setmetatable({ + getfd = function() return sock:getfd() end, + dirty = function() return sock:dirty() end + }, { + __call = ltn12.source.simplify(function() + local chunk, err, partial = sock:receive(socket.BLOCKSIZE) + if not err then return chunk + elseif err == "closed" then + sock:close() + return partial, ltn12.source.empty() + else return nil, err end + end) + }) +end + +socket.sourcet["http-chunked"] = function(sock) + return setmetatable({ + getfd = function() return sock:getfd() end, + dirty = function() return sock:dirty() end + }, { + __call = function() + -- get chunk size, skip extention + local line, err = sock:receive() + if err then return nil, err end + local size = tonumber(string.gsub(line, ";.*", ""), 16) + if not size then return nil, "invalid chunk size" end + -- was it the last chunk? + if size <= 0 then + -- skip trailer headers, if any + local line, err = sock:receive() + while not err and line ~= "" do + line, err = sock:receive() + end + return nil, err + else + -- get chunk and skip terminating CRLF + local chunk, err = sock:receive(size) + if err or socket.skip(2, sock:receive()) then return nil, err + else return chunk end + end + end + }) +end + +socket.sourcet["default"] = socket.sourcet["until-closed"] + +socket.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 @@ -- RCS ID: $Id$ ---------------------------------------------------------------------------- -- make sure LuaSocket is loaded -require"luasocket" +require("socket") -- get LuaSocket namespace local socket = _G[LUASOCKET_LIBNAME] -- 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 @@ -require "luasocket" +require "socket" require "ltn12" require "mime" 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 @@ -require "luasocket" +require"socket" host = host or "localhost" port = 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 = { -- sink = ltn12.sink.file(io.stdout) -- source = ltn12.source.chain(socket.smtp.message(mesgt), socket.smtp.stuff()) --- ltn12.pump(source, sink) +-- ltn12.pump.all(source, sink) print(socket.smtp.send { rcpt = "", 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 @@ -require "luasocket" +require"socket" host = host or "localhost" port = port or "8080"