Skip to content

Commit 3b28ebb

Browse files
committed
Escl: Replace host in job URIs
1 parent 0ea4f32 commit 3b28ebb

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

NAPS2.Escl/Client/EsclClient.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,18 @@ public async Task<EsclJob> CreateScanJob(EsclScanSettings settings)
9494
var response = await HttpClient.PostAsync(url, content);
9595
response.EnsureSuccessStatusCode();
9696
Logger.LogDebug("POST OK");
97+
98+
// The job URI might not have our preferred ip/host; in particular, for IPv6 link-local addresses, it won't
99+
// include the network interface.
100+
var hostAndPort = GetHostAndPort();
101+
var uriBuilder = new UriBuilder(response.Headers.Location!)
102+
{
103+
Host = hostAndPort.Substring(0, hostAndPort.LastIndexOf(":", StringComparison.InvariantCulture))
104+
};
105+
97106
return new EsclJob
98107
{
99-
Uri = response.Headers.Location!
108+
Uri = uriBuilder.Uri
100109
};
101110
}
102111

@@ -177,14 +186,19 @@ private async Task<XDocument> DoRequest(string endpoint)
177186
private string GetUrl(string endpoint)
178187
{
179188
var protocol = _service.Tls || _service.Port == 443 ? "https" : "http";
180-
var ipAndPort = new IPEndPoint(_service.RemoteEndpoint, _service.Port).ToString();
189+
return $"{protocol}://{GetHostAndPort()}/{_service.RootUrl}/{endpoint}";
190+
}
191+
192+
private string GetHostAndPort()
193+
{
194+
var host = new IPEndPoint(_service.RemoteEndpoint, _service.Port).ToString();
181195
#if NET6_0_OR_GREATER
182196
if (OperatingSystem.IsMacOS())
183197
{
184198
// Using the mDNS hostname is more reliable on Mac (but doesn't work at all on Windows)
185-
ipAndPort = $"{_service.Host}:{_service.Port}";
199+
host = $"{_service.Host}:{_service.Port}";
186200
}
187201
#endif
188-
return $"{protocol}://{ipAndPort}/{_service.RootUrl}/{endpoint}";
202+
return host;
189203
}
190204
}

0 commit comments

Comments
 (0)