예제 #1
0
        async Task HandleClientConnections(HttpListener listener)
        {
            var context = await listener.GetContextAsync().ConfigureAwait(false);

            var action = GetSiteAction(ref context);

            var method   = (context.Request.HttpMethod == "POST") ? HTTPMethod.POST : HTTPMethod.GET;
            var url      = context.Request.Url.PathAndQuery.Split('?');
            var path     = GetContentType((url[0] == "/") ? "/index.html" : url[0], ref context);
            var formdata = GetPostData((url.Length > 1 && method == HTTPMethod.GET) ?
                                       string.Format("?{0}", url[1]) : path, ref context, method);

            var target = GetRequestTarget(ref path, ref context);

            var provider = GetRequestedProvider(ref path);
            var reqType  = GetRequestType(ref context, ref provider);

            switch (target)
            {
            case RequestTarget.Site:
            case RequestTarget.Provider:
                var directEvArgs = new DataReceivedEventArgs();
                directEvArgs.Action      = action;
                directEvArgs.Context     = context;
                directEvArgs.Method      = method;
                directEvArgs.UserAgent   = context.Request.Headers["UAgent"];
                directEvArgs.Params      = formdata;
                directEvArgs.Provider    = provider;
                directEvArgs.Path        = path.Split('?')[0];
                directEvArgs.RequestType = reqType;
                directEvArgs.Target      = target;

                DataReceived?.Invoke(this, directEvArgs);
                break;

            case RequestTarget.File:
                var filePath = GetContentType(Filesystem.Combine(docroot, path), ref context);
                var data     = SendErrorDocument(404, string.Format("Not Found ({0})!", filePath), ref context);

                using (var fs = new Filesystem(docroot))
                {
                    if (fs.Exists(filePath))
                    {
                        Array.Clear(data, 0, data.Length);
                        data = fs.Read(filePath).Result;

                        context.Response.StatusCode        = 200;
                        context.Response.StatusDescription = "OK";
                    }
                }

                Send(ref data, ref context);
                break;

            default:
                break;
            }

            await HandleClientConnections(listener).ConfigureAwait(false);
        }
예제 #2
0
        public static string ReadTemplate(ref Filesystem fs, string name)
        {
            var tpl_path = string.Format("templates/{0}.tpl", name.Replace("-", "_"));

            if (!fs.Exists(tpl_path))
            {
                return(string.Format("<p class=\"exclaim\">Dispatcher (Template file for \"{0}\" was not found!</p>", name));
            }
            else
            {
                var output = fs.Read(tpl_path);
                return(Encoding.UTF8.GetString(output, 0, output.Length));
            }
        }
예제 #3
0
        void HandleClientConnections(IAsyncResult ar)
        {
            var context = listener.EndGetContext(ar);
            var reqType = RequestType.Sync;
            var target  = RequestTarget.Site;
            var action  = SiteAction.None;
            var method  = (context.Request.HttpMethod == "POST") ?
                          HTTPMethod.POST : HTTPMethod.GET;

            var path = GetContentType(context.Request.Url.LocalPath == "/" ? "/index.html" :
                                      context.Request.Url.LocalPath, ref context);

            var formdata = GetPostData(fs, path, context, method).Result;

            var provider = Provider.None;

            if (path.StartsWith("/providers/"))
            {
                target = RequestTarget.api;

                if (path.EndsWith("/server/"))
                {
                    provider = Provider.Server;
                }

                if (path.EndsWith("/users/"))
                {
                    provider = Provider.User;
                }

                if (path.EndsWith("/navigation/"))
                {
                    provider = Provider.Navigation;
                }

                if (path.EndsWith("/news/"))
                {
                    provider = Provider.News;
                }

                if (path.EndsWith("/admincp/"))
                {
                    provider = Provider.Admin;
                }
            }
            else
            {
                target = RequestTarget.Site;
            }

            var req = context.Request.Headers["Request-Type"];

            if (string.IsNullOrEmpty(req))
            {
                req = (target == RequestTarget.Site) ? "sync" : "async";
            }

            // In general we have this Header when we use Ajax!
            if (context.Request.Headers["X-Requested-With"] != null)
            {
                if (context.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
                {
                    req = "async";
                }
            }

            #region "Action"
            var act = context.Request.Headers["Action"];
            if (string.IsNullOrEmpty(act))
            {
                act = "none";
            }

            switch (act.ToLower())
            {
            case "add":
                action = SiteAction.Add;
                break;

            case "edit":
                action = SiteAction.Edit;
                break;

            case "del":
                action = SiteAction.Remove;
                break;

            case "login":
                action = SiteAction.Login;
                break;

            case "logout":
                action = SiteAction.Logout;
                break;

            case "clients":
                action = SiteAction.Clients;
                break;

            case "profile":
                action = SiteAction.Profile;
                break;

            case "show":
                action = SiteAction.Show;
                break;

            case "metadata":
                action = SiteAction.MetaData;
                break;

            case "register":
                action = SiteAction.Register;
                break;

            case "none":
                action = SiteAction.None;
                break;

            case "execute":
                action = SiteAction.Execute;
                break;

            case "teamlist":
                action   = SiteAction.Teamlist;
                provider = Provider.User;
                break;

            default:
                if (reqType != RequestType.Sync)
                {
                    throw new Exception(string.Format("<void> HandleClientConnections() -> " +
                                                      " (Get) SiteAction: Got unknown SiteAction \"{0}\"!", act.ToLower()));
                }
                break;
            }
            #endregion

            switch (req.ToLower())
            {
            case "async":
                reqType = RequestType.Async;
                break;

            case "sync":
                reqType = RequestType.Sync;
                break;

            default:
                throw new Exception(string.Format("<void> HandleClientConnections() -> " +
                                                  "(Get) RequestType: Got unknown request \"{0}\"!", req.ToLower()));
            }

            switch (target)
            {
            case RequestTarget.api:
            case RequestTarget.Site:
                if (path.EndsWith("/") || path == "/" || path.EndsWith(".html") || path.EndsWith(".htm"))
                {
                    DirectRequestReceived(this, new DirectRequestEventArgs
                    {
                        Method      = method,
                        Provider    = provider,
                        Context     = context,
                        Params      = formdata,
                        RequestType = reqType,
                        Target      = target,
                        Instance    = instance,
                        Path        = path.Split('?')[0],
                        Action      = action
                    });
                }
                else
                {
                    if (fs.Exists(path))
                    {
                        context.Response.StatusCode        = 200;
                        context.Response.StatusDescription = "OK";

                        var data = fs.Read(path);

                        if (path.EndsWith(".js"))
                        {
                            var _tmpText = Encoding.UTF8.GetString(data).Replace("[#XDMP_AUTH#]", this.passKey.ToString());
                            data = Encoding.UTF8.GetBytes(_tmpText);
                        }

                        Send(ref data, ref context);
                    }
                    else
                    {
                        var data = SendErrorDocument(title, 404,
                                                     string.Format("Datei \"{0}\" nicht gefunden!", path),
                                                     ref context);

                        Send(ref data, ref context);
                    }
                }
                break;

            default:
                throw new Exception(string.Format("<void> HandleClientConnections()" +
                                                  " -> (Get) RequestTarget: Got unknown request target \"{0}\"!", target.ToString()));
            }

            listener.BeginGetContext(HandleClientConnections, null);
        }