Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Problem/Bug]: WebResourceRequested not called from CSS with custom URI scheme #4362

Open
receiver1 opened this issue Feb 9, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@receiver1
Copy link

receiver1 commented Feb 9, 2024

What happened?

Hello! I'm developing Embedded Desktop Application using webview2 like GUI. I want statically link resources, so I registered a custom URI scheme and add WebResourceRequested handler. I ran into a problem that loading resources in CSS does not trigger this event.

My CSS:

@font-face {
  font-family: 'Consolas Bold';
  src: url('res://consolasbold.woff2');
  font-weight: bold;
  font-style: normal;
  font-display: swap;
}

My WebResourceRequested handler: (cut down version)

HRESULT STDMETHODCALLTYPE Invoke(
    ICoreWebView2 *sender, ICoreWebView2WebResourceRequestedEventArgs *args) {
  ICoreWebView2WebResourceRequest *request{};
  LPWSTR requestUrl{};
  args->get_Request(&request);
  request->get_Uri(&requestUrl);
  std::printf("%ws\n", requestUrl);
  return S_OK;
}

My scheme registration code:

const WCHAR *allowedOrigins[1] = {L"*"};
auto customSchemeRegistration =
    Microsoft::WRL::Make<CoreWebView2CustomSchemeRegistration>(L"res");
customSchemeRegistration->put_TreatAsSecure(TRUE);
customSchemeRegistration->SetAllowedOrigins(1, allowedOrigins);
customSchemeRegistration->put_HasAuthorityComponent(TRUE);
ICoreWebView2CustomSchemeRegistration *registrations[1] = {
    customSchemeRegistration.Get()};
options->SetCustomSchemeRegistrations(
    1,
    static_cast<ICoreWebView2CustomSchemeRegistration **>(registrations));

Importance

Blocking. My app's basic functions are not working due to this issue.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

No response

SDK Version

No response

Framework

Win32

Operating System

Windows 10

OS Version

No response

Repro steps

The behavior I expect is for the WebResourceRequested event to be raised when using a custom schema in CSS.
The current behavior outputs to me in the developer console: Failed to load resource: the server responded with a status of 401 (Unauthorized) and when refreshing the page GET res://consolasbold.woff2/ net::ERR_ABORTED 401 (Unauthorized)
Moreover, if you make the same request through the Fetch API in JavaScript, the resource is perfectly defined and obtained.
The only difference between these two requests that I found is the presence of "Origin: null" in the headers on the CSS side of the request

request made by CSS: (does not raise the WebResourceRequested event)
image
request executed JS: (raises the WebResourceRequested event)
image

Repros in Edge Browser

No

Regression

No, this never worked

Last working version (if regression)

No response

@receiver1 receiver1 added the bug Something isn't working label Feb 9, 2024
@ray007
Copy link

ray007 commented Feb 13, 2024

I think virtual host with http(s) scheme should work.
Custom protocols seem a bit broken for many things.

@omegakode
Copy link

So far my experience with custom schemes is that WebResourceRequested never fires, you get a not authorized error in the console. Only NavigationStarted/Ended fire.

@jameslund
Copy link

I ran into a this issue as well. To resolve it, I had to add a WebResourceResponseReceived handler (add_WebResourceResponseReceived). Simply returning S_OK from the handler and my WebResourceRequested started receiving calls for the 401 files.

@omegakode
Copy link

Thanks, i'll try it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants