Ejemplo n.º 1
0
        public static HttpResponse GetInfo(HttpResponse response, int status)
        {
            String Html_Content;
            //获取服务器日期
            DateTime dt   = DateTime.Now;
            string   Date = dt.GetDateTimeFormats('r')[0].ToString();

            //响应报文首行
            response.Version      = "HTTP/1.1";
            response.StatusCode   = status.ToString();
            response.ReasonPhrase = ReasonPhrase[status];
            //响应首部
            response.Header.Add("Date", Date);
            response.Header.Add("Server", "Niushen/6.6.66(Niuix) DAV/2 mod_jk/1.2.23");
            response.Header.Add("Content-Type", QuickMimeTypeMapper.GetMimeType(".html"));
            Html_Content = Content[status];
            //响应实体
            response.Content = System.Text.Encoding.Default.GetBytes(Html_Content);
            response.Header.Add("Content-Length", response.Content.Length.ToString());
            return(response);
        }
Ejemplo n.º 2
0
        public static void StaticHandler(HttpResponse response, HttpRequest request)
        {
            //解析请求资源Uri
            string resourceUri = ParseUri(request.Uri);
            string filename    = Path.GetFileName(resourceUri);

            //检查文件访问控制属性
            List <string> allowedmethod = GetIni.GetList(Path.GetDirectoryName(resourceUri) + @"\.access.ini", filename);

            //如果请求的方法不在文件的支持方法列表中,则触发405异常
            if (allowedmethod.Contains(request.Method) == false)
            {
                throw new HttpException.HttpException("405 method not allowed", 405);
            }

            //读取文件数据
            byte[] buffer = File.ReadAllBytes(resourceUri);

            //解析文件媒体类型
            string pattern   = @".[^.\/:*?<>|]*$";
            string extension = Regex.Match(filename, pattern).Value;

            response.Header.Add("Content-Type", QuickMimeTypeMapper.GetMimeType(extension));

            string encoding = "identity";   //默认使用identity编码

            if (response.Header.ContainsKey("Content-Encoding"))
            {
                //使用请求指定的编码
                encoding = response.Header["Content-Encoding"];
            }

            //对文件数据进行编码
            switch (encoding)
            {
            case "gzip":
                MemoryStream ms   = new MemoryStream();
                GZipStream   gzip = new GZipStream(ms, CompressionMode.Compress);
                gzip.Write(buffer, 0, buffer.Length);
                gzip.Close();
                response.Content = ms.ToArray();
                break;

            case "compress":
                break;

            case "deflate":
                MemoryStream  ms2     = new MemoryStream();
                DeflateStream deflate = new DeflateStream(ms2, CompressionMode.Compress);
                deflate.Write(buffer, 0, buffer.Length);
                deflate.Close();
                response.Content = ms2.ToArray();
                break;

            case "identity":
                response.Content = buffer;
                break;

            default:
                response.Content = buffer;
                break;
            }
        }