public static void SendFrame(HttpResponse response, Session session) { String frameContent; lock (DelegateFrameLock) { // Make sure frame is loaded if (null == DelegateFrameCache) { DelegateFrameCache = Minification.Html(File.ReadAllText(DelegateFramePath)); } // Prepare content frameContent = DelegateFrameCache.Replace("<div id=\"session\"></div>", "<div id=\"session\">" + session.Id.ToString() + "</div>"); //frameContent = File.ReadAllText(@"C:\Users\User\Dropbox\InvertedTomato\Projects\Amos3\benedit\Server\InvertedTomato.Amos3.Api\frame.html").Replace("<div id=\"session\"></div>", "<div id=\"session\">" + session.Id.ToString() + "</div>"); } // Send frame response.Write(frameContent); }
public abstract TriggerSetBase GenerateTriggerSet(Session session);
private void TriggerHandler(Session session, String page, HttpRequest request, HttpResponse response) { // Get type var triggerSetType = session.TriggerSet.GetType(); try { // Get method var method = triggerSetType.GetMethod(page); if (null == method) { // Check method exists SendJsonFailure("NotSupportedException", "Method '" + page + "' is not supported"); } else if (!method.IsPublic) { // Check method visibility SendJsonFailure("MethodAccessException", "Attempt to access a non-public method"); } else { // Invoke method response.Write((String)method.Invoke(session.TriggerSet, new object[] { request })); } } catch (ThreadAbortException) { } catch (Exception ex) { if (ex is System.Reflection.TargetInvocationException) { ex = ex.InnerException; } // Log exception LogException(ex, "TriggerHandler", session.OriginId); // Handle exception if (ex is ArgumentException || ex is ArgumentNullException || ex is NotImplementedException || ex is TargetParameterCountException || ex is SafeException) { SendJsonFailure(ex.GetType().ToString(), ex.Message); } else if (ex is System.Data.SqlClient.SqlException) { SendJsonFailure(ex.GetType().ToString(), "Database is temporarily unavailable"); } else { SendJsonFailure("System.Exception", "Undefined exception."); } } }
// Methods public abstract CommandSetBase GenerateCommandSet(Session session);
protected TriggerSetBase(Session session) { Session = session; Variant = session.Variant; DataSet = session.Variant.DataSet; }
private void RpcHandler(Session session, Stream inputStream, HttpRequest request, HttpResponse response) { if (inputStream.Length > 1024 * 1024) { SendJsonFailure("Amos3.RpcInputTooLarge", "Invalid inputStream; max request size of 1MB"); return; } // Get calls IList<RpcCall> calls; if (!session.EnvelopeHandler.TryDecodeCalls(inputStream, out calls)) { SendJsonFailure("Amos3.RpcEnvelopeDecodeFailure", "Invalid inputStream; does not match envelope format"); return; } // Get type var commandSetType = session.CommandSet.GetType(); // Make calls var rpcReturns = new List<RpcReturn>(); foreach (var call in calls) { // Get parameter signiture var types = call.MethodParameters.Select(a => a.GetType()).ToArray(); RpcReturn rpcReturn; try { // Get method var method = commandSetType.GetMethod(call.MethodName, types); if (null == method) { // Check method exists rpcReturn = new RpcReturn(call, "NotSupportedException", "Method '" + call.MethodName + "' is not supported"); } else if (!method.IsPublic) { // Check method visibility rpcReturn = new RpcReturn(call, "MethodAccessException", "Attempt to access a non-public method"); } else { // Invoke method rpcReturn = new RpcReturn(call, (object[])method.Invoke(session.CommandSet, call.MethodParameters)); } } catch (ThreadAbortException) { return; } catch (Exception ex) { if (ex is System.Reflection.TargetInvocationException) { ex = ex.InnerException; } // Log exception LogException(ex, "RpcHandler", session.OriginId); // Handle exception if (ex is ArgumentException || ex is ArgumentNullException || ex is NotImplementedException || ex is TargetParameterCountException || ex is SafeException) { rpcReturn = new RpcReturn(call, ex.GetType().ToString(), ex.Message); } else if (ex is System.Data.SqlClient.SqlException) { rpcReturn = new RpcReturn(call, ex.GetType().ToString(), "Database is temporarily unavailable"); } else { rpcReturn = new RpcReturn(call, "System.Exception", "Undefined exception."); } } rpcReturns.Add(rpcReturn); } // Set cache headers response.ExpiresAbsolute = DateTime.Now; response.Expires = 0; response.CacheControl = "private"; response.Cache.SetCacheability(HttpCacheability.NoCache); // Set compression var encodingsAccepted = request.Headers["Accept-Encoding"]; if (!string.IsNullOrEmpty(encodingsAccepted)) { encodingsAccepted = encodingsAccepted.ToLowerInvariant(); if (encodingsAccepted.Contains("deflate")) { response.AppendHeader("Content-Encoding", "deflate"); response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); } else if (encodingsAccepted.Contains("gzip")) { response.AppendHeader("Content-Encoding", "gzip"); response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); } } // Reply with responses response.ContentType = "application/json"; response.TrySkipIisCustomErrors = true; response.Write(session.EnvelopeHandler.EncodeReturn(rpcReturns)); }
public override TriggerSetBase GenerateTriggerSet(Session session) { return null; }
// Required stubs public override CommandSetBase GenerateCommandSet(Session session) { return null; }
internal static bool TryGet(Xid sessionId, out Session session) { return StoreDatabase.TryGetValue(sessionId, out session); }
internal static Session Create(HttpRequest request, Variant variant, IEnvelopeHandler envelopeHandler) { // Create session in database Xid id; using (var query = new DatabaseQuery(DatabaseQueryString.Create("SessionStartLog")) { Type = System.Data.CommandType.StoredProcedure }) { query.Parameters.Add(new SqlParameter("dataSetId", variant.DataSetId.ToGuid())); query.Parameters.Add(new SqlParameter("ip", request.UserHostAddress ?? "")); query.Parameters.Add(new SqlParameter("referrer", null == request.UrlReferrer ? "" : request.UrlReferrer.AbsoluteUri)); query.Parameters.Add(new SqlParameter("userAgent", request.UserAgent ?? "")); query.Parameters.Add(new SqlParameter("languages", null == request.UserLanguages ? "" : Json.Serialize(request.UserLanguages))); query.Parameters.Add(new SqlParameter("id", System.Data.SqlDbType.UniqueIdentifier) { Direction = System.Data.ParameterDirection.Output }); query.Execute(); id = (Xid)(Guid)query.Parameters["id"].Value; } return StoreDatabase[id] = new Session(id, variant, envelopeHandler); }
protected CommandSetBase(Session session) { Session = session; Variant = session.Variant; DataSet = session.Variant.DataSet; }