mirror of
				https://github.com/pocoproject/poco.git
				synced 2025-11-04 12:17:37 +01:00 
			
		
		
		
	trunk/branch integration: bugfix
This commit is contained in:
		@@ -1,7 +1,7 @@
 | 
			
		||||
//
 | 
			
		||||
// URI.cpp
 | 
			
		||||
//
 | 
			
		||||
// $Id: //poco/Main/Foundation/src/URI.cpp#15 $
 | 
			
		||||
// $Id: //poco/1.4/Foundation/src/URI.cpp#4 $
 | 
			
		||||
//
 | 
			
		||||
// Library: Foundation
 | 
			
		||||
// Package: URI
 | 
			
		||||
@@ -16,14 +16,14 @@
 | 
			
		||||
// execute, and transmit the Software, and to prepare derivative works of the
 | 
			
		||||
// Software, and to permit third-parties to whom the Software is furnished to
 | 
			
		||||
// do so, all subject to the following:
 | 
			
		||||
//
 | 
			
		||||
// 
 | 
			
		||||
// The copyright notices in the Software and this entire statement, including
 | 
			
		||||
// the above license grant, this restriction and the following disclaimer,
 | 
			
		||||
// must be included in all copies of the Software, in whole or in part, and
 | 
			
		||||
// all derivative works of the Software, unless such copies or derivative
 | 
			
		||||
// works are solely in the form of machine-executable object code generated by
 | 
			
		||||
// a source language processor.
 | 
			
		||||
//
 | 
			
		||||
// 
 | 
			
		||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 | 
			
		||||
@@ -69,7 +69,7 @@ URI::URI(const char* uri):
 | 
			
		||||
	parse(std::string(uri));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
URI::URI(const std::string& scheme, const std::string& pathEtc):
 | 
			
		||||
	_scheme(scheme),
 | 
			
		||||
	_port(0)
 | 
			
		||||
@@ -81,7 +81,7 @@ URI::URI(const std::string& scheme, const std::string& pathEtc):
 | 
			
		||||
	parsePathEtc(beg, end);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
