From f405849b49614254f42eb3ee6147434458978623 Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Mon, 5 Dec 2011 22:50:33 +0100 Subject: Made the client able to remove the port from update urls. Resolves: Mana-Mantis #381. Reviewed-by: Ablu. --- src/client.cpp | 54 ++++++++++++++++++----------------------------- src/utils/stringutils.cpp | 49 +++++++++++++++++++++++++++++++++++++++++- src/utils/stringutils.h | 11 ++++++++++ 3 files changed, 80 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/client.cpp b/src/client.cpp index 642d9376..2d4ac8cc 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1267,47 +1267,31 @@ void Client::initConfiguration() */ void Client::initUpdatesDir() { - std::stringstream updates; - // If updatesHost is currently empty, fill it from config file if (mUpdateHost.empty()) - { mUpdateHost = config.getStringValue("updatehost"); - } - // Don't go out of range int he next check - if (mUpdateHost.length() < 2) + // Exit on empty update host. + if (mUpdateHost.empty()) return; - // Remove any trailing slash at the end of the update host - if (!mUpdateHost.empty() && mUpdateHost.at(mUpdateHost.size() - 1) == '/') - mUpdateHost.resize(mUpdateHost.size() - 1); + logger->log("Setting update host: %s", mUpdateHost.c_str()); - // Parse out any "http://" or "ftp://", and set the updates directory - size_t pos; - pos = mUpdateHost.find("://"); - if (pos != mUpdateHost.npos) - { - if (pos + 3 < mUpdateHost.length() && !mUpdateHost.empty()) - { - updates << "updates/" << mUpdateHost.substr(pos + 3); - mUpdatesDir = updates.str(); - } - else - { - logger->log("Error: Invalid update host: %s", mUpdateHost.c_str()); - errorMessage = strprintf(_("Invalid update host: %s"), - mUpdateHost.c_str()); - mState = STATE_ERROR; - } - } - else + std::string updateHost = getHostNameFromURL(mUpdateHost); + + // Exit on a wrong update host. + if (updateHost.length() < 2) { - logger->log("Warning: no protocol was specified for the update host"); - updates << "updates/" << mUpdateHost; - mUpdatesDir = updates.str(); + // Show the original updateHostname in the error message. + errorMessage = strprintf(_("Invalid update host: %s"), + mUpdateHost.c_str()); + mState = STATE_ERROR; + return; } + mUpdateHost = updateHost; + mUpdatesDir = "updates/" + mUpdateHost; + ResourceManager *resman = ResourceManager::getInstance(); // Verify that the updates directory exists. Create if necessary. @@ -1330,13 +1314,17 @@ void Client::initUpdatesDir() { logger->log("Error: %s can't be made, but doesn't exist!", newDir.c_str()); - errorMessage = _("Error creating updates directory!"); + errorMessage = + strprintf(_("Error creating updates directory!\n(%s)"), + newDir.c_str()); mState = STATE_ERROR; } #else logger->log("Error: %s/%s can't be made, but doesn't exist!", mLocalDataDir.c_str(), mUpdatesDir.c_str()); - errorMessage = _("Error creating updates directory!"); + errorMessage = + strprintf(_("Error creating updates directory!\n(%s/%s)"), + mLocalDataDir.c_str(), mUpdatesDir.c_str()); mState = STATE_ERROR; #endif } diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index 96b67370..77914214 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -21,6 +21,8 @@ #include "utils/stringutils.h" +#include "log.h" + #include #include #include @@ -227,5 +229,50 @@ std::string autocomplete(std::vector &candidates, std::string normalize(const std::string &name) { std::string normalized = name; - return toLower(trim(normalized));; + return toLower(trim(normalized)); +} + +std::string removeTrailingSymbol(const std::string& s, const char c) +{ + // Remove the trailing symblol at the end of the string + if (!s.empty() && s.at(s.size() - 1) == c) + return s.substr(0, s.size() - 1); + return std::string(s); +} + +std::string getHostNameFromURL(const std::string& url) +{ + std::string myHostName; + + // Don't go out of range in the next check + if (url.length() < 2) + return myHostName; + + // Remove any trailing slash at the end of the update host + myHostName = removeTrailingSymbol(url, '/'); + + // Parse out any "http://", "ftp://", ect... + size_t pos = myHostName.find("://"); + if (pos == myHostName.npos) + { + logger->log("Warning: no protocol was specified for the url: %s", + url.c_str()); + } + + if (myHostName.empty() || pos + 3 >= myHostName.length()) + { + logger->log("Error: Invalid url: %s", url.c_str()); + return myHostName; + } + myHostName = myHostName.substr(pos + 3); + + // Remove possible trailing port (i.e.: localhost:8000 -> localhost) + pos = myHostName.find(":"); + if (pos != myHostName.npos) + myHostName = myHostName.substr(0, pos); + + // remove possible other junk + removeBadChars(myHostName); + + return myHostName; } diff --git a/src/utils/stringutils.h b/src/utils/stringutils.h index 2c6fad78..3ef4d513 100644 --- a/src/utils/stringutils.h +++ b/src/utils/stringutils.h @@ -149,4 +149,15 @@ std::string autocomplete(std::vector &candidates, */ std::string normalize(const std::string &name); +/** + * Remove a potential trailing symbol from a string. + */ +std::string removeTrailingSymbol(const std::string& s, const char c); + +/** + * Gets the hostname out of the given URL. + * i.e.: http://www.manasource.org:9601 -> www.manasource.org + */ +std::string getHostNameFromURL(const std::string& url); + #endif // UTILS_STRINGUTILS_H -- cgit v1.2.3-70-g09d2