House rules: primary constructors, a cancellation token on every async call, and the rules fail the build
Build / Build (push) Successful in 30s
Build / Build (push) Successful in 30s
- Services, controllers and jobs take their dependencies through primary constructors and use them directly; where a method already named a local after a dependency, the dependency takes its type's name. - Every async method takes a CancellationToken last and passes it down to MongoDB, HTTP and the job queue. The setup claim is still given back when a request is cancelled. - The federation and media options are read through IOptionsMonitor. - The Mastodon entities keep sending "" for empty strings, and every model field where null means something says so. - The inbox's 503 with Retry-After, which other servers retry, is the one server error left, and says why. - Arasaka.HouseRules.Analyzers checks all of this in every build, here and in PrivaPub.ClientModels as decePubClient builds it. Checked against GoToSocial in the pasture: 64 passed, 0 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
This commit is contained in:
1 parent
f0a14c737f
commit
0f73f69239
176 files changed
+2789
-3974
No files matched your search
@@ -38,17 +38,14 @@ namespace PrivaPub.Infrastructure.Http
|
||||
|
||||
// On the federation client: reads each response's CDN fingerprint for its host. A response without one forgets the
|
||||
// host's hint, so a server that leaves its CDN is seen as itself again.
|
||||
public class EdgeHintsHandler : DelegatingHandler
|
||||
public class EdgeHintsHandler(
|
||||
IEdgeHints hints) : DelegatingHandler
|
||||
{
|
||||
readonly IEdgeHints _hints;
|
||||
|
||||
public EdgeHintsHandler(IEdgeHints hints) => _hints = hints;
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = await base.SendAsync(request, cancellationToken);
|
||||
if (request.RequestUri is { } uri && (int)response.StatusCode is >= 200 and < 500)
|
||||
_hints.Remember(uri.Host, CdnCatalog.FromResponse(response));
|
||||
hints.Remember(uri.Host, CdnCatalog.FromResponse(response));
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,13 @@ namespace PrivaPub.Infrastructure.Http
|
||||
Task<IReadOnlyList<string>> GetStringArray(string url, int maxItems, CancellationToken token);
|
||||
}
|
||||
|
||||
public class FederationHttp : IFederationHttp
|
||||
public class FederationHttp(
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IMemoryCache cache,
|
||||
IOptionsMonitor<FederationOptions> federationOptions,
|
||||
IDomainBlocks domainBlocks,
|
||||
ILogger<FederationHttp> logger,
|
||||
IInteractionLedger ledger = default) : IFederationHttp
|
||||
{
|
||||
public const string ClientName = "Federation";
|
||||
public const int MaxResponseBytes = 1024 * 1024;
|
||||
@@ -58,34 +64,12 @@ namespace PrivaPub.Infrastructure.Http
|
||||
"application/json"
|
||||
};
|
||||
|
||||
readonly IHttpClientFactory _httpClientFactory;
|
||||
readonly IMemoryCache _cache;
|
||||
readonly IOptionsMonitor<FederationOptions> _options;
|
||||
readonly IDomainBlocks _domainBlocks;
|
||||
readonly ILogger<FederationHttp> _logger;
|
||||
readonly IInteractionLedger _ledger;
|
||||
|
||||
public FederationHttp(IHttpClientFactory httpClientFactory, IMemoryCache cache, IOptionsMonitor<FederationOptions> options,
|
||||
IDomainBlocks domainBlocks, ILogger<FederationHttp> logger, IInteractionLedger ledger = default)
|
||||
sealed class Exchange(string url, string purpose)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_cache = cache;
|
||||
_options = options;
|
||||
_domainBlocks = domainBlocks;
|
||||
_logger = logger;
|
||||
_ledger = ledger;
|
||||
}
|
||||
public string Host { get; } = Interactions.HostOf(url);
|
||||
public string Purpose { get; } = purpose;
|
||||
|
||||
sealed class Exchange
|
||||
{
|
||||
public Exchange(string url, string purpose)
|
||||
{
|
||||
Host = Interactions.HostOf(url);
|
||||
Purpose = purpose;
|
||||
}
|
||||
|
||||
public readonly string Host;
|
||||
public readonly string Purpose;
|
||||
public readonly long Started = System.Diagnostics.Stopwatch.GetTimestamp();
|
||||
public int? Status;
|
||||
public long? Bytes;
|
||||
@@ -102,15 +86,15 @@ namespace PrivaPub.Infrastructure.Http
|
||||
|
||||
void Record(Exchange exchange)
|
||||
{
|
||||
if (_ledger == default)
|
||||
if (ledger == default)
|
||||
return;
|
||||
var trigger = HttpScope.Trigger;
|
||||
if (trigger == HttpScope.Request)
|
||||
{
|
||||
_ledger.Count(exchange.Host, $"http:{exchange.Purpose}:{exchange.Outcome}", exchange.Bytes ?? 0);
|
||||
ledger.Count(exchange.Host, $"http:{exchange.Purpose}:{exchange.Outcome}", exchange.Bytes ?? 0);
|
||||
return;
|
||||
}
|
||||
_ledger.Record(new InteractionEvent
|
||||
ledger.Record(new InteractionEvent
|
||||
{
|
||||
Channel = Interactions.Http,
|
||||
Host = exchange.Host,
|
||||
@@ -151,7 +135,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
for (var hop = 0; hop <= MaxRedirects; hop++)
|
||||
{
|
||||
using var request = Request(target, "text/plain");
|
||||
using var response = await _httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
using var response = await httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
exchange.Answered(response);
|
||||
if (IsRedirect(response.StatusCode))
|
||||
{
|
||||
@@ -207,7 +191,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
for (var hop = 0; hop <= MaxRedirects; hop++)
|
||||
{
|
||||
using var request = Request(target, "application/json");
|
||||
using var response = await _httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
using var response = await httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
exchange.Answered(response);
|
||||
if (IsRedirect(response.StatusCode))
|
||||
{
|
||||
@@ -278,9 +262,9 @@ namespace PrivaPub.Infrastructure.Http
|
||||
{
|
||||
if (target is not { IsAbsoluteUri: true } || !string.IsNullOrEmpty(target.UserInfo))
|
||||
return false;
|
||||
if (_domainBlocks?.IsSuspended(target.Host) == true)
|
||||
if (domainBlocks?.IsSuspended(target.Host) == true)
|
||||
return false;
|
||||
var options = _options.CurrentValue;
|
||||
var options = federationOptions.CurrentValue;
|
||||
if (target.Scheme != Uri.UriSchemeHttps && !(options.AllowPlainHttp && target.Scheme == Uri.UriSchemeHttp))
|
||||
return false;
|
||||
if (options.AllowPrivateNetworks)
|
||||
@@ -322,7 +306,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
// status; 0 when it never answered)
|
||||
public async Task<(int Status, FetchedJson Json)> GetJsonStatus(string url, string accept, Action<HttpRequestMessage> sign, CancellationToken token)
|
||||
{
|
||||
if (Uri.TryCreate(url, UriKind.Absolute, out var target) && _cache.TryGetValue(NegativeKey(target), out Refusal remembered))
|
||||
if (Uri.TryCreate(url, UriKind.Absolute, out var target) && cache.TryGetValue(NegativeKey(target), out Refusal remembered))
|
||||
return (remembered.Status, default);
|
||||
var exchange = new Exchange(url, HttpScope.Purpose ?? "object");
|
||||
try
|
||||
@@ -344,7 +328,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
return default;
|
||||
}
|
||||
var negativeKey = NegativeKey(target);
|
||||
if (_cache.TryGetValue(negativeKey, out _))
|
||||
if (cache.TryGetValue(negativeKey, out _))
|
||||
{
|
||||
exchange.Refused("remembered");
|
||||
return default;
|
||||
@@ -361,7 +345,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
request.Headers.TryAddWithoutValidation("If-None-Match", etag);
|
||||
sign?.Invoke(request);
|
||||
|
||||
using var response = await _httpClientFactory.CreateClient(ClientName)
|
||||
using var response = await httpClientFactory.CreateClient(ClientName)
|
||||
.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
exchange.Answered(response);
|
||||
if (response.StatusCode == HttpStatusCode.NotModified && !string.IsNullOrEmpty(etag))
|
||||
@@ -412,7 +396,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
}
|
||||
|
||||
public bool FailedTemporarily(string url) =>
|
||||
Uri.TryCreate(url, UriKind.Absolute, out var target) && _cache.TryGetValue(NegativeKey(target), out Refusal refusal) && refusal.Transient;
|
||||
Uri.TryCreate(url, UriKind.Absolute, out var target) && cache.TryGetValue(NegativeKey(target), out Refusal refusal) && refusal.Transient;
|
||||
|
||||
public async Task<(byte[] Bytes, string ContentType)> GetMedia(string url, long maxBytes, CancellationToken token)
|
||||
{
|
||||
@@ -460,7 +444,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, target);
|
||||
request.Headers.Accept.ParseAdd("image/*, video/*, audio/*");
|
||||
using var response = await _httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
using var response = await httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
exchange.Answered(response);
|
||||
if (IsRedirect(response.StatusCode))
|
||||
{
|
||||
@@ -507,7 +491,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
catch (Exception ex) when (ex is HttpRequestException or BlockedDestinationException or OperationCanceledException && !token.IsCancellationRequested)
|
||||
{
|
||||
exchange.Failed(ex switch { BlockedDestinationException => "private-address", OperationCanceledException => "timeout", _ => "network" });
|
||||
_logger.LogInformation("Media {Url} refused: {Reason}", url, ex.Message);
|
||||
logger.LogInformation("Media {Url} refused: {Reason}", url, ex.Message);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
@@ -524,7 +508,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
{
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, target);
|
||||
request.Headers.Accept.ParseAdd("text/html, application/xhtml+xml");
|
||||
using var response = await _httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
using var response = await httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, timeout.Token);
|
||||
if (IsRedirect(response.StatusCode))
|
||||
{
|
||||
var location = response.Headers.Location;
|
||||
@@ -554,7 +538,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
}
|
||||
catch (Exception ex) when (ex is HttpRequestException or BlockedDestinationException or OperationCanceledException && !token.IsCancellationRequested)
|
||||
{
|
||||
_logger.LogInformation("Page {Url} refused: {Reason}", url, ex.Message);
|
||||
logger.LogInformation("Page {Url} refused: {Reason}", url, ex.Message);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
@@ -590,7 +574,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get, target);
|
||||
request.Headers.Accept.ParseAdd("video/*, audio/*, image/*");
|
||||
request.Headers.Range = range;
|
||||
var response = await _httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
|
||||
var response = await httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
|
||||
exchange.Answered(response);
|
||||
if (IsRedirect(response.StatusCode))
|
||||
{
|
||||
@@ -624,7 +608,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
catch (Exception ex) when (ex is HttpRequestException or BlockedDestinationException or OperationCanceledException && !token.IsCancellationRequested)
|
||||
{
|
||||
exchange.Failed(ex switch { BlockedDestinationException => "private-address", OperationCanceledException => "timeout", _ => "network" });
|
||||
_logger.LogInformation("Media stream {Url} refused: {Reason}", url, ex.Message);
|
||||
logger.LogInformation("Media stream {Url} refused: {Reason}", url, ex.Message);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
@@ -636,7 +620,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
await using var stream = await content.ReadAsStreamAsync(token);
|
||||
var buffer = new byte[limit];
|
||||
var total = 0;
|
||||
int read;
|
||||
var read = default(int);
|
||||
while (total < limit && (read = await stream.ReadAsync(buffer.AsMemory(total, limit - total), token)) > 0)
|
||||
total += read;
|
||||
return buffer[..total];
|
||||
@@ -646,7 +630,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
{
|
||||
if (!IsAllowed(request.RequestUri))
|
||||
throw new BlockedDestinationException(request.RequestUri?.Host);
|
||||
return await _httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
|
||||
return await httpClientFactory.CreateClient(ClientName).SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
|
||||
}
|
||||
|
||||
// copies at most limit bytes: how many, or -1 once there are more
|
||||
@@ -655,7 +639,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
await using var stream = await content.ReadAsStreamAsync(token);
|
||||
var chunk = new byte[64 * 1024];
|
||||
var copied = 0L;
|
||||
int read;
|
||||
var read = default(int);
|
||||
while ((read = await stream.ReadAsync(chunk, token)) > 0)
|
||||
{
|
||||
copied += read;
|
||||
@@ -671,7 +655,7 @@ namespace PrivaPub.Infrastructure.Http
|
||||
await using var stream = await content.ReadAsStreamAsync(token);
|
||||
using var buffer = new MemoryStream();
|
||||
var chunk = new byte[16 * 1024];
|
||||
int read;
|
||||
var read = default(int);
|
||||
while ((read = await stream.ReadAsync(chunk, token)) > 0)
|
||||
{
|
||||
if (buffer.Length + read > limit)
|
||||
@@ -704,8 +688,8 @@ namespace PrivaPub.Infrastructure.Http
|
||||
exchange.Failed(code);
|
||||
else
|
||||
exchange.Refused(code);
|
||||
_cache.Set(negativeKey, new Refusal(transient, exchange.Status ?? 0), NegativeCacheLifetime);
|
||||
_logger.LogInformation("GET {Url} refused: {Reason}", url, reason);
|
||||
cache.Set(negativeKey, new Refusal(transient, exchange.Status ?? 0), NegativeCacheLifetime);
|
||||
logger.LogInformation("GET {Url} refused: {Reason}", url, reason);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,13 +28,10 @@ namespace PrivaPub.Infrastructure.Http
|
||||
return new Restore(previous);
|
||||
}
|
||||
|
||||
sealed class Restore : IDisposable
|
||||
sealed class Restore(
|
||||
Tags previous) : IDisposable
|
||||
{
|
||||
readonly Tags _previous;
|
||||
|
||||
public Restore(Tags previous) => _previous = previous;
|
||||
|
||||
public void Dispose() => current.Value = _previous;
|
||||
public void Dispose() => current.Value = previous;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,11 +23,12 @@ namespace PrivaPub.Infrastructure.Http
|
||||
ConnectTimeout = connectTimeout ?? TimeSpan.FromSeconds(10),
|
||||
PooledConnectionLifetime = TimeSpan.FromMinutes(2),
|
||||
MaxResponseHeadersLength = 64,
|
||||
ConnectCallback = (context, token) => Connect(context.DnsEndPoint, options.AllowPrivateNetworks, token, connected)
|
||||
ConnectCallback = (context, token) => Connect(context.DnsEndPoint, options.AllowPrivateNetworks, connected, token)
|
||||
};
|
||||
|
||||
public static async ValueTask<Stream> Connect(DnsEndPoint endPoint, bool allowPrivateNetworks, CancellationToken token,
|
||||
IConnectedAddresses connected = default)
|
||||
public static async ValueTask<Stream> Connect(DnsEndPoint endPoint, bool allowPrivateNetworks,
|
||||
IConnectedAddresses connected = default,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
var addresses = await Resolve(endPoint.Host, token);
|
||||
if (addresses.Length == 0 || !allowPrivateNetworks && !addresses.All(IpRangeGuard.IsPublic))
|
||||
|
||||
Reference in new issue
Block a user