URI::URI(const std::string& scheme, const std::string& authority, const std::string& pathEtc):
 | 
			
		||||
	_scheme(scheme)
 | 
			
		||||
{
 | 
			
		||||
@@ -131,7 +131,7 @@ URI::URI(const URI& uri):
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
URI::URI(const URI& baseURI, const std::string& relativeURI):
 | 
			
		||||
	_scheme(baseURI._scheme),
 | 
			
		||||
	_userInfo(baseURI._userInfo),
 | 
			
		||||
@@ -165,7 +165,7 @@ URI& URI::operator = (const URI& uri)
 | 
			
		||||
	return *this;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
URI& URI::operator = (const std::string& uri)
 | 
			
		||||
{
 | 
			
		||||
	clear();
 | 
			
		||||
@@ -226,12 +226,16 @@ std::string URI::toString() const
 | 
			
		||||
		if (!_path.empty())
 | 
			
		||||
		{
 | 
			
		||||
			if (!auth.empty() && _path[0] != '/')
 | 
			
		||||
				uri += '/';
 | 
			
		||||
			encode(_path, RESERVED_PATH, uri);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	if (!_query.empty())
 | 
			
		||||
	{
 | 
			
		||||
                                uri += '/';
 | 
			
		||||
                        encode(_path, RESERVED_PATH, uri);
 | 
			
		||||
                }
 | 
			
		||||
                else if (!_query.empty() || !_fragment.empty())
 | 
			
		||||
                {
 | 
			
		||||
                        uri += '/';
 | 
			
		||||
                }
 | 
			
		||||
        }
 | 
			
		||||
        if (!_query.empty())
 | 
			
		||||
        {
 | 
			
		||||
		uri += '?';
 | 
			
		||||
		uri.append(_query);
 | 
			
		||||
	}
 | 
			
		||||
@@ -252,14 +256,14 @@ void URI::setScheme(const std::string& scheme)
 | 
			
		||||
		_port = getWellKnownPort();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
void URI::setUserInfo(const std::string& userInfo)
 | 
			
		||||
{
 | 
			
		||||
	_userInfo.clear();
 | 
			
		||||
	decode(userInfo, _userInfo);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
void URI::setHost(const std::string& host)
 | 
			
		||||
{
 | 
			
		||||
	_host = host;
 | 
			
		||||
@@ -280,25 +284,31 @@ void URI::setPort(unsigned short port)
 | 
			
		||||
	_port = port;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
std::string URI::getAuthority() const
 | 
			
		||||
{
 | 
			
		||||
	std::string auth;
 | 
			
		||||
	if (!_userInfo.empty())
 | 
			
		||||
	{
 | 
			
		||||
		auth.append(_userInfo);
 | 
			
		||||
		auth += '@';
 | 
			
		||||
	}
 | 
			
		||||
	auth.append(_host);
 | 
			
		||||
	if (_port && !isWellKnownPort())
 | 
			
		||||
	{
 | 
			
		||||
		auth += ':';
 | 
			
		||||
                auth.append(_userInfo);
 | 
			
		||||
                auth += '@';
 | 
			
		||||
        }
 | 
			
		||||
        if (_host.find(':') != std::string::npos)
 | 
			
		||||
        {
 | 
			
		||||
                auth += '[';
 | 
			
		||||
                auth += _host;
 | 
			
		||||
                auth += ']';
 | 
			
		||||
        }
 | 
			
		||||
        else auth.append(_host);
 | 
			
		||||
        if (_port && !isWellKnownPort())
 | 
			
		||||
        {
 | 
			
		||||
                auth += ':';
 | 
			
		||||
		NumberFormatter::append(auth, _port);
 | 
			
		||||
	}
 | 
			
		||||
	return auth;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
void URI::setAuthority(const std::string& authority)
 | 
			
		||||
{
 | 
			
		||||
	_userInfo.clear();
 | 
			
		||||
@@ -309,14 +319,14 @@ void URI::setAuthority(const std::string& authority)
 | 
			
		||||
	parseAuthority(beg, end);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
void URI::setPath(const std::string& path)
 | 
			
		||||
{
 | 
			
		||||
	_path.clear();
 | 
			
		||||
	decode(path, _path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
void URI::setRawQuery(const std::string& query)
 | 
			
		||||
{
 | 
			
		||||
	_query = query;
 | 
			
		||||
@@ -355,18 +365,18 @@ void URI::setPathEtc(const std::string& pathEtc)
 | 
			
		||||
	parsePathEtc(beg, end);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
std::string URI::getPathEtc() const
 | 
			
		||||
{
 | 
			
		||||
	std::string pathEtc;
 | 
			
		||||
	encode(_path, RESERVED_PATH, pathEtc);
 | 
			
		||||
	if (!_query.empty())
 | 
			
		||||
	{
 | 
			
		||||
		pathEtc += '?';
 | 
			
		||||
		pathEtc.append(_query);
 | 
			
		||||
	}
 | 
			
		||||
	if (!_fragment.empty())
 | 
			
		||||
	{
 | 
			
		||||
        if (!_query.empty())
 | 
			
		||||
        {
 | 
			
		||||
                pathEtc += '?';
 | 
			
		||||
                pathEtc += _query;
 | 
			
		||||
        }
 | 
			
		||||
        if (!_fragment.empty())
 | 
			
		||||
        {
 | 
			
		||||
		pathEtc += '#';
 | 
			
		||||
		encode(_fragment, RESERVED_FRAGMENT, pathEtc);
 | 
			
		||||
	}
 | 
			
		||||
@@ -378,15 +388,15 @@ std::string URI::getPathAndQuery() const
 | 
			
		||||
{
 | 
			
		||||
	std::string pathAndQuery;
 | 
			
		||||
	encode(_path, RESERVED_PATH, pathAndQuery);
 | 
			
		||||
	if (!_query.empty())
 | 
			
		||||
	{
 | 
			
		||||
		pathAndQuery += '?';
 | 
			
		||||
		encode(_query, RESERVED_QUERY, pathAndQuery);
 | 
			
		||||
	}
 | 
			
		||||
	return pathAndQuery;
 | 
			
		||||
        if (!_query.empty())
 | 
			
		||||
        {
 | 
			
		||||
                pathAndQuery += '?';
 | 
			
		||||
                pathAndQuery += _query;
 | 
			
		||||
        }
 | 
			
		||||
        return pathAndQuery;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
void URI::resolve(const std::string& relativeURI)
 | 
			
		||||
{
 | 
			
		||||
	URI parsedURI(relativeURI);
 | 
			
		||||
@@ -438,8 +448,8 @@ void URI::resolve(const URI& relativeURI)
 | 
			
		||||
				_query = relativeURI._query;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	_fragment = relativeURI._fragment;
 | 
			
		||||
        }
 | 
			
		||||
        _fragment = relativeURI._fragment;      
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -454,7 +464,7 @@ bool URI::empty() const
 | 
			
		||||
	return _scheme.empty() && _host.empty() && _path.empty() && _query.empty() && _fragment.empty();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
bool URI::operator == (const URI& uri) const
 | 
			
		||||
{
 | 
			
		||||
	return equals(uri);
 | 
			
		||||
@@ -492,7 +502,7 @@ bool URI::equals(const URI& uri) const
 | 
			
		||||
	    && _fragment == uri._fragment;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
void URI::normalize()
 | 
			
		||||
{
 | 
			
		||||
	removeDotSegments(!isRelative());
 | 
			
		||||
@@ -502,7 +512,7 @@ void URI::normalize()
 | 
			
		||||
void URI::removeDotSegments(bool removeLeading)
 | 
			
		||||
{
 | 
			
		||||
	if (_path.empty()) return;
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
	bool leadingSlash  = *(_path.begin()) == '/';
 | 
			
		||||
	bool trailingSlash = *(_path.rbegin()) == '/';
 | 
			
		||||
	std::vector<std::string> segments;
 | 
			
		||||
@@ -564,15 +574,15 @@ void URI::getPathSegments(const std::string& path, std::vector<std::string>& seg
 | 
			
		||||
 | 
			
		||||
void URI::encode(const std::string& str, const std::string& reserved, std::string& encodedStr)
 | 
			
		||||
{
 | 
			
		||||
	for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
 | 
			
		||||
	{
 | 
			
		||||
		char c = *it;
 | 
			
		||||
		if (c >= 'a' && c <= 'z' ||
 | 
			
		||||
		    c >= 'A' && c <= 'Z' ||
 | 
			
		||||
		    c >= '0' && c <= '9' ||
 | 
			
		||||
		    c == '-' || c == '_' ||
 | 
			
		||||
		    c == '.' || c == '~')
 | 
			
		||||
		{
 | 
			
		||||
        for (std::string::const_iterator it = str.begin(); it != str.end(); ++it)
 | 
			
		||||
        {
 | 
			
		||||
                char c = *it;
 | 
			
		||||
                if ((c >= 'a' && c <= 'z') || 
 | 
			
		||||
                    (c >= 'A' && c <= 'Z') || 
 | 
			
		||||
                    (c >= '0' && c <= '9') ||
 | 
			
		||||
                    c == '-' || c == '_' || 
 | 
			
		||||
                    c == '.' || c == '~')
 | 
			
		||||
                {
 | 
			
		||||
			encodedStr += c;
 | 
			
		||||
		}
 | 
			
		||||
		else if (c <= 0x20 || c >= 0x7F || ILLEGAL.find(c) != std::string::npos || reserved.find(c) != std::string::npos)
 | 
			
		||||
@@ -584,7 +594,7 @@ void URI::encode(const std::string& str, const std::string& reserved, std::strin
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
void URI::decode(const std::string& str, std::string& decodedStr)
 | 
			
		||||
{
 | 
			
		||||
	std::string::const_iterator it  = str.begin();
 | 
			
		||||
@@ -638,11 +648,19 @@ unsigned short URI::getWellKnownPort() const
 | 
			
		||||
	else if (_scheme == "nntp")
 | 
			
		||||
		return 119;
 | 
			
		||||
	else if (_scheme == "ldap")
 | 
			
		||||
		return 389;
 | 
			
		||||
	else if (_scheme == "https")
 | 
			
		||||
		return 443;
 | 
			
		||||
	else
 | 
			
		||||
		return 0;
 | 
			
		||||
                return 389;
 | 
			
		||||
        else if (_scheme == "https")
 | 
			
		||||
                return 443;
 | 
			
		||||
        else if (_scheme == "rtsp")
 | 
			
		||||
                return 554;
 | 
			
		||||
        else if (_scheme == "sip")
 | 
			
		||||
                return 5060;
 | 
			
		||||
        else if (_scheme == "sips")
 | 
			
		||||
                return 5061;
 | 
			
		||||
        else if (_scheme == "xmpp")
 | 
			
		||||
                return 5222;
 | 
			
		||||
        else
 | 
			
		||||
                return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -672,7 +690,7 @@ void URI::parse(const std::string& uri)
 | 
			
		||||
			}
 | 
			
		||||
			parsePathEtc(it, end);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		else 
 | 
			
		||||
		{
 | 
			
		||||
			it = uri.begin();
 | 
			
		||||
			parsePathEtc(it, end);
 | 
			
		||||
@@ -707,15 +725,16 @@ void URI::parseHostAndPort(std::string::const_iterator& it, const std::string::c
 | 
			
		||||
{
 | 
			
		||||
	if (it == end) return;
 | 
			
		||||
	std::string host;
 | 
			
		||||
	if (*it == '[')
 | 
			
		||||
	{
 | 
			
		||||
		// IPv6 address
 | 
			
		||||
		while (it != end && *it != ']') host += *it++;
 | 
			
		||||
		if (it == end) throw SyntaxException("unterminated IPv6 address");
 | 
			
		||||
		host += *it++;
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
        if (*it == '[')
 | 
			
		||||
        {
 | 
			
		||||
                // IPv6 address
 | 
			
		||||
                ++it;
 | 
			
		||||
                while (it != end && *it != ']') host += *it++;
 | 
			
		||||
                if (it == end) throw SyntaxException("unterminated IPv6 address");
 | 
			
		||||
                ++it;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
		while (it != end && *it != ':') host += *it++;
 | 
			
		||||
	}
 | 
			
		||||
	if (it != end && *it == ':')
 | 
			
		||||
@@ -761,7 +780,7 @@ void URI::parsePathEtc(std::string::const_iterator& it, const std::string::const
 | 
			
		||||
	{
 | 
			
		||||
		++it;
 | 
			
		||||
		parseFragment(it, end);
 | 
			
		||||
	}
 | 
			
		||||
	}	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -791,14 +810,14 @@ void URI::mergePath(const std::string& path)
 | 
			
		||||
		bool endsWithSlash = *(_path.rbegin()) == '/';
 | 
			
		||||
		if (!endsWithSlash && !segments.empty())
 | 
			
		||||
			segments.pop_back();
 | 
			
		||||
		addLeadingSlash = _path[0] == '/';
 | 
			
		||||
	}
 | 
			
		||||
	getPathSegments(path, segments);
 | 
			
		||||
	addLeadingSlash = addLeadingSlash || !path.empty() && path[0] == '/';
 | 
			
		||||
	bool hasTrailingSlash = !path.empty() && *(path.rbegin()) == '/';
 | 
			
		||||
	bool addTrailingSlash = false;
 | 
			
		||||
	for (std::vector<std::string>::const_iterator it = segments.begin(); it != segments.end(); ++it)
 | 
			
		||||
	{
 | 
			
		||||
                addLeadingSlash = _path[0] == '/';
 | 
			
		||||
        }
 | 
			
		||||
        getPathSegments(path, segments);
 | 
			
		||||
        addLeadingSlash = addLeadingSlash || (!path.empty() && path[0] == '/');
 | 
			
		||||
        bool hasTrailingSlash = (!path.empty() && *(path.rbegin()) == '/');
 | 
			
		||||
        bool addTrailingSlash = false;
 | 
			
		||||
        for (std::vector<std::string>::const_iterator it = segments.begin(); it != segments.end(); ++it)
 | 
			
		||||
        {
 | 
			
		||||
		if (*it == "..")
 | 
			
		||||
		{
 | 
			
		||||
			addTrailingSlash = true;
 | 
			
		||||
@@ -833,7 +852,7 @@ void URI::buildPath(const std::vector<std::string>& segments, bool leadingSlash,
 | 
			
		||||
		else _path += '/';
 | 
			
		||||
		_path.append(*it);
 | 
			
		||||
	}
 | 
			
		||||
	if (trailingSlash)
 | 
			
		||||
	if (trailingSlash) 
 | 
			
		||||
		_path += '/';
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user