Skip to content

Commit 5dca34c

Browse files
committed
HttpServer fix: return 400 bad request instead of no response
1 parent 199d6db commit 5dca34c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

OpenHardwareMonitor/Utilities/HttpServer.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ private async Task HandleContextAsync(HttpListenerContext context)
341341
}
342342
}
343343

344+
try
345+
{
344346
if (authenticated)
345347
{
346348
switch (request.HttpMethod)
@@ -422,15 +424,23 @@ private async Task HandleContextAsync(HttpListenerContext context)
422424
await SendResponseAsync(context.Response, responseString, "text/html");
423425
}
424426

425-
try
427+
context.Response.Close();
428+
}
429+
catch(ArgumentException ex)
426430
{
431+
context.Response.StatusCode = 400;
432+
string responseString = @$"<HTML><HEAD><TITLE>400 Bad Request</TITLE></HEAD>
433+
<BODY><H4>400 Bad Request</H4>
434+
{ex.Message}</BODY></HTML> ";
435+
await SendResponseAsync(context.Response, responseString, "text/html");
427436
context.Response.Close();
428437
}
429438
catch
430439
{
431440
// client closed connection before the content was sent
432441
}
433442
}
443+
434444
private async Task ServeResourceFileAsync(HttpListenerResponse response, string name, string ext)
435445
{
436446
// resource names do not support the hyphen

0 commit comments

Comments
 (0)