/// <summary> /// Gets the session name. /// </summary> public override string GetSessionName(IHttpPhpContext webctx) { // TODO: SessionOptions.CookieName // var config = ((Context)webctx).GetService<IConfigureOptions<Microsoft.AspNetCore.Builder.SessionOptions>>(); return(SessionDefaults.CookieName); }
public override bool Persist(IHttpPhpContext webctx, PhpArray session) { var httpContext = GetHttpContext(webctx); var state = httpContext.Session; //if (state.Mode == SessionStateMode.InProc) //{ // // removes all items (some could be changed or removed in PHP): // // TODO: some session variables could be added in ASP.NET application // state.Clear(); // // populates session collection from variables: // var enumerator = session.GetFastEnumerator(); // while (enumerator.MoveNext()) // { // // skips resources: // if (!(enumerator.CurrentValue.Object is PhpResource)) // { // state.Add(enumerator.CurrentKey.ToString(), enumerator.CurrentValue.ToClr()); // } // } //} //else { // if the session is maintained out-of-process, serialize the entire $_SESSION autoglobal // add the serialized $_SESSION to ASP.NET session: state[PhpNetSessionVars] = Serializer.Serialize((Context)webctx, (PhpValue)session, default(RuntimeTypeHandle)).ToBytes((Context)webctx); } // return(true); }
public override string GetSessionId(IHttpPhpContext webctx) { if (_lazyid == null) { // obtain the ID from cookies: var sessid = ((Context)webctx).Cookie[GetSessionName(webctx)]; if (Operators.IsEmpty(sessid)) { _isnewsession = true; if (_handler is SessionIdInterface idinterface) { _lazyid = idinterface.create_sid(); } else { _lazyid = session_create_id(); } webctx.AddCookie(GetSessionName(webctx), _lazyid, null); // TODO: lifespan } else { _isnewsession = false; _lazyid = sessid.ToStringOrThrow((Context)webctx); } } Debug.Assert(_lazyid != null); return(_lazyid); }
/// <summary> /// Initiates the session. /// </summary> public override bool StartSession(Context ctx, IHttpPhpContext webctx) { if (base.StartSession(ctx, webctx)) { var httpContext = GetHttpContext(webctx); var session = httpContext.Session; // override HttpSessionState._container : IHttpSessionState var container = session.GetContainer(); if (container != null) { if (container is PhpSessionStateContainer) { // unexpected; session already bound throw new InvalidOperationException("Session was not closed properly."); } // overwrite IHttpSessionState session.SetContainer(new PhpSessionStateContainer(container, ctx.Session)); } else { // cannot resolve the IHttpSessionState container } // return(true); } else { return(false); } }
/// <summary> /// Closes the session and either persists the session data or abandons the session. /// </summary> public virtual void CloseSession(Context ctx, IHttpPhpContext webctx, bool abandon) { if (webctx.SessionState != PhpSessionState.Started) { return; } webctx.SessionState = PhpSessionState.InProgress; // try { if (!abandon) { Persist(webctx, ctx.Session ?? PhpArray.Empty); } else { Abandon(webctx); } } finally { webctx.SessionState = PhpSessionState.Closed; } }
/// <summary> /// Initiates the session. /// </summary> public override bool StartSession(Context ctx, IHttpPhpContext webctx) { if (base.StartSession(ctx, webctx)) { //var httpContext = GetHttpContext(webctx); //var session = httpContext.Session; //// override HttpSessionState._container : IHttpSessionState //var oldcontainer = session.GetContainer(); //if (oldcontainer is SharedSession) //{ // // unexpected; session already bound //} //else if (oldcontainer != null) //{ // // overwrite IHttpSessionState // session.SetContainer(new SharedSession(oldcontainer, ctx.Session)); //} // return(true); } else { return(false); } }
public override string GetSessionId(IHttpPhpContext webctx) { if (_lazyid == null) { // obtain the ID from cookies: var sessid = ((Context)webctx).Cookie[GetSessionName(webctx)]; if (Operators.IsEmpty(sessid)) { _isnewsession = true; if (_handler is SessionHandler legacyhandler) { _lazyid = legacyhandler.create_sid(); } else { _lazyid = session_create_id(); } } else { _isnewsession = false; _lazyid = sessid.ToStringOrThrow((Context)webctx); } } Debug.Assert(_lazyid != null); return(_lazyid); }
public override PhpArray Load(IHttpPhpContext webctx) { // 1. gc if (GarbageCollectionCheck()) { _handler.gc(_gc_maxlifetime); } // 2. open if (!_handler.open(System.IO.Path.GetTempPath(), GetSessionName(webctx))) { return(null); } // 3. read var str = _handler.read(GetSessionId(webctx)); if (str.IsEmpty) { return(null); } var handler = GetSerializeHandler((Context)webctx); return(handler.Deserialize((Context)webctx, str, default(RuntimeTypeHandle)).AsArray()); }
public override bool Persist(IHttpPhpContext webctx, PhpArray session) { var ctx = (Context)webctx; var httpContext = GetHttpContext(webctx); var state = httpContext.Session; // at this point, all the session items are stored in $_SESSION already // store non-PHP objects into ASP.NET session state, serialize the rest as PHP array PhpArray sessionvars = null; if (state.Mode == SessionStateMode.InProc) { // removes all items (some could be changed or removed in PHP): state.Clear(); // populates session collection from variables: var enumerator = session.GetFastEnumerator(); while (enumerator.MoveNext()) { var value = enumerator.CurrentValue.GetValue(); // dereferenced value // skips resources if (value.IsResource) { continue; } if (value.IsObject && value.Object.GetPhpTypeInfo().IsPhpType) { // PHP objects will be serialized using PHP serializer if (sessionvars == null) { sessionvars = new PhpArray(session.Count); } sessionvars[enumerator.CurrentKey] = value; } else { state[enumerator.CurrentKey.ToString()] = value.ToClr(); } } } else { // If the session is maintained out-of-process, // serialize the entire $_SESSION autoglobal as it is. sessionvars = session; } // Add the serialized $_SESSION to ASP.NET session. // Even in case this array is empty, this keeps the ASP.NET session state alive. state[PhpSessionVars] = sessionvars != null && sessionvars.Count != 0 ? Serializer.Serialize(ctx, sessionvars, default(RuntimeTypeHandle)).ToBytes(ctx) : Array.Empty <byte>(); // return(true); }
public override string GetSessionName(IHttpPhpContext webctx) { if (_lazyname == null) { _lazyname = ((Context)webctx).Configuration.GetSessionConfiguration().SessionName ?? SessionConfiguration.DefaultSessionName; } return(_lazyname); }
public override void Abandon(IHttpPhpContext webctx) { if (!_isnewsession) { webctx.AddCookie(GetSessionName(webctx), string.Empty, DateTimeOffset.UtcNow); } // destroy _handler.destroy(GetSessionId(webctx)); }
public override PhpArray Load(IHttpPhpContext webctx) { var ctx = (Context)webctx; var httpContext = GetHttpContext(webctx); EnsureSessionId(httpContext); var session = httpContext.Session; var underlyingstate = session.GetContainer(); // use the underlying IHttpSessionState because Session will be changed to our handler then // session contains both ASP.NET session and PHP session variables PhpArray result = null; // serialized $_SESSION array if (session[PhpSessionVars] is byte[] data && data.Length != 0) { if (Serializer.TryDeserialize(ctx, data, out var value)) { result = value.ArrayOrNull(); } } foreach (string name in session) { if (name == PhpSessionVars) { continue; } if (result == null) { result = new PhpArray(session.Count); } if (underlyingstate != null) { result[name] = new SessionValue(underlyingstate, name); } else { // in case we won't get IHttpSessionState: var value = PhpValue.FromClr(session[name]); //if (value.IsObject) //{ // // NOTE: values that are bound to specific Context are stored using PHP serialization into PhpSessionVars array // // CONSIDER: what if value has a reference to a Context - clone the value? //} result[name] = value; } } return(result ?? PhpArray.NewEmpty()); }
public override bool SetSessionName(IHttpPhpContext webctx, string name) { if (webctx.SessionState != PhpSessionState.Closed) { // session name cannot be changed after the session started throw new InvalidOperationException(); } _lazyname = name; return(true); }
public override PhpArray Load(IHttpPhpContext webctx) { var ctx = (Context)webctx; var httpContext = GetHttpContext(webctx); EnsureSessionId(httpContext); var session = httpContext.Session; // session contains both ASP.NET session and PHP session variables PhpArray result = null; // serialized $_SESSION array if (session[PhpSessionVars] is byte[] data) { if (Serializer.TryDeserialize(ctx, data, out var value)) { result = value.ArrayOrNull(); } } // .NET session items if (session.Mode == SessionStateMode.InProc) { foreach (string name in session) { if (name == PhpSessionVars) { continue; } if (result == null) { result = new PhpArray(session.Count); } var value = PhpValue.FromClr(session[name]); //if (value.IsObject) //{ // // CONSIDER: what if value has reference to a different Context - change it? clone the value? do not store it into the session in the first place? //} result[name] = value; } } else { // TODO: deserialize .NET session variables // CONSIDER: isn't it too much of overhead? } return(result ?? PhpArray.NewEmpty()); }
/// <summary> /// Called when sessions are started. /// </summary> public override PhpArray Load(IHttpPhpContext webctx) { var result = new PhpArray(); var ctx = (RequestContextCore)webctx; var isession = ctx.HttpContext.Session; // throws if session is not configured if (isession.IsAvailable) { foreach (var key in isession.Keys) { if (isession.TryGetValue(key, out byte[] bytes))
public override bool Persist(IHttpPhpContext webctx, PhpArray session) { // var handler = GetSerializeHandler((Context)webctx); // return // 1. write (_handler.write(GetSessionId(webctx), handler.Serialize((Context)webctx, (PhpValue)session, default(RuntimeTypeHandle))) && // 2. close _handler.close()); }
public override bool SetSessionId(IHttpPhpContext webctx, string newid) { if (webctx.HeadersSent) { return(false); } // _lazyid = newid; _isnewsession = true; return(true); }
/// <summary> /// Close the session (either abandon or persist). /// </summary> public override void CloseSession(Context ctx, IHttpPhpContext webctx, bool abandon) { // set the original IHttpSessionState back var session = GetHttpContext(webctx).Session; if (session.GetContainer() is PhpSessionStateContainer shared) { session.SetContainer(shared.UnderlayingContainer); } // persist $_SESSION base.CloseSession(ctx, webctx, abandon); }
/// <summary> /// Close the session (either abandon or persist). /// </summary> public override void CloseSession(Context ctx, IHttpPhpContext webctx, bool abandon) { base.CloseSession(ctx, webctx, abandon); // set the original IHttpSessionState back var session = GetHttpContext(webctx).Session; var container = session.GetContainer(); if (container is SharedSession shared && ReferenceEquals(shared.PhpSession, ctx.Session)) { session.SetContainer(shared.UnderlayingContainer); } }
public override void Abandon(IHttpPhpContext webctx) { // abandon asp.net core session var isession = GeHttpContext(webctx).Session; if (isession != null) { isession.Clear(); isession.CommitAsync() .GetAwaiter() .GetResult(); } }
public override string GetSessionId(IHttpPhpContext webctx) { var isession = GeHttpContext(webctx).Session; if (isession != null) { return(isession.Id); } else { return(string.Empty); } }
/// <summary> /// Checks if sessions were configured. /// </summary> public override bool IsEnabled(IHttpPhpContext webctx) { var httpctx = GeHttpContext(webctx); try { var session = httpctx.Session; // throws if session is not configured return(session != null); } catch { return(false); } }
/// <summary> /// Checks if sessions were configured. /// </summary> public override bool IsEnabled(IHttpPhpContext webctx) { var ctx = (RequestContextCore)webctx; try { var session = ctx.HttpContext.Session; // throws if session is not configured return(session != null); } catch { return(false); } }
public override bool Persist(IHttpPhpContext webctx, PhpArray session) { webctx.AddCookie(GetSessionName(webctx), _lazyid, null); // TODO: lifespan // var handler = GetSerializeHandler((Context)webctx); // return // 1. write (_handler.write(GetSessionId(webctx), handler.Serialize((Context)webctx, (PhpValue)session, default(RuntimeTypeHandle))) && // 2. close _handler.close()); }
/// <summary> /// Discard session array changes and finish session. /// </summary> public virtual void AbortSession(Context ctx, IHttpPhpContext webctx) { if (webctx.SessionState != PhpSessionState.Started) { return; } webctx.SessionState = PhpSessionState.InProgress; try { // TODO: clear $_SESSION ? } finally { webctx.SessionState = PhpSessionState.Closed; } }
public override PhpArray Load(IHttpPhpContext webctx) { var ctx = (Context)webctx; var httpContext = GetHttpContext(webctx); EnsureSessionId(httpContext); //// removes dummy item keeping the session alive: //if (httpContext.Session[AspNetSessionHandler.PhpNetSessionVars] as string == AspNetSessionHandler.DummySessionItem) //{ // httpContext.Session.Remove(AspNetSessionHandler.PhpNetSessionVars); //} // var state = httpContext.Session; PhpArray result = null; //if (state.Mode == SessionStateMode.InProc) //{ // result = new PhpArray(); // foreach (string name in state) // { // var value = PhpValue.FromClr(state[name]); // // TODO: rebind value.Context // result[name] = value; // } //} //else { if (state[PhpNetSessionVars] is byte[] data) { if (Serializer.TryDeserialize(ctx, data, out var value)) { result = value.ArrayOrNull(); } } // TODO: deserialize .NET session variables } return(result ?? PhpArray.NewEmpty()); }
/// <summary> /// Starts the session if it is not started yet. /// </summary> public virtual bool StartSession(Context ctx, IHttpPhpContext webctx) { // checks and changes session state: if (webctx.SessionState != PhpSessionState.Closed) { return(false); } webctx.SessionState = PhpSessionState.InProgress; try { // ensures session and reads session data var session_array = this.Load(webctx) ?? PhpArray.NewEmpty(); // sets the auto-global variable (the previous content of $_SESSION array is discarded): ctx.Session = session_array; ctx.Globals[SESSION_Variable] = session_array; //if (ctx.Configuration.Core.RegisterGlobals) //{ // // ctx.RegisterSessionGlobals(); //} // adds/updates a SID constant: if (!ctx.DefineConstant(SID_Constant, GetSessionId(webctx), true)) { throw new InvalidOperationException("SID already set."); // TODO: allow overwriting } } catch { webctx.SessionState = PhpSessionState.Closed; return(false); } finally { // webctx.SessionState = PhpSessionState.Started; } // return(true); }
public override PhpArray Load(IHttpPhpContext webctx) { var httpContext = GetHttpContext(webctx); EnsureSessionId(httpContext); //// removes dummy item keeping the session alive: //if (httpContext.Session[AspNetSessionHandler.PhpNetSessionVars] as string == AspNetSessionHandler.DummySessionItem) //{ // httpContext.Session.Remove(AspNetSessionHandler.PhpNetSessionVars); //} // var state = httpContext.Session; PhpArray result = null; //if (state.Mode == SessionStateMode.InProc) //{ // result = new PhpArray(); // foreach (string name in state) // { // var value = PhpValue.FromClr(state[name]); // // TODO: rebind value.Context // result[name] = value; // } //} //else { var data = state[PhpNetSessionVars] as byte[]; if (data != null) { result = Serializer.Deserialize((Context)webctx, new PhpString(data), default(RuntimeTypeHandle)).ArrayOrNull(); } } return(result ?? PhpArray.NewEmpty()); }
/// <summary> /// Gets the session ID (SID constant). /// </summary> public abstract string GetSessionId(IHttpPhpContext webctx);
/// <summary> /// Frees the session. /// Next time a new (empty) sesison should be created. /// </summary> /// <param name="webctx">Current web context.</param> public abstract void Abandon(IHttpPhpContext webctx);