Exemple #1
0
        public string ProcessHTTPRequest(string request, ClientConnection _connection)
        {
            string path;
            Dictionary <string, string> ParamList;

            if (!HttpHelper.ParseRequest(request, out path, out ParamList))
            {
                Console.Out.Write("Error: bad request");
                return("error");
            }

            //	parse global set-parameters
            Dictionary <string, string> SkippedParams = new Dictionary <string, string>();

            {
                foreach (var p in ParamList)
                {
                    bool handled = false;

                    if (p.Key.ToUpper() == "EXEC")
                    {
                        if (Execute(p.Value))
                        {
                            handled = true;
                        }
                    }

                    if (!handled)
                    {
                        SkippedParams[p.Key] = p.Value;
                    }
                }
                ParamList = SkippedParams;
            }

            //  file-oriented webserver
            if (path == "")
            {
                path = "index.html";
            }

            string wwwFilename = wwwRootFolder + path;

            //	check for .cfg-requests
            string suffix = "";

            if (path.Length > 0)
            {
                int p = path.LastIndexOf('.');
                if (p >= 0)
                {
                    suffix = path.Substring(p + 1);
                }
            }

            switch (suffix.ToUpper())
            {
            case "HTML":
            {
                break;
            }

            case "CFG":
            {
                string cfg = CreateCFGFile(path);

                StreamWriter o = File.CreateText(wwwRootFolder + path);
                o.Write(cfg);
                o.Close();

                break;
            }

            case "JS":
            {
                if (path.ToUpper().EndsWith("SERVERSETUP.JS"))
                {
                    string SetupJS = CreateSetupJS(wwwRootFolder + "ServerSetup.js.template", _connection);
                    return(SetupJS);
                }
                break;
            }
            }

            if (path.ToUpper() == "DUMP")
            {
                //ProcessMemoryDump(ParamList["Adr"]);
            }

            if (File.Exists(wwwFilename))
            {
                Console.Out.Write("GET {0}", wwwFilename);

                string html = File.ReadAllText(wwwFilename);
                // parameter injection
                var e = ParamList.GetEnumerator();
                while (e.MoveNext())
                {
                    string key = string.Format("%{0}%", e.Current.Key);
                    html = html.Replace(key, e.Current.Value);
                }

                return(html);
            }

            Console.Out.Write("GET {0}", path);
            return(string.Format("Error: invalid URL {0}", path));
        }