コード例 #1
0
ファイル: LinkType.cs プロジェクト: invertedtomato/Amos3
 public LinkType(int key, Xid targetNodeTypeId, int maxItems, GroupTypeCollection groupRead, GroupTypeCollection groupWrite)
 {
     Key = key;
     TargetNodeTypeId = targetNodeTypeId;
     MaxItems = maxItems;
     GroupsReadInner = groupRead;
     GroupsWriteInner = groupWrite;
 }
コード例 #2
0
ファイル: DataSet.cs プロジェクト: invertedtomato/Amos3
        public GenericAsset GetAsset(Xid id, Xid requestingNodeId, MaxAge maxAge)
        {
            // Convert to a many lookup
            var result = GetAssets(new List<Xid>() { id }, requestingNodeId, maxAge);

            // Return object
            return result.Assets.First();
        }
コード例 #3
0
        public AssetKeyedReference(Xid id, string key)
        {
            if (null == key) {
                throw new ArgumentNullException("key");
            }

            Id = id;
            Key = key;
        }
コード例 #4
0
ファイル: ARecord.cs プロジェクト: invertedtomato/Amos3
 protected ARecord(Xid id, Xid domainId, string fileName, string contentType, long length, Xid? derivedFromId, string key)
 {
     Id = id;
     DomainId = domainId;
     FileName = fileName;
     ContentType = contentType;
     Length = length;
     DerivedFromId = derivedFromId;
     Key = key;
     AsAt = DateTime.UtcNow;
 }
コード例 #5
0
ファイル: NRecord.cs プロジェクト: invertedtomato/Amos3
 protected NRecord(Xid id, Xid domainId, Xid typeId, Xid dataSetId, Dictionary<int, String> properties, Dictionary<int, List<Xid>> links, Dictionary<Xid, List<Xid>> groups)
 {
     Id = id;
     DomainId = domainId;
     TypeId = typeId;
     DataSetId = dataSetId;
     PropertiesInternal = properties;
     LinksInternal = links;
     GroupsInternal = groups;
     AsAt = DateTime.UtcNow;
 }
コード例 #6
0
ファイル: GenericAsset.cs プロジェクト: invertedtomato/Amos3
        internal GenericAsset(Variant variant, Xid id, Xid requestingNodeId, MaxAge maxAge)
        {
            Variant = variant;
            DataSet = variant.DataSet;
            Cache = variant.Cache;
            Schema = variant.Schema;

            Id = id;
            RequestingNodeId = requestingNodeId;

            // Load record
            Reload(maxAge);
        }
コード例 #7
0
ファイル: DataSet.cs プロジェクト: invertedtomato/Amos3
        internal DataSet(Xid id, Variant variant)
        {
            Id = id;
            Variant = variant;
            Cache = Variant.Cache;

            // Check dataset is defined in database
            using (var query = new DatabaseQuery(DatabaseQueryString.Create("DataSetCheck")) { Type = CommandType.StoredProcedure }) {
                query.Bind("dataSetId", Id.ToGuid());
                query.BindOutput("exists", SqlDbType.Int);
                query.Execute();
            }
        }
コード例 #8
0
        public AssetKeyedReference(string value)
        {
            if (null == value) {
                throw new ArgumentNullException("value");
            }

            if (!Validate(value)) {
                throw new ArgumentException("Invalid keyed reference", "v");
            }

            var svalue = value.Split('~');
            Id = new Xid(svalue[0]);
            Key = svalue[1];
        }
