1 // Copyright (c) 2009 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 #ifndef NET_TOOLS_FLIP_SERVER_URL_UTILITIES_H__ 6 #define NET_TOOLS_FLIP_SERVER_URL_UTILITIES_H__ 7 8 #include <string> 9 10 namespace net { 11 12 struct UrlUtilities { 13 // Get the host from an url 14 static std::string GetUrlHost(const std::string& url) { 15 size_t b = url.find("//"); 16 if (b == std::string::npos) 17 b = 0; 18 else 19 b += 2; 20 size_t next_slash = url.find_first_of('/', b); 21 size_t next_colon = url.find_first_of(':', b); 22 if (next_slash != std::string::npos 23 && next_colon != std::string::npos 24 && next_colon < next_slash) { 25 return std::string(url, b, next_colon - b); 26 } 27 if (next_slash == std::string::npos) { 28 if (next_colon != std::string::npos) { 29 return std::string(url, next_colon - b); 30 } else { 31 next_slash = url.size(); 32 } 33 } 34 return std::string(url, b, next_slash - b); 35 } 36 37 // Get the host + path portion of an url 38 // e.g http://www.foo.com/path 39 // returns www.foo.com/path 40 static std::string GetUrlHostPath(const std::string& url) { 41 size_t b = url.find("//"); 42 if (b == std::string::npos) 43 b = 0; 44 else 45 b += 2; 46 return std::string(url, b); 47 } 48 49 // Get the path portion of an url 50 // e.g http://www.foo.com/path 51 // returns /path 52 static std::string GetUrlPath(const std::string& url) { 53 size_t b = url.find("//"); 54 if (b == std::string::npos) 55 b = 0; 56 else 57 b += 2; 58 b = url.find("/", b+1); 59 if (b == std::string::npos) 60 return "/"; 61 62 return std::string(url, b); 63 } 64 }; 65 66 } // namespace net 67 68 #endif // NET_TOOLS_FLIP_SERVER_URL_UTILITIES_H__ 69 70