// Ryzom - MMORPG Framework
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
#include "nel/misc/common.h"
#include "nel/gui/url_parser.h"
using namespace std;
#ifdef DEBUG_NEW
#define new DEBUG_NEW
#endif
namespace NLGUI
{
// ***************************************************************************
CUrlParser::CUrlParser(const std::string &uri)
{
parse(uri);
}
// ***************************************************************************
void CUrlParser::parse(std::string uri)
{
const size_t npos = std::string::npos;
size_t pos;
size_t offset = 0;
// strip fragment if present
pos = uri.find("#");
if (pos != npos)
{
hash = uri.substr(pos + 1);
uri = uri.substr(0, pos);
}
// scan for scheme
pos = uri.find(":");
if (pos != npos && pos >= 1)
{
for (uint i=0; i 0)
{
// full or last segment
sub = path.substr(pos-1, 4);
if (sub == "/../" || sub == "/..")
{
if (pos > 1)
lhp = path.find_last_of("/", pos - 2);
else
lhp = 0;
// pos points to first dot in ..
// lhp points to start slash (/) of last segment
pos += sub.size() - 1;
path.replace(lhp, pos - lhp, "/");
pos = lhp;
}
}
}// sub == ".."
} // path[pos] == '.'
pos++;
}// while
}
bool CUrlParser::isAbsolute() const
{
return !scheme.empty() && !authority.empty();
}
// serialize URL back to string
std::string CUrlParser::toString() const
{
std::string result;
if (!scheme.empty())
result += scheme + ":";
if (!authority.empty())
{
result += authority;
}
// path already has leading slash
if (!path.empty())
result += path;
if (!query.empty())
if (query.find_first_of("?") != 0)
result += "?";
result += query;
if (!hash.empty())
result += "#" + hash;
return result;
}
}// namespace