Skip to content

Commit

Permalink
StreamOpen: Impl fromXml() using QXmlStreamReader
Browse files Browse the repository at this point in the history
  • Loading branch information
lnjX committed Apr 28, 2024
1 parent eea6556 commit 9325554
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
32 changes: 31 additions & 1 deletion src/base/QXmppStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,36 @@ void QXmppStream::setSocket(QSslSocket *socket)

namespace QXmpp::Private {

StreamOpen StreamOpen::fromXml(QXmlStreamReader &reader)
{
Q_ASSERT(reader.isStartElement());
Q_ASSERT(reader.name() == u"stream");
Q_ASSERT(reader.namespaceUri() == ns_stream);

StreamOpen out;
const auto attributes = reader.attributes();
auto attribute = [&](QStringView ns, QStringView name) {
for (const auto &a : attributes) {
if (a.name() == name && a.namespaceUri() == ns) {
return a.value().toString();
}
}
return QString();
};

out.from = attribute({}, u"from");
out.to = attribute({}, u"to");

const auto namespaceDeclarations = reader.namespaceDeclarations();
for (const auto &ns : namespaceDeclarations) {
if (ns.prefix().isEmpty()) {
out.xmlns = ns.namespaceUri().toString();
}
}

return out;
}

void StreamOpen::toXml(QXmlStreamWriter *writer) const
{
writer->writeStartDocument();
Expand All @@ -133,7 +163,7 @@ void StreamOpen::toXml(QXmlStreamWriter *writer) const
}
writer->writeAttribute(QSL65("to"), to);
writer->writeAttribute(QSL65("version"), QSL65("1.0"));
writer->writeDefaultNamespace(toString65(xmlns));
writer->writeDefaultNamespace(xmlns);
writer->writeNamespace(toString65(ns_stream), QSL65("stream"));
writer->writeCharacters({});
}
Expand Down
4 changes: 3 additions & 1 deletion src/base/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@

#include <QString>

class QXmlStreamReader;
class QXmlStreamWriter;

namespace QXmpp::Private {

struct StreamOpen {
static StreamOpen fromXml(QXmlStreamReader &reader);
void toXml(QXmlStreamWriter *) const;

QString to;
QString from;
QStringView xmlns;
QString xmlns;
};

} // namespace QXmpp::Private
Expand Down
2 changes: 1 addition & 1 deletion src/client/QXmppOutgoingClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ void QXmppOutgoingClient::handleStart()
d->socket.sendData(serializeXml(StreamOpen {
d->config.domain(),
d->config.user().isEmpty() ? QString() : d->config.jidBare(),
ns_client,
ns_client.toString(),
}));
}

Expand Down
12 changes: 11 additions & 1 deletion tests/qxmppstream/tst_qxmppstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,18 @@ void tst_QXmppStream::testProcessData()
void tst_QXmppStream::streamOpen()
{
auto xml = "<?xml version='1.0' encoding='UTF-8'?><stream:stream from='[email protected]' to='im.example.com' version='1.0' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
StreamOpen s { "im.example.com", "[email protected]", ns_client };

StreamOpen s { "im.example.com", "[email protected]", ns_client.toString() };
serializePacket(s, xml);

QXmlStreamReader r(xml);
QCOMPARE(r.readNext(), QXmlStreamReader::StartDocument);
QCOMPARE(r.readNext(), QXmlStreamReader::StartElement);
auto streamOpen = StreamOpen::fromXml(r);
QVERIFY(streamOpen.has_value());
QCOMPARE(streamOpen->from, "[email protected]");
QCOMPARE(streamOpen->to, "im.example.com");
QCOMPARE(streamOpen->xmlns, ns_client);
}

void tst_QXmppStream::testStreamError()
Expand Down

0 comments on commit 9325554

Please sign in to comment.