コード例 #9
0
ファイル: Variant.cs プロジェクト: invertedtomato/Amos3
        public Guid LogException(Exception ex, string environment, Xid requestingNodeId)
        {
            // Get session details
            var sessionId = new Xid();
            string clientIp = null;
            if (null != HttpContext.Current) {
                if (null != HttpContext.Current.Session && !string.IsNullOrEmpty(HttpContext.Current.Session.SessionID)) {
                    //Wrong SID sessionId = HttpContext.Current.Session.SessionID;
                }
                if (null != HttpContext.Current.Request) {
                    clientIp = HttpContext.Current.Request.UserHostAddress;
                }
            }

            // Log inner exception
            Guid? innerExceptionId = null == ex.InnerException ? null : (Guid?)LogException(ex.InnerException, environment, requestingNodeId);

            using (var query = new DatabaseQuery(DatabaseQueryString.Create("ExceptionLog2")) { Type = System.Data.CommandType.StoredProcedure }) {
                query.Parameters.Add(new SqlParameter("dataSetId", DataSetId.ToGuid()));
                query.Parameters.Add(new SqlParameter("environment", environment ?? "<null>"));
                query.Parameters.Add(new SqlParameter("message", ex.Message.Substr(0, 1024)));
                var data = "";
                if (null != ex.Data) {
                    data = ex.Data.ToString().Substr(0, 1024);
                }
                query.Parameters.Add(new SqlParameter("data", data));
                query.Parameters.Add(new SqlParameter("sourceSite", ex.Source));
                query.Parameters.Add(new SqlParameter("stackTrace", ex.StackTrace.ToString()));
                if (null == innerExceptionId) {
                    query.Parameters.Add(new SqlParameter("innerException", DBNull.Value));
                } else {
                    query.Parameters.Add(new SqlParameter("innerException", innerExceptionId));
                }
                query.Parameters.Add(new SqlParameter("caught", false));
                query.Parameters.Add(new SqlParameter("clientIp", clientIp ?? "<null>"));
                query.Parameters.Add(new SqlParameter("sessionId", sessionId.ToGuid()));
                query.Parameters.Add(new SqlParameter("requestingNodeId", requestingNodeId.ToGuid()));

                query.Parameters.Add(new SqlParameter("id", System.Data.SqlDbType.UniqueIdentifier) { Direction = System.Data.ParameterDirection.Output });
                query.Execute();

                return (Guid)query.Parameters["id"].Value;
            }
        }
コード例 #10
0
ファイル: DataSet.cs プロジェクト: invertedtomato/Amos3
        public ResultantAssets GetAssets(IEnumerable<Xid> ids, Xid requestingNodeId, MaxAge maxAge)
        {
            if (null == ids) {
                throw new ArgumentNullException("ids");
            }
            if ((int)maxAge < -2) {
                throw new ArgumentOutOfRangeException("maxAge", "must be at least -2"); // -1 means infinate, -2 means noload
            }

            var pending = new HashSet<Xid>(ids);

            // Fetch from cache
            if (maxAge == MaxAge.Any || (int)maxAge > 0) {
                foreach (var record in GenericAsset.GetFromCache(Variant, pending, maxAge)) {
                    pending.Remove(record.Id);
                }
            }

            // Cache any remaining items
            if (pending.Count > 0) {
                foreach (var record in GenericAsset.GetFromDatabase(Variant, pending)) {
                    pending.Remove(record.Id);
                }
            }

            // Add nodes to output
            var output = new List<GenericAsset>(ids.Select(id => new GenericAsset(Variant, id, requestingNodeId, MaxAge.Any)));

            // Add non-existant nodes to output
            foreach (var id in pending) {
                var node = new GenericAsset(Variant, id, requestingNodeId, MaxAge.NoLoad);
                node.SetExists(false);
                output.Add(node);
            }

            return new ResultantAssets(output, maxAge);
        }
