[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | de294335 | 2009-10-22 23:06:12 | [diff] [blame] | 5 | #ifndef NET_BASE_NET_UTIL_H_ |
| 6 | #define NET_BASE_NET_UTIL_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 8 | |
[email protected] | e537c35 | 2008-08-20 21:42:17 | [diff] [blame] | 9 | #include "build/build_config.h" |
| 10 | |
[email protected] | 93e7901 | 2011-01-26 04:13:59 | [diff] [blame] | 11 | #if defined(OS_WIN) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 12 | #include <windows.h> |
[email protected] | 93e7901 | 2011-01-26 04:13:59 | [diff] [blame] | 13 | #include <ws2tcpip.h> |
| 14 | #elif defined(OS_POSIX) |
| 15 | #include <sys/socket.h> |
[email protected] | e537c35 | 2008-08-20 21:42:17 | [diff] [blame] | 16 | #endif |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 17 | |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 18 | #include <list> |
[email protected] | d16659f | 2009-05-18 16:47:32 | [diff] [blame] | 19 | #include <string> |
[email protected] | d95fa18 | 2009-09-09 17:01:16 | [diff] [blame] | 20 | #include <set> |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 21 | #include <vector> |
[email protected] | d16659f | 2009-05-18 16:47:32 | [diff] [blame] | 22 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | #include "base/basictypes.h" |
[email protected] | 5420bc1e | 2009-07-09 22:48:16 | [diff] [blame] | 24 | #include "base/string16.h" |
[email protected] | a23de857 | 2009-06-03 02:16:32 | [diff] [blame] | 25 | #include "net/base/escape.h" |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 26 | #include "net/base/net_api.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 27 | |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 28 | struct addrinfo; |
[email protected] | 498c1a6b2 | 2008-11-24 23:37:04 | [diff] [blame] | 29 | class FilePath; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 30 | class GURL; |
| 31 | |
[email protected] | 13ef7c0 | 2008-11-20 22:30:13 | [diff] [blame] | 32 | namespace base { |
| 33 | class Time; |
| 34 | } |
| 35 | |
[email protected] | 01dbd93 | 2009-06-23 22:52:42 | [diff] [blame] | 36 | namespace url_canon { |
| 37 | struct CanonHostInfo; |
| 38 | } |
| 39 | |
[email protected] | f9fe863 | 2009-05-22 18:15:24 | [diff] [blame] | 40 | namespace url_parse { |
| 41 | struct Parsed; |
| 42 | } |
| 43 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 44 | namespace net { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 45 | |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 46 | // Used by FormatUrl to specify handling of certain parts of the url. |
| 47 | typedef uint32 FormatUrlType; |
| 48 | typedef uint32 FormatUrlTypes; |
| 49 | |
[email protected] | c09fb1c7 | 2010-11-03 23:25:55 | [diff] [blame] | 50 | // Used by GetHeaderParamValue to determine how to handle quotes in the value. |
| 51 | class QuoteRule { |
| 52 | public: |
| 53 | enum Type { |
| 54 | KEEP_OUTER_QUOTES, |
| 55 | REMOVE_OUTER_QUOTES, |
| 56 | }; |
| 57 | |
| 58 | private: |
| 59 | QuoteRule(); |
| 60 | }; |
| 61 | |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 62 | // Nothing is ommitted. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 63 | NET_API extern const FormatUrlType kFormatUrlOmitNothing; |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 64 | |
| 65 | // If set, any username and password are removed. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 66 | NET_API extern const FormatUrlType kFormatUrlOmitUsernamePassword; |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 67 | |
| 68 | // If the scheme is 'http://', it's removed. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 69 | NET_API extern const FormatUrlType kFormatUrlOmitHTTP; |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 70 | |
[email protected] | 79845ef | 2010-06-02 02:37:40 | [diff] [blame] | 71 | // Omits the path if it is just a slash and there is no query or ref. This is |
| 72 | // meaningful for non-file "standard" URLs. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 73 | NET_API extern const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname; |
[email protected] | 79845ef | 2010-06-02 02:37:40 | [diff] [blame] | 74 | |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 75 | // Convenience for omitting all unecessary types. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 76 | NET_API extern const FormatUrlType kFormatUrlOmitAll; |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 77 | |
[email protected] | d95fa18 | 2009-09-09 17:01:16 | [diff] [blame] | 78 | // Holds a list of ports that should be accepted despite bans. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 79 | NET_TEST extern std::multiset<int> explicitly_allowed_ports; |
[email protected] | d95fa18 | 2009-09-09 17:01:16 | [diff] [blame] | 80 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 81 | // Given the full path to a file name, creates a file: URL. The returned URL |
| 82 | // may not be valid if the input is malformed. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 83 | NET_API GURL FilePathToFileURL(const FilePath& path); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 84 | |
| 85 | // Converts a file: URL back to a filename that can be passed to the OS. The |
| 86 | // file URL must be well-formed (GURL::is_valid() must return true); we don't |
| 87 | // handle degenerate cases here. Returns true on success, false if it isn't a |
| 88 | // valid file URL. On failure, *file_path will be empty. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 89 | NET_API bool FileURLToFilePath(const GURL& url, FilePath* file_path); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 90 | |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 91 | // Splits an input of the form <host>[":"<port>] into its consitituent parts. |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 92 | // Saves the result into |*host| and |*port|. If the input did not have |
| 93 | // the optional port, sets |*port| to -1. |
| 94 | // Returns true if the parsing was successful, false otherwise. |
[email protected] | d16659f | 2009-05-18 16:47:32 | [diff] [blame] | 95 | // The returned host is NOT canonicalized, and may be invalid. If <host> is |
| 96 | // an IPv6 literal address, the returned host includes the square brackets. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 97 | NET_TEST bool ParseHostAndPort(std::string::const_iterator host_and_port_begin, |
| 98 | std::string::const_iterator host_and_port_end, |
| 99 | std::string* host, |
| 100 | int* port); |
| 101 | NET_TEST bool ParseHostAndPort(const std::string& host_and_port, |
| 102 | std::string* host, |
| 103 | int* port); |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 104 | |
| 105 | // Returns a host:port string for the given URL. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 106 | NET_API std::string GetHostAndPort(const GURL& url); |
[email protected] | 71e4573a | 2009-05-21 22:03:00 | [diff] [blame] | 107 | |
| 108 | // Returns a host[:port] string for the given URL, where the port is omitted |
| 109 | // if it is the default for the URL's scheme. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 110 | NET_TEST std::string GetHostAndOptionalPort(const GURL& url); |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 111 | |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 112 | // Returns the string representation of an address, like "192.168.0.1". |
| 113 | // Returns empty string on failure. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 114 | NET_API std::string NetAddressToString(const struct addrinfo* net_address); |
| 115 | NET_API std::string NetAddressToString(const struct sockaddr* net_address, |
| 116 | socklen_t address_len); |
[email protected] | 50d7d728 | 2009-03-02 21:45:18 | [diff] [blame] | 117 | |
[email protected] | d1388f4 | 2010-06-16 03:14:42 | [diff] [blame] | 118 | // Same as NetAddressToString, but additionally includes the port number. For |
| 119 | // example: "192.168.0.1:99" or "[::1]:80". |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 120 | NET_API std::string NetAddressToStringWithPort( |
| 121 | const struct addrinfo* net_address); |
| 122 | NET_API std::string NetAddressToStringWithPort( |
| 123 | const struct sockaddr* net_address, |
| 124 | socklen_t address_len); |
[email protected] | d1388f4 | 2010-06-16 03:14:42 | [diff] [blame] | 125 | |
[email protected] | eba29134 | 2009-03-03 21:30:46 | [diff] [blame] | 126 | // Returns the hostname of the current system. Returns empty string on failure. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 127 | NET_API std::string GetHostName(); |
[email protected] | eba29134 | 2009-03-03 21:30:46 | [diff] [blame] | 128 | |
[email protected] | 99d6935 | 2009-09-16 00:20:29 | [diff] [blame] | 129 | // Extracts the unescaped username/password from |url|, saving the results |
| 130 | // into |*username| and |*password|. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 131 | NET_TEST void GetIdentityFromURL(const GURL& url, |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 132 | string16* username, |
| 133 | string16* password); |
[email protected] | 99d6935 | 2009-09-16 00:20:29 | [diff] [blame] | 134 | |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 135 | // Returns either the host from |url|, or, if the host is empty, the full spec. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 136 | NET_API std::string GetHostOrSpecFromURL(const GURL& url); |
[email protected] | 9d797f3 | 2010-04-23 07:17:54 | [diff] [blame] | 137 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 138 | // Return the value of the HTTP response header with name 'name'. 'headers' |
[email protected] | 8effd3f6 | 2011-03-25 16:29:07 | [diff] [blame] | 139 | // should be in the format that URLRequest::GetResponseHeaders() returns. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 140 | // Returns the empty string if the header is not found. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 141 | NET_API std::string GetSpecificHeader(const std::string& headers, |
| 142 | const std::string& name); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 143 | |
| 144 | // Return the value of the HTTP response header field's parameter named |
| 145 | // 'param_name'. Returns the empty string if the parameter is not found or is |
| 146 | // improperly formatted. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 147 | NET_API std::string GetHeaderParamValue(const std::string& header, |
| 148 | const std::string& param_name, |
| 149 | QuoteRule::Type quote_rule); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 150 | |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 151 | // Return the filename extracted from Content-Disposition header. The following |
| 152 | // formats are tried in order listed below: |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 153 | // |
[email protected] | c09fb1c7 | 2010-11-03 23:25:55 | [diff] [blame] | 154 | // 1. RFC 5987 |
| 155 | // 2. RFC 2047 |
| 156 | // 3. Raw-8bit-characters : |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 157 | // a. UTF-8, b. referrer_charset, c. default os codepage. |
[email protected] | c09fb1c7 | 2010-11-03 23:25:55 | [diff] [blame] | 158 | // 4. %-escaped UTF-8. |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 159 | // |
[email protected] | c09fb1c7 | 2010-11-03 23:25:55 | [diff] [blame] | 160 | // In step 3, if referrer_charset is empty(i.e. unknown), 3b is skipped. |
| 161 | // In step 4, the fallback charsets tried in step 3 are not tried. We |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 162 | // can consider doing that later. |
| 163 | // |
[email protected] | c09fb1c7 | 2010-11-03 23:25:55 | [diff] [blame] | 164 | // When a param value is ASCII, but is not in format #2 or format #4 above, |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 165 | // it is returned as it is unless it's pretty close to two supported |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 166 | // formats but not well-formed. In that case, an empty string is returned. |
| 167 | // |
| 168 | // In any case, a caller must check for the empty return value and resort to |
| 169 | // another means to get a filename (e.g. url). |
| 170 | // |
| 171 | // This function does not do any escaping and callers are responsible for |
| 172 | // escaping 'unsafe' characters (e.g. (back)slash, colon) as they see fit. |
| 173 | // |
| 174 | // TODO(jungshik): revisit this issue. At the moment, the only caller |
| 175 | // net_util::GetSuggestedFilename and it calls ReplaceIllegalCharacters. The |
| 176 | // other caller is a unit test. Need to figure out expose this function only to |
| 177 | // net_util_unittest. |
| 178 | // |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 179 | NET_TEST std::string GetFileNameFromCD(const std::string& header, |
| 180 | const std::string& referrer_charset); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 181 | |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 182 | // Converts the given host name to unicode characters. This can be called for |
| 183 | // any host name, if the input is not IDN or is invalid in some way, we'll just |
| 184 | // return the ASCII source so it is still usable. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 185 | // |
| 186 | // The input should be the canonicalized ASCII host name from GURL. This |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 187 | // function does NOT accept UTF-8! |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 188 | // |
| 189 | // |languages| is a comma separated list of ISO 639 language codes. It |
| 190 | // is used to determine whether a hostname is 'comprehensible' to a user |
| 191 | // who understands languages listed. |host| will be converted to a |
| 192 | // human-readable form (Unicode) ONLY when each component of |host| is |
| 193 | // regarded as 'comprehensible'. Scipt-mixing is not allowed except that |
| 194 | // Latin letters in the ASCII range can be mixed with a limited set of |
| 195 | // script-language pairs (currently Han, Kana and Hangul for zh,ja and ko). |
| 196 | // When |languages| is empty, even that mixing is not allowed. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 197 | NET_API string16 IDNToUnicode(const std::string& host, |
| 198 | const std::string& languages); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 199 | |
[email protected] | 01dbd93 | 2009-06-23 22:52:42 | [diff] [blame] | 200 | // Canonicalizes |host| and returns it. Also fills |host_info| with |
| 201 | // IP address information. |host_info| must not be NULL. |
| 202 | std::string CanonicalizeHost(const std::string& host, |
| 203 | url_canon::CanonHostInfo* host_info); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 204 | |
[email protected] | 833fa26 | 2009-10-27 21:06:11 | [diff] [blame] | 205 | // Returns true if |host| is not an IP address and is compliant with a set of |
| 206 | // rules based on RFC 1738 and tweaked to be compatible with the real world. |
| 207 | // The rules are: |
[email protected] | 96be43e9 | 2009-10-16 19:49:22 | [diff] [blame] | 208 | // * One or more components separated by '.' |
| 209 | // * Each component begins and ends with an alphanumeric character |
[email protected] | 833fa26 | 2009-10-27 21:06:11 | [diff] [blame] | 210 | // * Each component contains only alphanumeric characters and '-' or '_' |
[email protected] | 96be43e9 | 2009-10-16 19:49:22 | [diff] [blame] | 211 | // * The last component does not begin with a digit |
[email protected] | d120c372 | 2009-11-03 18:17:26 | [diff] [blame] | 212 | // * Optional trailing dot after last component (means "treat as FQDN") |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame] | 213 | // If |desired_tld| is non-NULL, the host will only be considered invalid if |
| 214 | // appending it as a trailing component still results in an invalid host. This |
| 215 | // helps us avoid marking as "invalid" user attempts to open "www.401k.com" by |
| 216 | // typing 4-0-1-k-<ctrl>+<enter>. |
[email protected] | 96be43e9 | 2009-10-16 19:49:22 | [diff] [blame] | 217 | // |
| 218 | // NOTE: You should only pass in hosts that have been returned from |
| 219 | // CanonicalizeHost(), or you may not get accurate results. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 220 | NET_API bool IsCanonicalizedHostCompliant(const std::string& host, |
| 221 | const std::string& desired_tld); |
[email protected] | 96be43e9 | 2009-10-16 19:49:22 | [diff] [blame] | 222 | |
[email protected] | 5420bc1e | 2009-07-09 22:48:16 | [diff] [blame] | 223 | // Call these functions to get the html snippet for a directory listing. |
| 224 | // The return values of both functions are in UTF-8. |
| 225 | std::string GetDirectoryListingHeader(const string16& title); |
| 226 | |
| 227 | // Given the name of a file in a directory (ftp or local) and |
| 228 | // other information (is_dir, size, modification time), it returns |
| 229 | // the html snippet to add the entry for the file to the directory listing. |
| 230 | // Currently, it's a script tag containing a call to a Javascript function |
| 231 | // |addRow|. |
| 232 | // |
[email protected] | 193c351 | 2010-05-11 09:19:30 | [diff] [blame] | 233 | // |name| is the file name to be displayed. |raw_bytes| will be used |
| 234 | // as the actual target of the link (so for example, ftp links should use |
| 235 | // server's encoding). If |raw_bytes| is an empty string, UTF-8 encoded |name| |
| 236 | // will be used. |
| 237 | // |
| 238 | // Both |name| and |raw_bytes| are escaped internally. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 239 | NET_API std::string GetDirectoryListingEntry(const string16& name, |
| 240 | const std::string& raw_bytes, |
| 241 | bool is_dir, int64 size, |
| 242 | base::Time modified); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 243 | |
| 244 | // If text starts with "www." it is removed, otherwise text is returned |
| 245 | // unmodified. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 246 | NET_API string16 StripWWW(const string16& text); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 247 | |
| 248 | // Gets the filename from the raw Content-Disposition header (as read from the |
| 249 | // network). Otherwise uses the last path component name or hostname from |
[email protected] | 630947c | 2009-11-04 18:37:31 | [diff] [blame] | 250 | // |url|. If there is no filename or it can't be used, the given |default_name|, |
| 251 | // will be used unless it is empty. |
[email protected] | de294335 | 2009-10-22 23:06:12 | [diff] [blame] | 252 | |
| 253 | // Note: it's possible for the suggested filename to be empty (e.g., |
[email protected] | 13c34d1 | 2009-09-14 20:51:04 | [diff] [blame] | 254 | // file:///). referrer_charset is used as one of charsets |
[email protected] | c9825a4 | 2009-05-01 22:51:50 | [diff] [blame] | 255 | // to interpret a raw 8bit string in C-D header (after interpreting |
| 256 | // as UTF-8 fails). See the comment for GetFilenameFromCD for more details. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 257 | NET_API string16 GetSuggestedFilename(const GURL& url, |
| 258 | const std::string& content_disposition, |
| 259 | const std::string& referrer_charset, |
| 260 | const string16& default_name); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 261 | |
| 262 | // Checks the given port against a list of ports which are restricted by |
| 263 | // default. Returns true if the port is allowed, false if it is restricted. |
| 264 | bool IsPortAllowedByDefault(int port); |
| 265 | |
| 266 | // Checks the given port against a list of ports which are restricted by the |
| 267 | // FTP protocol. Returns true if the port is allowed, false if it is |
| 268 | // restricted. |
| 269 | bool IsPortAllowedByFtp(int port); |
| 270 | |
[email protected] | d95fa18 | 2009-09-09 17:01:16 | [diff] [blame] | 271 | // Check if banned |port| has been overriden by an entry in |
| 272 | // |explicitly_allowed_ports_|. |
| 273 | bool IsPortAllowedByOverride(int port); |
| 274 | |
[email protected] | f6f1ba3cf | 2008-11-11 01:06:15 | [diff] [blame] | 275 | // Set socket to non-blocking mode |
| 276 | int SetNonBlocking(int fd); |
| 277 | |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 278 | // Formats the host in |url| and appends it to |output|. The host formatter |
| 279 | // takes the same accept languages component as ElideURL(). |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 280 | void AppendFormattedHost(const GURL& url, |
[email protected] | 04866c4 | 2011-05-03 20:03:50 | [diff] [blame] | 281 | const std::string& languages, |
| 282 | string16* output); |
[email protected] | f9fe863 | 2009-05-22 18:15:24 | [diff] [blame] | 283 | |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 284 | // Creates a string representation of |url|. The IDN host name may be in Unicode |
[email protected] | 69c579e | 2010-04-23 20:01:00 | [diff] [blame] | 285 | // if |languages| accepts the Unicode representation. |format_type| is a bitmask |
| 286 | // of FormatUrlTypes, see it for details. |unescape_rules| defines how to clean |
| 287 | // the URL for human readability. You will generally want |UnescapeRule::SPACES| |
| 288 | // for display to the user if you can handle spaces, or |UnescapeRule::NORMAL| |
| 289 | // if not. If the path part and the query part seem to be encoded in %-encoded |
| 290 | // UTF-8, decodes %-encoding and UTF-8. |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 291 | // |
| 292 | // The last three parameters may be NULL. |
| 293 | // |new_parsed| will be set to the parsing parameters of the resultant URL. |
[email protected] | a23de857 | 2009-06-03 02:16:32 | [diff] [blame] | 294 | // |prefix_end| will be the length before the hostname of the resultant URL. |
[email protected] | 421de2ab | 2011-04-13 18:43:05 | [diff] [blame] | 295 | // |
| 296 | // (|offset[s]_for_adjustment|) specifies one or more offsets into the original |
| 297 | // |url|'s spec(); each offset will be modified to reflect changes this function |
| 298 | // makes to the output string. For example, if |url| is "http://a:[email protected]/", |
| 299 | // |omit_username_password| is true, and an offset is 12 (the offset of '.'), |
| 300 | // then on return the output string will be "http://c.com/" and the offset will |
| 301 | // be 8. If an offset cannot be successfully adjusted (e.g. because it points |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 302 | // into the middle of a component that was entirely removed, past the end of the |
| 303 | // string, or into the middle of an encoding sequence), it will be set to |
[email protected] | 9f284f13 | 2010-08-31 06:14:17 | [diff] [blame] | 304 | // string16::npos. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 305 | NET_API string16 FormatUrl(const GURL& url, |
| 306 | const std::string& languages, |
| 307 | FormatUrlTypes format_types, |
| 308 | UnescapeRule::Type unescape_rules, |
| 309 | url_parse::Parsed* new_parsed, |
| 310 | size_t* prefix_end, |
| 311 | size_t* offset_for_adjustment); |
| 312 | NET_API string16 FormatUrlWithOffsets( |
| 313 | const GURL& url, |
| 314 | const std::string& languages, |
| 315 | FormatUrlTypes format_types, |
| 316 | UnescapeRule::Type unescape_rules, |
| 317 | url_parse::Parsed* new_parsed, |
| 318 | size_t* prefix_end, |
| 319 | std::vector<size_t>* offsets_for_adjustment); |
[email protected] | f9fe863 | 2009-05-22 18:15:24 | [diff] [blame] | 320 | |
[email protected] | 79845ef | 2010-06-02 02:37:40 | [diff] [blame] | 321 | // This is a convenience function for FormatUrl() with |
| 322 | // format_types = kFormatUrlOmitAll and unescape = SPACES. This is the typical |
| 323 | // set of flags for "URLs to display to the user". You should be cautious about |
| 324 | // using this for URLs which will be parsed or sent to other applications. |
[email protected] | 1f659d97 | 2010-08-30 04:49:56 | [diff] [blame] | 325 | inline string16 FormatUrl(const GURL& url, const std::string& languages) { |
| 326 | return FormatUrl(url, languages, kFormatUrlOmitAll, UnescapeRule::SPACES, |
| 327 | NULL, NULL, NULL); |
| 328 | } |
[email protected] | f9fe863 | 2009-05-22 18:15:24 | [diff] [blame] | 329 | |
[email protected] | 79845ef | 2010-06-02 02:37:40 | [diff] [blame] | 330 | // Returns whether FormatUrl() would strip a trailing slash from |url|, given a |
| 331 | // format flag including kFormatUrlOmitTrailingSlashOnBareHostname. |
| 332 | bool CanStripTrailingSlash(const GURL& url); |
| 333 | |
[email protected] | 7ec7c18 | 2009-08-04 02:33:58 | [diff] [blame] | 334 | // Strip the portions of |url| that aren't core to the network request. |
| 335 | // - user name / password |
| 336 | // - reference section |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 337 | NET_TEST GURL SimplifyUrlForRequest(const GURL& url); |
[email protected] | 7ec7c18 | 2009-08-04 02:33:58 | [diff] [blame] | 338 | |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 339 | NET_API void SetExplicitlyAllowedPorts(const std::string& allowed_ports); |
[email protected] | d95fa18 | 2009-09-09 17:01:16 | [diff] [blame] | 340 | |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 341 | class NET_API ScopedPortException { |
[email protected] | bfba365 | 2010-12-10 14:43:00 | [diff] [blame] | 342 | public: |
| 343 | ScopedPortException(int port); |
| 344 | ~ScopedPortException(); |
| 345 | |
| 346 | private: |
| 347 | int port_; |
| 348 | |
| 349 | DISALLOW_COPY_AND_ASSIGN(ScopedPortException); |
| 350 | }; |
| 351 | |
[email protected] | 32eaa33 | 2010-02-08 22:15:54 | [diff] [blame] | 352 | // Perform a simplistic test to see if IPv6 is supported by trying to create an |
| 353 | // IPv6 socket. |
| 354 | // TODO(jar): Make test more in-depth as needed. |
| 355 | bool IPv6Supported(); |
| 356 | |
[email protected] | 2f3bc65c | 2010-07-23 17:47:10 | [diff] [blame] | 357 | // Returns true if it can determine that only loopback addresses are configured. |
| 358 | // i.e. if only 127.0.0.1 and ::1 are routable. |
| 359 | bool HaveOnlyLoopbackAddresses(); |
| 360 | |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 361 | // IPAddressNumber is used to represent an IP address's numeric value as an |
| 362 | // array of bytes, from most significant to least significant. This is the |
| 363 | // network byte ordering. |
| 364 | // |
| 365 | // IPv4 addresses will have length 4, whereas IPv6 address will have length 16. |
| 366 | typedef std::vector<unsigned char> IPAddressNumber; |
| 367 | |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 368 | static const size_t kIPv4AddressSize = 4; |
| 369 | static const size_t kIPv6AddressSize = 16; |
| 370 | |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 371 | // Parses an IP address literal (either IPv4 or IPv6) to its numeric value. |
| 372 | // Returns true on success and fills |ip_number| with the numeric value. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 373 | NET_TEST bool ParseIPLiteralToNumber(const std::string& ip_literal, |
| 374 | IPAddressNumber* ip_number); |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 375 | |
| 376 | // Converts an IPv4 address to an IPv4-mapped IPv6 address. |
| 377 | // For example 192.168.0.1 would be converted to ::ffff:192.168.0.1. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 378 | NET_TEST IPAddressNumber ConvertIPv4NumberToIPv6Number( |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 379 | const IPAddressNumber& ipv4_number); |
| 380 | |
| 381 | // Parses an IP block specifier from CIDR notation to an |
| 382 | // (IP address, prefix length) pair. Returns true on success and fills |
| 383 | // |*ip_number| with the numeric value of the IP address and sets |
| 384 | // |*prefix_length_in_bits| with the length of the prefix. |
| 385 | // |
| 386 | // CIDR notation literals can use either IPv4 or IPv6 literals. Some examples: |
| 387 | // |
| 388 | // 10.10.3.1/20 |
| 389 | // a:b:c::/46 |
| 390 | // ::1/128 |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 391 | NET_API bool ParseCIDRBlock(const std::string& cidr_literal, |
| 392 | IPAddressNumber* ip_number, |
| 393 | size_t* prefix_length_in_bits); |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 394 | |
| 395 | // Compares an IP address to see if it falls within the specified IP block. |
| 396 | // Returns true if it does, false otherwise. |
| 397 | // |
| 398 | // The IP block is given by (|ip_prefix|, |prefix_length_in_bits|) -- any |
| 399 | // IP address whose |prefix_length_in_bits| most significant bits match |
| 400 | // |ip_prefix| will be matched. |
| 401 | // |
| 402 | // In cases when an IPv4 address is being compared to an IPv6 address prefix |
| 403 | // and vice versa, the IPv4 addresses will be converted to IPv4-mapped |
| 404 | // (IPv6) addresses. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 405 | NET_TEST bool IPNumberMatchesPrefix(const IPAddressNumber& ip_number, |
| 406 | const IPAddressNumber& ip_prefix, |
| 407 | size_t prefix_length_in_bits); |
[email protected] | 5439283 | 2010-06-08 23:25:04 | [diff] [blame] | 408 | |
[email protected] | 0d1f4c4 | 2011-05-05 15:27:50 | [diff] [blame] | 409 | // Makes a copy of |info|. The dynamically-allocated parts are copied as well. |
| 410 | // If |recursive| is true, chained entries via ai_next are copied too. |
| 411 | // The copy returned by this function should be freed using |
| 412 | // FreeCopyOfAddrinfo(), and NOT freeaddrinfo(). |
| 413 | struct addrinfo* CreateCopyOfAddrinfo(const struct addrinfo* info, |
| 414 | bool recursive); |
| 415 | |
| 416 | // Frees an addrinfo that was created by CreateCopyOfAddrinfo(). |
| 417 | void FreeCopyOfAddrinfo(struct addrinfo* info); |
| 418 | |
[email protected] | d1388f4 | 2010-06-16 03:14:42 | [diff] [blame] | 419 | // Returns the port field of the sockaddr in |info|. |
[email protected] | 93e7901 | 2011-01-26 04:13:59 | [diff] [blame] | 420 | const uint16* GetPortFieldFromAddrinfo(const struct addrinfo* info); |
| 421 | uint16* GetPortFieldFromAddrinfo(struct addrinfo* info); |
[email protected] | d1388f4 | 2010-06-16 03:14:42 | [diff] [blame] | 422 | |
| 423 | // Returns the value of |info's| port (in host byte ordering). |
[email protected] | fe89ea7 | 2011-05-12 02:02:40 | [diff] [blame] | 424 | uint16 GetPortFromAddrinfo(const struct addrinfo* info); |
[email protected] | d1388f4 | 2010-06-16 03:14:42 | [diff] [blame] | 425 | |
[email protected] | 93e7901 | 2011-01-26 04:13:59 | [diff] [blame] | 426 | // Same except for struct sockaddr. |
| 427 | const uint16* GetPortFieldFromSockaddr(const struct sockaddr* address, |
| 428 | socklen_t address_len); |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 429 | NET_TEST int GetPortFromSockaddr(const struct sockaddr* address, |
| 430 | socklen_t address_len); |
[email protected] | 93e7901 | 2011-01-26 04:13:59 | [diff] [blame] | 431 | |
[email protected] | fe89ea7 | 2011-05-12 02:02:40 | [diff] [blame] | 432 | // Sets every addrinfo in the linked list |head| as having a port field of |
| 433 | // |port|. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 434 | NET_TEST void SetPortForAllAddrinfos(struct addrinfo* head, uint16 port); |
[email protected] | fe89ea7 | 2011-05-12 02:02:40 | [diff] [blame] | 435 | |
[email protected] | 2fd33ee9 | 2011-03-25 22:30:21 | [diff] [blame] | 436 | // Returns true if |host| is one of the names (e.g. "localhost") or IP |
| 437 | // addresses (IPv4 127.0.0.0/8 or IPv6 ::1) that indicate a loopback. |
| 438 | // |
| 439 | // Note that this function does not check for IP addresses other than |
| 440 | // the above, although other IP addresses may point to the local |
| 441 | // machine. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 442 | NET_TEST bool IsLocalhost(const std::string& host); |
[email protected] | 2fd33ee9 | 2011-03-25 22:30:21 | [diff] [blame] | 443 | |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 444 | // struct that is used by GetNetworkList() to represent a network |
| 445 | // interface. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 446 | struct NET_API NetworkInterface { |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 447 | NetworkInterface(); |
| 448 | NetworkInterface(const std::string& name, const IPAddressNumber& address); |
| 449 | ~NetworkInterface(); |
| 450 | |
| 451 | std::string name; |
| 452 | IPAddressNumber address; |
| 453 | }; |
| 454 | |
[email protected] | 91ed478b | 2011-04-21 23:20:07 | [diff] [blame] | 455 | typedef std::vector<NetworkInterface> NetworkInterfaceList; |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 456 | |
| 457 | // Returns list of network interfaces except loopback interface. If an |
| 458 | // interface has more than one address, a separate entry is added to |
| 459 | // the list for each address. |
| 460 | // Can be called only on a thread that allows IO. |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame^] | 461 | NET_API bool GetNetworkList(NetworkInterfaceList* networks); |
[email protected] | 611cc90 | 2011-03-19 00:36:39 | [diff] [blame] | 462 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 463 | } // namespace net |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 464 | |
[email protected] | de294335 | 2009-10-22 23:06:12 | [diff] [blame] | 465 | #endif // NET_BASE_NET_UTIL_H_ |