コード例 #1
0
        /// <summary>
        /// Respond to HTTP requests
        /// </summary>
        /// <param name="rq">request parameters</param>
        /// <param name="rp">response parameters</param>
        private void OnResponse(ref HTTPRequestParams rq, ref HTTPResponse rp)
        {
            // Handle post
            if (rq.Method == "POST")
            {
                string postStr = Encoding.ASCII.GetString(rq.BodyData, 0, rq.BodySize);
                Dictionary <string, string> cgivars = new Dictionary <string, string>();

                string[] settings = postStr.Split("&;".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                foreach (string setting in settings)
                {
                    string[] nameValue = setting.Split("=".ToCharArray(), 2);
                    if (nameValue.Length == 2 && nameValue[1] != "")
                    {
                        string name  = HttpUtility.UrlDecode(nameValue[0]);
                        string value = HttpUtility.UrlDecode(nameValue[1]);
                        cgivars.Add(name, value);
                    }
                }

                this.OnPost(cgivars);
                // respond same as for GET
            }

            // handle top using frames
            if (rq.URL == "/")
            {
                string html = this.GetTopPage();
                rp.BodyData = Encoding.ASCII.GetBytes(html);
                return;
            }

            // handle generated menu and forms
            if (rq.URL.EndsWith(".cgi"))
            {
                if (rq.URL == "/menu.cgi")
                {
                    string html = this.GetMenuPage();
                    rp.BodyData = Encoding.ASCII.GetBytes(html);
                }
                else                 // contents
                {
                    string formName = System.IO.Path.GetFileNameWithoutExtension(rq.URL);
                    string html     = this.GetFormPage(formName);
                    rp.BodyData = Encoding.ASCII.GetBytes(html);
                }
                return;
            }

            string path = theFolder + "\\" + rq.URL.Replace("/", "\\");

            path = Path.GetFullPath(path);              // get absolute path
            bool valid = path.StartsWith(theFolder);    // make it secure

            if (valid && Directory.Exists(path))
            {
                if (File.Exists(path + "index.htm"))
                {
                    path += "\\index.htm";
                }
                else
                {
                    string[] dirs  = Directory.GetDirectories(path);
                    string[] files = Directory.GetFiles(path);

                    HtmlBuilder b = new HtmlBuilder();
                    b.open("html");
                    b.open("head");
                    b.close("head");
                    b.open("body");
                    b.open("h2");
                    b.append(b.text("Folder listing for " + path.Substring(theFolder.Length + 1)));
                    b.close("h2");
                    for (int i = 0; i < dirs.Length; i++)
                    {
                        b.link(rq.URL + "/" + Path.GetFileName(dirs[i]),
                               "[" + Path.GetFileName(dirs[i]) + "]");
                        b.br();
                    }
                    for (int i = 0; i < files.Length; i++)
                    {
                        b.link(rq.URL + "/" + Path.GetFileName(files[i]),
                               "[" + Path.GetFileName(files[i]) + "]");
                        b.br();
                    }
                    rp.BodyData = Encoding.ASCII.GetBytes(b.ToString());
                    return;
                }
            }

            if (valid && File.Exists(path))
            {
                RegistryKey rk = Registry.ClassesRoot.OpenSubKey(Path.GetExtension(path), true);

                // Get the data from a specified item in the key.
                String s = (String)rk.GetValue("Content Type");

                // Open the stream and read it back.
                rp.fs = File.Open(path, FileMode.Open);
                if (s != "")
                {
                    rp.Headers["Content-type"] = s;
                }
            }
            else
            {
                rp.Status = (int)HTTPResponseStatus.NOT_FOUND;

                HtmlBuilder b = new HtmlBuilder();
                b.open("html");
                b.open("head");
                b.close("head");
                b.open("body");
                b.append(b.text("File not found!!"));

                rp.BodyData = Encoding.ASCII.GetBytes(b.ToString());
            }
        }