コード例 #11
0
ファイル: Variant.cs プロジェクト: invertedtomato/Amos3
        private void ProcessRequestInner(HttpContext context, HttpRequest request, HttpResponse response)
        {
            if (request.Path == "/null") {
                return;
            }

            // If session
            if (request.Path.Substr(0, 2) == "/(" && request.Path.Substr(24, 1) == ")") {
                // Find sessionId
                Xid sessionId;
                try {
                    sessionId = new Xid(request.Path.Substr(2, 22));
                } catch (FormatException) {
                    SendJsonFailure("Amos3.Session", "Session ID paramater malformed");
                    return;
                }

                // Find session
                Session session;
                if (!Session.TryGet(sessionId, out session)) {
                    SendJsonFailure("Amos3.Session.Expired", "Session has expired");
                    return;
                }

                // Determine if Trigger or RPC
                if (request.Path.CountCharacter('/') == 1) {
                    // RPC
                    RpcHandler(session, request.InputStream, request, response);
                } else {
                    // Trigger
                    TriggerHandler(session, request.Url.Segments[2], request, response);
                }
                return;
            }

            // Find client IP
            var ip = context.Request.ServerVariables["REMOTE_ADDR"];
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            // Handle crawlers
            if (null != context.Request.QueryString["_escaped_fragment_"] && context.Request.QueryString["_escaped_fragment_"].Length > 1) {
                // Mark IP as a crawler for the next 30 seconds
                crawlers[ip] = DateTime.Now;

                context.Response.Status = "301 Moved Permanently";
                context.Response.AddHeader("Location", (RequireSecureHttp ? "https://" : "http://") + context.Request.ServerVariables["SERVER_NAME"] + context.Request.QueryString["_escaped_fragment_"]);
                return;
            }

            if (crawlers.ContainsKey(ip) ||
                context.Request.UserAgent.IndexOf("facebookexternalhit") != -1 ||
                context.Request.UserAgent.IndexOf("Feedfetcher-Google") != -1 ||
                context.Request.UserAgent.IndexOf("bingbot") != -1 ||
                context.Request.UserAgent.IndexOf("Googlebot") != -1 ||
                context.Request.UserAgent.IndexOf("Slurp") != -1 ||
                context.Request.UserAgent.IndexOf("search.msn.com") != -1 ||
                context.Request.UserAgent.IndexOf("nutch") != -1 ||
                context.Request.UserAgent.IndexOf("simpy") != -1 ||
                context.Request.UserAgent.IndexOf("bot") != -1 ||
                context.Request.UserAgent.IndexOf("ASPSeek") != -1 ||
                context.Request.UserAgent.IndexOf("crawler") != -1 ||
                context.Request.UserAgent.IndexOf("msnbot") != -1 ||
                context.Request.UserAgent.IndexOf("ASPSeek") != -1 ||
                context.Request.UserAgent.IndexOf("Libwww-perl") != -1 ||
                context.Request.UserAgent.IndexOf("FAST") != -1 ||
                context.Request.UserAgent.IndexOf("Baidu") != -1 ||
                context.Request.UserAgent.IndexOf("YandexBot") != -1
                ) {
                // Add static content
                StaticRender(context.Request.Url.AbsolutePath.Substring(1), context);

                // Remove old Crawlers
                var limit = DateTime.Now.AddSeconds(-30);
                var remove = new List<string>();
                foreach (var crawler in crawlers) {
                    if (crawler.Value < limit) remove.Add(crawler.Key);
                }
                foreach (var crawlerip in remove) {
                    crawlers.Remove(crawlerip);
                }

                return;
            }

            // If downloading document
            if (request.Path.ToLower().StartsWith("/downloaddocument")) {
                DocumentDownload.GetDocument(request);
                return;
            }

            // If frame
            if (request.Path == "/delegateframe") {
                // Create a new session
                var session = Session.Create(request, this, new EnvelopeHandlerMulti());

                // Send frame
                DelegateFrame.SendFrame(response, session);
                return;
            }

            // If robots
            if (request.Path.ToUpperInvariant() == "/ROBOTS.TXT") {
                Robots(context);
                return;
            }

            // Check we have WAR
            if (null == HttpRootFile) {
                response.Write("No WAR.");
                return;
            }

            // If needed, upgrade the connection to HTTPS
            if (RequireSecureHttp && !request.IsSecureConnection && string.Compare(request.Url.Host, "localhost", true) != 0) {
                var url = request.Url.ToString();
                url = "https" + url.Substr(url.IndexOf(":"));
                response.Redirect(url);
                return;
            }

            // Find target details
            var path = request.Path == "/" ? "/index.html" : request.Path;

            // Stop hacks
            if (path.IndexOf("..") > -1 || path.IndexOf("//") > -1 || path.IndexOf("\\\\") > -1) {
                response.SendFailure(HttpStatusCode.NotFound, "Denied.");
            }

            // Get content
            byte[] page;
            lock (WebCache) {
                // Read cache
                page = WebCache[path] as byte[];

                // If cache miss..
                if (null == page) {

                    // Open WAR file
                    if (null == HttpRoot) {
                        try {
                            HttpRoot = ZipFile.Read(HttpRootFile.FullName);
                        } catch (Exception ex) {
                            // Log the exception
                            LogException(ex, "Variant-HttpRoot", Ids.System);

                            // Warn users that we're updating
                            response.Write("We're currently updating our content. Hang tight - the site will be back shortly!");
                            return;
                        }

                        if (null == HttpRoot["/index.html"]) {
                            throw new Exception("Missing '/index.html' in WAR");
                        }
                    }

                    // Open file
                    ZipEntry file;
                    if (null == (file = HttpRoot[path])) {
                        // File not found - redirect to virtual space

                        response.CacheControl = "public";
                        response.Expires = 31536000;
                        response.ExpiresAbsolute = DateTime.Now.AddYears(1);
                        response.Cache.SetCacheability(HttpCacheability.Public);
                        response.RedirectPermanent("/#!" + path, true);
                        return;
                    }

                    // Extract
                    using (var stream = new MemoryStream()) {
                        file.Extract(stream);
                        page = stream.ReadAllToByteArray();
                    }

                    // Add to cache
                    WebCache[path] = page;
                }
            }

            // Set cache headers
            if (path.Contains(".nocache.")) {
                response.CacheControl = "private";
                response.Expires = 0;
                response.ExpiresAbsolute = DateTime.Now;
                response.Cache.SetCacheability(HttpCacheability.NoCache);
            } else if (path.Contains(".cache.")) {
                response.CacheControl = "public";
                response.Expires = 365 * 24 * 60 * 60;
                response.ExpiresAbsolute = DateTime.Now.AddDays(365);
                response.Cache.SetCacheability(HttpCacheability.Public);
            } else {
                response.CacheControl = "private";
                response.Expires = 1 * 24 * 60 * 60;
                response.ExpiresAbsolute = DateTime.Now.AddDays(1);
                response.Cache.SetCacheability(HttpCacheability.Private);
            }

            // Set content type
            switch (Path.GetExtension(path).ToUpperInvariant()) {
                case ".JS":
                    response.ContentType = "text/javascript";
                    break;
                case ".JSON":
                    response.ContentType = "application/json";
                    break;
                case ".CSS":
                    response.ContentType = "text/css";
                    break;
                case ".JPG":
                case ".JPEG":
                    response.ContentType = "image/jpeg";
                    break;
                case ".PNG":
                    response.ContentType = "image/png";
                    break;
                case ".GIF":
                    response.ContentType = "image/gif";
                    break;
            }

            // Set compression
            response.Compress(request);

            // Write
            response.OutputStream.Write(page, 0, page.Length);
        }
