From 1a31ded1c1fe7d079c1c10f2c7673cd79d278779 Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Mon, 11 Dec 2023 17:58:18 -0800 Subject: [PATCH] Escl: Replace host in job URIs --- NAPS2.Escl/Client/EsclClient.cs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/NAPS2.Escl/Client/EsclClient.cs b/NAPS2.Escl/Client/EsclClient.cs index 941f24be70..a976008a20 100644 --- a/NAPS2.Escl/Client/EsclClient.cs +++ b/NAPS2.Escl/Client/EsclClient.cs @@ -94,9 +94,18 @@ public async Task CreateScanJob(EsclScanSettings settings) var response = await HttpClient.PostAsync(url, content); response.EnsureSuccessStatusCode(); Logger.LogDebug("POST OK"); + + // The job URI might not have our preferred ip/host; in particular, for IPv6 link-local addresses, it won't + // include the network interface. + var hostAndPort = GetHostAndPort(); + var uriBuilder = new UriBuilder(response.Headers.Location!) + { + Host = hostAndPort.Substring(0, hostAndPort.IndexOf(":", StringComparison.InvariantCulture)) + }; + return new EsclJob { - Uri = response.Headers.Location! + Uri = uriBuilder.Uri }; } @@ -177,14 +186,19 @@ private async Task DoRequest(string endpoint) private string GetUrl(string endpoint) { var protocol = _service.Tls || _service.Port == 443 ? "https" : "http"; - var ipAndPort = new IPEndPoint(_service.RemoteEndpoint, _service.Port).ToString(); + return $"{protocol}://{GetHostAndPort()}/{_service.RootUrl}/{endpoint}"; + } + + private string GetHostAndPort() + { + var host = new IPEndPoint(_service.RemoteEndpoint, _service.Port).ToString(); #if NET6_0_OR_GREATER if (OperatingSystem.IsMacOS()) { // Using the mDNS hostname is more reliable on Mac (but doesn't work at all on Windows) - ipAndPort = $"{_service.Host}:{_service.Port}"; + host = $"{_service.Host}:{_service.Port}"; } #endif - return $"{protocol}://{ipAndPort}/{_service.RootUrl}/{endpoint}"; + return host; } } \ No newline at end of file