2026-10-03 11:10:42 +02:00
|
|
|
namespace PrivaPub.Infrastructure.Http
|
|
|
|
|
{
|
|
|
|
|
public static class HttpScope
|
|
|
|
|
{
|
|
|
|
|
public const string Request = "request";
|
|
|
|
|
|
2026-10-03 12:19:29 +02:00
|
|
|
sealed record Tags(string Purpose, string Trigger, bool Crawl, string UserAgent = default);
|
2026-10-03 11:10:42 +02:00
|
|
|
|
|
|
|
|
static readonly AsyncLocal<Tags> current = new();
|
|
|
|
|
|
|
|
|
|
public static string Purpose => current.Value?.Purpose;
|
|
|
|
|
public static string Trigger => current.Value?.Trigger ?? Request;
|
|
|
|
|
public static bool Crawling => current.Value?.Crawl == true;
|
2026-10-03 12:19:29 +02:00
|
|
|
public static string UserAgent => current.Value?.UserAgent;
|
2026-10-03 11:10:42 +02:00
|
|
|
|
|
|
|
|
public static IDisposable For(string purpose) => Push(tags => tags with { Purpose = purpose });
|
|
|
|
|
|
|
|
|
|
public static IDisposable Default(string purpose) => Push(tags => tags with { Purpose = tags.Purpose ?? purpose });
|
|
|
|
|
|
|
|
|
|
public static IDisposable Triggered(string trigger) => Push(tags => tags with { Trigger = trigger });
|
|
|
|
|
|
2026-10-03 12:19:29 +02:00
|
|
|
public static IDisposable Crawl(string userAgent = default) => Push(tags => tags with { Crawl = true, UserAgent = userAgent ?? tags.UserAgent });
|
2026-10-03 11:10:42 +02:00
|
|
|
|
|
|
|
|
static IDisposable Push(Func<Tags, Tags> change)
|
|
|
|
|
{
|
|
|
|
|
var previous = current.Value;
|
|
|
|
|
current.Value = change(previous ?? new Tags(default, default, false));
|
|
|
|
|
return new Restore(previous);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sealed class Restore : IDisposable
|
|
|
|
|
{
|
|
|
|
|
readonly Tags _previous;
|
|
|
|
|
|
|
|
|
|
public Restore(Tags previous) => _previous = previous;
|
|
|
|
|
|
|
|
|
|
public void Dispose() => current.Value = _previous;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|