Skip to content

Commit 5692f4b

Browse files
authored
Update HostResourceMapping.md
1 parent 7e84b2d commit 5692f4b

File tree

1 file changed

+79
-45
lines changed

1 file changed

+79
-45
lines changed

specs/HostResourceMapping.md

Lines changed: 79 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,61 @@ To access local files in WebView2, define a virtual host name to local folder ma
77
and then refer to local files under the folder with a normal http/https url with the virtual host name.
88

99
# Examples
10-
Suppose the app has index.html file in `asset` sub folder in the folder where its exe file is. The following code snippet demonstrates how to define a mapping
11-
for `appassets.webview2.microsoft.com` host name so that it can be accessed using normal http/https url like https://appassets.webview2.microsoft.com/index.html.
10+
Suppose the app has index.html file in `asset` subfolder located on disk under the same path as the app's executable file.
11+
The following code snippet demonstrates how to define a mapping for `appassets.example` host name so that it can be accessed
12+
using normal http/https url like https://appassets.example/index.html.
1213

1314
## Win32 C++
1415
```cpp
1516
webview2->SetVirtualHostNameToFolderMapping(
16-
L"appassets.webview2.microsoft.com", L"assets", COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY_CORS);
17-
webview2->Navigate(L"https://appassets.webview2.microsoft.com/index.html");
17+
L"appassets.example", L"assets", COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY_CORS);
18+
webview2->Navigate(L"https://appassets.example/index.html");
1819
```
1920
2021
## .Net
2122
```c#
2223
webView.CoreWebView2.SetVirtualHostNameToFolderMapping(
23-
"appassets.webview2.microsoft.com",
24+
"appassets.example",
2425
"assets", CoreWebView2HostResourceAccessKind.DenyCors);
25-
webView.Source = new Uri("https://appassets.webview2.microsoft.com/index.html");
26+
webView.Source = new Uri("https://appassets.example/index.html");
2627
```
2728

2829
## WinRT
29-
Suppose the app has index.html file in a `book1` sub folder of the app's LocalFolder folder. The following code snippet demonstrates how to define a mapping
30-
for `book1.appassets.webview2.microsoft.com` host name so that it can be accessed using normal http/https url like https://book1.appassets.webview2.microsoft.com/index.html.
30+
Suppose the app has index.html file in a `book1` subfolder under the app's LocalFolder folder. The following code snippet demonstrates how to define a mapping
31+
for `book1.example` host name so that it can be accessed using normal http/https url like https://book1.example/index.html.
3132
```c#
3233
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
33-
string book1_path = localFolder.Path + @"\book1";
34+
string book1Path = localFolder.Path + @"\book1";
3435
webView.CoreWebView2.SetVirtualHostNameToFolderMapping(
35-
new Windows.Networking.HostName("book1.appassets.webview2.microsoft.com"),
36-
book1_path, CoreWebView2HostResourceAccessKind.DenyCors);
37-
webView.Source = new Uri("https://book1.appassets.webview2.microsoft.com/index.html");
36+
new Windows.Networking.HostName("book1.example"),
37+
book1Path, CoreWebView2HostResourceAccessKind.DenyCors);
38+
webView.Source = new Uri("https://book1.example/index.html");
3839
```
3940

4041

