- local ok, err = stream:feed(data);
- if ok then return; end
- log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
- session:close("not-well-formed");
+ data = filter("bytes/in", data);
+ if data then
+ local ok, err = stream:feed(data);
+ if not ok then
+ log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_"));
+ session:close("not-well-formed");
+ end
+ end
+ end
+
+ if co_running() ~= session.thread and not session.paused then
+ if session.state == "wait" then
+ session.state = "ready";
+ local ok, state = co_resume(session.thread);
+ if not ok then
+ log("error", "Traceback[c2s]: %s", state);
+ elseif state == "wait" then
+ return;
+ end
+ end
+ -- We're not currently running, so start the thread to process pending stanzas
+ local s, thread = session.pending_stanzas, session.thread;
+ local n = #s;
+ while n > 0 and session.state ~= "wait" do
+ session.log("debug", "processing %d stanzas", n);
+ local consumed;
+ for i = 1,n do
+ local stanza = s[i];
+ local ok, state = co_resume(thread, stanza);
+ if not ok then
+ log("error", "Traceback[c2s]: %s", state);
+ elseif state == "wait" then
+ consumed = i;
+ session.state = "wait";
+ break;
+ end
+ end
+ if not consumed then consumed = n; end
+ for i = 1, #s do
+ s[i] = s[consumed+i];
+ end
+ n = #s;
+ end