mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-10-27 00:21:06 -04:00
refactor(data): share server URL validation
The drawing library client needs the same base-address rule as the nest repository. Extract it into one internal helper so both clients accept and reject exactly the same addresses. Behavior is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7a5c06bc99
commit
0ee5b9d93e
2 files changed
+22
-8
No files matched your search
@@ -38,14 +38,7 @@ public sealed class RemoteNestRepository : INestRepository, IDisposable
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_ownsClient = false;
|
||||
var url = (baseUrl ?? "").Trim().TrimEnd('/');
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out var baseUri)
|
||||
|| (baseUri.Scheme != Uri.UriSchemeHttp && baseUri.Scheme != Uri.UriSchemeHttps))
|
||||
{
|
||||
throw new ArgumentException($"Invalid nest server URL: '{baseUrl}'", nameof(baseUrl));
|
||||
}
|
||||
// Keep the trailing slash so relative combines replace no path segment.
|
||||
_baseUri = new Uri(baseUri.AbsoluteUri.TrimEnd('/') + "/");
|
||||
_baseUri = ServerUrl.ParseBase(baseUrl, nameof(baseUrl));
|
||||
}
|
||||
|
||||
private Uri Url(string relative) => new(_baseUri, relative);
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace OpenNest.Data;
|
||||
|
||||
/// <summary>Base-address validation shared by the nest server's HTTP clients.</summary>
|
||||
internal static class ServerUrl
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the absolute http(s) base address with exactly one trailing slash, so
|
||||
/// relative combines replace no path segment. Throws for any other address.
|
||||
/// </summary>
|
||||
public static Uri ParseBase(string? baseUrl, string paramName)
|
||||
{
|
||||
var url = (baseUrl ?? "").Trim().TrimEnd('/');
|
||||
if (!Uri.TryCreate(url, UriKind.Absolute, out var baseUri)
|
||||
|| (baseUri.Scheme != Uri.UriSchemeHttp && baseUri.Scheme != Uri.UriSchemeHttps))
|
||||
{
|
||||
throw new ArgumentException($"Invalid nest server URL: '{baseUrl}'", paramName);
|
||||
}
|
||||
|
||||
return new Uri(baseUri.AbsoluteUri.TrimEnd('/') + "/");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user