Esempio n. 1
0
        public void GetuidSingleValid()
        {
            DbxReset();

            // Prepare
            var p = new Dictionary<string, object>() { { "string", "string" }, { "number", 1 }, { "boolean", true }, { "stringenum", "a" } };
            var o = Dbx.Create("test", "system", p);
            var expected = Util.JsonSerialize(new ArrayList() { new Dictionary<string, object>() { { "result", "ok" }, { "object", o.Output("system") } } });

            // Execute
            var target = new Getuid();
            target.uids = new List<string>() { o.Uid };
            var actual = Util.JsonSerialize(target.Execute());

            // Cleanup
            o.Delete();

            // Check
            Assert.AreEqual(expected, actual);

            DbxReset();
        }
Esempio n. 2
0
        public void GetuidZeroInvalid()
        {
            DbxReset();

            // Prepare
            var expected = Util.JsonSerialize(new ArrayList() { new Dictionary<string, object>() { { "result", "ok" }, { "object", "" } } });

            // Execute
            var target = new Getuid();
            target.uids = new List<string>() { };

            try {
                var actual = Util.JsonSerialize(target.Execute());
            } catch (UserErrorException) {
                return;
            }

            // Cleanup

            // Check
            Assert.Fail();

            DbxReset();
        }
Esempio n. 3
0
        public void ProcessRequest(HttpContext context)
        {
            try {
                // Set headers
                context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
                context.Response.Expires = -1500;
                context.Response.CacheControl = "private";
                context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                context.Response.Headers["Access-Control-Allow-Origin"] = "*";
                context.Response.Headers["Access-Control-Allow-Methods"] = "GET, POST";
                context.Response.Headers["Access-Control-Allow-Headers"] = "Content-Type, X-Requested-With, X-File-Name";
                context.Response.ContentType = "application/json"; // Must be before queryies - occasionally this is rewritted (IE workaround and Blob)

                string path = "";
                string querystr = "";

                // Check session is active (have they created a session, and have a valid SID?)
                if (!Session.Active) {
                    fail("channel.nosession", "Session not started or valid");
                    return;
                }

                // Figure action
                path = context.Request.RawUrl;// Can't use PathInfo - on certain cases it returns a blank string
                var pos = path.IndexOf('?');
                if (pos < 0) {
                    if (context.Request.InputStream.Length == 0) {
                        fail("query.missing", "Request missing query string");
                        return;
                    } else if (context.Request.InputStream.Length > 250 * 1024) {
                        fail("query.toolong", "Query is too long");
                        return;
                    } else {
                        // POST query

                        querystr = "";
                        var buffer = new byte[1024];
                        var remaining = context.Request.InputStream.Length;

                        while (remaining > 0) {
                            var chunk = context.Request.InputStream.Read(buffer, 0, 1024);
                            querystr += System.Text.Encoding.UTF8.GetString(buffer, 0, chunk);
                            remaining -= chunk;
                        }
                    }
                } else {
                    // GET query
                    path = path.substr(0, pos);

                    // Fetch query sting
                    querystr = context.Request.Url.Query;
                    if (string.IsNullOrEmpty(querystr)) {
                        fail("query.invalid.blank", "Query is blank");
                        return;
                    }
                    querystr = querystr.Substring(1);

                    // Decode URL
                    querystr = HttpUtility.UrlDecode(querystr).Trim();
                }
                path = path.substr(path.LastIndexOf('/') + 1);

                // Check if the SID has been sync'd
                if (!Session.Current.Synced) {
                    fail("channel.nosync", "Session not synced");
                    return;
                }

                // Check we are secure (except if AllowInsecure,
                if (!context.Request.IsSecureConnection && !Session.Current.AllowInsecure) {
                    fail("channel.nosecure", "Session not secure - please use https");
                    return;
                }

                // Match query to query provider
                Amos.Query.Interface query = null;
                try {
                    switch (path) {
                        case "getuid":
                            query = new Getuid();
                            ((Getuid)query).uids = Util.JsonDeserialize<List<string>>(querystr);
                            break;
                        case "geteq":
                            query = Util.JsonDeserialize<Geteq>(querystr);
                            break;
                        case "create":
                            query = Util.JsonDeserialize<Create>(querystr);
                            break;
                        case "update":
                            query = new Update();
                            ((Update)query).items = Util.JsonDeserialize<List<UpdateItem>>(querystr);
                            break;
                        case "impersonate":
                            query = Util.JsonDeserialize<Impersonate>(querystr);
                            break;
                        case "fn":
                            query = Util.JsonDeserialize<Function>(querystr);
                            break;
                        case "authenticate":
                            query = Util.JsonDeserialize<Authenticate>(querystr);
                            break;
                        case "me":
                            query = Util.JsonDeserialize<Me>(querystr);
                            break;
                        default:
                            fail("badmethod", "Unsupported method '" + path + "'");
                            return;
                    }
                } catch (ArgumentException e) {
                    fail("query.invalid.parse", "Invalid query string - " + e.Message);
                    return;
                } catch (InvalidOperationException) {
                    fail("query.invalid.parameters", "Invalid query structure");
                    return;
                } catch (Exception) {
                    fail("query.invalid.other", "Failed processing query");
                    return;
                }

                // Execute
                var output = new Dictionary<string, object>() { { "ok", true } };

                // Start timing
                var watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                output["data"] = query.Execute();

                // Stop timing
                watch.Stop();
                output["time"] = watch.ElapsedMilliseconds;

                // Print output
                context.Response.Write(Util.JsonSerialize(output));

            } catch (UserErrorException e) {
                // Fail nicely with user issues
                fail(e.Type, e.Message);
            } catch (System.Threading.ThreadAbortException) {
            } catch (Exception e) {
                // Fail bluntly with our issues

                Log.Exception(Session.Current.Tid, Session.Current.Sid, Session.Current.Uid, e.GetType().ToString(), e.Source, e.TargetSite.ToString(), e.Message, e.StackTrace);

                // If debug, continue
                //if (Config.Debug) throw;

                // Dump generic error
                fail("unspecified", "A serverside error has occured");
            }
        }