From ff0d538d250956ce31f2e9bcc01fa827c5e812a5 Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Mon, 11 Dec 2023 18:20:57 -0800 Subject: [PATCH] Escl: Only use the path from the job URI We want to use the same host etc. as all other requests, particularly for IPv6 link-local addresses which need the network interface. --- NAPS2.Escl/Client/EsclClient.cs | 29 ++++++++++++------- NAPS2.Escl/EsclJob.cs | 2 +- .../Scan/Internal/Escl/EsclScanDriver.cs | 2 +- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/NAPS2.Escl/Client/EsclClient.cs b/NAPS2.Escl/Client/EsclClient.cs index 941f24be70..03441a3d13 100644 --- a/NAPS2.Escl/Client/EsclClient.cs +++ b/NAPS2.Escl/Client/EsclClient.cs @@ -88,15 +88,16 @@ public async Task CreateScanJob(EsclScanSettings settings) // new XElement(ScanNs + "Threshold", settings.Threshold), new XElement(PwgNs + "DocumentFormat", settings.DocumentFormat))); var content = new StringContent(doc, Encoding.UTF8, "text/xml"); - var url = GetUrl("ScanJobs"); + var url = GetUrl($"/{_service.RootUrl}/ScanJobs"); Logger.LogDebug("ESCL POST {Url}", url); Logger.LogDebug("{Doc}", doc); var response = await HttpClient.PostAsync(url, content); response.EnsureSuccessStatusCode(); Logger.LogDebug("POST OK"); + return new EsclJob { - Uri = response.Headers.Location! + UriPath = response.Headers.Location!.AbsolutePath }; } @@ -104,7 +105,7 @@ public async Task CreateScanJob(EsclScanSettings settings) { if (pageProgress != null) { - var progressUrl = job.Uri + "/Progress"; + var progressUrl = GetUrl($"{job.UriPath}/Progress"); var progressResponse = await ProgressHttpClient.GetStreamAsync(progressUrl); var streamReader = new StreamReader(progressResponse); _ = Task.Run(async () => @@ -119,7 +120,7 @@ public async Task CreateScanJob(EsclScanSettings settings) }); } // TODO: Maybe check Content-Location on the response header to ensure no duplicate document? - var url = job.Uri + "/NextDocument"; + var url = GetUrl($"{job.UriPath}/NextDocument");; Logger.LogDebug("ESCL GET {Url}", url); var response = await DocumentHttpClient.GetAsync(url); if (response.StatusCode is HttpStatusCode.NotFound or HttpStatusCode.Gone) @@ -141,7 +142,7 @@ public async Task CreateScanJob(EsclScanSettings settings) public async Task ErrorDetails(EsclJob job) { - var url = job.Uri + "/ErrorDetails"; + var url = GetUrl($"{job.UriPath}/ErrorDetails");; Logger.LogDebug("ESCL GET {Url}", url); var response = await HttpClient.GetAsync(url); response.EnsureSuccessStatusCode(); @@ -150,8 +151,9 @@ public async Task ErrorDetails(EsclJob job) public async Task CancelJob(EsclJob job) { - Logger.LogDebug("ESCL DELETE {Url}", job.Uri); - var response = await HttpClient.DeleteAsync(job.Uri); + var url = GetUrl(job.UriPath); + Logger.LogDebug("ESCL DELETE {Url}", url); + var response = await HttpClient.DeleteAsync(url); if (!response.IsSuccessStatusCode) { Logger.LogDebug("DELETE failed: {Status}", response.StatusCode); @@ -164,7 +166,7 @@ public async Task CancelJob(EsclJob job) private async Task DoRequest(string endpoint) { // TODO: Retry logic - var url = GetUrl(endpoint); + var url = GetUrl($"/{_service.RootUrl}/{endpoint}"); Logger.LogDebug("ESCL GET {Url}", url); var response = await HttpClient.GetAsync(url, CancelToken); response.EnsureSuccessStatusCode(); @@ -177,14 +179,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()}{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 diff --git a/NAPS2.Escl/EsclJob.cs b/NAPS2.Escl/EsclJob.cs index cecc3c5cfc..13743300a6 100644 --- a/NAPS2.Escl/EsclJob.cs +++ b/NAPS2.Escl/EsclJob.cs @@ -2,5 +2,5 @@ namespace NAPS2.Escl; public class EsclJob { - public required Uri Uri { get; init; } + public required string UriPath { get; init; } } \ No newline at end of file diff --git a/NAPS2.Sdk/Scan/Internal/Escl/EsclScanDriver.cs b/NAPS2.Sdk/Scan/Internal/Escl/EsclScanDriver.cs index bf796ff884..12b33e1940 100644 --- a/NAPS2.Sdk/Scan/Internal/Escl/EsclScanDriver.cs +++ b/NAPS2.Sdk/Scan/Internal/Escl/EsclScanDriver.cs @@ -152,7 +152,7 @@ public async Task Scan(ScanOptions options, CancellationToken cancelToken, IScan throw; } var status = await client.GetStatus(); - var jobState = status.JobStates.Get(job.Uri.AbsolutePath); + var jobState = status.JobStates.Get(job.UriPath); if (jobState is not (EsclJobState.Pending or EsclJobState.Processing or EsclJobState.Unknown)) { // Only retry if the job is pending or processing