| |
| INT NormalizeUrl( |
| |
LPCTSTR lpszUrl, |
|
| |
DWORD dwFlags, |
|
| |
LPTSTR lpszNormalized, |
|
| |
INT nMaxLength |
|
| ); |
| INT NormalizeUrl( |
| |
LPCTSTR lpszUrl, |
|
| |
DWORD dwFlags, |
|
| |
CString& strNormalized |
|
| ); |
The NormalizeUrl method parses and normalizes a URL
into a safe, navigable form.
Parameters
- lpszUrl
- A null-terminated string which specifies the URL to be normalized.
This parameter cannot be NULL or an empty string.
- dwFlags
- Specifies one or more bit flags that control how the URL is normalized.
A value of zero specifies that only standard normalization will be
performed, such as removing any leading and trailing whitespace and
ensuring the URL does not contain any illegal characters. Multiple
flags may be combined using a bitwise or operator.
| Value |
Description |
| URL_FLAG_DEFAULT_HTTPS |
When no URL scheme is provided and no scheme is otherwise
required, select https:// by default. If an explicit port
is present, port 443 selects https and any other port
selects http. Explicit input schemes are preserved. |
| URL_FLAG_REQUIRE_HTTP |
When no scheme is provided, force http://. If the
input already specifies a scheme, it is preserved. |
| URL_FLAG_REQUIRE_HTTPS |
When no scheme is provided, force https://. If the
input already specifies a scheme, it is preserved. |
| URL_FLAG_REJECT_LOCALHOST |
Reject URLs that specify localhost as the host name. |
| URL_FLAG_REJECT_IPV4 |
Reject URLs that specify a numeric IPv4 address (for example,
192.0.2.1) as the host. |
| URL_FLAG_REJECT_IPV6 |
Reject URLs that specify a bracketed IPv6 address (for example,
[2001:db8::1]) as the host. |
| URL_FLAG_ASCII_HOSTNAME |
Host names are checked to ensure that they only contain legal
ASCII characters. Unicode host names are converted to ASCII
(punycode) before validation, and the host name is normalized to
lowercase. |
| URL_FLAG_ESCAPE_PATH |
Percent-encode the path component as needed. Non-ASCII characters
are encoded as UTF-8 and then percent-encoded per byte. Valid %HH
sequences are preserved and upper-cased. The path is also normalized
by removing duplicate slashes and collapsing . and ..
segments of the path. |
| URL_FLAG_ESCAPE_QUERY |
Percent-encode the query and fragment tail as needed. The first
? is treated as the start of the tail and characters commonly
used in queries (such as &, =, +,
;, #) are permitted unescaped. Non-ASCII
characters are encoded as UTF-8 and then percent-encoded. |
| URL_FLAG_VALIDATE_UTF8 |
Applicable to projects using a multi-byte character set. Require
the URL to use UTF-8 encoding and fail if the method encounters an
invalid Unicode character. When this flag is not specified, and the
URL does not use UTF-8 encoding, the method attempts to normalize
the URL using the active code page. |
| URL_FLAG_REJECT_LOCAL_FILES |
Reject absolute local and UNC paths and existing file://
URLs. When specified, UNC paths are also rejected and unsafe file
extensions are not allowed. |
| URL_FLAG_REJECT_UNC_PATHS |
When local file normalization is permitted, reject UNC paths (for example,
\\server\share). Files paths which use a mapped drive to a
network share will be allowed. |
| URL_FLAG_ALLOW_UNSAFE_FILES |
When converting local/UNC paths to file:// URLs or
normalizing existing file:// URLs, permit any file extension.
If this option is not specified, only a common set of safe, renderable
file types are allowed. This includes HTML files, text files and common
image formats. |
- lpszNormalized
- A null-terminated string buffer which will contain the normalized
URL when the method returns. This parameter may be NULL, in which
case the method returns the number of characters required to store
the normalized URL. If a buffer is provided, it must be large enough
to contain the entire URL. An alternate version of this method
accepts a CString which will contain the normalized URL when
the method returns.
- nMaxLength
- An integer value which specifies the maximum number of characters
which can be copied into the string buffer, including the terminating
null character. If this parameter is zero, the method will return
the number of characters required to store the normalized URL and no
characters will be copied into the buffer.
Return Value
An integer which specifies the length of the normalized URL, not
including the terminating null character. A return value of zero indicates
the URL could not be normalized, either due to an invalid URL or the
output buffer not being large enough to store the complete URL. Call
the GetLastError method for more information about the cause
of the failure.
Remarks
The NormalizeUrl method parses and normalizes a
candidate URL without navigating. In most cases you do not need
to call this method directly because the Navigate method
already performs basic normalization internally before it passes
the URL to the browser. Use NormalizeUrl when you
want to enforce additional policy restrictions with the option
flags, or notify users of clear validation failures before
attempting navigation.
Normalization includes, depending on the flags you choose:
- Scheme defaults and enforcement. If the input omits a scheme,
you can default to HTTPS (
URL_FLAG_DEFAULT_HTTPS) or require
http:// or https:// explicitly using URL_FLAG_REQUIRE_HTTP or
URL_FLAG_REQUIRE_HTTPS. If the normalizer cannot accept the
scheme under your selected policy, the call fails. The same default
behavior is applied with the Navigate method.
- Hostname policy. You may reject localhost, numeric IPv4, or
bracketed IPv6 literals, or enforce ASCII hostnames (Unicode
hostnames are converted to Punycode and lower-cased). The host
parsing also ensures the input isn't a Windows path accidentally
pasted in place of a URL.
- Path and query normalization. Paths may be percent-encoded as
needed, duplicate slashes removed, and path traversal segments are
collapsed; queries and fragments may be percent-encoded while
preserving common query delimiters.
- Local file handling. Absolute local and UNC paths or existing
file:// URLs can be converted to a normalized file:// form unless
you explicitly reject them with
URL_FLAG_REJECT_LOCAL_FILES
(and optionally the URL_FLAG_REJECT_UNC_PATHS option).
If you intend to only permit web content, enable these reject flags.
At navigation time the library also tracks whether the target is a
local file.
- If your project uses the multi-byte character set instead of
Unicode, and
URL_FLAG_VALIDATE_UTF8 is specified, the method will
fail if the URL contains any invalid UTF-8 encoding. Without this
flag, if the method detects invalid UTF-8 characters, it will
attempt to validate the URL using the active code page. It is
recommended that applications use UTF-8 or UTF-16 Unicode whenever
possible.
This method validates and normalizes URLs but does not expand
redirects, verify server reachability, or fetch headers. Use it to block
undesired inputs before navigation (such as disallowing bare IPs or
local files), or to present a user-friendly, fully qualified URL.
Example
DWORD dwNormFlags =
URL_FLAG_DEFAULT_HTTPS |
URL_FLAG_ASCII_HOSTNAME |
URL_FLAG_ESCAPE_PATH |
URL_FLAG_ESCAPE_QUERY |
URL_FLAG_REJECT_LOCAL_FILES |
URL_FLAG_REJECT_UNC_PATHS;
DWORD dwError = NO_ERROR;
INT nLength = pWebView->NormalizeUrl(pszUserInput, dwNormFlags, NULL, 0);
if (nLength <= 0)
dwError = pWebView->GetLastError();
if (dwError == NO_ERROR)
{
LPTSTR pszNormalizedUrl = (LPTSTR)malloc((nLength + 1) * sizeof(TCHAR));
if (!pszNormalizedUrl)
return; // Failed; virtual memory exhausted
nLength = pWebView->NormalizeUrl(pszUserInput, dwNormFlags, pszNormalizedUrl, nLength + 1);
if (nLength <= 0)
{
dwError = pWebView->GetLastError();
}
else
{
if (!pWebView->Navigate(pszNormalizedUrl))
dwError = pWebView->GetLastError();
}
free(pszNormalizedUrl);
}
Requirements
Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib
Unicode: Implemented as Unicode and ANSI versions
See Also
|
|