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