Skip to content

Commit 9086efa

Browse files
committed
Fix InvalidOperationException when logging relative Uris.
#163
1 parent 1e08492 commit 9086efa

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/KubeClient.Extensions.WebSockets/K8sWebSocket.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
namespace KubeClient.Extensions.WebSockets
2727
{
28+
using KubeClient.Utilities;
29+
2830
/// <summary>
2931
/// Connection factory for Kubernetes web sockets.
3032
/// </summary>
@@ -173,7 +175,7 @@ private static byte[] BuildRequestHeader(Uri uri, K8sWebSocketOptions options, s
173175
{
174176
StringBuilder builder = new StringBuilder()
175177
.Append("GET ")
176-
.Append(uri.PathAndQuery)
178+
.Append(uri.SafeGetPathAndQuery())
177179
.Append(" HTTP/1.1\r\n");
178180

179181
// Add all of the required headers, honoring Host header if set.

src/KubeClient/ResourceClients/KubeResourceClient.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020
namespace KubeClient.ResourceClients
2121
{
22-
using KubeClient.Models.ContractResolvers;
2322
using Models;
23+
using Models.ContractResolvers;
2424
using Models.Converters;
25+
using Utilities;
2526

2627
/// <summary>
2728
/// The base class for Kubernetes resource API clients.
@@ -595,7 +596,7 @@ protected IObservable<string> ObserveLines(Func<HttpRequest> requestFactory, str
595596
{
596597
HttpRequest request = requestFactory();
597598

598-
logger.LogDebug("Start streaming {RequestMethod} request for {RequestUri}...", HttpMethod.Get.Method, request.Uri.PathAndQuery);
599+
logger.LogDebug("Start streaming {RequestMethod} request for {RequestUri}...", HttpMethod.Get.Method, request.Uri.SafeGetPathAndQuery());
599600

600601
using (HttpResponseMessage responseMessage = await Http.GetStreamedAsync(request, subscriptionCancellationToken).ConfigureAwait(false))
601602
{

src/KubeClient/Utilities/UriHelper.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
namespace KubeClient.Utilities
4+
{
5+
/// <summary>
6+
/// Helper methods for working with <see cref="Uri"/>s.
7+
/// </summary>
8+
public static class UriHelper
9+
{
10+
/// <summary>
11+
/// Get the path (and, if present, the query) of a URI.
12+
/// </summary>
13+
/// <param name="uri">
14+
/// The target <see cref="Uri"/>.
15+
/// </param>
16+
/// <returns>
17+
/// The URI's path and query.
18+
/// </returns>
19+
/// <remarks>
20+
/// Unlike <see cref="Uri.PathAndQuery"/>, also handles relative URIs.
21+
/// </remarks>
22+
public static string SafeGetPathAndQuery(this Uri uri)
23+
{
24+
if (uri == null)
25+
throw new ArgumentNullException(nameof(uri));
26+
27+
if (uri.IsAbsoluteUri)
28+
return uri.PathAndQuery;
29+
30+
// Slightly ugly, but System.Uri doesn't attempt to parse relative URIs so we have to resort to System.UriBuilder.
31+
UriBuilder uriComponents = new UriBuilder(uri);
32+
33+
return $"{uriComponents.Path}{uriComponents.Query}";
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)