diff --git a/.appveyor.yml b/.appveyor.yml index 2f422dcb..5475bca7 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -1,5 +1,5 @@ # Specify version format -version: "5.0.11.{build}" +version: "5.0.12.{build}" # Image to use image: Visual Studio 2019 diff --git a/examples/HttpServer/Program.cs b/examples/HttpServer/Program.cs index aba310b0..279dcbb6 100644 --- a/examples/HttpServer/Program.cs +++ b/examples/HttpServer/Program.cs @@ -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")) { @@ -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()); diff --git a/examples/HttpsServer/Program.cs b/examples/HttpsServer/Program.cs index 250d40c1..88e713d8 100644 --- a/examples/HttpsServer/Program.cs +++ b/examples/HttpsServer/Program.cs @@ -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")) { @@ -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()); diff --git a/source/NetCoreServer/HttpResponse.cs b/source/NetCoreServer/HttpResponse.cs index 905b92bf..a2e3719a 100644 --- a/source/NetCoreServer/HttpResponse.cs +++ b/source/NetCoreServer/HttpResponse.cs @@ -565,13 +565,26 @@ public HttpResponse MakeOkResponse(int status = 200) /// /// Make ERROR response /// - /// Error content (default is "") - /// OK status (default is 200 (OK)) - public HttpResponse MakeErrorResponse(string error = "", int status = 500) + /// Error content (default is "") + /// Error content type (default is "text/plain; charset=UTF-8") + public HttpResponse MakeErrorResponse(string content = "", string contentType = "text/plain; charset=UTF-8") + { + return MakeErrorResponse(500, content, contentType); + } + + /// + /// Make ERROR response + /// + /// Error status + /// Error content (default is "") + /// Error content type (default is "text/plain; charset=UTF-8") + 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; } @@ -589,7 +602,7 @@ public HttpResponse MakeHeadResponse() /// /// Make GET response /// - /// String content + /// String content (default is "") /// Content type (default is "text/plain; charset=UTF-8") public HttpResponse MakeGetResponse(string content = "", string contentType = "text/plain; charset=UTF-8") { @@ -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) diff --git a/source/NetCoreServer/NetCoreServer.csproj b/source/NetCoreServer/NetCoreServer.csproj index cc2f918f..910b93fe 100644 --- a/source/NetCoreServer/NetCoreServer.csproj +++ b/source/NetCoreServer/NetCoreServer.csproj @@ -2,7 +2,7 @@ net5.0 - 5.0.11.0 + 5.0.12.0 Ivan Shynkarenka Copyright (c) 2019-2021 Ivan Shynkarenka https://github.com/chronoxor/NetCoreServer diff --git a/source/NetCoreServer/WebSocket.cs b/source/NetCoreServer/WebSocket.cs index 7fe83384..ad7b9967 100644 --- a/source/NetCoreServer/WebSocket.cs +++ b/source/NetCoreServer/WebSocket.cs @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/tests/HttpTests.cs b/tests/HttpTests.cs index 90806afe..ed97aaab 100644 --- a/tests/HttpTests.cs +++ b/tests/HttpTests.cs @@ -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")) { @@ -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()); diff --git a/tests/HttpsTests.cs b/tests/HttpsTests.cs index 1be8a724..c78b6d3c 100644 --- a/tests/HttpsTests.cs +++ b/tests/HttpsTests.cs @@ -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")) { @@ -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());