using System.Collections.Concurrent;
using System.Net;
using System.Security.Cryptography;
using System.Text;
namespace Agent.Tests;
///
/// A loopback HTTP server for real download round trips: objects with an MD5 ETag
/// (or a wrong one), requests that fail before they succeed, slow responses, and
/// a count of how many are served at once.
///
internal sealed class TestFileServer : IDisposable
{
private readonly HttpListener listener = new();
private readonly string baseUrl;
private readonly ConcurrentDictionary entries = new();
private int inFlight;
private int maxInFlight;
private int requests;
public sealed class Entry
{
public required byte[] Body { get; init; }
public string? ETag { get; set; }
/// Requests answered 500 before the object is served.
public int FailFirst { get; set; }
/// A wait before the headers.
public TimeSpan Delay { get; set; }
/// The body in this many pieces, with this wait between them.
public int Chunks { get; set; } = 1;
public TimeSpan ChunkDelay { get; set; }
}
public TestFileServer()
{
for (var attempt = 0; ; attempt++)
{
var probe = new System.Net.Sockets.TcpListener(IPAddress.Loopback, 0);
probe.Start();
var port = ((IPEndPoint)probe.LocalEndpoint).Port;
probe.Stop();
baseUrl = $"http://127.0.0.1:{port}";
listener.Prefixes.Clear();
listener.Prefixes.Add(baseUrl + "/");
try
{
listener.Start();
break;
}
catch (HttpListenerException) when (attempt < 5)
{
}
}
_ = Task.Run(async () =>
{
while (listener.IsListening)
{
HttpListenerContext context;
try
{
context = await listener.GetContextAsync();
}
catch (Exception)
{
return;
}
_ = Task.Run(() => ServeAsync(context));
}
});
}
public int MaxConcurrent => Volatile.Read(ref maxInFlight);
public int Requests => Volatile.Read(ref requests);
public Entry Add(string path, string content, bool etag = true) => Add(path, Encoding.UTF8.GetBytes(content), etag);
public Entry Add(string path, byte[] body, bool etag = true)
{
var entry = new Entry { Body = body, ETag = etag ? Convert.ToHexStringLower(MD5.HashData(body)) : null };
entries[path] = entry;
return entry;
}
public string Url(string path) => baseUrl + path;
private async Task ServeAsync(HttpListenerContext context)
{
Interlocked.Increment(ref requests);
var now = Interlocked.Increment(ref inFlight);
for (var seen = Volatile.Read(ref maxInFlight); now > seen; seen = Volatile.Read(ref maxInFlight))
{
Interlocked.CompareExchange(ref maxInFlight, now, seen);
}
try
{
if (!entries.TryGetValue(context.Request.Url!.AbsolutePath, out var entry))
{
context.Response.StatusCode = 404;
return;
}
if (entry.Delay > TimeSpan.Zero)
{
await Task.Delay(entry.Delay);
}
lock (entry)
{
if (entry.FailFirst > 0)
{
entry.FailFirst--;
context.Response.StatusCode = 500;
return;
}
}
context.Response.StatusCode = 200;
context.Response.ContentLength64 = entry.Body.Length;
if (entry.ETag is not null)
{
context.Response.AddHeader("ETag", $"\"{entry.ETag}\"");
}
var size = Math.Max(1, (entry.Body.Length + entry.Chunks - 1) / entry.Chunks);
for (var offset = 0; offset < entry.Body.Length; offset += size)
{
await context.Response.OutputStream.WriteAsync(entry.Body.AsMemory(offset, Math.Min(size, entry.Body.Length - offset)));
await context.Response.OutputStream.FlushAsync();
if (entry.ChunkDelay > TimeSpan.Zero)
{
await Task.Delay(entry.ChunkDelay);
}
}
}
catch (Exception)
{
// The client went away (a cancelled download).
}
finally
{
Interlocked.Decrement(ref inFlight);
try
{
context.Response.Close();
}
catch (Exception)
{
}
}
}
public void Dispose() => listener.Stop();
}