using System.Net;
using System.Net.Sockets;
using System.Text;
using Febe.Core.Abstractions;
using Febe.Core.Collection.Requests;
using Febe.SitoDiProva;
using Febe.Tests.Browser.TestSupport;
using Febe.Tests.Collection.Support;
namespace Febe.Tests.Collection;
///
/// What the HTTP site client (issue #36, S2) puts on the wire, read by a bare listener on 127.0.0.1: the headers of the
/// catalog with the session's values and nothing else, the cookies as one line, the GraphQL form with the variables
/// typed as the template; and where it refuses to go at all. Never Instagram: an Instagram address is only built,
/// never asked.
///
[TestClass]
public sealed class HttpSiteRequesterShapeTests
{
private const string UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36";
[TestMethod]
public async Task APlainAddressCarriesTheCatalogHeadersTheSessionCookiesAndNothingElse()
{
using var listener = new OneShotListener("""{"profilo":{"nome":"x"}}""");
using var requester = Requester(listener.Port);
// An activity of the host (ASP.NET Core starts one per request) must not add «traceparent» to what leaves.
using var activity = new System.Diagnostics.Activity("prova").Start();
var answer = await requester.SendAsync(SiteRequest.For("profilo", (CaseValues.Username, "marta.prova")), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None);
var request = await listener.Request;
Assert.AreEqual(200, answer.Status);
Assert.IsTrue(answer.RootFieldsPresent);
Assert.AreEqual("GET /api/dati/profilo/marta.prova?scheda=post HTTP/1.1", request.Line);
CollectionAssert.AreEquivalent(
new[] { "host", "accept", "user-agent", "x-app-id", "x-csrftoken", "cookie", "accept-encoding" },
request.Headers.Select(h => h.Name).ToArray(),
"Only the catalog's headers, the cookie line, and what the HTTP stack must write.");
Assert.AreEqual(UserAgent, request.Header("user-agent"), "Copied as it is, double space included (rule 4).");
Assert.AreEqual("application/json", request.Header("accept"));
Assert.AreEqual(TestSite.AppId, request.Header("x-app-id"));
Assert.AreEqual("csrf-1", request.Header("x-csrftoken"), "The value of the cookie csrftoken.");
Assert.AreEqual("csrftoken=csrf-1; sessione_prova=attiva; rur=\"FRC\\05412\"", request.Header("cookie"), "One line, the session's order.");
Assert.AreEqual("gzip, deflate, br", request.Header("accept-encoding"));
Assert.AreEqual($"127.0.0.1:{listener.Port}", request.Header("host"));
}
[TestMethod]
public async Task AGraphQlQueryIsTheFormOfThePageWithTheVariablesTypedAsTheTemplate()
{
using var listener = new OneShotListener("""{"profilo":{"nome":"x"}}""");
using var requester = Requester(listener.Port);
await requester.SendAsync(SiteRequest.For(TestSite.ProfileQuery, (CaseValues.Username, "marta.prova")), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None);
var request = await listener.Request;
Assert.AreEqual("POST /api/graphql HTTP/1.1", request.Line);
CollectionAssert.AreEquivalent(
new[] { "host", "accept", "content-length", "content-type", "user-agent", "x-app-id", "x-csrftoken", "x-fb-friendly-name", "x-ig-www-claim", "cookie", "accept-encoding" },
request.Headers.Select(h => h.Name).ToArray());
Assert.AreEqual("application/x-www-form-urlencoded", request.Header("content-type"), "As the catalog writes it, no charset added.");
Assert.AreEqual(TestSite.ProfileQuery, request.Header("x-fb-friendly-name"));
Assert.AreEqual("0", request.Header("x-ig-www-claim"));
Assert.AreEqual(
"av=17841400000000077&__a=1&fb_dtsg=dtsg-di-prova&lsd=lsd-di-prova&fb_api_req_friendly_name=ProvaProfiloQuery"
+ "&variables=%7B%22username%22%3A%22marta.prova%22%2C%22scheda%22%3A%22post%22%2C%22primi%22%3A12%2C%22raccolte%22%3Atrue%7D"
+ "&doc_id=8000000000000001",
request.Body,
"Fields in the order of the file; the variables as JSON.stringify writes them: 12 a number, true a boolean.");
Assert.AreEqual(Encoding.UTF8.GetByteCount(request.Body).ToString(System.Globalization.CultureInfo.InvariantCulture), request.Header("content-length"));
}
[TestMethod]
public void VariablesAndFormFieldsAreWrittenAsThePageWritesThem()
{
var search = RequestCatalog.LoadInstagram().Operations.Single(o => o.Name == "PolarisSearchBoxRefetchableQuery");
var variables = HttpSiteRequester.Variables(search, SiteRequest.For(search.Name, (CaseValues.SearchText, "caffè & «latte» \"x\"")));
StringAssert.Contains(variables, "\"query\":\"caffè & «latte» \\\"x\\\"\"", "No escaping of letters or of < > & (JSON.stringify).");
Assert.IsFalse(variables.Contains("\": ", StringComparison.Ordinal) || variables.Contains(", \"", StringComparison.Ordinal), "No spaces between tokens.");
Assert.AreEqual("a+b%26c%3Dd%C3%A8*-._%2B%2F", HttpSiteRequester.FormEncode("a b&c=dè*-._+/"));
}
[TestMethod]
public async Task NoRedirectIsFollowed()
{
using var listener = new OneShotListener(string.Empty, status: "302 Found", extraHeaders: "Location: http://127.0.0.1:1/accounts/login/?next=%2Fmarta.prova%2F\r\nSet-Cookie: sessione_prova=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/\r\n");
var session = new InMemorySiteSession(Session());
using var requester = new HttpSiteRequester(RequestCatalog.LoadTestSite(), session, HttpSiteRequesterOptions.TestSite(new Uri($"http://127.0.0.1:{listener.Port}/")), new VirtualClock());
var answer = await requester.SendAsync(SiteRequest.For("profilo", (CaseValues.Username, "marta.prova")), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None);
Assert.AreEqual(302, answer.Status);
Assert.AreEqual("/accounts/login/", answer.RedirectPathShape);
Assert.IsTrue(answer.Deletes(TestSite.SessionCookie));
Assert.IsNull(session.Read()!.Cookie(TestSite.SessionCookie));
Assert.AreEqual(1, listener.Connections, "One request: the redirect was not followed (port 1 was never tried either).");
}
[TestMethod]
[DataRow("prova", "http://example.com/")]
[DataRow("prova", "http://10.1.2.3:8080/")]
[DataRow("prova", "https://www.instagram.com/")]
[DataRow("prova", "http://localhost:8481/")]
[DataRow("prova", "http://127.0.0.1:8481/api/")]
[DataRow("prova", "http://utente:segreto@127.0.0.1:8481/")]
[DataRow("prova", "ftp://127.0.0.1/")]
[DataRow("instagram", "http://www.instagram.com/")]
[DataRow("instagram", "https://instagram.com/")]
[DataRow("instagram", "https://www.instagram.com:8443/")]
[DataRow("instagram", "https://www.instagram.com.example.net/")]
[DataRow("instagram", "https://127.0.0.1/")]
public void AnyOtherAddressIsRefusedBeforeAnyConnection(string site, string address)
{
var catalog = site == "instagram" ? SettledCatalog.Instagram() : RequestCatalog.LoadTestSite();
var options = site == "instagram" ? HttpSiteRequesterOptions.Instagram() with { SiteAddress = new Uri(address) } : HttpSiteRequesterOptions.TestSite(new Uri(address));
var refused = Assert.ThrowsExactly(() => new HttpSiteRequester(catalog, new InMemorySiteSession(Session()), options, new VirtualClock()));
Assert.IsFalse(refused.Message.Contains("segreto", StringComparison.Ordinal));
}
[TestMethod]
public void TheOnlyAddressesAreInstagramOverHttpsAndLoopbackTestSites()
{
Assert.IsNull(HttpSiteRequester.RefusalOfAddress(SettledCatalog.Instagram(), HttpSiteRequesterOptions.InstagramAddress));
Assert.IsNull(HttpSiteRequester.RefusalOfAddress(RequestCatalog.LoadTestSite(), new Uri("http://127.0.0.1:8481/")));
Assert.IsNull(HttpSiteRequester.RefusalOfAddress(RequestCatalog.LoadTestSite(), new Uri("http://127.0.0.2:8481/")));
Assert.IsNull(HttpSiteRequester.RefusalOfAddress(RequestCatalog.LoadTestSite(), new Uri("https://[::1]:8443/")));
// Built, never asked: the constructor opens no connection.
using var instagram = new HttpSiteRequester(SettledCatalog.Instagram(), new InMemorySiteSession(null), HttpSiteRequesterOptions.Instagram(), new VirtualClock());
Assert.AreEqual("https://www.instagram.com/", instagram.Site.AbsoluteUri);
}
[TestMethod]
public void TheHandlerFollowsNothingKeepsNoCookiesAndSpeaksHttp2OrHttp11()
{
using var site = HttpSiteRequester.CreateHandler(HttpSiteRequesterOptions.Instagram(), loopback: false);
Assert.IsFalse(site.AllowAutoRedirect);
Assert.IsFalse(site.UseCookies);
Assert.IsTrue(site.UseProxy, "The system proxy, as WebView2.");
Assert.IsNull(site.Proxy, "Null is HttpClient.DefaultProxy, the system's.");
Assert.AreEqual(System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.Brotli, site.AutomaticDecompression);
Assert.AreEqual(TimeSpan.FromSeconds(10), site.ConnectTimeout);
Assert.IsNull(site.ActivityHeadersPropagator, "No traceparent or Request-Id added to the request.");
using var local = HttpSiteRequester.CreateHandler(HttpSiteRequesterOptions.TestSite(new Uri("http://127.0.0.1:1/")), loopback: true);
Assert.IsFalse(local.UseProxy, "A loopback test site is never reached through a proxy.");
}
[TestMethod]
public async Task ARenewedCookieAndTheClaimAreKeptAndTheAnswerShowsOnlyTheirNames()
{
using var listener = new OneShotListener("""{"profilo":{}}""", extraHeaders: "Set-Cookie: csrftoken=csrf-2; Max-Age=31449600; Path=/; Secure\r\nSet-Cookie: estraneo=1; Domain=altro.example; Path=/\r\nx-ig-set-www-claim: hmac.nuovo\r\nRetry-After: 30\r\n");
var session = new InMemorySiteSession(Session());
using var requester = new HttpSiteRequester(RequestCatalog.LoadTestSite(), session, HttpSiteRequesterOptions.TestSite(new Uri($"http://127.0.0.1:{listener.Port}/")), new VirtualClock());
var answer = await requester.SendAsync(SiteRequest.For("profilo", (CaseValues.Username, "marta.prova")), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None);
CollectionAssert.AreEqual(new[] { "csrftoken" }, answer.CookiesSet.ToArray(), "A cookie of another domain is not the site's.");
Assert.IsTrue(answer.SetsClaim);
Assert.AreEqual(TimeSpan.FromSeconds(30), answer.RetryAfter);
Assert.AreEqual("csrf-2", session.Read()!.Cookie("csrftoken"));
Assert.IsNull(session.Read()!.Cookie("estraneo"));
Assert.AreEqual("hmac.nuovo", session.Read()!.Fields["claim"]);
Assert.IsFalse(answer.ToString().Contains("csrf-2", StringComparison.Ordinal));
Assert.IsFalse(answer.ToString().Contains("hmac", StringComparison.Ordinal));
}
[TestMethod]
public void TheVariablesAreWhatJsonStringifyWrites()
{
var search = RequestCatalog.LoadInstagram().Operations.Single(o => o.Name == "PolarisSearchBoxRefetchableQuery");
var variables = HttpSiteRequester.Variables(search, SiteRequest.For(search.Name, (CaseValues.SearchText, "mare \U0001F30A \\ \"blu\"")));
StringAssert.Contains(variables, "\"query\":\"mare \U0001F30A \\\\ \\\"blu\\\"\"", "The emoji as it is, never \\uD83C\\uDF0A.");
using (var parsed = System.Text.Json.JsonDocument.Parse(variables))
{
Assert.IsNotNull(parsed, "Still JSON.");
}
// JSON.stringify (ECMA-262 QuoteJSONString): U+2028, U+2029 and DEL as they are; controls and a lone surrogate
// as lower-case \u; the short escapes.
var text = new System.Text.StringBuilder();
HttpSiteRequester.WriteString(text, "a\u2028b\u2029c\u007F d\u0001e\u001f\b\f\n\r\t" + "\ud800" + "!");
Assert.AreEqual("\"a\u2028b\u2029c\u007F d\\u0001e\\u001f\\b\\f\\n\\r\\t\\ud800!\"", text.ToString());
}
[TestMethod]
public async Task ARenewalCountsAsSoonAsTheHeadersArriveWhateverHappensToTheBody()
{
// The body is larger than allowed: no answer, but the site rotated the cookie and the claim with the headers.
using (var listener = new OneShotListener(new string('x', 4096), extraHeaders: "Set-Cookie: sessione_prova=rinnovata; Path=/\r\nSet-Cookie: rur=\"CLN\\05412\"; Path=/\r\nx-ig-set-www-claim: hmac.nuovo\r\n"))
{
var session = new InMemorySiteSession(Session());
using var requester = new HttpSiteRequester(RequestCatalog.LoadTestSite(), session, HttpSiteRequesterOptions.TestSite(new Uri($"http://127.0.0.1:{listener.Port}/")) with { MaxBodyBytes = 64 }, new VirtualClock());
var large = await Assert.ThrowsExactlyAsync(() => requester.SendAsync(Profile(), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None));
Assert.AreEqual(SiteUnreachableReason.TooLarge, large.Reason);
Assert.AreEqual("rinnovata", session.Read()!.Cookie(TestSite.SessionCookie));
Assert.AreEqual("\"CLN\\05412\"", session.Read()!.Cookie("rur"), "Quotes kept, as the browser keeps them.");
Assert.AreEqual("hmac.nuovo", session.Read()!.Fields["claim"]);
}
// The body never ends and the caller cancels while it is read: the deletion of the session cookie still counts.
using (var listener = new OneShotListener("{\"pro", extraHeaders: "Set-Cookie: sessione_prova=; expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/\r\n", declaredLength: 1000, holdOpen: true))
{
var session = new InMemorySiteSession(Session());
var log = new List();
using var requester = new HttpSiteRequester(RequestCatalog.LoadTestSite(), session, HttpSiteRequesterOptions.TestSite(new Uri($"http://127.0.0.1:{listener.Port}/")), new VirtualClock(), log.Add);
using var caller = new CancellationTokenSource();
var sending = requester.SendAsync(Profile(), Permit.Grant(ActionKind.ProfileOpen, null), caller.Token);
await listener.Request;
await WaitUntilAsync(() => session.Updates == 1);
await caller.CancelAsync();
await Assert.ThrowsAsync(() => sending);
Assert.IsNull(session.Read()!.Cookie(TestSite.SessionCookie));
Assert.IsTrue(log.Any(l => l.Contains("ha cancellato il cookie di sessione", StringComparison.Ordinal)));
}
}
[TestMethod]
[DataRow("gzip")]
[DataRow("br")]
[DataRow("deflate")]
public async Task ABodyThatDoesNotDecompressIsBrokenNotTooLarge(string encoding)
{
using var listener = new OneShotListener(string.Empty, extraHeaders: $"Content-Encoding: {encoding}\r\n", raw: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
using var requester = Requester(listener.Port);
var broken = await Assert.ThrowsExactlyAsync(() => requester.SendAsync(Profile(), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None));
Assert.AreEqual(SiteUnreachableReason.Broken, broken.Reason);
}
[TestMethod]
public async Task AnExpiredSessionCookieIsAMissingSessionAndAnExpiredCookieIsNotSent()
{
var clock = new VirtualClock(new DateTimeOffset(2026, 11, 16, 10, 0, 0, TimeSpan.Zero));
var values = Session();
using var listener = new OneShotListener("""{"profilo":{}}""");
// «rur» expired a minute ago: not in the cookie line.
var session = new InMemorySiteSession(new SiteSessionValues(values.Cookies, values.UserAgent, values.Fields, values.DocIds, new Dictionary { ["rur"] = clock.Now.AddMinutes(-1) }));
using (var requester = new HttpSiteRequester(RequestCatalog.LoadTestSite(), session, HttpSiteRequesterOptions.TestSite(new Uri($"http://127.0.0.1:{listener.Port}/")), clock))
{
await requester.SendAsync(Profile(), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None);
Assert.AreEqual("csrftoken=csrf-1; sessione_prova=attiva", (await listener.Request).Header("cookie"));
}
// The session cookie expired: nothing leaves, a Disconnessione.
var expired = new InMemorySiteSession(new SiteSessionValues(values.Cookies, values.UserAgent, values.Fields, values.DocIds, new Dictionary { [TestSite.SessionCookie] = clock.Now }));
using var late = new HttpSiteRequester(RequestCatalog.LoadTestSite(), expired, HttpSiteRequesterOptions.TestSite(new Uri($"http://127.0.0.1:{listener.Port}/")), clock);
var refused = await Assert.ThrowsExactlyAsync(() => late.SendAsync(Profile(), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None));
Assert.AreEqual(TestSite.SessionCookie, refused.Missing);
Assert.AreEqual(1, listener.Connections);
}
[TestMethod]
public async Task WithoutAClaimYetTheQuerySendsThePagesZero()
{
using var listener = new OneShotListener("""{"profilo":{}}""");
var values = Session();
var fields = values.Fields.Where(f => f.Key != "claim").ToDictionary();
using var requester = new HttpSiteRequester(RequestCatalog.LoadTestSite(), new InMemorySiteSession(new SiteSessionValues(values.Cookies, values.UserAgent, fields, values.DocIds)), HttpSiteRequesterOptions.TestSite(new Uri($"http://127.0.0.1:{listener.Port}/")), new VirtualClock());
await requester.SendAsync(SiteRequest.For(TestSite.ProfileQuery, (CaseValues.Username, "marta.prova")), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None);
Assert.AreEqual("0", (await listener.Request).Header("x-ig-www-claim"));
}
[TestMethod]
public async Task DisposeEndsARequestInFlightAsACancellationAndRefusesLaterOnes()
{
using var listener = new OneShotListener("{", declaredLength: 1000, holdOpen: true);
var requester = Requester(listener.Port);
var sending = requester.SendAsync(Profile(), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None);
await listener.Request;
requester.Dispose();
await Assert.ThrowsAsync(() => sending);
await Assert.ThrowsExactlyAsync(() => requester.SendAsync(Profile(), Permit.Grant(ActionKind.ProfileOpen, null), CancellationToken.None));
requester.Dispose();
}
[TestMethod]
public void OnWindowsTheProxyVariablesWebView2IgnoresStopTheClient()
{
var none = new Dictionary();
var set = new Dictionary { ["HTTPS_PROXY"] = "http://utente:segreto@proxy.example:3128", ["NO_PROXY"] = "localhost" };
Assert.IsNull(HttpSiteRequester.EnvironmentProxyRefusal(windows: true, n => none.GetValueOrDefault(n)));
Assert.IsNull(HttpSiteRequester.EnvironmentProxyRefusal(windows: false, n => set.GetValueOrDefault(n)), "In Docker Chromium reads them too.");
var refusal = HttpSiteRequester.EnvironmentProxyRefusal(windows: true, n => set.GetValueOrDefault(n));
Assert.IsNotNull(refusal);
StringAssert.Contains(refusal, "HTTPS_PROXY");
Assert.IsFalse(refusal.Contains("segreto", StringComparison.Ordinal) || refusal.Contains("proxy.example", StringComparison.Ordinal), "Never the value.");
}
private static SiteRequest Profile() => SiteRequest.For("profilo", (CaseValues.Username, "marta.prova"));
private static async Task WaitUntilAsync(Func condition)
{
using var limit = new CancellationTokenSource(TimeSpan.FromSeconds(30));
while (!condition())
{
await Task.Delay(10, limit.Token);
}
}
private static HttpSiteRequester Requester(int port) =>
new(RequestCatalog.LoadTestSite(), new InMemorySiteSession(Session()), HttpSiteRequesterOptions.TestSite(new Uri($"http://127.0.0.1:{port}/")), new VirtualClock());
private static SiteSessionValues Session() => new(
[new("csrftoken", "csrf-1"), new(TestSite.SessionCookie, SiteControl.InitialSession), new("rur", "\"FRC\\05412\"")],
UserAgent,
new Dictionary { ["app_id"] = TestSite.AppId, ["lsd"] = TestSite.Lsd, ["fb_dtsg"] = TestSite.Dtsg, ["claim"] = "0", ["account_id"] = "17841400000000077" },
TestSite.DocIds);
/// The head and body of one HTTP/1.1 request, as they arrived.
private sealed record RawRequest(string Line, IReadOnlyList<(string Name, string Value)> Headers, string Body)
{
public string Header(string name) => Headers.Single(h => h.Name == name).Value;
}
///
/// A bare HTTP/1.1 listener on 127.0.0.1 that reads requests as they arrive and answers each with the same
/// response, closing the connection: what the client really wrote, with nothing in between.
///
private sealed class OneShotListener : IDisposable
{
private readonly TcpListener _listener;
private readonly TaskCompletionSource _first = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly CancellationTokenSource _stop = new();
private readonly byte[] _response;
private int _connections;
private readonly bool _holdOpen;
/// The body, as UTF-8.
/// Status line after the version.
/// More header lines, each ending in CRLF.
/// The body as bytes, in place of .
/// The Content-Length to declare, when it is not the body's (a body that never ends).
/// Keeps the connection open after writing, until disposed.
public OneShotListener(string body, string status = "200 OK", string extraHeaders = "", byte[]? raw = null, int? declaredLength = null, bool holdOpen = false)
{
Port = LocalTestSite.FreePort();
_listener = new TcpListener(IPAddress.Loopback, Port);
_listener.Start();
var bytes = raw ?? Encoding.UTF8.GetBytes(body);
_holdOpen = holdOpen;
_response = [.. Encoding.ASCII.GetBytes($"HTTP/1.1 {status}\r\nContent-Type: application/json; charset=utf-8\r\nContent-Length: {declaredLength ?? bytes.Length}\r\n{extraHeaders}Connection: close\r\n\r\n"), .. bytes];
_ = Task.Run(LoopAsync);
}
public int Port { get; }
public int Connections => Volatile.Read(ref _connections);
public Task Request => _first.Task.WaitAsync(TimeSpan.FromSeconds(30));
public void Dispose()
{
_stop.Cancel();
_listener.Stop();
_stop.Dispose();
}
private async Task LoopAsync()
{
while (!_stop.IsCancellationRequested)
{
TcpClient client;
try
{
client = await _listener.AcceptTcpClientAsync(_stop.Token);
}
catch (Exception ex) when (ex is OperationCanceledException or SocketException or ObjectDisposedException)
{
return;
}
Interlocked.Increment(ref _connections);
using (client)
{
try
{
var stream = client.GetStream();
var request = await ReadAsync(stream);
_first.TrySetResult(request);
await stream.WriteAsync(_response, _stop.Token);
if (_holdOpen)
{
await Task.Delay(Timeout.Infinite, _stop.Token);
}
}
catch (Exception ex) when (ex is IOException or OperationCanceledException or InvalidDataException)
{
_first.TrySetException(ex);
}
}
}
}
private async Task ReadAsync(NetworkStream stream)
{
var buffer = new List();
var chunk = new byte[4096];
int end;
while ((end = IndexOfHeadEnd(buffer)) < 0)
{
var read = await stream.ReadAsync(chunk, _stop.Token);
if (read == 0)
{
throw new InvalidDataException("Richiesta interrotta.");
}
buffer.AddRange(chunk.AsSpan(0, read).ToArray());
}
var head = Encoding.ASCII.GetString([.. buffer.Take(end)]).Split("\r\n");
var headers = head.Skip(1).Select(l =>
{
var colon = l.IndexOf(':', StringComparison.Ordinal);
return (l[..colon].Trim().ToLowerInvariant(), l[(colon + 1)..].Trim());
}).ToList();
var length = headers.Where(h => h.Item1 == "content-length").Select(h => int.Parse(h.Item2, System.Globalization.CultureInfo.InvariantCulture)).FirstOrDefault();
var body = buffer.Skip(end + 4).ToList();
while (body.Count < length)
{
var read = await stream.ReadAsync(chunk, _stop.Token);
if (read == 0)
{
break;
}
body.AddRange(chunk.AsSpan(0, read).ToArray());
}
return new RawRequest(head[0], headers, Encoding.UTF8.GetString([.. body]));
}
private static int IndexOfHeadEnd(List buffer)
{
for (var i = 0; i + 3 < buffer.Count; i++)
{
if (buffer[i] == '\r' && buffer[i + 1] == '\n' && buffer[i + 2] == '\r' && buffer[i + 3] == '\n')
{
return i;
}
}
return -1;
}
}
}