Skip to content

Commit

Permalink
feat(socket) improve wrapper compatibility
Browse files Browse the repository at this point in the history
prevenbt hard errors from LuaSec non-implemented methods. Add
getalpn and getsniname for LuaSec sockets
  • Loading branch information
Tieske committed Jan 11, 2023
1 parent 6313484 commit 27fb243
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ <h2><a name="history"></a>History</h2>
<li>Fix: creating a new timer with a bad delay setting would throw a bad error message.</li>
<li>Refactor: submodules are now part of copas (lazily loaded) and do not need to be required anymore</li>
<li>Feat: runtime script added to directly run copas based code</li>
<li>Fix: improved socket wrapper for lacking LuaSec methods (<code>setoption, getoption, getpeername, getsockname</code>)</li>
<li>Feat: added LuaSec methods to wrapper (<code>getalpn, getsniname</code>)</li>
</ul></dd>

<dt><strong>Copas 4.6.0</strong> [30/Dec/2022]</dt>
Expand Down
53 changes: 50 additions & 3 deletions src/copas.lua
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,14 @@ local _skt_mt_tcp = {
bind = function(self, ...) return self.socket:bind(...) end,

-- TODO: is this DNS related? hence blocking?
getsockname = function(self, ...) return self.socket:getsockname(...) end,
getsockname = function(self, ...)
local ok, ip, port, family = pcall(self.socket.getsockname, self.socket, ...)
if ok then
return ip, port, family
else
return nil, "not implemented by LuaSec"
end
end,

getstats = function(self, ...) return self.socket:getstats(...) end,

Expand All @@ -926,10 +933,33 @@ local _skt_mt_tcp = {

accept = function(self, ...) return self.socket:accept(...) end,

setoption = function(self, ...) return self.socket:setoption(...) end,
setoption = function(self, ...)
local ok, res, err = pcall(self.socket.setoption, self.socket, ...)
if ok then
return res, err
else
return nil, "not implemented by LuaSec"
end
end,

getoption = function(self, ...)
local ok, val, err = pcall(self.socket.getoption, self.socket, ...)
if ok then
return val, err
else
return nil, "not implemented by LuaSec"
end
end,

-- TODO: is this DNS related? hence blocking?
getpeername = function(self, ...) return self.socket:getpeername(...) end,
getpeername = function(self, ...)
local ok, ip, port, family = pcall(self.socket.getpeername, self.socket, ...)
if ok then
return ip, port, family
else
return nil, "not implemented by LuaSec"
end
end,

shutdown = function(self, ...) return self.socket:shutdown(...) end,

Expand All @@ -950,6 +980,23 @@ local _skt_mt_tcp = {
return self
end,

getalpn = function(self, ...)
local ok, proto, err = pcall(self.socket.getalpn, self.socket, ...)
if ok then
return proto, err
else
return nil, "not a tls socket"
end
end,

getsniname = function(self, ...)
local ok, name, err = pcall(self.socket.getsniname, self.socket, ...)
if ok then
return name, err
else
return nil, "not a tls socket"
end
end,
}
}

Expand Down

0 comments on commit 27fb243

Please sign in to comment.