Skip to content

Start adding the ability to intercept all requests #28876

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mattleibow
Copy link
Member

@mattleibow mattleibow commented Apr 9, 2025

Description of Change

Intercepting requests before they leave the browser will allow for the addition of headers as well as cancellation of the requests.

The API is only really fully shapeable once all the platforms are complete as each platform is really different, but the idea I have right now is a mix of all the platforms.

// NOTE: all names are random to allow for faster coding
// as we already know I can't come up with good names
// so hopefully you all can!

// This event will appear on all 3 web view controls
class WebView {
    public event EventHandler<AboutToSendRequestEventArgs> AboutToSendRequest;
}

// Cross-platform event args in order to do basic operations
class AboutToSendRequestEventArgs : EventArgs {
    // access to the platiorm args if needed
    public AboutToSendRequestEventArgsPlatformEventArgs PlatformArgs { get; }

    // some useful properties
    public Uri RequestUrl { get; }
    public IDictionary<string, string> Headers { get; } // maybe this is a wrapper type around the Request

    // the ability to cancel a request
    public bool Cancel { get; set; }

    // the ability to set a response instead of the default response
    public void SetResponse(int statusCode, IDictionary<string, string> headers, Stream contentStream);
    // could also be some custom type with GetStreamAsync() methods
    public void SetResponse(IResponseOfSomeSort respose);

    // maybe
    public IRequestOfSomeSort Request { get; }

    // or maybe some helpers
    public void SetHeaderValue(string header, string value);
}

// Platform-specific event args that allows direct access to the event that is happening
class AboutToSendRequestEventArgsPlatformEventArgs {
#if WINDOWS
    public WebView2 Sender { get; }
    public WebView2WebResourceRequestEventArgs WebResourceRequestEventArgs { get; }
#elif ...
#endif
}

Usage would be something like:

// 1. providing custom response
webView.RequestHappening += (sender, e) => {
    if (e.Request.Url == "https://myapp.com/some/endpoint?with=query&string=values") {
      e.Response = GetResponse();
    }
};

// 2. adding headers
webView.RequestHappening += (sender, e) => {
    if (e.Request.Url == "https://myapp.com/some/endpoint?with=query&string=values") {
      e.Request.Headers.Add("Header", "Value");
    }
};

// 3. cancelling requests
webView.RequestHappening += (sender, e) => {
    if (e.Request.Url == "https://myapp.com/some/endpoint?with=query&string=values") {
      e.Cancel = true;
    }
};

Tasks

  • WebView
    • Android
    • iOS/macOS
    • Windows
  • HybridWebView
    • Android
    • iOS/macOS
    • Windows
  • BlazorWebView
    • Android
    • iOS/macOS
    • Windows

Issues Fixed

Fixes #11382

@Copilot Copilot bot review requested due to automatic review settings April 9, 2025 00:00
@mattleibow mattleibow requested a review from a team as a code owner April 9, 2025 00:00
@mattleibow mattleibow requested review from rmarinho and PureWeen April 9, 2025 00:00
@mattleibow mattleibow marked this pull request as draft April 9, 2025 00:00
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.

Files not reviewed (1)
  • src/Controls/tests/DeviceTests/Resources/Raw/HybridTestRoot/index.html: Language not supported
Comments suppressed due to low confidence (2)

src/Core/src/Handlers/HybridWebView/HybridWebViewHandler.Windows.cs:241

  • Switching to a universal filter '*' may intercept more requests than intended, including external resources. Consider verifying that this change does not introduce performance impacts or unintended side effects.
webView.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All);

src/Controls/samples/Controls.Sample/Pages/Controls/HybridWebViewPage.xaml.cs:22

  • [nitpick] The string literal 'badthing' is used directly in the condition; consider extracting it into a named constant to improve readability and ensure consistency.
if (e.PlatformArgs.WebResourceRequestedEventArgs.Request.Uri.Contains("badthing", StringComparison.InvariantCultureIgnoreCase))

@mattleibow mattleibow added this to the .NET 10.0-preview4 milestone Apr 9, 2025
@mattleibow
Copy link
Member Author

Maybe if we land this, we won't need a "proxy" endpoint as all endpoints will become proxy-able: #25867

Intercepting requests before they leave the browser will allow for the
addition of headers as well as cancellation of the requests.

Fixes #11382
@mattleibow mattleibow force-pushed the dev/intercept-requests branch from 979e54c to 77b714c Compare April 14, 2025 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Intercepting requests from BlazorWebView
2 participants