Skip to content

Commit

Permalink
Fix incorrect body length if has Chinese characters in the content.
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Mar 7, 2020
1 parent a0349d4 commit e61e019
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Specify version format
version: "3.0.4.{build}"
version: "3.0.5.{build}"

# Image to use
image: Visual Studio 2019
Expand Down
8 changes: 5 additions & 3 deletions source/NetCoreServer/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,10 @@ public HttpRequest AddCookie(string name, string value)
/// <param name="body">Body string content (default is "")</param>
public HttpRequest SetBody(string body = "")
{
int length = string.IsNullOrEmpty(body) ? 0 : Encoding.UTF8.GetByteCount(body);

// Append content length header
SetHeader("Content-Length", body.Length.ToString());
SetHeader("Content-Length", length.ToString());

_cache.Append("\r\n");

Expand All @@ -249,8 +251,8 @@ public HttpRequest SetBody(string body = "")
// Append the HTTP request body
_cache.Append(body);
_bodyIndex = index;
_bodySize = body.Length;
_bodyLength = body.Length;
_bodySize = length;
_bodyLength = length;
_bodyLengthProvided = true;
return this;
}
Expand Down
8 changes: 5 additions & 3 deletions source/NetCoreServer/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,10 @@ public HttpResponse SetCookie(string name, string value, int maxAge = 86400, str
/// <param name="body">Body string content (default is "")</param>
public HttpResponse SetBody(string body = "")
{
int length = string.IsNullOrEmpty(body) ? 0 : Encoding.UTF8.GetByteCount(body);

// Append content length header
SetHeader("Content-Length", body.Length.ToString());
SetHeader("Content-Length", length.ToString());

_cache.Append("\r\n");

Expand All @@ -469,8 +471,8 @@ public HttpResponse SetBody(string body = "")
// Append the HTTP response body
_cache.Append(body);
_bodyIndex = index;
_bodySize = body.Length;
_bodyLength = body.Length;
_bodySize = length;
_bodyLength = length;
_bodyLengthProvided = true;
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions source/NetCoreServer/NetCoreServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Version>3.0.4</Version>
<Version>3.0.5</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019 Ivan Shynkarenka</Copyright>
<Copyright>Copyright (c) 2019-2020 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
<Description>Ultra fast and low latency asynchronous socket server &amp; client C# .NET Core library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down

0 comments on commit e61e019

Please sign in to comment.