Skip to content

Commit e61e019

Browse files
committed
Fix incorrect body length if has Chinese characters in the content.
#47
1 parent a0349d4 commit e61e019

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Specify version format
2-
version: "3.0.4.{build}"
2+
version: "3.0.5.{build}"
33

44
# Image to use
55
image: Visual Studio 2019

source/NetCoreServer/HttpRequest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,10 @@ public HttpRequest AddCookie(string name, string value)
239239
/// <param name="body">Body string content (default is "")</param>
240240
public HttpRequest SetBody(string body = "")
241241
{
242+
int length = string.IsNullOrEmpty(body) ? 0 : Encoding.UTF8.GetByteCount(body);
243+
242244
// Append content length header
243-
SetHeader("Content-Length", body.Length.ToString());
245+
SetHeader("Content-Length", length.ToString());
244246

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

@@ -249,8 +251,8 @@ public HttpRequest SetBody(string body = "")
249251
// Append the HTTP request body
250252
_cache.Append(body);
251253
_bodyIndex = index;
252-
_bodySize = body.Length;
253-
_bodyLength = body.Length;
254+
_bodySize = length;
255+
_bodyLength = length;
254256
_bodyLengthProvided = true;
255257
return this;
256258
}

source/NetCoreServer/HttpResponse.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,10 @@ public HttpResponse SetCookie(string name, string value, int maxAge = 86400, str
459459
/// <param name="body">Body string content (default is "")</param>
460460
public HttpResponse SetBody(string body = "")
461461
{
462+
int length = string.IsNullOrEmpty(body) ? 0 : Encoding.UTF8.GetByteCount(body);
463+
462464
// Append content length header
463-
SetHeader("Content-Length", body.Length.ToString());
465+
SetHeader("Content-Length", length.ToString());
464466

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

@@ -469,8 +471,8 @@ public HttpResponse SetBody(string body = "")
469471
// Append the HTTP response body
470472
_cache.Append(body);
471473
_bodyIndex = index;
472-
_bodySize = body.Length;
473-
_bodyLength = body.Length;
474+
_bodySize = length;
475+
_bodyLength = length;
474476
_bodyLengthProvided = true;
475477
return this;
476478
}

source/NetCoreServer/NetCoreServer.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
5-
<Version>3.0.4</Version>
5+
<Version>3.0.5</Version>
66
<Authors>Ivan Shynkarenka</Authors>
7-
<Copyright>Copyright (c) 2019 Ivan Shynkarenka</Copyright>
7+
<Copyright>Copyright (c) 2019-2020 Ivan Shynkarenka</Copyright>
88
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
99
<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>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>

0 commit comments

Comments
 (0)