2
0

Fix response closed on OPTIONS request

This commit is contained in:
Glax 2024-09-07 20:36:00 +02:00
parent 15f3e2b8ec
commit 9b8b80cd24

View File

@ -63,10 +63,11 @@ public class Server : GlobalBase
{ {
HttpListenerRequest request = context.Request; HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response; HttpListenerResponse response = context.Response;
if(request.HttpMethod == "OPTIONS") if (request.Url!.LocalPath.Contains("favicon"))
SendResponse(HttpStatusCode.OK, context.Response); {
if(request.Url!.LocalPath.Contains("favicon"))
SendResponse(HttpStatusCode.NoContent, response); SendResponse(HttpStatusCode.NoContent, response);
return;
}
switch (request.HttpMethod) switch (request.HttpMethod)
{ {
@ -79,7 +80,10 @@ public class Server : GlobalBase
case "DELETE": case "DELETE":
HandleDelete(request, response); HandleDelete(request, response);
break; break;
default: case "OPTIONS":
SendResponse(HttpStatusCode.OK, context.Response);
break;
default:
SendResponse(HttpStatusCode.BadRequest, response); SendResponse(HttpStatusCode.BadRequest, response);
break; break;
} }
@ -707,14 +711,15 @@ public class Server : GlobalBase
private void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null) private void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null)
{ {
//Log($"Response: {statusCode} {content}"); //Log($"Response: {statusCode} {content}");
response.StatusCode = (int)statusCode; response.StatusCode = (int)statusCode;
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With"); response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE"); response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE");
response.AddHeader("Access-Control-Max-Age", "1728000"); response.AddHeader("Access-Control-Max-Age", "1728000");
response.AppendHeader("Access-Control-Allow-Origin", "*"); response.AppendHeader("Access-Control-Allow-Origin", "*");
try try
{ {
if (content is not Stream) if (content is not Stream)
{ {
response.ContentType = "application/json"; response.ContentType = "application/json";
@ -750,7 +755,7 @@ public class Server : GlobalBase
stream.Close(); stream.Close();
} }
} }
catch (HttpListenerException e) catch (Exception e)
{ {
Log(e.ToString()); Log(e.ToString());
} }