// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading; using System.Threading.Tasks; namespace Progmasoft.Tazel.Cache; /// /// The HTTP side of a remote cache, as Bazel's HTTP caching protocol lays it out and bazel-remote serves it: action /// results at /ac/<sha256> and file contents at /cas/<sha256>, read with GET and HEAD and written /// with PUT. /// internal sealed class RemoteCache : IDisposable { private readonly HttpClient _http; private readonly string _base; /// /// The cache at . A that starts with Basic or /// Bearer is sent as it is in Authorization; any other is sent as a bearer token. /// public RemoteCache(Uri address, string? token, HttpMessageHandler? handler = null) { ArgumentNullException.ThrowIfNull(address); if (address.Scheme != Uri.UriSchemeHttps && !address.IsLoopback) { throw new ArgumentException($"a remote cache is reached over https only, not {address}", nameof(address)); } Address = address; _base = address.ToString().TrimEnd('/'); _http = handler is null ? new HttpClient() : new HttpClient(handler); _http.Timeout = TimeSpan.FromMinutes(5); _http.DefaultRequestHeaders.UserAgent.ParseAdd($"tazel/{Cli.Commands.TazelVersion.ToString(3)}"); if (token is { Length: > 0 }) { _http.DefaultRequestHeaders.Authorization = token.StartsWith("Basic ", StringComparison.Ordinal) || token.StartsWith("Bearer ", StringComparison.Ordinal) ? AuthenticationHeaderValue.Parse(token) : new AuthenticationHeaderValue("Bearer", token); } } public Uri Address { get; } /// The action result stored under , or null when there is none. public Task GetActionAsync(string key, CancellationToken cancel) => GetAsync($"ac/{key}", cancel); /// Stores as the action result of . public Task PutActionAsync(string key, byte[] message, CancellationToken cancel) => PutAsync($"ac/{key}", new ByteArrayContent(message), cancel); /// Whether the cache holds the content . public async Task HasContentAsync(string hash, CancellationToken cancel) { using var request = new HttpRequestMessage(HttpMethod.Head, $"{_base}/cas/{hash}"); using var response = await _http.SendAsync(request, cancel).ConfigureAwait(false); if (response.StatusCode == HttpStatusCode.NotFound) { return false; } Check(response, "HEAD", $"cas/{hash}"); return true; } /// Writes the content to ; false when there is none. public async Task GetContentAsync(string hash, Stream destination, CancellationToken cancel) { ArgumentNullException.ThrowIfNull(destination); using var response = await _http.GetAsync($"{_base}/cas/{hash}", HttpCompletionOption.ResponseHeadersRead, cancel) .ConfigureAwait(false); if (response.StatusCode == HttpStatusCode.NotFound) { return false; } Check(response, "GET", $"cas/{hash}"); await response.Content.CopyToAsync(destination, cancel).ConfigureAwait(false); return true; } /// Stores the file as the content . public async Task PutContentAsync(string hash, string path, CancellationToken cancel) { var stream = File.OpenRead(path); await using (stream.ConfigureAwait(false)) { await PutAsync($"cas/{hash}", new StreamContent(stream), cancel).ConfigureAwait(false); } } /// Stores as the content . public Task PutContentAsync(string hash, byte[] bytes, CancellationToken cancel) => PutAsync($"cas/{hash}", new ByteArrayContent(bytes), cancel); /// The content , or null when there is none; for small contents only. public Task GetContentAsync(string hash, CancellationToken cancel) => GetAsync($"cas/{hash}", cancel); public void Dispose() => _http.Dispose(); private async Task GetAsync(string path, CancellationToken cancel) { using var response = await _http.GetAsync($"{_base}/{path}", cancel).ConfigureAwait(false); if (response.StatusCode == HttpStatusCode.NotFound) { return null; } Check(response, "GET", path); return await response.Content.ReadAsByteArrayAsync(cancel).ConfigureAwait(false); } private async Task PutAsync(string path, HttpContent content, CancellationToken cancel) { using (content) { content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); using var response = await _http.PutAsync($"{_base}/{path}", content, cancel).ConfigureAwait(false); Check(response, "PUT", path); } } private void Check(HttpResponseMessage response, string method, string path) { if (!response.IsSuccessStatusCode) { throw new RemoteCacheException( $"{method} {Address.Host}/{path} answered {(int)response.StatusCode} {response.ReasonPhrase}"); } } } /// A key of the remote cache in MODULE.csx cannot be read. public sealed class CacheKeyException(string message) : Exception(message); /// A remote cache answered with an error or could not be reached. public sealed class RemoteCacheException(string message) : Exception(message);