Skip to content

Commit

Permalink
improve HttpServer answer method
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Nov 18, 2023
1 parent 9468b97 commit 9454c57
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/HTTP/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ private void _ReceiveRequests()
);
}

if (context.Request.IsWebSocketRequest is false)
{
bool answer = false;
bool foundPath = false;

if (request.IsWebSocket is false) // IS HTTP CONNECTION
{
foreach (var path in _paths)
{
if (request.ComparePath(path.url))
Expand All @@ -222,38 +222,24 @@ private void _ReceiveRequests()
_onPath?.Invoke(null, new PathContainer(request, response, null));
}

answer = true;
foundPath = true;
}
}

if (!answer)
{
string data = $"{request.RawRequest.HttpMethod.ToUpper()} {request.Path}";
response.Send(404, data);
}
}
else
{
bool answer = false;

foreach (var path in _paths)
{
if (request.ComparePath(path.url))
{
if (path.isWebSocket)
{
answer = true;
foundPath = true;
}
}
}

if (answer is false)
{
// TODO: Check best way for refuse connection.
string data = $"{request.RawRequest.HttpMethod.ToUpper()} {request.Path}";
response.Send(404, data);
}
else
if (foundPath)
{
ThreadPool.QueueUserWorkItem(async __ =>
{
Expand All @@ -269,6 +255,22 @@ private void _ReceiveRequests()
});
}
}

if (foundPath is false)
{
if (request.IsWebSocket)
{
// TODO: Check best way for refuse websocket connection.
string data = $"{request.RawRequest.HttpMethod.ToUpper()} {request.Path}";
response.Send(404, data);
}
else
{
// TODO: Check best may for refuse http connection.
string data = $"{request.RawRequest.HttpMethod.ToUpper()} {request.Path}";
response.Send(404, data);
}
}
}
catch (Exception e)
{
Expand Down

0 comments on commit 9454c57

Please sign in to comment.