Skip to content

Commit

Permalink
Escl: Replace host in job URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Dec 12, 2023
1 parent 0ea4f32 commit 1a31ded
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions NAPS2.Escl/Client/EsclClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,18 @@ public async Task<EsclJob> 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
};
}

Expand Down Expand Up @@ -177,14 +186,19 @@ private async Task<XDocument> 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;
}
}

0 comments on commit 1a31ded

Please sign in to comment.