コード例 #12
0
ファイル: Variant.cs プロジェクト: invertedtomato/Amos3
        public void ProcessRequest(HttpContext context)
        {
            if (null == context) {
                throw new ArgumentNullException("context");
            }

            // Start timing
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            try {
                ProcessRequestInner(context, context.Request, context.Response);
            } catch (ThreadAbortException) { } catch (Exception ex) {
                var id = LogException(ex, "ProcessRequest", Ids.System);
                context.Response.SendFailure(HttpStatusCode.InternalServerError, "Unexpected issue. Logged as " + id.ToString());

                // Okay, this is pretty major, let it hit the surface.
                throw;
            } finally {
                // Stop timing
                stopwatch.Stop();

                // Create session in database
                var sessionId = new Xid();
                using (var query = new DatabaseQuery(DatabaseQueryString.Create("RequestLog")) { Type = System.Data.CommandType.StoredProcedure }) {
                    query.Parameters.Add(new SqlParameter("dataSetId", DataSetId.ToGuid()));
                    query.Parameters.Add(new SqlParameter("ip", context.Request.UserHostAddress ?? ""));
                    query.Parameters.Add(new SqlParameter("referrer", null == context.Request.UrlReferrer ? "" : context.Request.UrlReferrer.AbsoluteUri));
                    query.Parameters.Add(new SqlParameter("userAgent", context.Request.UserAgent ?? ""));
                    string langs = null;
                    if (null != context.Request.UserLanguages) {
                        langs = Json.Serialize(context.Request.UserLanguages);
                    }
                    if (null == langs) {
                        langs = "";
                    }
                    query.Parameters.Add(new SqlParameter("languages", langs));

                    query.Parameters.Add(new SqlParameter("path", null == context.Request.Path ? "" : context.Request.Path));
                    query.Parameters.Add(new SqlParameter("sessionId", sessionId.ToGuid()));
                    query.Parameters.Add(new SqlParameter("processingTime", stopwatch.ElapsedMilliseconds));

                    query.Parameters.Add(new SqlParameter("id", System.Data.SqlDbType.UniqueIdentifier) { Direction = System.Data.ParameterDirection.Output });
                    query.Execute();
                }
            }
        }
