Annotate

fallbacks/lxp.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
parent 5776:bd0ff8ae98a8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3804
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local coroutine = coroutine;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 local tonumber = tonumber;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 local string = string;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local setmetatable, getmetatable = setmetatable, getmetatable;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 local pairs = pairs;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 local deadroutine = coroutine.create(function() end);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 coroutine.resume(deadroutine);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 module("lxp")
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13 local entity_map = setmetatable({
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 ["amp"] = "&";
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 ["gt"] = ">";
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 ["lt"] = "<";
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17 ["apos"] = "'";
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 ["quot"] = "\"";
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 }, {__index = function(_, s)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 if s:sub(1,1) == "#" then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 if s:sub(2,2) == "x" then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 return string.char(tonumber(s:sub(3), 16));
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 else
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 return string.char(tonumber(s:sub(2)));
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 });
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 local function xml_unescape(str)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 return (str:gsub("&(.-);", entity_map));
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32 local function parse_tag(s)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 local name,sattr=(s):gmatch("([^%s]+)(.*)")();
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34 local attr = {};
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 for a,b in (sattr):gmatch("([^=%s]+)=['\"]([^'\"]*)['\"]") do attr[a] = xml_unescape(b); end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 return name, attr;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 local function parser(data, handlers, ns_separator)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 local function read_until(str)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 local pos = data:find(str, nil, true);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 while not pos do
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 data = data..coroutine.yield();
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 pos = data:find(str, nil, true);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 local r = data:sub(1, pos);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 data = data:sub(pos+1);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 return r;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 local function read_before(str)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 local pos = data:find(str, nil, true);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 while not pos do
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 data = data..coroutine.yield();
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 pos = data:find(str, nil, true);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 local r = data:sub(1, pos-1);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 data = data:sub(pos);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 return r;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 local function peek()
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 while #data == 0 do data = coroutine.yield(); end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 return data:sub(1,1);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 3899
diff changeset
64
3804
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 local ns = { xml = "http://www.w3.org/XML/1998/namespace" };
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 ns.__index = ns;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 local function apply_ns(name, dodefault)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 local prefix,n = name:match("^([^:]*):(.*)$");
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 if prefix and ns[prefix] then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70 return ns[prefix]..ns_separator..n;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72 if dodefault and ns[""] then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 return ns[""]..ns_separator..name;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 return name;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 local function push(tag, attr)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 ns = setmetatable({}, ns);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 for k,v in pairs(attr) do
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 local xmlns = k == "xmlns" and "" or k:match("^xmlns:(.*)$");
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81 if xmlns then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 ns[xmlns] = v;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 attr[k] = nil;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 local newattr, n = {}, 0;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 for k,v in pairs(attr) do
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 n = n+1;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 k = apply_ns(k);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 newattr[n] = k;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 newattr[k] = v;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
92 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 tag = apply_ns(tag, true);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 ns[0] = tag;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
95 ns.__index = ns;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
96 return tag, newattr;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 local function pop()
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
99 local tag = ns[0];
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
100 ns = getmetatable(ns);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 return tag;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 3899
diff changeset
103
3804
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
104 while true do
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
105 if peek() == "<" then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
106 local elem = read_until(">"):sub(2,-2);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
107 if elem:sub(1,1) == "!" or elem:sub(1,1) == "?" then -- neglect comments and processing-instructions
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
108 elseif elem:sub(1,1) == "/" then -- end tag
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
109 elem = elem:sub(2);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
110 local name = pop();
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
111 handlers:EndElement(name); -- TODO check for start-end tag name match
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
112 elseif elem:sub(-1,-1) == "/" then -- empty tag
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
113 elem = elem:sub(1,-2);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
114 local name,attr = parse_tag(elem);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
115 name,attr = push(name,attr);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
116 handlers:StartElement(name,attr);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
117 name = pop();
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 handlers:EndElement(name);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119 else -- start tag
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 local name,attr = parse_tag(elem);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 name,attr = push(name,attr);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 handlers:StartElement(name,attr);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 else
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
125 local text = read_before("<");
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 handlers:CharacterData(xml_unescape(text));
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
127 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
129 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131 function new(handlers, ns_separator)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
132 local co = coroutine.create(parser);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 return {
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134 parse = function(self, data)
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 if not data then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 co = deadroutine;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 return true; -- eof
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
138 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
139 local success, result = coroutine.resume(co, data, handlers, ns_separator);
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 if result then
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
141 co = deadroutine;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
142 return nil, result; -- error
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
143 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
144 return true; -- success
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
145 end;
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
146 };
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
147 end
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
148
da75eb23dbba fallbacks/lxp.lua: Pure Lua pseudo-XML parser. Implements the same API as LuaExpat.
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
149 return _M;