Skip to content

Commit

Permalink
MakeErrorResponse() method refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chronoxor committed Feb 22, 2021
1 parent 58d6b49 commit f2190ff
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 21 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: "5.0.11.{build}"
version: "5.0.12.{build}"

# Image to use
image: Visual Studio 2019
Expand Down
4 changes: 2 additions & 2 deletions examples/HttpServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected override void OnReceivedRequest(HttpRequest request)
SendResponseAsync(Response.MakeGetResponse(value));
}
else
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + key, 404));
SendResponseAsync(Response.MakeErrorResponse(404, "Required cache value was not found for the key: " + key));
}
else if ((request.Method == "POST") || (request.Method == "PUT"))
{
Expand Down Expand Up @@ -117,7 +117,7 @@ protected override void OnReceivedRequest(HttpRequest request)
SendResponseAsync(Response.MakeGetResponse(value));
}
else
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + key, 404));
SendResponseAsync(Response.MakeErrorResponse(404, "Deleted cache value was not found for the key: " + key));
}
else if (request.Method == "OPTIONS")
SendResponseAsync(Response.MakeOptionsResponse());
Expand Down
4 changes: 2 additions & 2 deletions examples/HttpsServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected override void OnReceivedRequest(HttpRequest request)
SendResponseAsync(Response.MakeGetResponse(value));
}
else
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + key, 404));
SendResponseAsync(Response.MakeErrorResponse(404, "Required cache value was not found for the key: " + key));
}
else if ((request.Method == "POST") || (request.Method == "PUT"))
{
Expand Down Expand Up @@ -119,7 +119,7 @@ protected override void OnReceivedRequest(HttpRequest request)
SendResponseAsync(Response.MakeGetResponse(value));
}
else
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + key, 404));
SendResponseAsync(Response.MakeErrorResponse(404, "Deleted cache value was not found for the key: " + key));
}
else if (request.Method == "OPTIONS")
SendResponseAsync(Response.MakeOptionsResponse());
Expand Down
25 changes: 19 additions & 6 deletions source/NetCoreServer/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,13 +565,26 @@ public HttpResponse MakeOkResponse(int status = 200)
/// <summary>
/// Make ERROR response
/// </summary>
/// <param name="error">Error content (default is "")</param>
/// <param name="status">OK status (default is 200 (OK))</param>
public HttpResponse MakeErrorResponse(string error = "", int status = 500)
/// <param name="content">Error content (default is "")</param>
/// <param name="contentType">Error content type (default is "text/plain; charset=UTF-8")</param>
public HttpResponse MakeErrorResponse(string content = "", string contentType = "text/plain; charset=UTF-8")
{
return MakeErrorResponse(500, content, contentType);
}

/// <summary>
/// Make ERROR response
/// </summary>
/// <param name="status">Error status</param>
/// <param name="content">Error content (default is "")</param>
/// <param name="contentType">Error content type (default is "text/plain; charset=UTF-8")</param>
public HttpResponse MakeErrorResponse(int status, string content = "", string contentType = "text/plain; charset=UTF-8")
{
Clear();
SetBegin(status);
SetBody(error);
if (!string.IsNullOrEmpty(contentType))
SetHeader("Content-Type", contentType);
SetBody(content);
return this;
}

