1/* 2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012, 2013 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 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 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 URL_h 27#define URL_h 28 29#include <wtf/Forward.h> 30#include <wtf/HashMap.h> 31#include <wtf/RetainPtr.h> 32#include <wtf/text/WTFString.h> 33 34#if USE(CF) 35typedef const struct __CFURL* CFURLRef; 36#endif 37 38#if USE(SOUP) 39#include "GUniquePtrSoup.h" 40#endif 41 42#if USE(FOUNDATION) 43OBJC_CLASS NSURL; 44#endif 45 46namespace WebCore { 47 48class TextEncoding; 49struct URLHash; 50 51enum ParsedURLStringTag { ParsedURLString }; 52 53class URL { 54public: 55 // Generates a URL which contains a null string. 56 URL() { invalidate(); } 57 58 // The argument is an absolute URL string. The string is assumed to be output of URL::string() called on a valid 59 // URL object, or indiscernible from such. 60 // It is usually best to avoid repeatedly parsing a string, unless memory saving outweigh the possible slow-downs. 61 URL(ParsedURLStringTag, const String&); 62 explicit URL(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { } 63 bool isHashTableDeletedValue() const { return string().isHashTableDeletedValue(); } 64 65 // Resolves the relative URL with the given base URL. If provided, the 66 // TextEncoding is used to encode non-ASCII characers. The base URL can be 67 // null or empty, in which case the relative URL will be interpreted as 68 // absolute. 69 // FIXME: If the base URL is invalid, this always creates an invalid 70 // URL. Instead I think it would be better to treat all invalid base URLs 71 // the same way we treate null and empty base URLs. 72 URL(const URL& base, const String& relative); 73 URL(const URL& base, const String& relative, const TextEncoding&); 74 75 static URL fakeURLWithRelativePart(const String&); 76 77 String strippedForUseAsReferrer() const; 78 79 // FIXME: The above functions should be harmonized so that passing a 80 // base of null or the empty string gives the same result as the 81 // standard String constructor. 82 83 // Makes a deep copy. Helpful only if you need to use a URL on another 84 // thread. Since the underlying StringImpl objects are immutable, there's 85 // no other reason to ever prefer copy() over plain old assignment. 86 URL copy() const; 87 88 bool isNull() const; 89 bool isEmpty() const; 90 bool isValid() const; 91 92 // Returns true if you can set the host and port for the URL. 93 // Non-hierarchical URLs don't have a host and port. 94 bool canSetHostOrPort() const { return isHierarchical(); } 95 96 bool canSetPathname() const { return isHierarchical(); } 97 bool isHierarchical() const; 98 99 const String& string() const { return m_string; } 100 101 String stringCenterEllipsizedToLength(unsigned length = 1024) const; 102 103 String protocol() const; 104 String host() const; 105 unsigned short port() const; 106 bool hasPort() const; 107 String user() const; 108 String pass() const; 109 String path() const; 110 String lastPathComponent() const; 111 String query() const; 112 String fragmentIdentifier() const; 113 bool hasFragmentIdentifier() const; 114 115 String baseAsString() const; 116 117 String fileSystemPath() const; 118 119 // Returns true if the current URL's protocol is the same as the null- 120 // terminated ASCII argument. The argument must be lower-case. 121 bool protocolIs(const char*) const; 122 bool protocolIsData() const { return protocolIs("data"); } 123 bool protocolIsInHTTPFamily() const; 124 bool isLocalFile() const; 125 bool isBlankURL() const; 126 127 bool setProtocol(const String&); 128 void setHost(const String&); 129 130 void removePort(); 131 void setPort(unsigned short); 132 133 // Input is like "foo.com" or "foo.com:8000". 134 void setHostAndPort(const String&); 135 136 void setUser(const String&); 137 void setPass(const String&); 138 139 // If you pass an empty path for HTTP or HTTPS URLs, the resulting path 140 // will be "/". 141 void setPath(const String&); 142 143 // The query may begin with a question mark, or, if not, one will be added 144 // for you. Setting the query to the empty string will leave a "?" in the 145 // URL (with nothing after it). To clear the query, pass a null string. 146 void setQuery(const String&); 147 148 void setFragmentIdentifier(const String&); 149 void removeFragmentIdentifier(); 150 151 friend bool equalIgnoringFragmentIdentifier(const URL&, const URL&); 152 153 friend bool protocolHostAndPortAreEqual(const URL&, const URL&); 154 155 unsigned hostStart() const; 156 unsigned hostEnd() const; 157 158 unsigned pathStart() const; 159 unsigned pathEnd() const; 160 unsigned pathAfterLastSlash() const; 161 162 operator const String&() const { return string(); } 163 164#if USE(CF) 165 URL(CFURLRef); 166 RetainPtr<CFURLRef> createCFURL() const; 167#endif 168 169#if USE(SOUP) 170 URL(SoupURI*); 171 GUniquePtr<SoupURI> createSoupURI() const; 172#endif 173 174#if USE(FOUNDATION) 175 URL(NSURL*); 176 operator NSURL*() const; 177#endif 178#ifdef __OBJC__ 179 operator NSString*() const { return string(); } 180#endif 181 182 const URL* innerURL() const { return 0; } 183 184#ifndef NDEBUG 185 void print() const; 186#endif 187 188 bool isSafeToSendToAnotherThread() const; 189 190private: 191 void invalidate(); 192 static bool protocolIs(const String&, const char*); 193 void init(const URL&, const String&, const TextEncoding&); 194 void copyToBuffer(Vector<char, 512>& buffer) const; 195 196 // Parses the given URL. The originalString parameter allows for an 197 // optimization: When the source is the same as the fixed-up string, 198 // it will use the passed-in string instead of allocating a new one. 199 void parse(const String&); 200 void parse(const char* url, const String* originalString = 0); 201 202 bool hasPath() const; 203 204 String m_string; 205 bool m_isValid : 1; 206 bool m_protocolIsInHTTPFamily : 1; 207 208 int m_schemeEnd; 209 int m_userStart; 210 int m_userEnd; 211 int m_passwordEnd; 212 int m_hostEnd; 213 int m_portEnd; 214 int m_pathAfterLastSlash; 215 int m_pathEnd; 216 int m_queryEnd; 217 int m_fragmentEnd; 218}; 219 220bool operator==(const URL&, const URL&); 221bool operator==(const URL&, const String&); 222bool operator==(const String&, const URL&); 223bool operator!=(const URL&, const URL&); 224bool operator!=(const URL&, const String&); 225bool operator!=(const String&, const URL&); 226 227bool equalIgnoringFragmentIdentifier(const URL&, const URL&); 228bool protocolHostAndPortAreEqual(const URL&, const URL&); 229 230const URL& blankURL(); 231 232// Functions to do URL operations on strings. 233// These are operations that aren't faster on a parsed URL. 234// These are also different from the URL functions in that they don't require the string to be a valid and parsable URL. 235// This is especially important because valid javascript URLs are not necessarily considered valid by URL. 236 237bool protocolIs(const String& url, const char* protocol); 238bool protocolIsJavaScript(const String& url); 239bool protocolIsInHTTPFamily(const String& url); 240 241bool isDefaultPortForProtocol(unsigned short port, const String& protocol); 242bool portAllowed(const URL&); // Blacklist ports that should never be used for Web resources. 243 244bool isValidProtocol(const String&); 245 246String mimeTypeFromDataURL(const String& url); 247String mimeTypeFromURL(const URL&); 248 249// Unescapes the given string using URL escaping rules, given an optional 250// encoding (defaulting to UTF-8 otherwise). DANGER: If the URL has "%00" 251// in it, the resulting string will have embedded null characters! 252String decodeURLEscapeSequences(const String&); 253String decodeURLEscapeSequences(const String&, const TextEncoding&); 254 255String encodeWithURLEscapeSequences(const String&); 256 257// Inlines. 258 259inline bool operator==(const URL& a, const URL& b) 260{ 261 return a.string() == b.string(); 262} 263 264inline bool operator==(const URL& a, const String& b) 265{ 266 return a.string() == b; 267} 268 269inline bool operator==(const String& a, const URL& b) 270{ 271 return a == b.string(); 272} 273 274inline bool operator!=(const URL& a, const URL& b) 275{ 276 return a.string() != b.string(); 277} 278 279inline bool operator!=(const URL& a, const String& b) 280{ 281 return a.string() != b; 282} 283 284inline bool operator!=(const String& a, const URL& b) 285{ 286 return a != b.string(); 287} 288 289// Inline versions of some non-GoogleURL functions so we can get inlining 290// without having to have a lot of ugly ifdefs in the class definition. 291 292inline bool URL::isNull() const 293{ 294 return m_string.isNull(); 295} 296 297inline bool URL::isEmpty() const 298{ 299 return m_string.isEmpty(); 300} 301 302inline bool URL::isValid() const 303{ 304 return m_isValid; 305} 306 307inline bool URL::hasPath() const 308{ 309 return m_pathEnd != m_portEnd; 310} 311 312inline bool URL::hasPort() const 313{ 314 return m_hostEnd < m_portEnd; 315} 316 317inline bool URL::protocolIsInHTTPFamily() const 318{ 319 return m_protocolIsInHTTPFamily; 320} 321 322inline unsigned URL::hostStart() const 323{ 324 return (m_passwordEnd == m_userStart) ? m_passwordEnd : m_passwordEnd + 1; 325} 326 327inline unsigned URL::hostEnd() const 328{ 329 return m_hostEnd; 330} 331 332inline unsigned URL::pathStart() const 333{ 334 return m_portEnd; 335} 336 337inline unsigned URL::pathEnd() const 338{ 339 return m_pathEnd; 340} 341 342inline unsigned URL::pathAfterLastSlash() const 343{ 344 return m_pathAfterLastSlash; 345} 346 347#if PLATFORM(IOS) 348void enableURLSchemeCanonicalization(bool); 349#endif 350 351} // namespace WebCore 352 353namespace WTF { 354 355 // URLHash is the default hash for String 356 template<typename T> struct DefaultHash; 357 template<> struct DefaultHash<WebCore::URL> { 358 typedef WebCore::URLHash Hash; 359 }; 360 361} // namespace WTF 362 363#endif // URL_h 364