4142
# Remarks
42-
Choose a virtual host name that will not be used by real sites.
43-
If you own a domain (like example.com), an option is to use a sub domain reserved for the app (like my-app.example.com).
44-
We have also reserved a special domain appassets.webview2.microsoft.com for this usage.
45-
43+
You should typically choose virtual host names that are never used by real sites.
44+
If you own a domain such as example.com, another option is to use a subdomain reserved for the app (like my-app.example.com).
45+
[RFC 6761](https://tools.ietf.org/html/rfc6761) has reserved several special-use domain
46+
names that are guaranteed to not be used by real sites (for example, .example, .test, and
47+
.invalid.)
48+
Apps should use distinct domain names when mapping folder from different sources that
49+
should be isolated from each other. For instance, the app might use app-file.example for
50+
files that ship as part of the app, and book1.example might be used for files containing
51+
books from a less trusted source that were previously downloaded and saved to the disk by
52+
the app.
4653
The host name used in the APIs is canonicalized using Chromium's host name parsing logic
4754
before being used internally.
48-
All host names that are canonicalized to the same string are considered identical.
49-
For example, `EXAMPLE.COM` and `example.com` are considered the same host name.
50-
An international host name and its puny coded host name are considered the same host name.
51-
There is no DSN resolution for host name and trailing '.' is not normalized as part of canonicalization.
52-
Therefore `example.com` and `example.com.` are treated as different host names. So are `virtual-host-name` and `virtual-host-name.example.com` when the DNS suffix of the machine is `example.com`.
53-
54-
Give only minimal cross origin access necessary to run the app. If there is no need to access local resources
55-
from other origins, use COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY.
55+
All host names that are canonicalized to the same string are considered identical.
56+
For example, `EXAMPLE.COM` and `example.com` are treated as the same host name.
57+
An international host name and its Punycode-encoded host name are considered the same host
58+
name. There is no DNS resolution for host name and the trailing '.' is not normalized as
59+
part of canonicalization.
60+
Therefore `example.com` and `example.com.` are treated as different host names. Similarly,
61+
`virtual-host-name` and `virtual-host-name.example.com` are treated as different host names
62+
even if the machine has a DNS suffix of `example.com`.
63+
Specify the minimal cross-origin access necessary to run the app. If there is not a need to
64+
access local resources from other origins, use COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY.
5665

5766
# API Notes
5867
See [API Details](#api-details) section below for API reference.
@@ -97,35 +106,60 @@ typedef enum COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND {
97106
98107
interface ICoreWebView2_2 : ICoreWebView2 {
99108
100-
/// Set a mapping between a virtual host name and host resources in a folder.
101-
/// After setting the mapping, the app can then use http or https urls with the specified hostName as host name
102-
/// of the urls to access files in the local folder specified by folderPath.
103-
/// This applies to top level document and iframe navigations as well as sub resource references from a document.
104-
/// This also applies to dedicated and shared worker scripts, but does not apply to service worker scripts.
105-
/// accessKind specifies the kind of access control to the host resources from other sites.
106-
/// Both absolute and relative path are supported for folderPath. Relative path is interpreted as relative
107-
/// to the folder where the exe of the app is in.
108-
/// For example, after calling AddVirtualHostNameToFolderMapping(L"appassets.webview2.microsoft.com", L"assets",
109-
/// COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY), navigating to https://appassets.webview2.microsoft.com/my-local-file.html
110-
/// will show content from my-local-file.html in the assets sub folder of the folder that has the app's exe file.
109+
/// Set a mapping between a virtual host name and a folder path to make available to web sites
110+
/// via that host name.
111+
///
112+
/// After setting the mapping, documents loaded in the WebView can use HTTP or HTTPS URLs at
113+
/// the specified host name specified by hostName to access files in the local folder specified
114+
/// by folderPath.
115+
///
116+
/// This mapping applies to both top-level document and iframe navigations as well as subresource
117+
/// references from a document. This also applies to dedicated and shared worker scripts but does
118+
/// not apply to service worker scripts.
119+
///
120+
/// accessKind specifies the level of access to resources under the virtual host from other sites.
121+
/// Both absolute and relative paths are supported for folderPath. Relative paths are interpreted
122+
/// as relative to the folder where the exe of the app is in.
123+
///
124+
/// For example, after calling
125+
/// ```
126+
/// SetVirtualHostNameToFolderMapping(
127+
/// L"appassets.example", L"assets",
128+
/// COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY);
129+
/// ```
130+
/// navigating to `https://appassets.example/my-local-file.html` will
131+
/// show content from my-local-file.html in the assets subfolder located on disk under the same
132+
/// path as the app's executable file.
111133
///
112-
/// Choose a virtual host name that will not be used by real sites.
113-
/// If you own a domain (like example.com), an option is to use a sub domain reserved for
134+
/// You should typically choose virtual host names that are never used by real sites.
135+
/// If you own a domain such as example.com, another option is to use a subdomain reserved for
114136
/// the app (like my-app.example.com).
115-
/// We have also reserved a special domain appassets.webview2.microsoft.com for this usage.
137+
///
138+
/// [RFC 6761](https://tools.ietf.org/html/rfc6761) has reserved several special-use domain
139+
/// names that are guaranteed to not be used by real sites (for example, .example, .test, and
140+
/// .invalid.)
141+
///
142+
/// Apps should use distinct domain names when mapping folder from different sources that
143+
/// should be isolated from each other. For instance, the app might use app-file.example for
144+
/// files that ship as part of the app, and book1.example might be used for files containing
145+
/// books from a less trusted source that were previously downloaded and saved to the disk by
146+
/// the app.
116147
///
117148
/// The host name used in the APIs is canonicalized using Chromium's host name parsing logic
118149
/// before being used internally.
150+
///
119151
/// All host names that are canonicalized to the same string are considered identical.
120-
/// For example, `EXAMPLE.COM` and `example.com` are considered the same host name.
121-
/// An international host name and its puny coded host name are considered the same host name.
122-
/// There is no DSN resolution for host name and trailing '.' is not normalized as part of canonicalization.
123-
/// Therefore `example.com` and `example.com.` are treated as different host names. So are
124-
/// `virtual-host-name` and `virtual-host-name.example.com` when the DNS suffix of the machine is `example.com`.
152+
/// For example, `EXAMPLE.COM` and `example.com` are treated as the same host name.
153+
/// An international host name and its Punycode-encoded host name are considered the same host
154+
/// name. There is no DNS resolution for host name and the trailing '.' is not normalized as
155+
/// part of canonicalization.
125156
///
126-
/// Give only minimal cross origin access necessary to run the app. If there is no need to
127-
/// access local resources from other origins, use COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY.
157+
/// Therefore `example.com` and `example.com.` are treated as different host names. Similarly,
158+
/// `virtual-host-name` and `virtual-host-name.example.com` are treated as different host names
159+
/// even if the machine has a DNS suffix of `example.com`.
128160
///
161+
/// Specify the minimal cross-origin access necessary to run the app. If there is not a need to
162+
/// access local resources from other origins, use COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND_DENY.
129163
HRESULT SetVirtualHostNameToFolderMapping(
130164
[in] LPCWSTR hostName,
131165
[in] LPCWSTR folderPath,

0 commit comments

Comments
 (0)