Expand All @@ -589,7 +602,7 @@ public HttpResponse MakeHeadResponse()
/// <summary>
/// Make GET response
/// </summary>
/// <param name="content">String content</param>
/// <param name="content">String content (default is "")</param>
/// <param name="contentType">Content type (default is "text/plain; charset=UTF-8")</param>
public HttpResponse MakeGetResponse(string content = "", string contentType = "text/plain; charset=UTF-8")
{
Expand Down Expand Up @@ -900,7 +913,7 @@ internal bool ReceiveBody(byte[] buffer, int offset, int size)
return true;
}
}
else
else
{
// Check the body content to find the response body end
if (_bodySize >= 4)
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>net5.0</TargetFramework>
<Version>5.0.11.0</Version>
<Version>5.0.12.0</Version>
<Authors>Ivan Shynkarenka</Authors>
<Copyright>Copyright (c) 2019-2021 Ivan Shynkarenka</Copyright>
<RepositoryUrl>https://github.com/chronoxor/NetCoreServer</RepositoryUrl>
Expand Down
10 changes: 5 additions & 5 deletions source/NetCoreServer/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public bool PerformServerUpgrade(HttpRequest request, HttpResponse response)
if ((string.Compare(value, "Upgrade", StringComparison.OrdinalIgnoreCase) != 0) && (string.Compare(value, "keep-alive, Upgrade", StringComparison.OrdinalIgnoreCase) != 0))
{
error = true;
response.MakeErrorResponse("Invalid WebSocket handshaked request: 'Connection' header value must be 'Upgrade' or 'keep-alive, Upgrade'", 400);
response.MakeErrorResponse(400, "Invalid WebSocket handshaked request: 'Connection' header value must be 'Upgrade' or 'keep-alive, Upgrade'");
break;
}

Expand All @@ -167,7 +167,7 @@ public bool PerformServerUpgrade(HttpRequest request, HttpResponse response)
if (string.Compare(value, "websocket", StringComparison.OrdinalIgnoreCase) != 0)
{
error = true;
response.MakeErrorResponse("Invalid WebSocket handshaked request: 'Upgrade' header value must be 'websocket'", 400);
response.MakeErrorResponse(400, "Invalid WebSocket handshaked request: 'Upgrade' header value must be 'websocket'");
break;
}

Expand All @@ -178,7 +178,7 @@ public bool PerformServerUpgrade(HttpRequest request, HttpResponse response)
if (string.IsNullOrEmpty(value))
{
error = true;
response.MakeErrorResponse("Invalid WebSocket handshaked request: 'Sec-WebSocket-Key' header value must be non empty", 400);
response.MakeErrorResponse(400, "Invalid WebSocket handshaked request: 'Sec-WebSocket-Key' header value must be non empty");
break;
}

Expand All @@ -199,7 +199,7 @@ public bool PerformServerUpgrade(HttpRequest request, HttpResponse response)
if (string.Compare(value, "13", StringComparison.OrdinalIgnoreCase) != 0)
{
error = true;
response.MakeErrorResponse("Invalid WebSocket handshaked request: 'Sec-WebSocket-Version' header value must be '13'", 400);
response.MakeErrorResponse(400, "Invalid WebSocket handshaked request: 'Sec-WebSocket-Version' header value must be '13'");
break;
}

Expand All @@ -215,7 +215,7 @@ public bool PerformServerUpgrade(HttpRequest request, HttpResponse response)
if (!connection || !upgrade || !wsKey || !wsVersion)
{
if (!error)
response.MakeErrorResponse("Invalid WebSocket response", 400);
response.MakeErrorResponse(400, "Invalid WebSocket response");
_wsHandler.SendUpgrade(response);
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/HttpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected override void OnReceivedRequest(HttpRequest request)
SendResponseAsync(Response.MakeGetResponse(value));
}
else
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + key, 404));
SendResponseAsync(Response.MakeErrorResponse(404, "Required cache value was not found for the key: " + key));
}
else if ((request.Method == "POST") || (request.Method == "PUT"))
{
Expand Down Expand Up @@ -116,7 +116,7 @@ protected override void OnReceivedRequest(HttpRequest request)
SendResponseAsync(Response.MakeGetResponse(value));
}
else
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + key, 404));
SendResponseAsync(Response.MakeErrorResponse(404, "Deleted cache value was not found for the key: " + key));
}
else if (request.Method == "OPTIONS")
SendResponseAsync(Response.MakeOptionsResponse());
Expand Down
4 changes: 2 additions & 2 deletions tests/HttpsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected override void OnReceivedRequest(HttpRequest request)
SendResponseAsync(Response.MakeGetResponse(value));
}
else
SendResponseAsync(Response.MakeErrorResponse("Required cache value was not found for the key: " + key, 404));
SendResponseAsync(Response.MakeErrorResponse(404, "Required cache value was not found for the key: " + key));
}
else if ((request.Method == "POST") || (request.Method == "PUT"))
{
Expand Down Expand Up @@ -73,7 +73,7 @@ protected override void OnReceivedRequest(HttpRequest request)
SendResponseAsync(Response.MakeGetResponse(value));
}
else
SendResponseAsync(Response.MakeErrorResponse("Deleted cache value was not found for the key: " + key, 404));
SendResponseAsync(Response.MakeErrorResponse(404, "Deleted cache value was not found for the key: " + key));
}
else if (request.Method == "OPTIONS")
SendResponseAsync(Response.MakeOptionsResponse());
Expand Down

0 comments on commit f2190ff

Please sign in to comment.