MicrosoftEdge / MicrosoftEdge/WebView2Feedback

Issue handing Webview2 a custom Response in WebResourceRequested

Open
#2,789 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug tracked
Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

Description
In a WebResourceRequested event I am taking the Request and using a HTTPClient to get the response. I then hand the response to the CoreWebView2WebResourceRequestedEventArg Response. I am doing this to add a Content-Disposition with the value attachment to the response so that Webview2 will then start downloading the file.

The result downloaded is an empty text file.

Version
SDK: 1.0.1340 pre release
Runtime: 105.0.1343.33
Framework: WPF
OS: Win10

Repro Steps

private async void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
{
	CoreWebView2Deferral deferral = e.GetDeferral();

	IHttpClientFactory factory = IPSurferHTTPClientFactory.HTTPClientFactory;
	var client = factory.CreateClient("HTTPRequester");
	using HttpRequestMessage httpreq = ConvertRequest(e.Request);
	using var response = await client.SendAsync(httpreq);
	System.Net.HttpStatusCode sc = response.StatusCode;
	response.EnsureSuccessStatusCode();
	var totalBytes = response.Content.Headers.ContentLength;
	//**************************************************
	//  NOTE: If I take this response and handle it myself I get the file to be downloaded. I therefore assume everything above is working as planned.
	//**************************************************
	e.Response = await ConvertResponseAsync(response);
	deferral.Complete();
}

The 2 converters which I hope are right

private HttpRequestMessage ConvertRequest(CoreWebView2WebResourceRequest request)
{
	HttpRequestMessage req = new((HttpMethod.Get), request.Uri);

	foreach (var header in request.Headers)
	{
		req.Headers.Add(header.Key, header.Value);
	}
	return req;
}

private async Task<CoreWebView2WebResourceResponse> ConvertResponseAsync(HttpResponseMessage aResponse)
{
	var stream = await aResponse.Content.ReadAsStreamAsync();
	var statCode = (int)aResponse.StatusCode;

	CoreWebView2WebResourceResponse cwv2Response =this.CoreWebView2.Environment.CreateWebResourceResponse(stream, statCode, aResponse.ReasonPhrase, "");

	CoreWebView2HttpResponseHeaders heads = cwv2Response.Headers;

	foreach (var header in aResponse.Headers)
	{
		string headerContent = string.Join(",", header.Value.ToArray()); ;
		heads.AppendHeader(header.Key, headerContent);

		
	}
	heads.AppendHeader(@"Content-Disposition", @"attachment");
	return cwv2Response;
}

If I have converted the HttpResponseMessage to a CoreWebView2WebResourceResponse correctly then I am assuming there is a bug.

AB#41459256

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the CoreWebView2_WebResourceRequested handler and trace ConvertRequest and ConvertResponseAsync, especially the stream passed to CreateWebResourceResponse and the appended headers. Reproduce the WPF case using the provided SDK and runtime versions; done means the downloaded file contains the expected response body and honors Content-Disposition: attachment.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.