-
-
Notifications
You must be signed in to change notification settings - Fork 7
Show real download progress for Windows updates via a loopback proxy #2468
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
hahn-kev-bot
wants to merge
1
commit into
develop
Choose a base branch
from
claude/windows-update-progress-a2f07d
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
backend/FwLite/FwLiteMaui.Tests/UpdateDownloadProxyTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| using System.Net; | ||
| using System.Net.Http.Headers; | ||
| using System.Net.Sockets; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
|
|
||
| namespace FwLiteMaui.Tests; | ||
|
|
||
| #if WINDOWS | ||
| public class UpdateDownloadProxyTests | ||
| { | ||
| // Deterministic payload the fake upstream serves, with byte-range support. | ||
| private static readonly byte[] Payload = Enumerable.Range(0, 100_000).Select(i => (byte)(i % 251)).ToArray(); | ||
|
|
||
| [Fact] | ||
| public async Task ForwardsFullDownloadAndReportsByteTotal() | ||
| { | ||
| await using var upstream = new FakeUpstream(Payload); | ||
| long reported = 0; | ||
| await using var proxy = await UpdateDownloadProxy.StartAsync(upstream.Url, NullLogger.Instance, total => reported = total); | ||
|
|
||
| using var client = new HttpClient(); | ||
| var body = await client.GetByteArrayAsync(proxy.LocalUri); | ||
|
|
||
| body.Should().Equal(Payload); | ||
| reported.Should().Be(Payload.Length); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ForwardsRangeRequestAsPartialContent() | ||
| { | ||
| await using var upstream = new FakeUpstream(Payload); | ||
| await using var proxy = await UpdateDownloadProxy.StartAsync(upstream.Url, NullLogger.Instance, _ => { }); | ||
|
|
||
| using var client = new HttpClient(); | ||
| using var request = new HttpRequestMessage(HttpMethod.Get, proxy.LocalUri); | ||
| request.Headers.Range = new RangeHeaderValue(10, 19); | ||
| using var response = await client.SendAsync(request); | ||
|
|
||
| response.StatusCode.Should().Be(HttpStatusCode.PartialContent); | ||
| response.Content.Headers.ContentRange!.From.Should().Be(10); | ||
| response.Content.Headers.ContentRange!.To.Should().Be(19); | ||
| var body = await response.Content.ReadAsByteArrayAsync(); | ||
| body.Should().Equal(Payload[10..20]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReturnsNotFoundForUnknownPath() | ||
| { | ||
| await using var upstream = new FakeUpstream(Payload); | ||
| await using var proxy = await UpdateDownloadProxy.StartAsync(upstream.Url, NullLogger.Instance, _ => { }); | ||
|
|
||
| var wrongPath = new Uri(proxy.LocalUri, "wrong"); | ||
| using var client = new HttpClient(); | ||
| using var response = await client.GetAsync(wrongPath); | ||
|
|
||
| response.StatusCode.Should().Be(HttpStatusCode.NotFound); | ||
| } | ||
|
|
||
| /// <summary>Minimal HTTP origin that serves a byte array with Range/HEAD support.</summary> | ||
| private sealed class FakeUpstream : IAsyncDisposable | ||
| { | ||
| private readonly HttpListener _listener = new(); | ||
| private readonly byte[] _payload; | ||
| private readonly CancellationTokenSource _cts = new(); | ||
| private readonly Task _loop; | ||
|
|
||
| public string Url { get; } | ||
|
|
||
| public FakeUpstream(byte[] payload) | ||
| { | ||
| _payload = payload; | ||
| var probe = new TcpListener(IPAddress.Loopback, 0); | ||
| probe.Start(); | ||
| var port = ((IPEndPoint)probe.LocalEndpoint).Port; | ||
| probe.Stop(); | ||
|
|
||
| Url = $"http://localhost:{port}/asset"; | ||
| _listener.Prefixes.Add($"http://localhost:{port}/"); | ||
| _listener.Start(); | ||
| _loop = Task.Run(AcceptLoopAsync); | ||
| } | ||
|
|
||
| private async Task AcceptLoopAsync() | ||
| { | ||
| while (!_cts.IsCancellationRequested) | ||
| { | ||
| HttpListenerContext ctx; | ||
| try { ctx = await _listener.GetContextAsync(); } | ||
| catch { break; } | ||
| _ = Task.Run(() => Handle(ctx)); | ||
| } | ||
| } | ||
|
|
||
| private async Task Handle(HttpListenerContext ctx) | ||
| { | ||
| try | ||
| { | ||
| ctx.Response.Headers["Accept-Ranges"] = "bytes"; | ||
| var rangeHeader = ctx.Request.Headers["Range"]; | ||
| if (!string.IsNullOrEmpty(rangeHeader) && RangeHeaderValue.TryParse(rangeHeader, out var range)) | ||
| { | ||
| var from = (int)(range.Ranges.First().From ?? 0); | ||
| var to = (int)(range.Ranges.First().To ?? _payload.Length - 1); | ||
| var length = to - from + 1; | ||
| ctx.Response.StatusCode = (int)HttpStatusCode.PartialContent; | ||
| ctx.Response.Headers["Content-Range"] = $"bytes {from}-{to}/{_payload.Length}"; | ||
| ctx.Response.ContentLength64 = length; | ||
| if (ctx.Request.HttpMethod != "HEAD") | ||
| await ctx.Response.OutputStream.WriteAsync(_payload.AsMemory(from, length)); | ||
| } | ||
| else | ||
| { | ||
| ctx.Response.StatusCode = (int)HttpStatusCode.OK; | ||
| ctx.Response.ContentLength64 = _payload.Length; | ||
| if (ctx.Request.HttpMethod != "HEAD") | ||
| await ctx.Response.OutputStream.WriteAsync(_payload); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| ctx.Response.Close(); | ||
| } | ||
| } | ||
|
|
||
| public async ValueTask DisposeAsync() | ||
| { | ||
| await _cts.CancelAsync(); | ||
| try { _listener.Stop(); } catch { /* ignore */ } | ||
| #pragma warning disable VSTHRD003 // _loop is our own Task.Run started in the ctor | ||
| try { await _loop; } catch { /* ignore */ } | ||
| #pragma warning restore VSTHRD003 | ||
| ((IDisposable)_listener).Dispose(); | ||
| _cts.Dispose(); | ||
| } | ||
| } | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Devin AI pointed out that Deploy runs for max 2 minutes due to:
var completedTask = await Task.WhenAny(updateTask, Task.Delay(TimeSpan.FromMinutes(2)));at which point it simply does
return UpdateResult.Started;That then results in
await using var proxy =being disposed, which presumably "cleans up"/cancels the download.So, I think this needs some work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now we'll:
It could we weird initially or if something goes wrong, but we expect it to work, so it's acceptable for now.