Compare commits

...

5 Commits

Author SHA1 Message Date
682fd0bc2a Merge branch 'refs/heads/cuttingedge-merge-ServerV2' into cuttingedge 2024-08-26 13:22:09 +02:00
dfa8e66f34 Fix try-block in Server.cs 2024-08-26 13:21:54 +02:00
8f51d22303 Fix try-block in Server.cs 2024-08-26 13:21:34 +02:00
d41de84262 Merge branch 'refs/heads/cuttingedge-merge-ServerV2' into cuttingedge
# Conflicts:
#	Tranga/Server.cs
2024-08-26 13:21:05 +02:00
1bd20791b8 Add Cache-Control headers 2024-08-26 13:18:48 +02:00

View File

@ -706,10 +706,6 @@ public class Server : GlobalBase
private void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null) private void SendResponse(HttpStatusCode statusCode, HttpListenerResponse response, object? content = null)
{ {
if (response.OutputStream.CanWrite == false)
{
Log($"No response sent to request: Stream closed before response could be sent.");
}
//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");
@ -722,13 +718,16 @@ public class Server : GlobalBase
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.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();
} }
else if (content is FileStream stream) 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");
switch (contentType.ToLower()) switch (contentType.ToLower())
{ {
case "gif": case "gif":
@ -747,9 +746,9 @@ public class Server : GlobalBase
} }
stream.CopyTo(response.OutputStream); stream.CopyTo(response.OutputStream);
response.OutputStream.Close();
stream.Close(); stream.Close();
} }
response.OutputStream.Close();
} }
catch (HttpListenerException e) catch (HttpListenerException e)
{ {