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

[Bug]: WebView2 WebResourceRequested does not fired for WebSockets #4303

Open
t-marek opened this issue Jan 15, 2024 · 5 comments
Open

[Bug]: WebView2 WebResourceRequested does not fired for WebSockets #4303

t-marek opened this issue Jan 15, 2024 · 5 comments
Assignees
Labels
bug Something isn't working tracked We are tracking this work internally.

Comments

@t-marek
Copy link

t-marek commented Jan 15, 2024

What happened?

Hi,

I'm using WebView2 in custom WFP application. I need to add custom headers into requests so I use WebResourceRquested event. Here is my code:

`using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;

namespace WebViewAddingHeaders;
internal class MyWebBrowser: WebView2
{
public MyWebBrowser()
{
CoreWebView2InitializationCompleted += OnInitializationCompleted;
}

private void OnInitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e)
{
	CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);
	CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
}

private void CoreWebView2_WebResourceRequested(object? sender, CoreWebView2WebResourceRequestedEventArgs e)
{
	e.Request.Headers.SetHeader("MY_TESTING_HEADER", "Testing header value");
}

}`

But I noticed that this event is not fired for web sockets. I try to access python + streamlit application. There are sent multiple requests. For "normal" requests the header is present.
image
But it is not present for web sockets
image

I tried to filter only WebSockets in AddWebResourceRequestedFilter. I tried to enable custom scheme for ws. But nothing helped.

Is it possible to catch and enhance all requests, including web sockets?

Importance

Important. My app's user experience is significantly compromised.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

1.0.2210.55

SDK Version

No response

Framework

WPF

Operating System

Windows 11

OS Version

No response

Repro steps

In description.

Repros in Edge Browser

No

Regression

No, this never worked

Last working version (if regression)

No response

AB#48649859

@t-marek t-marek added the bug Something isn't working label Jan 15, 2024
@t-marek
Copy link
Author

t-marek commented Jan 22, 2024

@yildirimcagri-msft Any update about this?

@yildirimcagri-msft yildirimcagri-msft added the tracked We are tracking this work internally. label Jan 24, 2024
@yildirimcagri-msft
Copy link
Member

Thanks for reporting this, we added this to our backlog. This might take a while to test as we currently don't have a WebSocket backend to test this scenario for. For more information, I have some questions for you:

  • Did you confirm via debugger that the event is never firing for WS requests or is it possible that it is firing, but the header set via the event is not being effective?
  • Do you see the WS requests in DevTools?
  • For a potential workaround, does the CDP Fetch API work? You can follow this guide to interact with WebView2 via CDP session.

@haowxums
Copy link

I would like to add my case here that creating an instance of WebSocket did not send any messages to the socker server.

@t-marek
Copy link
Author

t-marek commented Jan 25, 2024

Thanks for reporting this, we added this to our backlog. This might take a while to test as we currently don't have a WebSocket backend to test this scenario for. For more information, I have some questions for you:

  • Did you confirm via debugger that the event is never firing for WS requests or is it possible that it is firing, but the header set via the event is not being effective?
  • Do you see the WS requests in DevTools?
  • For a potential workaround, does the CDP Fetch API work? You can follow this guide to interact with WebView2 via CDP session.

Hi @yildirimcagri-msft ,

Thank you for your response. Regarding your questions:

  1. This event is not firing even when debugging. Breakpoint in CoreWebView2_WebResourceRequested is not hit for the WS request.
  2. I see the WS requests in Dev tools, but my header is also not set
    image
  3. Thank you for the workaround, I will try

Regards
Tom

@t-marek t-marek closed this as completed Jan 26, 2024
@t-marek t-marek reopened this Jan 26, 2024
@t-marek
Copy link
Author

t-marek commented Jan 29, 2024

Hi @yildirimcagri-msft ,

I tried suggested workaround but without success.

At first I used events DevToolsProtocolHelper.Fetch.RequestPaused and DevToolsProtocolHelper.Network.RequestWillBeSent. But non of them is fired for web sockets.

Then I tried Network.WebSocketCreated, but there is no access to Request.

Network.WebSocketWillSendHandshakeRequest has access to request. Class Network.Headers is "empty" with no properties or methods. And the documentation is really poor. I faced to this issue that is still open #2488. I tried workaround from last post but it doesn't work in this event. I'm able to read the headers but I haven't found a way, how to set them. Despite documentation this seems to be fired after the request is sent. At first I see request in Chrome Dev tools and then breakpoint in code is hit.

I wanted also try method Network.SetExtraHTTPHeadersAsync(). I was able to add "extra" header but it was again present only in non-web socket requests.

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

No branches or pull requests

3 participants