コード例 #13
0
        public void testInit()
        {
            // TODO How to I add multiple groups to a domain

            Admin = ANodeDomainType.Base.Admin;
            User = new GroupType("QssObZZOF0uULXq19YWOhg");
            domainSystem = DataSet.SystemCreateDomain(Ids.System);
            domainAdmin = DataSet.SystemCreateDomain(Admin.Id);
            domainUser = DataSet.SystemCreateDomain(User.Id);
            node1 = DataSet.SystemCreateNode(domainSystem, ANodeType.Base.Id, Ids.System);
            node2 = DataSet.SystemCreateNode(domainSystem, ANodeType.Base.Id, Ids.System);
            nodeUnloaded = DataSet.GetNode(node2.Id, Ids.System, MaxAge.NoLoad);
        }
コード例 #14
0
ファイル: NRecord.cs プロジェクト: invertedtomato/Amos3
        public bool IsGrouped(Xid domainId, Xid groupId)
        {
            // Load groupsInDomain
            IEnumerable<Xid> groupsInDomain = GetGroup(domainId);

            // Return if the link contains the target nodeId
            return groupsInDomain.Contains(groupId);
        }
コード例 #15
0
ファイル: ARecord.cs プロジェクト: invertedtomato/Amos3
 public ARecordLoader(Xid id, Xid domainId, string fileName, string contentType, long length, Xid? derivedFromId, string key)
     : base(id, domainId, fileName, contentType, length, derivedFromId, key)
 {
 }
コード例 #16
0
ファイル: GroupType.cs プロジェクト: invertedtomato/Amos3
 public GroupType(Xid id)
 {
     Id = id;
 }
コード例 #17
0
ファイル: NRecord.cs プロジェクト: invertedtomato/Amos3
        public void AddLink(int linkKey, Xid destinationId)
        {
            // Fetch/create link
            List<Xid> nodesInKey;
            if (!LinksInternal.TryGetValue(linkKey, out nodesInKey)) {
                LinksInternal[linkKey] = nodesInKey = new List<Xid>();
            }

            // Add node to link
            nodesInKey.Add(destinationId);
        }
コード例 #18
0
ファイル: NRecord.cs プロジェクト: invertedtomato/Amos3
        public void AddGroup(Xid domainId, Xid groupId)
        {
            // Fetch/create groupsInDomain
            List<Xid> groupsInDomain;
            if (!GroupsInternal.TryGetValue(domainId, out groupsInDomain)) {
                groupsInDomain = GroupsInternal[domainId] = new List<Xid>();
            }

            // Add group
            groupsInDomain.Add(groupId);
        }
コード例 #19
0
ファイル: NRecord.cs プロジェクト: invertedtomato/Amos3
 public NRecordLoader(Xid id, Xid domainId, Xid typeId, Xid dataSetId)
     : base(id, domainId, typeId, dataSetId, new Dictionary<int, string>(), new Dictionary<int, List<Xid>>(), new Dictionary<Xid, List<Xid>>())
 {
 }
