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