blob: 148ab5db2d0bf1dd6cb123a2eb97347660fb2e8b [file] [log] [blame]
initial.commit586acc5fe2008-07-26 22:42:521// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#ifndef NET_BASE_NET_UTIL_H__
31#define NET_BASE_NET_UTIL_H__
32
[email protected]e537c352008-08-20 21:42:1733#include "build/build_config.h"
34
initial.commit586acc5fe2008-07-26 22:42:5235#include <string>
[email protected]e537c352008-08-20 21:42:1736
37#ifdef OS_WIN
initial.commit586acc5fe2008-07-26 22:42:5238#include <windows.h>
[email protected]e537c352008-08-20 21:42:1739#endif
initial.commit586acc5fe2008-07-26 22:42:5240
41#include "base/basictypes.h"
42#include "googleurl/src/url_canon.h"
43#include "googleurl/src/url_parse.h"
44
45class GURL;
46
[email protected]8ac1a752008-07-31 19:40:3747namespace net {
initial.commit586acc5fe2008-07-26 22:42:5248
49// Given the full path to a file name, creates a file: URL. The returned URL
50// may not be valid if the input is malformed.
51GURL FilePathToFileURL(const std::wstring& file_path);
52
53// Converts a file: URL back to a filename that can be passed to the OS. The
54// file URL must be well-formed (GURL::is_valid() must return true); we don't
55// handle degenerate cases here. Returns true on success, false if it isn't a
56// valid file URL. On failure, *file_path will be empty.
57bool FileURLToFilePath(const GURL& url, std::wstring* file_path);
58
59// Return the value of the HTTP response header with name 'name'. 'headers'
60// should be in the format that URLRequest::GetResponseHeaders() returns.
61// Returns the empty string if the header is not found.
62std::wstring GetSpecificHeader(const std::wstring& headers,
63 const std::wstring& name);
64std::string GetSpecificHeader(const std::string& headers,
65 const std::string& name);
66
67// Return the value of the HTTP response header field's parameter named
68// 'param_name'. Returns the empty string if the parameter is not found or is
69// improperly formatted.
70std::wstring GetHeaderParamValue(const std::wstring& field,
71 const std::wstring& param_name);
72std::string GetHeaderParamValue(const std::string& field,
73 const std::string& param_name);
74
75// Return the filename extracted from Content-Disposition header. Only two
76// formats are supported: a. %-escaped UTF-8 b. RFC 2047.
77//
78// A non-ASCII param value is just returned as it is (assuming a NativeMB
79// encoding). When a param value is ASCII, but is not in one of two forms
80// supported, it is returned as it is unless it's pretty close to two supported
81// formats but not well-formed. In that case, an empty string is returned.
82//
83// In any case, a caller must check for the empty return value and resort to
84// another means to get a filename (e.g. url).
85//
86// This function does not do any escaping and callers are responsible for
87// escaping 'unsafe' characters (e.g. (back)slash, colon) as they see fit.
88//
89// TODO(jungshik): revisit this issue. At the moment, the only caller
90// net_util::GetSuggestedFilename and it calls ReplaceIllegalCharacters. The
91// other caller is a unit test. Need to figure out expose this function only to
92// net_util_unittest.
93//
94std::wstring GetFileNameFromCD(const std::string& header);
95
96// Converts the given host name to unicode characters, APPENDING them to the
97// the given output string. This can be called for any host name, if the
98// input is not IDN or is invalid in some way, we'll just append the ASCII
99// source to the output so it is still usable.
100//
101// The input should be the canonicalized ASCII host name from GURL. This
102// function does NOT accept UTF-8! Its length must also be given (this is
103// designed to work on the substring of the host out of a URL spec).
104//
105// |languages| is a comma separated list of ISO 639 language codes. It
106// is used to determine whether a hostname is 'comprehensible' to a user
107// who understands languages listed. |host| will be converted to a
108// human-readable form (Unicode) ONLY when each component of |host| is
109// regarded as 'comprehensible'. Scipt-mixing is not allowed except that
110// Latin letters in the ASCII range can be mixed with a limited set of
111// script-language pairs (currently Han, Kana and Hangul for zh,ja and ko).
112// When |languages| is empty, even that mixing is not allowed.
113void IDNToUnicode(const char* host,
114 int host_len,
115 const std::wstring& languages,
116 std::wstring* out);
117
118// Canonicalizes |host| and returns it. If |is_ip_address| is non-NULL, sets it
119// to true if |host| is an IP address.
[email protected]e537c352008-08-20 21:42:17120std::string CanonicalizeHost(const std::string& host, bool* is_ip_address);
121std::string CanonicalizeHost(const std::wstring& host, bool* is_ip_address);
initial.commit586acc5fe2008-07-26 22:42:52122
123// Call these functions to get the html for a directory listing.
124// They will pass non-7bit-ascii characters unescaped, allowing
125// the browser to interpret the encoding (utf8, etc).
126std::string GetDirectoryListingHeader(const std::string& title);
[email protected]e537c352008-08-20 21:42:17127#ifdef OS_WIN
initial.commit586acc5fe2008-07-26 22:42:52128std::string GetDirectoryListingEntry(const std::string& name, DWORD attrib,
129 int64 size, const FILETIME* modified);
[email protected]e537c352008-08-20 21:42:17130#endif
initial.commit586acc5fe2008-07-26 22:42:52131
132// If text starts with "www." it is removed, otherwise text is returned
133// unmodified.
134std::wstring StripWWW(const std::wstring& text);
135
136// Gets the filename from the raw Content-Disposition header (as read from the
137// network). Otherwise uses the last path component name or hostname from
138// |url|. Note: it's possible for the suggested filename to be empty (e.g.,
139// file:/// or view-cache:).
140std::wstring GetSuggestedFilename(const GURL& url,
141 const std::string& content_disposition,
142 const std::wstring& default_name);
143
144// DEPRECATED: Please use the above version of this method.
145std::wstring GetSuggestedFilename(const GURL& url,
146 const std::wstring& content_disposition,
147 const std::wstring& default_name);
148
149// Checks the given port against a list of ports which are restricted by
150// default. Returns true if the port is allowed, false if it is restricted.
151bool IsPortAllowedByDefault(int port);
152
153// Checks the given port against a list of ports which are restricted by the
154// FTP protocol. Returns true if the port is allowed, false if it is
155// restricted.
156bool IsPortAllowedByFtp(int port);
157
[email protected]8ac1a752008-07-31 19:40:37158} // namespace net
initial.commit586acc5fe2008-07-26 22:42:52159
160#endif // NET_BASE_NET_UTIL_H__