From 12885e1350b9bc6638b5e91c89e055b9f835f18e Mon Sep 17 00:00:00 2001 From: cypherpotato Date: Thu, 4 Jul 2024 16:13:42 -0300 Subject: [PATCH] skip empty cookies --- src/Http/HttpRequest.cs | 21 +++++++++++++-------- src/Http/HttpServer.cs | 4 +--- src/Http/HttpServer__Core.cs | 12 ++++++------ src/Sisk.Core.csproj | 2 +- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/Http/HttpRequest.cs b/src/Http/HttpRequest.cs index 008739a..e7db93f 100644 --- a/src/Http/HttpRequest.cs +++ b/src/Http/HttpRequest.cs @@ -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); } } } diff --git a/src/Http/HttpServer.cs b/src/Http/HttpServer.cs index 035114c..92f85d0 100644 --- a/src/Http/HttpServer.cs +++ b/src/Http/HttpServer.cs @@ -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(); @@ -169,7 +168,6 @@ out Router router /// The configuration object of the server. public HttpServer(HttpServerConfiguration configuration) { - _listenerCallback = new AsyncCallback(ListenerCallback); ServerConfiguration = configuration; handler = new HttpServerHandlerRepository(this); } @@ -276,7 +274,7 @@ public void Start() } httpListener.Start(); - httpListener.BeginGetContext(_listenerCallback, httpListener); + httpListener.BeginGetContext(ListenerCallback, null); handler.ServerStarted(this); } diff --git a/src/Http/HttpServer__Core.cs b/src/Http/HttpServer__Core.cs index 6403d7a..3df2107 100644 --- a/src/Http/HttpServer__Core.cs +++ b/src/Http/HttpServer__Core.cs @@ -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(); @@ -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); @@ -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); } diff --git a/src/Sisk.Core.csproj b/src/Sisk.Core.csproj index f07280d..4c10697 100644 --- a/src/Sisk.Core.csproj +++ b/src/Sisk.Core.csproj @@ -31,7 +31,7 @@ 1.0.0.0 1.0.0.0 - 1.0.0.0-rc2 + 1.0.0.0-rc3 LICENSE.txt False