diff options
Diffstat (limited to 'net/base/net_util.h')
-rw-r--r-- | net/base/net_util.h | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/net/base/net_util.h b/net/base/net_util.h new file mode 100644 index 0000000..d0955a1 --- /dev/null +++ b/net/base/net_util.h @@ -0,0 +1,153 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef NET_BASE_NET_UTIL_H__ +#define NET_BASE_NET_UTIL_H__ + +#include <string> +#include <windows.h> + +#include "base/basictypes.h" +#include "googleurl/src/url_canon.h" +#include "googleurl/src/url_parse.h" + +class GURL; + +namespace net_util { + +// Given the full path to a file name, creates a file: URL. The returned URL +// may not be valid if the input is malformed. +GURL FilePathToFileURL(const std::wstring& file_path); + +// Converts a file: URL back to a filename that can be passed to the OS. The +// file URL must be well-formed (GURL::is_valid() must return true); we don't +// handle degenerate cases here. Returns true on success, false if it isn't a +// valid file URL. On failure, *file_path will be empty. +bool FileURLToFilePath(const GURL& url, std::wstring* file_path); + +// Return the value of the HTTP response header with name 'name'. 'headers' +// should be in the format that URLRequest::GetResponseHeaders() returns. +// Returns the empty string if the header is not found. +std::wstring GetSpecificHeader(const std::wstring& headers, + const std::wstring& name); +std::string GetSpecificHeader(const std::string& headers, + const std::string& name); + +// Return the value of the HTTP response header field's parameter named +// 'param_name'. Returns the empty string if the parameter is not found or is +// improperly formatted. +std::wstring GetHeaderParamValue(const std::wstring& field, + const std::wstring& param_name); +std::string GetHeaderParamValue(const std::string& field, + const std::string& param_name); + +// Return the filename extracted from Content-Disposition header. Only two +// formats are supported: a. %-escaped UTF-8 b. RFC 2047. +// +// A non-ASCII param value is just returned as it is (assuming a NativeMB +// encoding). When a param value is ASCII, but is not in one of two forms +// supported, it is returned as it is unless it's pretty close to two supported +// formats but not well-formed. In that case, an empty string is returned. +// +// In any case, a caller must check for the empty return value and resort to +// another means to get a filename (e.g. url). +// +// This function does not do any escaping and callers are responsible for +// escaping 'unsafe' characters (e.g. (back)slash, colon) as they see fit. +// +// TODO(jungshik): revisit this issue. At the moment, the only caller +// net_util::GetSuggestedFilename and it calls ReplaceIllegalCharacters. The +// other caller is a unit test. Need to figure out expose this function only to +// net_util_unittest. +// +std::wstring GetFileNameFromCD(const std::string& header); + +// Converts the given host name to unicode characters, APPENDING them to the +// the given output string. This can be called for any host name, if the +// input is not IDN or is invalid in some way, we'll just append the ASCII +// source to the output so it is still usable. +// +// The input should be the canonicalized ASCII host name from GURL. This +// function does NOT accept UTF-8! Its length must also be given (this is +// designed to work on the substring of the host out of a URL spec). +// +// |languages| is a comma separated list of ISO 639 language codes. It +// is used to determine whether a hostname is 'comprehensible' to a user +// who understands languages listed. |host| will be converted to a +// human-readable form (Unicode) ONLY when each component of |host| is +// regarded as 'comprehensible'. Scipt-mixing is not allowed except that +// Latin letters in the ASCII range can be mixed with a limited set of +// script-language pairs (currently Han, Kana and Hangul for zh,ja and ko). +// When |languages| is empty, even that mixing is not allowed. +void IDNToUnicode(const char* host, + int host_len, + const std::wstring& languages, + std::wstring* out); + +// Canonicalizes |host| and returns it. If |is_ip_address| is non-NULL, sets it +// to true if |host| is an IP address. +template <typename str> +std::string CanonicalizeHost(const str& host, bool* is_ip_address); + +// Call these functions to get the html for a directory listing. +// They will pass non-7bit-ascii characters unescaped, allowing +// the browser to interpret the encoding (utf8, etc). +std::string GetDirectoryListingHeader(const std::string& title); +std::string GetDirectoryListingEntry(const std::string& name, DWORD attrib, + int64 size, const FILETIME* modified); + +// If text starts with "www." it is removed, otherwise text is returned +// unmodified. +std::wstring StripWWW(const std::wstring& text); + +// Gets the filename from the raw Content-Disposition header (as read from the +// network). Otherwise uses the last path component name or hostname from +// |url|. Note: it's possible for the suggested filename to be empty (e.g., +// file:/// or view-cache:). +std::wstring GetSuggestedFilename(const GURL& url, + const std::string& content_disposition, + const std::wstring& default_name); + +// DEPRECATED: Please use the above version of this method. +std::wstring GetSuggestedFilename(const GURL& url, + const std::wstring& content_disposition, + const std::wstring& default_name); + +// Checks the given port against a list of ports which are restricted by +// default. Returns true if the port is allowed, false if it is restricted. +bool IsPortAllowedByDefault(int port); + +// Checks the given port against a list of ports which are restricted by the +// FTP protocol. Returns true if the port is allowed, false if it is +// restricted. +bool IsPortAllowedByFtp(int port); + +} // namespace net_util + +#endif // NET_BASE_NET_UTIL_H__ |