From f2d95bc93d363b39fb7bdebb435345d50baaea9b Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Fri, 15 Nov 2019 13:47:01 +0100 Subject: [PATCH] WIP / Session Managment --- HTTPServer.cs | 20 ++++++++++++++------ HTTPServerConnection.cs | 2 -- HttpApplication.cs | 2 -- HttpRequest.cs | 32 +++++++++++--------------------- session/Session.cs | 30 ++++++++++++++++++++++++++++-- session/SessionCache.cs | 7 ++----- websocket/WebSocket.cs | 2 +- 7 files changed, 56 insertions(+), 39 deletions(-) diff --git a/HTTPServer.cs b/HTTPServer.cs index 1ea495a..d717d25 100644 --- a/HTTPServer.cs +++ b/HTTPServer.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using ln.http.resources.session; using ln.logging; using ln.types.threads; using ln.types; @@ -10,6 +9,7 @@ using ln.types.net; using ln.http.cert; using System.Globalization; using System.Net.Sockets; +using ln.http.session; namespace ln.http @@ -124,7 +124,7 @@ namespace ln.http if (httpRequest == null) break; - httpRequest.ApplySession(SessionCache); + httpRequest.MakeCurrent(); HttpApplication application = GetHttpApplication(new URI(httpRequest.BaseURI.ToString())); @@ -133,11 +133,17 @@ namespace ln.http HttpResponse response = application.GetResponse(httpRequest); - keepAlive = httpRequest.GetRequestHeader("connection","keep-alive").Equals("keep-alive") && response.GetHeader("connection", "keep-alive").Equals("keep-alive"); - response.SetHeader("connection", keepAlive ? "keep-alive" : "close"); - - connection.SendResponse(response); + if (response != null) + { + keepAlive = httpRequest.GetRequestHeader("connection", "keep-alive").Equals("keep-alive") && response.GetHeader("connection", "keep-alive").Equals("keep-alive"); + response.SetHeader("connection", keepAlive ? "keep-alive" : "close"); + connection.SendResponse(response); + } + else + { + keepAlive = false; + } } while (keepAlive); } catch (Exception e) @@ -153,6 +159,8 @@ namespace ln.http } } + HttpRequest.ClearCurrent(); + connection.GetStream().Close(); } diff --git a/HTTPServerConnection.cs b/HTTPServerConnection.cs index c46b4d9..420f02b 100644 --- a/HTTPServerConnection.cs +++ b/HTTPServerConnection.cs @@ -66,8 +66,6 @@ namespace ln.http using (CurrentRequest = new HttpRequest(this.HTTPServer,httpReader, (IPEndPoint)TcpClient.Client.LocalEndPoint)) { - CurrentRequest.ApplySession(HTTPServer.SessionCache); - Interpreted = DateTime.Now; try diff --git a/HttpApplication.cs b/HttpApplication.cs index fb5be37..ca8ca07 100644 --- a/HttpApplication.cs +++ b/HttpApplication.cs @@ -1,6 +1,4 @@ using System; -using ln.http; -using ln.http.resources.session; namespace ln.http { public abstract class HttpApplication diff --git a/HttpRequest.cs b/HttpRequest.cs index 44adf6a..7efdb4f 100644 --- a/HttpRequest.cs +++ b/HttpRequest.cs @@ -1,18 +1,19 @@ using System; using System.IO; -using System.Text; using System.Collections.Generic; using System.Linq; -using System.Net; -using System.Net.Sockets; using ln.http.exceptions; -using ln.http.resources.session; using ln.types.net; +using System.Threading; +using ln.http.session; namespace ln.http { public class HttpRequest : IDisposable { + static ThreadLocal current = new ThreadLocal(); + static public HttpRequest Current => current.Value; + Dictionary requestHeaders; Dictionary requestCookies; @@ -33,7 +34,9 @@ namespace ln.http public QueryStringParameters Query { get; private set; } - public Session Session { get; private set; } + public Session Session => HTTPServer.SessionCache.GetSession(this); + public HttpUser CurrentUser => Session.CurrentUser; + public MemoryStream ContentStream { get; } public TextReader ContentReader @@ -85,6 +88,9 @@ namespace ln.http ContentStream = new MemoryStream(requestBody); } + public void MakeCurrent() => current.Value = this; + public static void ClearCurrent() => current.Value = null; + private void Setup() { SetupResourceURI(); @@ -133,20 +139,6 @@ namespace ln.http } } - public void ApplySession(SessionCache sessionCache) - { - Session session = sessionCache.ApplySession(this); - Session = session; - if (Session == null) - { - requestHeaders.Add("X-LNH-Session-Failed", "true"); - Session = new Session(); - } - } - - public HttpUser CurrentUser => Session.CurrentUser; - - public override string ToString() { //return string.Format("[HttpRequest: RemoteEndpoint={0}, Hostname={1} Port={2} URI={4}, Method={4}, RequestURL={5}, Protocol={6} Query={7}]", RemoteEndpoint, URI, Method, RequestURL, Protocol, Hostname, Port,Query); @@ -200,8 +192,6 @@ namespace ln.http { contentReader?.Dispose(); ContentStream?.Dispose(); - - Session = null; } } diff --git a/session/Session.cs b/session/Session.cs index 714ce2f..90bdfe1 100644 --- a/session/Session.cs +++ b/session/Session.cs @@ -2,9 +2,9 @@ using System.Collections.Generic; using System.Linq; -namespace ln.http.resources.session +namespace ln.http.session { - public class Session + public class Session : IDisposable { public Guid SessionID { get; private set; } public long SessionAge => DateTimeOffset.Now.ToUnixTimeSeconds() - LastTouch; @@ -27,6 +27,13 @@ namespace ln.http.resources.session set => this.elements[name] = value; } + public T Get(string name) where T:class + { + if (elements.ContainsKey(name)) + return elements[name] as T; + return null; + } + public String[] ElementNames => this.elements.Keys.ToArray(); public void Touch() @@ -39,5 +46,24 @@ namespace ln.http.resources.session } + public void Dispose() + { + foreach (object o in elements.Values) + { +// if (o is IDisposable disposable) +// { +// try +// { +// disposable.Dispose(); +// } +//#pragma warning disable RECS0022 // catch-Klausel, die System.Exception abfängt und keinen Text aufweist +// catch (Exception) +//#pragma warning restore RECS0022 // catch-Klausel, die System.Exception abfängt und keinen Text aufweist + // { + + // } + //} + } + } } } diff --git a/session/SessionCache.cs b/session/SessionCache.cs index 3d875af..79cdca6 100644 --- a/session/SessionCache.cs +++ b/session/SessionCache.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -namespace ln.http.resources.session +namespace ln.http.session { public class SessionCache { @@ -48,7 +48,7 @@ namespace ln.http.resources.session httpResponse.AddCookie(sessionCookie); } - public Session ApplySession(HttpRequest httpRequest) + public Session GetSession(HttpRequest httpRequest) { Guid sessionID = FindSessionID(httpRequest); if (!Guid.Empty.Equals(sessionID) && Contains(sessionID)) @@ -71,8 +71,5 @@ namespace ln.http.resources.session } } - - - } } diff --git a/websocket/WebSocket.cs b/websocket/WebSocket.cs index 703ee45..ed8941a 100644 --- a/websocket/WebSocket.cs +++ b/websocket/WebSocket.cs @@ -69,7 +69,7 @@ namespace ln.http.websocket ); HTTPServerConnection.SendResponse(Stream, httpResponse); - HTTPServerConnection.Current.Value.AbortRequested += (connection) => Close(); + //HTTPServerConnection.Current.Value.AbortRequested += (connection) => Close(); State = WebSocketState.OPEN; }