diff options
Diffstat (limited to 'src/utilfuns/url.cpp')
-rw-r--r-- | src/utilfuns/url.cpp | 90 |
1 files changed, 51 insertions, 39 deletions
diff --git a/src/utilfuns/url.cpp b/src/utilfuns/url.cpp index 549c283..a909a04 100644 --- a/src/utilfuns/url.cpp +++ b/src/utilfuns/url.cpp @@ -1,25 +1,25 @@ /****************************************************************************** -* url.cpp - code for an URL parser utility class -* -* $Id: url.cpp 2250 2009-02-14 05:48:58Z scribe $ -* -* Copyright 2003 CrossWire Bible Society (http://www.crosswire.org) -* CrossWire Bible Society -* P. O. Box 2528 -* Tempe, AZ 85280-2528 -* -* This program is free software; you can redistribute it and/or modify it -* under the terms of the GNU General Public License as published by the -* Free Software Foundation version 2. -* -* This program is distributed in the hope that it will be useful, but -* WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* General Public License for more details. -* -*/ + * + * url.cpp - code for an URL parser utility class + * + * $Id: url.cpp 2980 2013-09-14 21:51:47Z scribe $ + * + * Copyright 2004-2013 CrossWire Bible Society (http://www.crosswire.org) + * CrossWire Bible Society + * P. O. Box 2528 + * Tempe, AZ 85280-2528 + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation version 2. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + */ -//Sword includes #include <url.h> #include <swlog.h> @@ -29,10 +29,12 @@ #include <stdio.h> #include <iostream> + SWORD_NAMESPACE_START + namespace { - typedef std::map< unsigned char, SWBuf > DataMap; + typedef std::map<unsigned char, SWBuf> DataMap; DataMap m; static class __init { public: @@ -52,10 +54,11 @@ namespace { } ___init; } + /** * Constructors/Destructors */ -URL::URL(const char* url) +URL::URL(const char *url) : url(""), protocol(""), hostname(""), @@ -67,27 +70,32 @@ URL::URL(const char* url) } } -const char* URL::getProtocol() const { + +const char *URL::getProtocol() const { return protocol.c_str(); } -const char* URL::getHostName () const { + +const char *URL::getHostName () const { return hostname.c_str(); } -const char* URL::getPath() const { + +const char *URL::getPath() const { return path.c_str(); } + const URL::ParameterMap &URL::getParameters() const { return parameterMap; } + /** * Returns the value of an URL parameter. For the URL "http://www.crosswire.org/index.jsp?page=test&user=nobody" the value of the parameter "page" would be "test". * If the parameter is not set an empty string is returned. */ -const char* URL::getParameterValue(const char* name) const { +const char *URL::getParameterValue(const char *name) const { static SWBuf emptyStr(""); ParameterMap::const_iterator it = parameterMap.find(name); @@ -106,22 +114,22 @@ const char* URL::getParameterValue(const char* name) const { * Parse the URL into the protocol, the hostname, the path and the paramters with their values * */ -void URL::parse () { +void URL::parse() { /* format example protocol://hostname/path/path/path.pl?param1=value1&param2=value2 - * we include the script name in the path, so the path would be /path/path/path.pl in this example - * & could also be & - */ + * we include the script name in the path, so the path would be /path/path/path.pl in this example + * & could also be & + */ + + //1. Init + const char *urlPtr = url.c_str(); - //1. Init - const char* urlPtr = url.c_str(); - - protocol = ""; - hostname = ""; - path = ""; - parameterMap.clear(); + protocol = ""; + hostname = ""; + path = ""; + parameterMap.clear(); // 2. Get the protocol, which is from the begining to the first :// - const char* end = strchr( urlPtr, ':' ); + const char *end = strchr( urlPtr, ':' ); if (end) { //protocol was found protocol.append(urlPtr, end-urlPtr); urlPtr = end + 1; @@ -174,6 +182,7 @@ void URL::parse () { SWBuf paramName; SWBuf paramValue; + if (checkAnchor) checkAnchor = false; /* end = strchr(urlPtr, '#'); if (!end) { @@ -188,7 +197,7 @@ void URL::parse () { paramValue = ""; //search for the equal sign to find the value part - const char* valueStart = strchr(end, '='); + const char *valueStart = strchr(end, '='); if (valueStart) { const char* valueEnd = strstr(valueStart, "&") ? strstr(valueStart, "&") : strstr(valueStart, "&"); //try to find a new paramter part @@ -234,6 +243,7 @@ const SWBuf URL::encode(const char *urlText) { return url; } + const SWBuf URL::decode(const char *encoded) { /*static*/ SWBuf text; text = encoded; @@ -274,4 +284,6 @@ const SWBuf URL::decode(const char *encoded) { return text; } + SWORD_NAMESPACE_END + |