OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "base/basictypes.h" | 21 #include "base/basictypes.h" |
22 #include "base/strings/string16.h" | 22 #include "base/strings/string16.h" |
23 #include "net/base/address_family.h" | 23 #include "net/base/address_family.h" |
24 #include "net/base/escape.h" | 24 #include "net/base/escape.h" |
25 #include "net/base/net_export.h" | 25 #include "net/base/net_export.h" |
26 #include "net/base/net_log.h" | 26 #include "net/base/net_log.h" |
27 | 27 |
28 class GURL; | 28 class GURL; |
29 | 29 |
30 namespace base { | 30 namespace base { |
| 31 class FilePath; |
31 class Time; | 32 class Time; |
32 } | 33 } |
33 | 34 |
34 namespace url_canon { | 35 namespace url_canon { |
35 struct CanonHostInfo; | 36 struct CanonHostInfo; |
36 } | 37 } |
37 | 38 |
38 namespace url_parse { | 39 namespace url_parse { |
39 struct Parsed; | 40 struct Parsed; |
40 } | 41 } |
(...skipping 27 matching lines...) Expand all Loading... |
68 // Omits the path if it is just a slash and there is no query or ref. This is | 69 // Omits the path if it is just a slash and there is no query or ref. This is |
69 // meaningful for non-file "standard" URLs. | 70 // meaningful for non-file "standard" URLs. |
70 NET_EXPORT extern const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname; | 71 NET_EXPORT extern const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname; |
71 | 72 |
72 // Convenience for omitting all unecessary types. | 73 // Convenience for omitting all unecessary types. |
73 NET_EXPORT extern const FormatUrlType kFormatUrlOmitAll; | 74 NET_EXPORT extern const FormatUrlType kFormatUrlOmitAll; |
74 | 75 |
75 // Returns the number of explicitly allowed ports; for testing. | 76 // Returns the number of explicitly allowed ports; for testing. |
76 NET_EXPORT_PRIVATE extern size_t GetCountOfExplicitlyAllowedPorts(); | 77 NET_EXPORT_PRIVATE extern size_t GetCountOfExplicitlyAllowedPorts(); |
77 | 78 |
| 79 // Given the full path to a file name, creates a file: URL. The returned URL |
| 80 // may not be valid if the input is malformed. |
| 81 NET_EXPORT GURL FilePathToFileURL(const base::FilePath& path); |
| 82 |
| 83 // Converts a file: URL back to a filename that can be passed to the OS. The |
| 84 // file URL must be well-formed (GURL::is_valid() must return true); we don't |
| 85 // handle degenerate cases here. Returns true on success, false if it isn't a |
| 86 // valid file URL. On failure, *file_path will be empty. |
| 87 NET_EXPORT bool FileURLToFilePath(const GURL& url, base::FilePath* file_path); |
| 88 |
78 // Splits an input of the form <host>[":"<port>] into its consitituent parts. | 89 // Splits an input of the form <host>[":"<port>] into its consitituent parts. |
79 // Saves the result into |*host| and |*port|. If the input did not have | 90 // Saves the result into |*host| and |*port|. If the input did not have |
80 // the optional port, sets |*port| to -1. | 91 // the optional port, sets |*port| to -1. |
81 // Returns true if the parsing was successful, false otherwise. | 92 // Returns true if the parsing was successful, false otherwise. |
82 // The returned host is NOT canonicalized, and may be invalid. If <host> is | 93 // The returned host is NOT canonicalized, and may be invalid. If <host> is |
83 // an IPv6 literal address, the returned host includes the square brackets. | 94 // an IPv6 literal address, the returned host includes the square brackets. |
84 NET_EXPORT bool ParseHostAndPort( | 95 NET_EXPORT bool ParseHostAndPort( |
85 std::string::const_iterator host_and_port_begin, | 96 std::string::const_iterator host_and_port_begin, |
86 std::string::const_iterator host_and_port_end, | 97 std::string::const_iterator host_and_port_end, |
87 std::string* host, | 98 std::string* host, |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 bool is_dir, int64 size, | 248 bool is_dir, int64 size, |
238 base::Time modified); | 249 base::Time modified); |
239 | 250 |
240 // If text starts with "www." it is removed, otherwise text is returned | 251 // If text starts with "www." it is removed, otherwise text is returned |
241 // unmodified. | 252 // unmodified. |
242 NET_EXPORT base::string16 StripWWW(const base::string16& text); | 253 NET_EXPORT base::string16 StripWWW(const base::string16& text); |
243 | 254 |
244 // Runs |url|'s host through StripWWW(). |url| must be valid. | 255 // Runs |url|'s host through StripWWW(). |url| must be valid. |
245 NET_EXPORT base::string16 StripWWWFromHost(const GURL& url); | 256 NET_EXPORT base::string16 StripWWWFromHost(const GURL& url); |
246 | 257 |
| 258 // Generates a filename using the first successful method from the following (in |
| 259 // order): |
| 260 // |
| 261 // 1) The raw Content-Disposition header in |content_disposition| as read from |
| 262 // the network. |referrer_charset| is used to decode non-ASCII strings. |
| 263 // 2) |suggested_name| if specified. |suggested_name| is assumed to be in |
| 264 // UTF-8. |
| 265 // 3) The filename extracted from the |url|. |referrer_charset| will be used to |
| 266 // interpret the URL if there are non-ascii characters. |
| 267 // 4) |default_name|. If non-empty, |default_name| is assumed to be a filename |
| 268 // and shouldn't contain a path. |default_name| is not subject to validation |
| 269 // or sanitization, and therefore shouldn't be a user supplied string. |
| 270 // 5) The hostname portion from the |url| |
| 271 // |
| 272 // Then, leading and trailing '.'s will be removed. On Windows, trailing spaces |
| 273 // are also removed. The string "download" is the final fallback if no filename |
| 274 // is found or the filename is empty. |
| 275 // |
| 276 // Any illegal characters in the filename will be replaced by '-'. If the |
| 277 // filename doesn't contain an extension, and a |mime_type| is specified, the |
| 278 // preferred extension for the |mime_type| will be appended to the filename. |
| 279 // The resulting filename is then checked against a list of reserved names on |
| 280 // Windows. If the name is reserved, an underscore will be prepended to the |
| 281 // filename. |
| 282 // |
| 283 // Note: |mime_type| should only be specified if this function is called from a |
| 284 // thread that allows IO. |
| 285 NET_EXPORT base::string16 GetSuggestedFilename( |
| 286 const GURL& url, |
| 287 const std::string& content_disposition, |
| 288 const std::string& referrer_charset, |
| 289 const std::string& suggested_name, |
| 290 const std::string& mime_type, |
| 291 const std::string& default_name); |
| 292 |
| 293 // Similar to GetSuggestedFilename(), but returns a FilePath. |
| 294 NET_EXPORT base::FilePath GenerateFileName( |
| 295 const GURL& url, |
| 296 const std::string& content_disposition, |
| 297 const std::string& referrer_charset, |
| 298 const std::string& suggested_name, |
| 299 const std::string& mime_type, |
| 300 const std::string& default_name); |
| 301 |
| 302 // Valid components: |
| 303 // * are not empty |
| 304 // * are not Windows reserved names (CON, NUL.zip, etc.) |
| 305 // * do not have trailing separators |
| 306 // * do not equal kCurrentDirectory |
| 307 // * do not reference the parent directory |
| 308 // * do not contain illegal characters |
| 309 // * do not end with Windows shell-integrated extensions (even on posix) |
| 310 // * do not begin with '.' (which would hide them in most file managers) |
| 311 // * do not end with ' ' or '.' |
| 312 NET_EXPORT bool IsSafePortablePathComponent(const base::FilePath& component); |
| 313 |
| 314 // Basenames of valid relative paths are IsSafePortableBasename(), and internal |
| 315 // path components of valid relative paths are valid path components as |
| 316 // described above IsSafePortableBasename(). Valid relative paths are not |
| 317 // absolute paths. |
| 318 NET_EXPORT bool IsSafePortableRelativePath(const base::FilePath& path); |
| 319 |
| 320 // Ensures that the filename and extension is safe to use in the filesystem. |
| 321 // |
| 322 // Assumes that |file_path| already contains a valid path or file name. On |
| 323 // Windows if the extension causes the file to have an unsafe interaction with |
| 324 // the shell (see net_util::IsShellIntegratedExtension()), then it will be |
| 325 // replaced by the string 'download'. If |file_path| doesn't contain an |
| 326 // extension or |ignore_extension| is true then the preferred extension, if one |
| 327 // exists, for |mime_type| will be used as the extension. |
| 328 // |
| 329 // On Windows, the filename will be checked against a set of reserved names, and |
| 330 // if so, an underscore will be prepended to the name. |
| 331 // |
| 332 // |file_name| can either be just the file name or it can be a full path to a |
| 333 // file. |
| 334 // |
| 335 // Note: |mime_type| should only be non-empty if this function is called from a |
| 336 // thread that allows IO. |
| 337 NET_EXPORT void GenerateSafeFileName(const std::string& mime_type, |
| 338 bool ignore_extension, |
| 339 base::FilePath* file_path); |
| 340 |
247 // Checks |port| against a list of ports which are restricted by default. | 341 // Checks |port| against a list of ports which are restricted by default. |
248 // Returns true if |port| is allowed, false if it is restricted. | 342 // Returns true if |port| is allowed, false if it is restricted. |
249 NET_EXPORT bool IsPortAllowedByDefault(int port); | 343 NET_EXPORT bool IsPortAllowedByDefault(int port); |
250 | 344 |
251 // Checks |port| against a list of ports which are restricted by the FTP | 345 // Checks |port| against a list of ports which are restricted by the FTP |
252 // protocol. Returns true if |port| is allowed, false if it is restricted. | 346 // protocol. Returns true if |port| is allowed, false if it is restricted. |
253 NET_EXPORT_PRIVATE bool IsPortAllowedByFtp(int port); | 347 NET_EXPORT_PRIVATE bool IsPortAllowedByFtp(int port); |
254 | 348 |
255 // Check if banned |port| has been overriden by an entry in | 349 // Check if banned |port| has been overriden by an entry in |
256 // |explicitly_allowed_ports_|. | 350 // |explicitly_allowed_ports_|. |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
507 DSCP_CS5 = 40, // Video | 601 DSCP_CS5 = 40, // Video |
508 DSCP_EF = 46, // Voice | 602 DSCP_EF = 46, // Voice |
509 DSCP_CS6 = 48, // Voice | 603 DSCP_CS6 = 48, // Voice |
510 DSCP_CS7 = 56, // Control messages | 604 DSCP_CS7 = 56, // Control messages |
511 DSCP_LAST = DSCP_CS7 | 605 DSCP_LAST = DSCP_CS7 |
512 }; | 606 }; |
513 | 607 |
514 } // namespace net | 608 } // namespace net |
515 | 609 |
516 #endif // NET_BASE_NET_UTIL_H_ | 610 #endif // NET_BASE_NET_UTIL_H_ |
OLD | NEW |