Mercurial > prosody-hg
annotate util/template.lua @ 13652:a08065207ef0
net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:
This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.
This depends on LuaSec changes which are not yet upstream.
From Martijn's original email:
So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
Note that the requirement for close() on a socket to block for up to
the current linger interval is not conditional on the O_NONBLOCK
setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.
For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
| author | Martijn van Duren <martijn@openbsd.org> |
|---|---|
| date | Thu, 06 Feb 2025 15:04:38 +0000 |
| parents | d10957394a3c |
| children |
| rev | line source |
|---|---|
|
7196
94ec474debf5
util.template: Silence luacheck warnings about unused loop vars
Kim Alvefur <zash@zash.se>
parents:
6777
diff
changeset
|
1 -- luacheck: ignore 213/i |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
2 local stanza_mt = require "prosody.util.stanza".stanza_mt; |
|
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 local setmetatable = setmetatable; |
|
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 local pairs = pairs; |
|
3637
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
5 local ipairs = ipairs; |
|
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 local error = error; |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
7 local envload = require "prosody.util.envload".envload; |
|
3637
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
8 local debug = debug; |
|
4495
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
9 local t_remove = table.remove; |
|
12975
d10957394a3c
util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
10 local parse_xml = require "prosody.util.xml".parse; |
|
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 |
|
6777
5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents:
5214
diff
changeset
|
12 local _ENV = nil; |
|
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8418
diff
changeset
|
13 -- luacheck: std none |
|
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 |
|
4495
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
15 local function trim_xml(stanza) |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
16 for i=#stanza,1,-1 do |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
17 local child = stanza[i]; |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
18 if child.name then |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
19 trim_xml(child); |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
20 else |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
21 child = child:gsub("^%s*", ""):gsub("%s*$", ""); |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
22 stanza[i] = child; |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
23 if child == "" then t_remove(stanza, i); end |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
24 end |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
25 end |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
26 end |
|
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
27 |
|
3637
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
28 local function create_string_string(str) |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
29 str = ("%q"):format(str); |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
30 str = str:gsub("{([^}]*)}", function(s) |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
31 return '"..(data["'..s..'"]or"").."'; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
32 end); |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
33 return str; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
34 end |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
35 local function create_attr_string(attr, xmlns) |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
36 local str = '{'; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
37 for name,value in pairs(attr) do |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
38 if name ~= "xmlns" or value ~= xmlns then |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
39 str = str..("[%q]=%s;"):format(name, create_string_string(value)); |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
40 end |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
41 end |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
42 return str..'}'; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
43 end |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
44 local function create_clone_string(stanza, lookup, xmlns) |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
45 if not lookup[stanza] then |
|
3640
4bc88bb748d1
util.template: Don't add stanza.last_add. 20% faster.
Waqas Hussain <waqas20@gmail.com>
parents:
3637
diff
changeset
|
46 local s = ('setmetatable({name=%q,attr=%s,tags={'):format(stanza.name, create_attr_string(stanza.attr, xmlns)); |
|
3637
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
47 -- add tags |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
48 for i,tag in ipairs(stanza.tags) do |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
49 s = s..create_clone_string(tag, lookup, stanza.attr.xmlns)..";"; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
50 end |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
51 s = s..'};'; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
52 -- add children |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
53 for i,child in ipairs(stanza) do |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
54 if child.name then |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
55 s = s..create_clone_string(child, lookup, stanza.attr.xmlns)..";"; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
56 else |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
57 s = s..create_string_string(child)..";" |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
58 end |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
59 end |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
60 s = s..'}, stanza_mt)'; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
61 s = s:gsub('%.%.""', ""):gsub('([=;])""%.%.', "%1"):gsub(';"";', ";"); -- strip empty strings |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
62 local n = #lookup + 1; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
63 lookup[n] = s; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
64 lookup[stanza] = "_"..n; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
65 end |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
66 return lookup[stanza]; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
67 end |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
68 local function create_cloner(stanza, chunkname) |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
69 local lookup = {}; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
70 local name = create_clone_string(stanza, lookup, ""); |
|
7197
ff514c1b1c27
util.template: Use separate variables for source and compiled function [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7196
diff
changeset
|
71 local src = "local setmetatable,stanza_mt=...;return function(data)"; |
|
3637
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
72 for i=1,#lookup do |
|
7197
ff514c1b1c27
util.template: Use separate variables for source and compiled function [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7196
diff
changeset
|
73 src = src.."local _"..i.."="..lookup[i]..";"; |
|
3546
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
74 end |
|
7197
ff514c1b1c27
util.template: Use separate variables for source and compiled function [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7196
diff
changeset
|
75 src = src.."return "..name..";end"; |
|
8418
ad1e10c93b41
util.template: Use util.envload instead of loadstring which is deprecated in Lua 5.2
Kim Alvefur <zash@zash.se>
parents:
7197
diff
changeset
|
76 local f,err = envload(src, chunkname); |
|
3637
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
77 if not f then error(err); end |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
78 return f(setmetatable, stanza_mt); |
|
3546
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
79 end |
|
cb1600dea3ad
util.template: Optimized to be almost as fast as manual stanza building.
Waqas Hussain <waqas20@gmail.com>
parents:
3545
diff
changeset
|
80 |
|
3637
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
81 local template_mt = { __tostring = function(t) return t.name end }; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
82 local function create_template(templates, text) |
|
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 local stanza, err = parse_xml(text); |
|
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 if not stanza then error(err); end |
|
4495
c0f5c78cb817
util.template: Refactoring to make the string->stanza conversion code more generic.
Waqas Hussain <waqas20@gmail.com>
parents:
3640
diff
changeset
|
85 trim_xml(stanza); |
|
3637
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
86 |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
87 local info = debug.getinfo(3, "Sl"); |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
88 info = info and ("template(%s:%d)"):format(info.short_src:match("[^\\/]*$"), info.currentline) or "template(unknown)"; |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
89 |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
90 local template = setmetatable({ apply = create_cloner(stanza, info), name = info, text = text }, template_mt); |
|
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
91 templates[text] = template; |
|
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 return template; |
|
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 end |
|
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 |
|
3637
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
95 local templates = setmetatable({}, { __mode = 'k', __index = create_template }); |
|
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 return function(text) |
|
3637
bd491def3efb
util.template: Rewritten to be much faster than the util.stanza stanza building API.
Waqas Hussain <waqas20@gmail.com>
parents:
3546
diff
changeset
|
97 return templates[text]; |
|
3545
c85f9a4ae1c4
util.template: Initial commit. A template library for XML stanzas.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 end; |
