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