Fix try-block in Server.cs

This commit is contained in:
Glax 2024-08-26 13:21:34 +02:00
parent 1bd20791b8
commit dfa8e66f34

View File

@ -713,23 +713,18 @@ public class Server : GlobalBase
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
{
if (content is not Stream) if (content is not Stream)
{ {
response.ContentType = "application/json"; response.ContentType = "application/json";
response.AddHeader("Cache-Control", "no-store"); response.AddHeader("Cache-Control", "no-store");
try
{
response.OutputStream.Write(content is not null response.OutputStream.Write(content is not null
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content)) ? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))
: Array.Empty<byte>()); : Array.Empty<byte>());
response.OutputStream.Close(); response.OutputStream.Close();
} }
catch (HttpListenerException e) else if (content is FileStream stream)
{
Log(e.ToString());
}
}
else if(content is FileStream stream)
{ {
string contentType = stream.Name.Split('.')[^1]; string contentType = stream.Name.Split('.')[^1];
response.AddHeader("Cache-Control", "max-age=600"); response.AddHeader("Cache-Control", "max-age=600");
@ -749,9 +744,15 @@ public class Server : GlobalBase
response.ContentType = "text/plain"; response.ContentType = "text/plain";
break; break;
} }
stream.CopyTo(response.OutputStream); stream.CopyTo(response.OutputStream);
response.OutputStream.Close(); response.OutputStream.Close();
stream.Close(); stream.Close();
} }
} }
catch (HttpListenerException e)
{
Log(e.ToString());
}
}
} }