From d5cd90373b208f19be922a2e5a802e9b09466446 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Thu, 26 May 2022 11:48:55 +0800 Subject: [PATCH] fix: improve the portal config parsing --- GPClient/portalconfigresponse.cpp | 70 ++++++++++++++----------------- GPClient/portalconfigresponse.h | 6 +-- 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/GPClient/portalconfigresponse.cpp b/GPClient/portalconfigresponse.cpp index c5c578f..f44ca37 100644 --- a/GPClient/portalconfigresponse.cpp +++ b/GPClient/portalconfigresponse.cpp @@ -78,10 +78,7 @@ QList PortalConfigResponse::parseGateways(QXmlStreamReader &xmlReader // Parse the gateways -> external -> list -> entry if (xmlReader.name() == "entry" && xmlReader.isStartElement()) { GPGateway g; - QString address = xmlReader.attributes().value("name").toString(); - g.setAddress(address); - g.setPriorityRules(parsePriorityRules(xmlReader)); - g.setName(parseGatewayName(xmlReader)); + parseGateway(xmlReader, g); gateways.append(g); } } @@ -91,45 +88,44 @@ QList PortalConfigResponse::parseGateways(QXmlStreamReader &xmlReader return gateways; } -QMap PortalConfigResponse::parsePriorityRules(QXmlStreamReader &xmlReader) -{ - PLOGI << "Start parsing the priority rules..."; +void PortalConfigResponse::parseGateway(QXmlStreamReader &reader, GPGateway &gateway) { + PLOGI << "Start parsing gateway..."; - QMap priorityRules; - - while ((xmlReader.name() != "priority-rule" || !xmlReader.isEndElement()) && !xmlReader.hasError()) { - xmlReader.readNext(); - - if (xmlReader.name() == "entry" && xmlReader.isStartElement()) { - QString ruleName = xmlReader.attributes().value("name").toString(); - // Read the priority tag - while (xmlReader.name() != "priority"){ - xmlReader.readNext(); - } - int ruleValue = xmlReader.readElementText().toUInt(); - priorityRules.insert(ruleName, ruleValue); + auto finished = false; + while (!finished) { + if (reader.name() == "entry") { + auto address = reader.attributes().value("name").toString(); + gateway.setAddress(address); + } else if (reader.name() == "description") { // gateway name + gateway.setName(reader.readElementText()); + } else if (reader.name() == "priority-rule") { // priority rules + parsePriorityRule(reader, gateway); } + finished = !reader.readNextStartElement(); } - - PLOGI << "Finished parsing the priority rules."; - - return priorityRules; } -QString PortalConfigResponse::parseGatewayName(QXmlStreamReader &xmlReader) -{ - PLOGI << "Start parsing the gateway name..."; +void PortalConfigResponse::parsePriorityRule(QXmlStreamReader &reader, GPGateway &gateway) { + PLOGI << "Start parsing priority rule..."; - while (xmlReader.name() != "description" || !xmlReader.isEndElement()) { - xmlReader.readNext(); - if (xmlReader.name() == "description" && xmlReader.tokenType() == xmlReader.StartElement) { - PLOGI << "Finished parsing the gateway name"; - return xmlReader.readElementText(); + QMap priorityRules; + auto finished = false; + + while (!finished) { + // Parse the priority-rule -> entry + if (reader.name() == "entry") { + auto ruleName = reader.attributes().value("name").toString(); + // move to the priority value + while (reader.name() != "priority") { + reader.readNextStartElement(); + } + auto priority = reader.readElementText().toInt(); + priorityRules.insert(ruleName, priority); } + finished = !reader.readNextStartElement(); } - PLOGE << "Error: tag not found"; - return ""; + gateway.setPriorityRules(priorityRules); } QString PortalConfigResponse::userAuthCookie() const @@ -137,11 +133,6 @@ QString PortalConfigResponse::userAuthCookie() const return m_userAuthCookie; } -QString PortalConfigResponse::prelogonUserAuthCookie() const -{ - return m_prelogonAuthCookie; -} - QList PortalConfigResponse::allGateways() const { return m_gateways; @@ -176,3 +167,4 @@ void PortalConfigResponse::setPrelogonUserAuthCookie(const QString cookie) { m_prelogonAuthCookie = cookie; } + diff --git a/GPClient/portalconfigresponse.h b/GPClient/portalconfigresponse.h index b6fe0d9..bbfda12 100644 --- a/GPClient/portalconfigresponse.h +++ b/GPClient/portalconfigresponse.h @@ -19,7 +19,6 @@ public: const QString &username() const; QString password() const; QString userAuthCookie() const; - QString prelogonUserAuthCookie() const; QList allGateways() const; void setAllGateways(QList gateways); @@ -44,8 +43,9 @@ private: void setPrelogonUserAuthCookie(const QString cookie); static QList parseGateways(QXmlStreamReader &xmlReader); - static QMap parsePriorityRules(QXmlStreamReader &xmlReader); - static QString parseGatewayName(QXmlStreamReader &xmlReader); + static void parseGateway(QXmlStreamReader &reader, GPGateway &gateway); + static void parsePriorityRule(QXmlStreamReader &reader, GPGateway &gateway); + }; #endif // PORTALCONFIGRESPONSE_H