Skip to content

Commit

Permalink
Avoid roundtrip to string by serializing directly to UTF-8 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Mar 7, 2024
1 parent a53a4a1 commit 8ed97ce
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
7 changes: 2 additions & 5 deletions src/Docker.DotNet/Endpoints/ImageOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -307,9 +306,7 @@ private Dictionary<string, string> RegistryAuthHeaders(AuthConfig authConfig)
{
{
RegistryAuthHeaderKey,
Convert.ToBase64String(
Encoding.UTF8.GetBytes(
this._client.JsonSerializer.SerializeObject(authConfig ?? new AuthConfig())))
Convert.ToBase64String(this._client.JsonSerializer.SerializeObject(authConfig ?? new AuthConfig()))
.Replace("/", "_").Replace("+", "-")
// This is not documented in Docker API but from source code (https://github.com/docker/docker-ce/blob/10e40bd1548f69354a803a15fde1b672cc024b91/components/cli/cli/command/registry.go#L47)
// and from multiple internet sources it has to be base64-url-safe.
Expand All @@ -325,7 +322,7 @@ private Dictionary<string, string> RegistryConfigHeaders(IEnumerable<AuthConfig>
{
{
RegistryConfigHeaderKey,
Convert.ToBase64String(Encoding.UTF8.GetBytes(this._client.JsonSerializer.SerializeObject(configDictionary)))
Convert.ToBase64String(this._client.JsonSerializer.SerializeObject(configDictionary))
}
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/Docker.DotNet/Endpoints/SwarmOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Docker.DotNet
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Models;
Expand Down Expand Up @@ -226,7 +225,7 @@ private IDictionary<string, string> RegistryAuthHeaders(AuthConfig authConfig)
{
{
"X-Registry-Auth",
Convert.ToBase64String(Encoding.UTF8.GetBytes(this._client.JsonSerializer.SerializeObject(authConfig)))
Convert.ToBase64String(this._client.JsonSerializer.SerializeObject(authConfig))
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/Docker.DotNet/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public T DeserializeObject<T>(string json)
return System.Text.Json.JsonSerializer.Deserialize<T>(json, _options);
}

public string SerializeObject<T>(T value)
public byte[] SerializeObject<T>(T value)
{
return System.Text.Json.JsonSerializer.Serialize(value, _options);
return System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(value, _options);
}

public JsonContent GetHttpContent<T>(T value)
Expand Down

0 comments on commit 8ed97ce

Please sign in to comment.