summaryrefslogtreecommitdiff
path: root/src/utils/stringutils.cpp
diff options
context:
space:
mode:
authorYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2011-12-05 22:50:33 +0100
committerYohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer>2012-01-09 23:55:25 +0100
commitf405849b49614254f42eb3ee6147434458978623 (patch)
treeecf06af8ba1b049504957d4e2797ead26c81ec3e /src/utils/stringutils.cpp
parentf5172d4fcc10be73b72c1033cf9357a4dfe3544d (diff)
downloadmana-f405849b49614254f42eb3ee6147434458978623.tar.gz
mana-f405849b49614254f42eb3ee6147434458978623.tar.bz2
mana-f405849b49614254f42eb3ee6147434458978623.tar.xz
mana-f405849b49614254f42eb3ee6147434458978623.zip
Made the client able to remove the port from update urls.
Resolves: Mana-Mantis #381. Reviewed-by: Ablu.
Diffstat (limited to 'src/utils/stringutils.cpp')
-rw-r--r--src/utils/stringutils.cpp49
1 files changed, 48 insertions, 1 deletions
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 <string.h>
#include <algorithm>
#include <cstdarg>
@@ -227,5 +229,50 @@ std::string autocomplete(std::vector<std::string> &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;
}