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