📄 uri.h
字号:
//
// URI.h
//
// $Id: //poco/Main/Foundation/include/Foundation/URI.h#5 $
//
// Definition of the URI class.
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#ifndef Foundation_URI_INCLUDED
#define Foundation_URI_INCLUDED
#ifndef Foundation_Foundation_INCLUDED
#include "Foundation/Foundation.h"
#endif
#ifndef STD_VECTOR_INCLUDED
#include <vector>
#define STD_VECTOR_INCLUDED
#endif
Foundation_BEGIN
class Foundation_API URI
/// A Uniform Resource Identifier, as specified in RFC 3986.
///
/// The URI class provides methods for building URIs from their
/// parts, as well as for splitting URIs into their parts.
/// Furthermore, the class provides methods for resolving
/// relative URIs against base URIs.
///
/// The class automatically performs a few normalizations on
/// all URIs and URI parts passed to it:
/// * scheme identifiers are converted to lower case.
/// * percent-encoded characters are decoded
/// * optionally, dot segments are removed from paths (see normalize())
{
public:
URI();
/// Creates an empty URI.
URI(const std::string& uri);
/// Parses an URI from the given string. Throws a
/// SyntaxException if the uri is not valid.
URI(const std::string& scheme, const std::string& path);
/// Creates an URI from its parts.
URI(const std::string& scheme, const std::string& authority, const std::string& path);
/// Creates an URI from its parts.
URI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query);
/// Creates an URI from its parts.
URI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment);
/// Creates an URI from its parts.
URI(const URI& uri);
/// Copy constructor. Creates an URI from another one.
URI(const URI& baseURI, const std::string& relativeURI);
/// Creates an URI from a base URI and a relative URI, according to
/// the algorithm in section 5.2 of RFC 3986.
~URI();
/// Destroys the URI.
URI& operator = (const URI& uri);
/// Assignment operator.
URI& operator = (const std::string& uri);
/// Parses and assigns an URI from the given string. Throws a
/// SyntaxException if the uri is not valid.
void clear();
/// Clears all parts of the URI.
std::string toString() const;
/// Returns a string representation of the URI.
///
/// Characters in the path, query and fragment parts will be
/// percent-encoded as necessary.
const std::string& getScheme() const;
/// Returns the scheme part of the URI.
void setScheme(const std::string& scheme);
/// Sets the scheme part of the URI. The given scheme
/// is converted to lower-case.
///
/// A list of registered URI schemes can be found
/// at <http://www.iana.org/assignments/uri-schemes>.
const std::string& getUserInfo() const;
/// Returns the user-info part of the URI.
void setUserInfo(const std::string& userInfo);
/// Sets the user-info part of the URI.
const std::string& getHost() const;
/// Returns the host part of the URI.
void setHost(const std::string& host);
/// Sets the host part of the URI.
unsigned short getPort() const;
/// Returns the port number part of the URI.
///
/// If no port number (0) has been specified, the
/// well-known port number (e.g., 80 for http) for
/// the given scheme is returned if it is known.
/// Otherwise, 0 is returned.
void setPort(unsigned short port);
/// Sets the port number part of the URI.
std::string getAuthority() const;
/// Returns the authority part (userInfo, host and port)
/// of the URI.
///
/// If the port number is a well-known port
/// number for the given scheme (e.g., 80 for http), it
/// is not included in the authority.
void setAuthority(const std::string& authority);
/// Parses the given authority part for the URI and sets
/// the user-info, host, port components accordingly.
const std::string& getPath() const;
/// Returns the path part of the URI.
void setPath(const std::string& path);
/// Sets the path part of the URI.
const std::string& getQuery() const;
/// Returns the query part of the URI.
void setQuery(const std::string& query);
/// Sets the query part of the URI.
const std::string& getFragment() const;
/// Returns the fragment part of the URI.
void setFragment(const std::string& fragment);
/// Sets the fragment part of the URI.
void resolve(const std::string& relativeURI);
/// Resolves the given relative URI against the base URI.
/// See section 5.2 of RFC 3986 for the algorithm used.
void resolve(const URI& relativeURI);
/// Resolves the given relative URI against the base URI.
/// See section 5.2 of RFC 3986 for the algorithm used.
bool isRelative() const;
/// Returns true if the URI is a relative reference, false otherwise.
///
/// A relative reference does not contain a scheme identifier.
/// Relative references are usually resolved against an absolute
/// base reference.
bool empty() const;
/// Returns true if the URI is empty, false otherwise.
bool operator == (const URI& uri) const;
/// Returns true if both URIs are identical, false otherwise.
///
/// Two URIs are identical if their scheme, authority,
/// path, query and fragment part are identical.
bool operator == (const std::string& uri) const;
/// Parses the given URI and returns true if both URIs are identical,
/// false otherwise.
bool operator != (const URI& uri) const;
/// Returns true if both URIs are identical, false otherwise.
bool operator != (const std::string& uri) const;
/// Parses the given URI and returns true if both URIs are identical,
/// false otherwise.
void normalize();
/// Normalizes the URI by removing all but leading . and .. segments from the path.
///
/// If the first path segment in a relative path contains a colon (:),
/// such as in a Windows path containing a drive letter, a dot segment (./)
/// is prepended in accordance with section 3.3 of RFC 3986.
void getPathSegments(std::vector<std::string>& segments);
/// Places the single path segments (delimited by slashes) into the
/// given vector.
static void encode(const std::string& str, const std::string& reserved, std::string& encodedStr);
/// URI-encodes the given string by escaping reserved and non-ASCII
/// characters. The encoded string is appended to encodedStr.
static void decode(const std::string& str, std::string& decodedStr);
/// URI-decodes the given string by replacing percent-encoded
/// characters with the actual character. The decoded string
/// is appended to decodedStr.
protected:
bool equals(const URI& uri) const;
/// Returns true if both uri's are equivalent.
bool isWellKnownPort() const;
/// Returns true if the URI's port number is a well-known one
/// (for example, 80, if the scheme is http).
unsigned short getWellKnownPort() const;
/// Returns the well-known port number for the URI's scheme,
/// or 0 if the port number is not known.
void parse(const std::string& uri);
/// Parses and assigns an URI from the given string. Throws a
/// SyntaxException if the uri is not valid.
void parseAuthority(std::string::const_iterator& it, const std::string::const_iterator& end);
/// Parses and sets the user-info, host and port from the given data.
void parseHostAndPort(std::string::const_iterator& it, const std::string::const_iterator& end);
/// Parses and sets the host and port from the given data.
void parsePath(std::string::const_iterator& it, const std::string::const_iterator& end);
/// Parses and sets the path from the given data.
void parsePathEtc(std::string::const_iterator& it, const std::string::const_iterator& end);
/// Parses and sets the path, query and fragment from the given data.
void parseQuery(std::string::const_iterator& it, const std::string::const_iterator& end);
/// Parses and sets the query from the given data.
void parseFragment(std::string::const_iterator& it, const std::string::const_iterator& end);
/// Parses and sets the fragment from the given data.
void mergePath(const std::string& path);
/// Appends a path to the URI's path.
void removeDotSegments(bool removeLeading = true);
/// Removes all dot segments from the path.
static void getPathSegments(const std::string& path, std::vector<std::string>& segments);
/// Places the single path segments (delimited by slashes) into the
/// given vector.
void buildPath(const std::vector<std::string>& segments, bool leadingSlash, bool trailingSlash);
/// Builds the path from the given segments.
static const std::string RESERVED_PATH;
static const std::string RESERVED_QUERY;
static const std::string RESERVED_FRAGMENT;
static const std::string ILLEGAL;
private:
std::string _scheme;
std::string _userInfo;
std::string _host;
unsigned short _port;
std::string _path;
std::string _query;
std::string _fragment;
};
//
// inlines
//
inline const std::string& URI::getScheme() const
{
return _scheme;
}
inline const std::string& URI::getUserInfo() const
{
return _userInfo;
}
inline const std::string& URI::getHost() const
{
return _host;
}
inline const std::string& URI::getPath() const
{
return _path;
}
inline const std::string& URI::getQuery() const
{
return _query;
}
inline const std::string& URI::getFragment() const
{
return _fragment;
}
Foundation_END
#endif // Foundation_URI_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -