1 /* 2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef KURL_h 27 #define KURL_h 28 29 #include "weborigin/WebOriginExport.h" 30 #include "wtf/Forward.h" 31 #include "wtf/HashMap.h" 32 #include "wtf/HashTableDeletedValueType.h" 33 #include "wtf/OwnPtr.h" 34 #include "wtf/text/CString.h" 35 #include "wtf/text/TextEncoding.h" 36 #include "wtf/text/WTFString.h" 37 #include <url/url_canon.h> 38 #include <url/url_parse.h> 39 40 namespace WebCore { 41 42 struct KURLHash; 43 44 enum ParsedURLStringTag { ParsedURLString }; 45 46 class WEBORIGIN_EXPORT KURL { 47 public: 48 KURL() 49 : m_isValid(false) 50 , m_protocolIsInHTTPFamily(false) 51 { 52 } 53 54 KURL(const KURL&); 55 KURL& operator=(const KURL&); 56 57 // The argument is an absolute URL string. The string is assumed to be 58 // output of KURL::string() called on a valid KURL object, or indiscernible 59 // from such. It is usually best to avoid repeatedly parsing a string, 60 // unless memory saving outweigh the possible slow-downs. 61 KURL(ParsedURLStringTag, const String&); 62 explicit KURL(WTF::HashTableDeletedValueType); 63 64 // Creates an isolated URL object suitable for sending to another thread. 65 static KURL createIsolated(ParsedURLStringTag, const String&); 66 67 bool isHashTableDeletedValue() const { return string().isHashTableDeletedValue(); } 68 69 // Resolves the relative URL with the given base URL. If provided, the 70 // TextEncoding is used to encode non-ASCII characers. The base URL can be 71 // null or empty, in which case the relative URL will be interpreted as 72 // absolute. 73 // FIXME: If the base URL is invalid, this always creates an invalid 74 // URL. Instead I think it would be better to treat all invalid base URLs 75 // the same way we treate null and empty base URLs. 76 KURL(const KURL& base, const String& relative); 77 KURL(const KURL& base, const String& relative, const WTF::TextEncoding&); 78 79 // For conversions from other structures that have already parsed and 80 // canonicalized the URL. The input must be exactly what KURL would have 81 // done with the same input. 82 KURL(const AtomicString& canonicalString, const url_parse::Parsed&, bool isValid); 83 84 String strippedForUseAsReferrer() const; 85 86 // FIXME: The above functions should be harmonized so that passing a 87 // base of null or the empty string gives the same result as the 88 // standard String constructor. 89 90 // Makes a deep copy. Helpful only if you need to use a KURL on another 91 // thread. Since the underlying StringImpl objects are immutable, there's 92 // no other reason to ever prefer copy() over plain old assignment. 93 KURL copy() const; 94 95 bool isNull() const; 96 bool isEmpty() const; 97 bool isValid() const; 98 99 // Returns true if this URL has a path. Note that "http://foo.com/" has a 100 // path of "/", so this function will return true. Only invalid or 101 // non-hierarchical (like "javascript:") URLs will have no path. 102 bool hasPath() const; 103 104 // Returns true if you can set the host and port for the URL. 105 // Non-hierarchical URLs don't have a host and port. 106 bool canSetHostOrPort() const { return isHierarchical(); } 107 108 bool canSetPathname() const { return isHierarchical(); } 109 bool isHierarchical() const; 110 111 const String& string() const { return m_string; } 112 113 String elidedString() const; 114 115 String protocol() const; 116 String host() const; 117 unsigned short port() const; 118 bool hasPort() const; 119 String user() const; 120 String pass() const; 121 String path() const; 122 String lastPathComponent() const; 123 String query() const; 124 String fragmentIdentifier() const; 125 bool hasFragmentIdentifier() const; 126 127 String baseAsString() const; 128 129 // Returns true if the current URL's protocol is the same as the null- 130 // terminated ASCII argument. The argument must be lower-case. 131 bool protocolIs(const char*) const; 132 bool protocolIsData() const { return protocolIs("data"); } 133 bool protocolIsInHTTPFamily() const; 134 bool isLocalFile() const; 135 bool isBlankURL() const; 136 137 bool setProtocol(const String&); 138 void setHost(const String&); 139 140 void removePort(); 141 void setPort(unsigned short); 142 143 // Input is like "foo.com" or "foo.com:8000". 144 void setHostAndPort(const String&); 145 146 void setUser(const String&); 147 void setPass(const String&); 148 149 // If you pass an empty path for HTTP or HTTPS URLs, the resulting path 150 // will be "/". 151 void setPath(const String&); 152 153 // The query may begin with a question mark, or, if not, one will be added 154 // for you. Setting the query to the empty string will leave a "?" in the 155 // URL (with nothing after it). To clear the query, pass a null string. 156 void setQuery(const String&); 157 158 void setFragmentIdentifier(const String&); 159 void removeFragmentIdentifier(); 160 161 WEBORIGIN_EXPORT friend bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&); 162 163 unsigned hostStart() const; 164 unsigned hostEnd() const; 165 166 unsigned pathStart() const; 167 unsigned pathEnd() const; 168 unsigned pathAfterLastSlash() const; 169 170 operator const String&() const { return string(); } 171 172 const url_parse::Parsed& parsed() const { return m_parsed; } 173 174 const KURL* innerURL() const { return m_innerURL.get(); } 175 176 #ifndef NDEBUG 177 void print() const; 178 #endif 179 180 bool isSafeToSendToAnotherThread() const; 181 182 private: 183 void init(const KURL& base, const String& relative, const WTF::TextEncoding* queryEncoding); 184 185 String componentString(const url_parse::Component&) const; 186 String stringForInvalidComponent() const; 187 188 template<typename CHAR> 189 void replaceComponents(const url_canon::Replacements<CHAR>&); 190 191 template <typename CHAR> 192 void init(const KURL& base, const CHAR* relative, int relativeLength, const WTF::TextEncoding* queryEncoding); 193 void initInnerURL(); 194 void initProtocolIsInHTTPFamily(); 195 196 bool m_isValid; 197 bool m_protocolIsInHTTPFamily; 198 url_parse::Parsed m_parsed; 199 String m_string; 200 OwnPtr<KURL> m_innerURL; 201 }; 202 203 WEBORIGIN_EXPORT bool operator==(const KURL&, const KURL&); 204 WEBORIGIN_EXPORT bool operator==(const KURL&, const String&); 205 WEBORIGIN_EXPORT bool operator==(const String&, const KURL&); 206 WEBORIGIN_EXPORT bool operator!=(const KURL&, const KURL&); 207 WEBORIGIN_EXPORT bool operator!=(const KURL&, const String&); 208 WEBORIGIN_EXPORT bool operator!=(const String&, const KURL&); 209 210 WEBORIGIN_EXPORT bool equalIgnoringFragmentIdentifier(const KURL&, const KURL&); 211 212 WEBORIGIN_EXPORT const KURL& blankURL(); 213 214 // Functions to do URL operations on strings. 215 // These are operations that aren't faster on a parsed URL. 216 // These are also different from the KURL functions in that they don't require the string to be a valid and parsable URL. 217 // This is especially important because valid javascript URLs are not necessarily considered valid by KURL. 218 219 WEBORIGIN_EXPORT bool protocolIs(const String& url, const char* protocol); 220 WEBORIGIN_EXPORT bool protocolIsJavaScript(const String& url); 221 222 WEBORIGIN_EXPORT bool isValidProtocol(const String&); 223 224 // Unescapes the given string using URL escaping rules, given an optional 225 // encoding (defaulting to UTF-8 otherwise). DANGER: If the URL has "%00" 226 // in it, the resulting string will have embedded null characters! 227 WEBORIGIN_EXPORT String decodeURLEscapeSequences(const String&); 228 WEBORIGIN_EXPORT String decodeURLEscapeSequences(const String&, const WTF::TextEncoding&); 229 230 WEBORIGIN_EXPORT String encodeWithURLEscapeSequences(const String&); 231 232 // Inlines. 233 234 inline bool operator==(const KURL& a, const KURL& b) 235 { 236 return a.string() == b.string(); 237 } 238 239 inline bool operator==(const KURL& a, const String& b) 240 { 241 return a.string() == b; 242 } 243 244 inline bool operator==(const String& a, const KURL& b) 245 { 246 return a == b.string(); 247 } 248 249 inline bool operator!=(const KURL& a, const KURL& b) 250 { 251 return a.string() != b.string(); 252 } 253 254 inline bool operator!=(const KURL& a, const String& b) 255 { 256 return a.string() != b; 257 } 258 259 inline bool operator!=(const String& a, const KURL& b) 260 { 261 return a != b.string(); 262 } 263 264 } // namespace WebCore 265 266 namespace WTF { 267 268 // KURLHash is the default hash for String 269 template<typename T> struct DefaultHash; 270 template<> struct DefaultHash<WebCore::KURL> { 271 typedef WebCore::KURLHash Hash; 272 }; 273 274 } // namespace WTF 275 276 #endif // KURL_h 277