PageRenderTime 56ms CodeModel.GetById 39ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/lluri.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 177 lines | 68 code | 20 blank | 89 comment | 0 complexity | 72b9ce1c397ea38ac1369bcbd9902094 MD5 | raw file
  1/** 
  2 * @file lluri.h
  3 * @author Phoenix
  4 * @date 2006-02-05
  5 * @brief Declaration of the URI class.
  6 *
  7 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29#ifndef LL_LLURI_H
 30#define LL_LLURI_H
 31
 32#include <string>
 33
 34class LLSD;
 35class LLUUID;
 36class LLApp;
 37
 38/** 
 39 *
 40 * LLURI instances are immutable
 41 * See: http://www.ietf.org/rfc/rfc3986.txt
 42 *
 43 */
 44class LL_COMMON_API LLURI
 45{
 46public:
 47  LLURI();
 48  LLURI(const std::string& escaped_str);
 49  LLURI(const std::string& scheme,
 50		const std::string& userName,
 51		const std::string& password,
 52		const std::string& hostName,
 53		U16 hostPort,
 54		const std::string& escapedPath,
 55		const std::string& escapedQuery);
 56	  
 57  // construct from escaped string, as would be transmitted on the net
 58
 59	~LLURI();
 60
 61	static LLURI buildHTTP(
 62		const std::string& prefix,
 63		const LLSD& path);
 64
 65	static LLURI buildHTTP(
 66		const std::string& prefix,
 67		const LLSD& path,
 68		const LLSD& query);
 69	///< prefix is either a full URL prefix of the form
 70	/// "http://example.com:8080", or it can be simply a host and
 71	/// optional port like "example.com" or "example.com:8080", in
 72	/// these cases, the "http://" will be added
 73
 74	static LLURI buildHTTP(
 75		const std::string& host,
 76		const U32& port,
 77		const LLSD& path);
 78	static LLURI buildHTTP(
 79		const std::string& host,
 80		const U32& port,
 81		const LLSD& path,
 82		const LLSD& query);
 83
 84	std::string asString() const;
 85	///< the whole URI, escaped as needed
 86  
 87	/** @name Parts of a URI */
 88	//@{
 89	// These functions return parts of the decoded URI.  The returned
 90	// strings are un-escaped as needed
 91  
 92	// for all schemes
 93	std::string scheme() const;		///< ex.: "http", note lack of colon
 94	std::string opaque() const;		///< everything after the colon
 95  
 96  // for schemes that follow path like syntax (http, https, ftp)
 97  std::string authority() const;	// ex.: "host.com:80"
 98  std::string hostName() const;	// ex.: "host.com"
 99  std::string userName() const;
100  std::string password() const;
101  U16 hostPort() const;			// ex.: 80, will include implicit port
102  BOOL defaultPort() const;		// true if port is default for scheme
103  const std::string& escapedPath() const { return mEscapedPath; }
104  std::string path() const;		// ex.: "/abc/def", includes leading slash
105  LLSD pathArray() const;			// above decoded into an array of strings
106  std::string query() const;		// ex.: "x=34", section after "?"
107  const std::string& escapedQuery() const { return mEscapedQuery; }
108  LLSD queryMap() const;			// above decoded into a map
109  static LLSD queryMap(std::string escaped_query_string);
110
111	/**
112	 * @brief given a name value map, return a serialized query string.
113	 *
114
115	 * @param query_map a map of name value. every value must be
116	 * representable as a string.
117	 * @return Returns an url query string of '?n1=v1&n2=v2&...'
118	 */
119	static std::string mapToQueryString(const LLSD& query_map);
120
121	/** @name Escaping Utilities */
122	//@{
123	/**
124	 * @brief Escape the string passed except for unreserved
125	 *
126	 *  ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
127	 *  0123456789
128	 *  -._~
129	 *
130	 * @see http://www.ietf.org/rfc/rfc1738.txt
131	 *
132	 * @param str The raw URI to escape.
133	 * @return Returns the rfc 1738 escaped uri or an empty string.
134	 */
135	static std::string escape(const std::string& str);
136
137	/**
138	 * @brief Escape a string with a specified set of allowed characters.
139	 *
140	 * Escape a string by urlencoding all the characters that aren't
141	 * in the allowed string.
142	 * @param str The raw URI to escape.
143	 * @param allowed Character array of allowed characters
144	 * @param is_allowed_sorted Optimization hint if allowed array is sorted.
145	 * @return Returns the escaped uri or an empty string.
146	 */
147	static std::string escape(
148		const std::string& str,
149		const std::string& allowed,
150		bool is_allowed_sorted = false);
151
152	/**
153	 * @brief unescape an escaped URI string.
154	 *
155	 * @param str The escped URI to unescape.
156	 * @return Returns the unescaped uri or an empty string.
157	 */
158	static std::string unescape(const std::string& str);
159	//@}
160
161private:
162	 // only "http", "https", "ftp", and "secondlife" schemes are parsed
163	 // secondlife scheme parses authority as "" and includes it as part of
164	 // the path.  See lluri_tut.cpp
165	 // i.e. secondlife://app/login has mAuthority = "" and mPath = "/app/login"
166	void parseAuthorityAndPathUsingOpaque();
167	std::string mScheme;
168	std::string mEscapedOpaque;
169	std::string mEscapedAuthority;
170	std::string mEscapedPath;
171	std::string mEscapedQuery;
172};
173
174// this operator required for tut
175LL_COMMON_API bool operator!=(const LLURI& first, const LLURI& second);
176
177#endif // LL_LLURI_H