1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/history/url_utils.h" 6 7 #include <algorithm> 8 9 namespace history { 10 11 namespace { 12 13 // Comparator to enforce '\0' < '?' < '#' < '/' < other characters. 14 int GetURLCharPriority(char ch) { 15 switch (ch) { 16 case '\0': return 0; 17 case '?': return 1; 18 case '#': return 2; 19 case '/': return 3; 20 } 21 return 4; 22 } 23 24 } // namespace 25 26 // Instead of splitting URLs and extract path components, we can implement 27 // CanonicalURLStringCompare() using string operations only. The key idea is, 28 // treating '/' to be less than any valid path characters would make it behave 29 // as a separator, so e.g., "test" < "test-case" would be enforced by 30 // "test/..." < "test-case/...". We also force "?" < "/", so "test?query" < 31 // "test/stuff". Since the routine is merely lexicographical string comparison 32 // with remapping of chracter ordering, so it is a valid strict-weak ordering. 33 bool CanonicalURLStringCompare(const std::string& s1, const std::string& s2) { 34 const std::string::value_type* ch1 = s1.c_str(); 35 const std::string::value_type* ch2 = s2.c_str(); 36 while (*ch1 && *ch2 && *ch1 == *ch2) { 37 ++ch1; 38 ++ch2; 39 } 40 int pri_diff = GetURLCharPriority(*ch1) - GetURLCharPriority(*ch2); 41 // We want false to be returned if |pri_diff| > 0. 42 return (pri_diff != 0) ? pri_diff < 0 : *ch1 < *ch2; 43 } 44 45 bool HaveSameSchemeHostAndPort(const GURL&url1, const GURL& url2) { 46 return url1.scheme() == url2.scheme() && url1.host() == url2.host() && 47 url1.port() == url2.port(); 48 } 49 50 bool IsPathPrefix(const std::string& p1, const std::string& p2) { 51 if (p1.length() > p2.length()) 52 return false; 53 std::pair<std::string::const_iterator, std::string::const_iterator> 54 first_diff = std::mismatch(p1.begin(), p1.end(), p2.begin()); 55 // Necessary condition: |p1| is a string prefix of |p2|. 56 if (first_diff.first != p1.end()) 57 return false; // E.g.: (|p1| = "/test", |p2| = "/exam") => false. 58 59 // |p1| is string prefix. 60 if (first_diff.second == p2.end()) // Is exact match? 61 return true; // E.g.: ("/test", "/test") => true. 62 // |p1| is strict string prefix, check full match of last path component. 63 if (!p1.empty() && *p1.rbegin() == '/') // Ends in '/'? 64 return true; // E.g.: ("/test/", "/test/stuff") => true. 65 66 // Finally, |p1| does not end in "/": check first extra character in |p2|. 67 // E.g.: ("/test", "/test/stuff") => true; ("/test", "/testing") => false. 68 return *(first_diff.second) == '/'; 69 } 70 71 GURL ToggleHTTPAndHTTPS(const GURL& url) { 72 std::string new_scheme; 73 if (url.SchemeIs("http")) 74 new_scheme = "https"; 75 else if (url.SchemeIs("https")) 76 new_scheme = "http"; 77 else 78 return GURL::EmptyGURL(); 79 url_parse::Component comp; 80 comp.len = new_scheme.length(); 81 GURL::Replacements replacement; 82 replacement.SetScheme(new_scheme.c_str(), comp); 83 return url.ReplaceComponents(replacement); 84 } 85 86 } // namespace history 87