Skip to content

Commit

Permalink
skip empty cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Jul 4, 2024
1 parent 2fe53f0 commit 12885e1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
21 changes: 13 additions & 8 deletions src/Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,30 @@ public NameValueCollection Cookies
{
cookies = new NameValueCollection();
string? cookieHeader = listenerRequest.Headers[HttpKnownHeaderNames.Cookie];
if (cookieHeader != null)

if (!string.IsNullOrWhiteSpace(cookieHeader))
{
string[] cookieParts = cookieHeader.Split(';');
foreach (string cookieExpression in cookieParts)
string[] cookiePairs = cookieHeader.Split(';');

for (int i = 0; i < cookiePairs.Length; i++)
{
int eqPos = cookieExpression.IndexOf("=");
string cookieExpression = cookiePairs[i];

int eqPos = cookieExpression.IndexOf('=');
if (eqPos < 0)
{
throw new HttpRequestException(SR.HttpRequest_InvalidCookieSyntax);
}
string key = cookieExpression.Substring(0, eqPos).Trim();
string value = cookieExpression.Substring(eqPos + 1).Trim();

if (string.IsNullOrEmpty(key))
string cookieName = cookieExpression.Substring(0, eqPos).Trim();
string cookieValue = cookieExpression.Substring(eqPos + 1).Trim();

if (string.IsNullOrWhiteSpace(cookieName))
{
throw new HttpRequestException(SR.HttpRequest_InvalidCookieSyntax);
}

cookies[key] = WebUtility.UrlDecode(value);
cookies[cookieName] = WebUtility.UrlDecode(cookieValue);
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/Http/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public partial class HttpServer : IDisposable
private bool _isListening = false;
private bool _isDisposing = false;
private readonly HttpListener httpListener = new HttpListener();
private readonly AsyncCallback _listenerCallback;
private ListeningHost? _onlyListeningHost;
internal HttpEventSourceCollection _eventCollection = new HttpEventSourceCollection();
internal HttpWebSocketConnectionCollection _wsCollection = new HttpWebSocketConnectionCollection();
Expand Down Expand Up @@ -169,7 +168,6 @@ out Router router
/// <param name="configuration">The configuration object of the server.</param>
public HttpServer(HttpServerConfiguration configuration)
{
_listenerCallback = new AsyncCallback(ListenerCallback);
ServerConfiguration = configuration;
handler = new HttpServerHandlerRepository(this);
}
Expand Down Expand Up @@ -276,7 +274,7 @@ public void Start()
}

httpListener.Start();
httpListener.BeginGetContext(_listenerCallback, httpListener);
httpListener.BeginGetContext(ListenerCallback, null);

handler.ServerStarted(this);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Http/HttpServer__Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ internal static void SetCorsHeaders(HttpServerFlags serverFlags, HttpListenerReq

private void UnbindRouters()
{
foreach (var lh in ServerConfiguration.ListeningHosts)
for (int i = 0; i < ServerConfiguration.ListeningHosts.Count; i++)
{
var lh = ServerConfiguration.ListeningHosts[i];
if (lh.Router is { } router && ReferenceEquals(this, router.ParentServer))
{
router.FreeHttpServer();
Expand All @@ -89,8 +90,9 @@ private void UnbindRouters()

private void BindRouters()
{
foreach (var lh in ServerConfiguration.ListeningHosts)
for (int i = 0; i < ServerConfiguration.ListeningHosts.Count; i++)
{
var lh = ServerConfiguration.ListeningHosts[i];
if (lh.Router is { } router && router.ParentServer is null)
{
router.BindServer(this);
Expand All @@ -103,10 +105,8 @@ private void ListenerCallback(IAsyncResult result)
if (_isDisposing || !_isListening)
return;

var listener = (HttpListener)result.AsyncState!;
listener.BeginGetContext(_listenerCallback, listener);

HttpListenerContext context = listener.EndGetContext(result);
httpListener.BeginGetContext(ListenerCallback, null);
HttpListenerContext context = httpListener.EndGetContext(result);
ProcessRequest(context);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sisk.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<Version>1.0.0.0-rc2</Version>
<Version>1.0.0.0-rc3</Version>

<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<SignAssembly>False</SignAssembly>
Expand Down

0 comments on commit 12885e1

Please sign in to comment.