Ejemplo n.º 1
0
        public static byte[] Response(MyHttpRequestHeader header, string status, string content)
        {
            StringBuilder sb = new StringBuilder();

            byte[] buffer1 = Encoding.UTF8.GetBytes(content);
            switch (status)
            {
            case "404":
                sb.AppendLine("HTTP/1.1 200 OK");
                sb.AppendLine("Server: ScalerServer");
                sb.AppendLine(string.Format("Date: {0}", DateTime.Now.ToString("r")));
                sb.AppendLine("Content-Type: text/html");
                sb.AppendLine(string.Format("Connection: {0}", (header.KeepAlive ? "keep-alive" : "close")));
                sb.AppendLine(string.Format("Content-Length: {0}", buffer1.Length));
                sb.AppendLine();
                break;

            default:
                sb.AppendLine(string.Format("HTTP/1.1 {0} OK", status));
                sb.AppendLine("Server: ScalerServer");
                sb.AppendLine(string.Format("Date: {0}", DateTime.Now.ToString("r")));
                if (content.StartsWith("<", StringComparison.Ordinal))
                {
                    sb.AppendLine("Content-Type: text/xml");
                }
                else if (content.StartsWith("{", StringComparison.Ordinal) && content.EndsWith("}", StringComparison.Ordinal))
                {
                    sb.AppendLine("Content-Type: application/json");
                }
                else
                {
                    sb.AppendLine("Content-Type: text/html");
                }
                sb.AppendLine(string.Format("Connection: {0}", (header.KeepAlive ? "keep-alive" : "close")));
                sb.AppendLine(string.Format("Content-Length: {0}", buffer1.Length));
                sb.AppendLine();
                break;
            }
            byte[] buffer2 = Encoding.UTF8.GetBytes(sb.ToString());
            byte[] buffer  = new byte[buffer1.Length + buffer2.Length];
            buffer2.CopyTo(buffer, 0);
            buffer1.CopyTo(buffer, buffer2.Length);
            return(buffer);
        }
Ejemplo n.º 2
0
        public static byte[] Response(MyHttpRequestHeader header)
        {
#if DEBUG
            DateTime dt0 = DateTime.Now;
#endif
            if (!header.uri.AbsolutePath.EndsWith("/", StringComparison.Ordinal)) //文件
            {
                FileInfo file = new FileInfo(BasicPath + header.uri.AbsolutePath.Substring(1));
                if (file.Name.IndexOf(".") > -1)
                {
                    if (Exts.IndexOf(file.Extension.ToLower() + "|") > -1) //静态文件
                    {
                        return(Response(header, new FileInfo(BasicPath + System.Web.HttpUtility.UrlDecode(header.uri.AbsolutePath))));
                    }
                }
            }
            string path = BasicPath + header.uri.AbsolutePath;
            if (!header.uri.AbsolutePath.EndsWith("/", StringComparison.Ordinal))
            {
                path = path + "/";
            }
            if (File.Exists(path + "default.html"))
            {
                return(Response(header, new FileInfo(path + "default.html")));
            }
            else if (File.Exists(path + "index.html"))
            {
                return(Response(header, new FileInfo(path + "index.html")));
            }
            else if (File.Exists(path + "index.htm"))
            {
                return(Response(header, new FileInfo(path + "index.htm")));
            }
            else if (File.Exists(path + "default.aspx"))
            {
                return(Response(header, "200", "aspx"));
            }
            return(Response(header, "200", path));
        }
Ejemplo n.º 3
0
        public static byte[] Response(MyHttpRequestHeader header, FileInfo file)
        {
            StringBuilder sb = new StringBuilder();

            if (file.Exists)
            {
                sb.AppendLine("HTTP/1.1 200 OK");
                sb.AppendLine("Server: ScalerServer");
                sb.AppendLine(string.Format("Date: {0}", DateTime.Now.ToString("r")));
                switch (file.Extension.ToLower())
                {
                case ".css":
                    sb.AppendLine("Content-Type: text/css; charset=utf-8");
                    break;

                case ".png":
                    sb.AppendLine("Content-Type: image/png; charset=utf-8");
                    break;

                case ".jpg":
                    sb.AppendLine("Content-Type: image/jpeg; charset=utf-8");
                    break;

                case ".jpge":
                    sb.AppendLine("Content-Type: image/jpeg; charset=utf-8");
                    break;

                case ".html":
                    sb.AppendLine("Content-Type: text/html; charset=utf-8");
                    break;

                case ".htm":
                    sb.AppendLine("Content-Type: text/html; charset=utf-8");
                    break;

                case ".txt":
                    sb.AppendLine("Content-Type: text/html; charset=utf-8");
                    break;

                case ".js":
                    sb.AppendLine("Content-Type: application/javascript; charset=utf-8");
                    break;

                case ".gif":
                    sb.AppendLine("Content-Type: image/gif; charset=utf-8");
                    break;

                case ".config":
                    sb.AppendLine("Content-Type: image/gif; charset=utf-8");
                    break;

                case ".cs":
                    sb.AppendLine("Content-Type: image/gif; charset=utf-8");
                    break;

                case ".mdb":
                    sb.AppendLine("Content-Type: image/gif; charset=utf-8");
                    break;

                default:
                    sb.AppendLine(string.Format("Content-Type: application/{0}", file.Extension));
                    break;
                }
                sb.AppendLine(string.Format("Connection: {0}", (header.KeepAlive ? "keep-alive" : "close")));
                sb.AppendLine(string.Format("Content-Length: {0}", file.Length));
                sb.AppendLine();
            }
            else
            {
                string echo = string.Format("文件{0}不存在!", file.FullName);
                sb.AppendLine("HTTP/1.1 200 OK");
                sb.AppendLine("Server: ScalerServer");
                sb.AppendLine(string.Format("Date: {0}", DateTime.Now.ToString("r")));
                sb.AppendLine("Content-Type: text/html; charset=utf-8");
                sb.AppendLine("Connection: keep-alive");
                sb.AppendLine(string.Format("Content-Length: {0}", Encoding.UTF8.GetBytes(echo).Length));
                sb.AppendLine();
                sb.Append(echo);

                return(Encoding.UTF8.GetBytes(sb.ToString()));
            }
            FileStream   fs = file.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
            MemoryStream ms = new MemoryStream();

            ms.Write(Encoding.UTF8.GetBytes(sb.ToString()), 0, sb.Length);
            fs.CopyTo(ms);
            fs.Close();
            fs.Dispose();
            return(ms.ToArray());
        }
Ejemplo n.º 4
0
 public static byte[] Response(MyHttpRequestHeader header, int status)
 {
     return(Response(header, status.ToString(), ""));
 }