コード例 #20
0
ファイル: NRecord.cs プロジェクト: invertedtomato/Amos3
        public void SetLinked(int linkKey, Xid nodeId, bool active)
        {
            lock (LinksModifiedInternal) {
                List<Xid> nodesInLink;
                if (!LinksModifiedInternal.TryGetValue(linkKey, out nodesInLink)) {
                    List<Xid> tempLink;
                    if (LinksInternal.TryGetValue(linkKey, out tempLink)) {
                        nodesInLink = new List<Xid>(tempLink);
                    } else {
                        nodesInLink = new List<Xid>();
                    }
                    LinksModifiedInternal[linkKey] = nodesInLink;
                }

                if (active && !nodesInLink.Contains(nodeId)) {
                    nodesInLink.Add(nodeId);
                } else if (!active && nodesInLink.Contains(nodeId)) {
                    nodesInLink.Remove(nodeId);
                }
            }
        }
コード例 #21
0
ファイル: NRecord.cs プロジェクト: invertedtomato/Amos3
        public void SetGrouped(Xid domainId, Xid groupId, bool active)
        {
            lock (GroupsModifiedInternal) {
                List<Xid> groupsInDomain;
                if (!GroupsModifiedInternal.TryGetValue(domainId, out groupsInDomain)) {
                    List<Xid> tempLink;
                    if (GroupsInternal.TryGetValue(domainId, out tempLink)) {
                        groupsInDomain = new List<Xid>(tempLink);
                    } else {
                        groupsInDomain = new List<Xid>();
                    }
                    GroupsModifiedInternal[domainId] = groupsInDomain;
                }

                if (active && !groupsInDomain.Contains(groupId)) {
                    groupsInDomain.Add(groupId);
                } else if (!active && groupsInDomain.Contains(groupId)) {
                    groupsInDomain.Remove(groupId);
                }
            }
        }
コード例 #22
0
ファイル: NRecord.cs プロジェクト: invertedtomato/Amos3
        public bool IsLinked(int linkKey, Xid nodeId)
        {
            // Load link
            IEnumerable<Xid> nodesInLink = GetLink(linkKey);

            // Return if the link contains the target nodeId
            return (new List<Xid>(nodesInLink)).Contains(nodeId);
        }
コード例 #23
0
 internal RecordNotLoadedException(Xid id, Xid? typeId)
     : base("Item accessed before record loaded. Id: " + id + " TypeId: " + typeId + ".")
 {
 }
コード例 #24
0
 internal NodeNotFoundException(Xid nodeId)
     : base("Either you have references a non-existant node, or you are not allowed to access the node. Id:" + nodeId + ".")
 {
 }
コード例 #25
0
 internal InvalidKeyException(Xid id, int key, string message)
     : base("Invalid key " + key + " on type '" + id + "'. " + message)
 {
 }
コード例 #26
0
 public AssetReference(Xid value)
 {
     Value = value.ToString();
 }
コード例 #27
0
 internal KeyNotFoundException(Xid nodeId, int key)
     : base("Either you have references a non-existant key, or you are not allowed to access the key. Key: " + key + " NodeId:" + nodeId + ".")
 {
 }
コード例 #28
0
 internal UnsupportedDataTypeException(Xid id, int propertyKey, Type type)
     : base("Attempted to set property with unsupported data type. NodeId: " + id + " PropertyKey: " + propertyKey + " Type: " + type.ToString() + ".")
 {
 }
コード例 #29
0
 internal TypeIdMismatchException(Xid foundTypeId, Xid expectedTypeId)
     : base("Expected typeId '" + expectedTypeId + "' but found '" + foundTypeId + "'.")
 {
 }
コード例 #30
0
ファイル: NRecord.cs プロジェクト: invertedtomato/Amos3
 public IEnumerable<Xid> GetGroup(Xid domainId)
 {
     lock (GroupsModifiedInternal) {
         List<Xid> groupsInDomain;
         if (GroupsModifiedInternal.TryGetValue(domainId, out groupsInDomain)) {
             return new List<Xid>(groupsInDomain);
         }
         if (GroupsInternal.TryGetValue(domainId, out groupsInDomain)) {
             return new List<Xid>(groupsInDomain);
         }
         return new List<Xid>();
     }
 }