common/Protocol.hpp | 13 +++++++++++++ wsd/LOOLWSD.cpp | 22 +++++++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-)
New commits: commit f637f4a75af56ae16116a88730660d018f306131 Author: Jan Holesovsky <ke...@collabora.com> AuthorDate: Fri Nov 8 21:37:21 2019 +0100 Commit: Jan Holesovsky <ke...@collabora.com> CommitDate: Fri Nov 8 22:24:08 2019 +0100 killpoco: Remove StringTokenizer from LOOLWSD.cpp. Adds possibility to tokenize using a regex easily. Change-Id: Ie327d4faabec330c76d4cadb1d14bbe1527d332f Reviewed-on: https://gerrit.libreoffice.org/82333 Reviewed-by: Jan Holesovsky <ke...@collabora.com> Tested-by: Jan Holesovsky <ke...@collabora.com> diff --git a/common/Protocol.hpp b/common/Protocol.hpp index b915d1a7c..e1a5d426e 100644 --- a/common/Protocol.hpp +++ b/common/Protocol.hpp @@ -15,6 +15,7 @@ #include <cstring> #include <iomanip> #include <map> +#include <regex> #include <sstream> #include <string> #include <vector> @@ -140,6 +141,18 @@ namespace LOOLProtocol return tokenize(s.data(), s.size(), delimiter); } + /// Tokenize according to the regex, potentially skip empty tokens. + inline + std::vector<std::string> tokenize(const std::string& s, const std::regex& pattern, bool skipEmpty = false) + { + std::vector<std::string> tokens; + if (skipEmpty) + std::copy_if(std::sregex_token_iterator(s.begin(), s.end(), pattern, -1), std::sregex_token_iterator(), std::back_inserter(tokens), [](std::string in) { return !in.empty(); }); + else + std::copy(std::sregex_token_iterator(s.begin(), s.end(), pattern, -1), std::sregex_token_iterator(), std::back_inserter(tokens)); + return tokens; + } + inline std::vector<int> tokenizeInts(const char* data, const size_t size, const char delimiter = ',') { diff --git a/wsd/LOOLWSD.cpp b/wsd/LOOLWSD.cpp index 583455d93..4fad66287 100644 --- a/wsd/LOOLWSD.cpp +++ b/wsd/LOOLWSD.cpp @@ -92,7 +92,6 @@ using Poco::Net::PartHandler; #include <Poco/Process.h> #include <Poco/SAX/InputSource.h> #include <Poco/StreamCopier.h> -#include <Poco/StringTokenizer.h> #include <Poco/TemporaryFile.h> #include <Poco/URI.h> #include <Poco/Util/AbstractConfiguration.h> @@ -162,7 +161,6 @@ using Poco::Net::MessageHeader; using Poco::Net::NameValueCollection; using Poco::Path; using Poco::StreamCopier; -using Poco::StringTokenizer; using Poco::TemporaryFile; using Poco::URI; using Poco::Util::Application; @@ -2190,19 +2188,21 @@ private: } else { - StringTokenizer reqPathTokens(request.getURI(), "/?", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM); - if (reqPathTokens.count() > 1 && reqPathTokens[0] == "lool" && reqPathTokens[1] == "clipboard") + // split by /'s and ?'s + std::vector<std::string> reqPathTokens(LOOLProtocol::tokenize(request.getURI(), std::regex(R"(\s*[\/\?]\s*)"), /*skipEmpty =*/ true)); + + if (reqPathTokens.size() > 1 && reqPathTokens[0] == "lool" && reqPathTokens[1] == "clipboard") { // Util::dumpHex(std::cerr, "clipboard:\n", "", socket->getInBuffer()); // lots of data ... handleClipboardRequest(request, message, disposition); } else if (!(request.find("Upgrade") != request.end() && Poco::icompare(request["Upgrade"], "websocket") == 0) && - reqPathTokens.count() > 0 && reqPathTokens[0] == "lool") + reqPathTokens.size() > 0 && reqPathTokens[0] == "lool") { // All post requests have url prefix 'lool'. handlePostRequest(request, message, disposition); } - else if (reqPathTokens.count() > 2 && reqPathTokens[0] == "lool" && reqPathTokens[2] == "ws" && + else if (reqPathTokens.size() > 2 && reqPathTokens[0] == "lool" && reqPathTokens[2] == "ws" && request.find("Upgrade") != request.end() && Poco::icompare(request["Upgrade"], "websocket") == 0) { std::string decodedUri; // WOPISrc @@ -2532,8 +2532,8 @@ private: Poco::Net::HTTPResponse response; std::shared_ptr<StreamSocket> socket = _socket.lock(); - StringTokenizer tokens(request.getURI(), "/?"); - if (tokens.count() > 2 && tokens[2] == "convert-to") + std::vector<std::string> tokens(LOOLProtocol::tokenize(request.getURI(), std::regex(R"(\s*[\/\?]\s*)"))); + if (tokens.size() > 2 && tokens[2] == "convert-to") { // Validate sender - FIXME: should do this even earlier. if (!allowConvertTo(socket->clientAddress(), request, true)) @@ -2559,7 +2559,7 @@ private: bool bFullSheetPreview = sFullSheetPreview == "true" ? true : false; // prefer what is in the URI - if (tokens.count() > 3) + if (tokens.size() > 3) format = tokens[3]; bool sent = false; @@ -2659,7 +2659,7 @@ private: } return; } - else if (tokens.count() >= 4 && tokens[3] == "insertfile") + else if (tokens.size() >= 4 && tokens[3] == "insertfile") { LOG_INF("Insert file request."); @@ -2700,7 +2700,7 @@ private: } } } - else if (tokens.count() >= 6) + else if (tokens.size() >= 6) { LOG_INF("File download request."); // TODO: Check that the user in question has access to this file! _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits