Skip to content

Commit

Permalink
making HeaderWriter internal again
Browse files Browse the repository at this point in the history
  • Loading branch information
SiuTung08 committed Nov 12, 2024
1 parent 227d475 commit ffa37d7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Buffers;
using System.Text;
using NATS.Client.Core.Commands;
using NATS.Client.Core.Internal;

namespace NATS.Client.Core;
namespace NATS.Client.Core.Internal;

public class HeaderWriter
internal class HeaderWriter
{
private const byte ByteCr = (byte)'\r';
private const byte ByteLf = (byte)'\n';
Expand All @@ -21,7 +20,7 @@ public class HeaderWriter

private static ReadOnlySpan<byte> ColonSpace => new[] { ByteColon, ByteSpace };

public long GetBytesLength(NatsHeaders headers)
internal static long GetBytesLength(NatsHeaders headers, Encoding encoding)
{
var len = CommandConstants.NatsHeaders10NewLine.Length;
foreach (var kv in headers)
Expand All @@ -31,14 +30,14 @@ public long GetBytesLength(NatsHeaders headers)
if (value != null)
{
// key length
var keyLength = _encoding.GetByteCount(kv.Key);
var keyLength = encoding.GetByteCount(kv.Key);
len += keyLength;

// colon space length
len += ColonSpace.Length;

// value length
var valueLength = _encoding.GetByteCount(value);
var valueLength = encoding.GetByteCount(value);
len += valueLength;

// CrLf length
Expand Down Expand Up @@ -68,10 +67,7 @@ internal long Write(IBufferWriter<byte> bufferWriter, NatsHeaders headers)
var keySpan = bufferWriter.GetSpan(keyLength);
_encoding.GetBytes(kv.Key, keySpan);
if (!ValidateKey(keySpan.Slice(0, keyLength)))
{
throw new NatsException(
$"Invalid header key '{kv.Key}': contains colon, space, or other non-printable ASCII characters");
}
throw new NatsException($"Invalid header key '{kv.Key}': contains colon, space, or other non-printable ASCII characters");

bufferWriter.Advance(keyLength);
len += keyLength;
Expand Down
14 changes: 10 additions & 4 deletions src/NATS.Client.Core/NatsHeaders.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Collections;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Microsoft.Extensions.Primitives;
using NATS.Client.Core.Internal;

namespace NATS.Client.Core;

Expand Down Expand Up @@ -297,11 +299,15 @@ public void CopyTo(KeyValuePair<string, StringValues>[] array, int arrayIndex)
/// <summary>
/// Returns the bytes length of the header
/// </summary>
/// <param name="headerWriter">an instance of headerWriter</param>
/// <returns>bytes length of th header</returns>
public long GetBytesLength(HeaderWriter headerWriter)
/// <param name="encoding">Encoding used. Default to utf8 if not provided</param>
/// <returns>Bytes length of the header</returns>
public long GetBytesLength(Encoding? encoding = null)
{
return headerWriter.GetBytesLength(this);
// if null set to utf-8
encoding = encoding ?? Encoding.UTF8;

var len = HeaderWriter.GetBytesLength(this, encoding);
return len;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions tests/NATS.Client.CoreUnit.Tests/NatsHeaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ public void GetBytesLengthTest()
["a-long-header-key"] = "value",
["key"] = "a-long-header-value",
};
var writer = new HeaderWriter(Encoding.UTF8);
var bytesLength = headers.GetBytesLength(writer);
var encoding = Encoding.UTF8;
var bytesLength = headers.GetBytesLength(encoding);

var text = "NATS/1.0\r\nk1: v1\r\nk2: v2-0\r\nk2: v2-1\r\na-long-header-key: value\r\nkey: a-long-header-value\r\n\r\n";
var expected = new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes(text));
Expand Down

0 comments on commit ffa37d7

Please sign in to comment.