Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions source/NetCoreServer/Buffer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using System.Text;

Expand Down Expand Up @@ -123,8 +123,10 @@ public void Reserve(long capacity)

if (capacity > Capacity)
{
byte[] data = new byte[Math.Max(capacity, 2 * Capacity)];
Array.Copy(_data, 0, data, 0, _size);
long newCapacity = Math.Max(capacity, Math.Max(256, 2 * Capacity));
byte[] data = new byte[newCapacity];
if (_size > 0)
Array.Copy(_data, 0, data, 0, _size);
_data = data;
}
}
Expand Down Expand Up @@ -173,6 +175,8 @@ public long Append(byte value)
/// <returns>Count of append bytes</returns>
public long Append(byte[] buffer)
{
if (buffer == null || buffer.Length == 0)
return 0;
Reserve(_size + buffer.Length);
Array.Copy(buffer, 0, _data, _size, buffer.Length);
_size += buffer.Length;
Expand All @@ -188,8 +192,10 @@ public long Append(byte[] buffer)
/// <returns>Count of append bytes</returns>
public long Append(byte[] buffer, long offset, long size)
{
if (buffer == null || size <= 0)
return 0;
Reserve(_size + size);
Array.Copy(buffer, offset, _data, _size, size);
Array.Copy(buffer, (int)offset, _data, (int)_size, (int)size);
_size += size;
return size;
}
Expand All @@ -201,6 +207,8 @@ public long Append(byte[] buffer, long offset, long size)
/// <returns>Count of append bytes</returns>
public long Append(ReadOnlySpan<byte> buffer)
{
if (buffer.IsEmpty)
return 0;
Reserve(_size + buffer.Length);
buffer.CopyTo(new Span<byte>(_data, (int)_size, buffer.Length));
_size += buffer.Length;
Expand All @@ -212,7 +220,7 @@ public long Append(ReadOnlySpan<byte> buffer)
/// </summary>
/// <param name="buffer">Buffer to append</param>
/// <returns>Count of append bytes</returns>
public long Append(Buffer buffer) => Append(buffer.AsSpan());
public long Append(Buffer buffer) => buffer != null ? Append(buffer.AsSpan()) : 0;

/// <summary>
/// Append the given text in UTF-8 encoding
Expand All @@ -221,6 +229,8 @@ public long Append(ReadOnlySpan<byte> buffer)
/// <returns>Count of append bytes</returns>
public long Append(string text)
{
if (string.IsNullOrEmpty(text))
return 0;
int length = Encoding.UTF8.GetMaxByteCount(text.Length);
Reserve(_size + length);
long result = Encoding.UTF8.GetBytes(text, 0, text.Length, _data, (int)_size);
Expand All @@ -235,6 +245,8 @@ public long Append(string text)
/// <returns>Count of append bytes</returns>
public long Append(ReadOnlySpan<char> text)
{
if (text.IsEmpty)
return 0;
int length = Encoding.UTF8.GetMaxByteCount(text.Length);
Reserve(_size + length);
long result = Encoding.UTF8.GetBytes(text, new Span<byte>(_data, (int)_size, length));
Expand Down
12 changes: 10 additions & 2 deletions source/NetCoreServer/HttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
Expand Down Expand Up @@ -87,7 +87,15 @@ public HttpRequest(string method, string url, string protocol = "HTTP/1.1")
/// <summary>
/// Get the HTTP request body as byte array
/// </summary>
public byte[] BodyBytes { get { return _cache.Data[_bodyIndex..(_bodyIndex + _bodySize)]; } }
public byte[] BodyBytes
{
get
{
var result = new byte[_bodySize];
Array.Copy(_cache.Data, _bodyIndex, result, 0, _bodySize);
return result;
}
}
/// <summary>
/// Get the HTTP request body as byte span
/// </summary>
Expand Down
12 changes: 10 additions & 2 deletions source/NetCoreServer/HttpResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
Expand Down Expand Up @@ -156,7 +156,15 @@ public HttpResponse(int status, string statusPhrase, string protocol)
/// <summary>
/// Get the HTTP request body as byte array
/// </summary>
public byte[] BodyBytes { get { return _cache.Data[_bodyIndex..(_bodyIndex + _bodySize)]; } }
public byte[] BodyBytes
{
get
{
var result = new byte[_bodySize];
Array.Copy(_cache.Data, _bodyIndex, result, 0, _bodySize);
return result;
}
}
/// <summary>
/// Get the HTTP request body as read-only byte span
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Version>10.0.0.0</Version>
<Version>10.0.1.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2026 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
Loading