Esempio n. 1
0
        private void OutputFileResource(FileContentType fct, FileResource fr, HttpResponse response)
        {
            if (!Debug)
            {
                string IfNoneMatch = response.Request.IfNoneMatch;
                if (!string.IsNullOrEmpty(IfNoneMatch) && IfNoneMatch == fr.FileMD5)
                {
                    if (Server.EnableLog(EventArgs.LogType.Info))
                    {
                        Server.BaseServer.Log(EventArgs.LogType.Info, null, $"HTTP {response.Request.ID} {response.Request.RemoteIPAddress} get {response.Request.Url} source no modify ");
                    }
                    if (Server.Options.StaticResurceCacheTime > 0)
                    {
                        response.Header.Add(HeaderTypeFactory.CACHE_CONTROL, "public, max-age=" + Server.Options.StaticResurceCacheTime);
                    }
                    NoModifyResult result = new NoModifyResult();
                    response.Result(result);
                    return;
                }
            }
            if (!Debug)
            {
                if (!string.IsNullOrEmpty(fr.FileMD5))
                {
                    response.Header.Add(HeaderTypeFactory.ETAG, fr.FileMD5);
                    if (Server.Options.StaticResurceCacheTime > 0)
                    {
                        response.Header.Add(HeaderTypeFactory.CACHE_CONTROL, "public, max-age=" + Server.Options.StaticResurceCacheTime);
                    }
                }
            }
            EventFileResponseArgs efra = new EventFileResponseArgs();

            efra.Request  = response.Request;
            efra.Response = response;
            efra.Resource = fr;
            FileResponse?.Invoke(this, efra);
            if (!efra.Cancel)
            {
                if (fr.GZIP)
                {
                    SetGZIP(response);
                }
                SetChunked(response);
                if (Server.EnableLog(EventArgs.LogType.Info))
                {
                    Server.BaseServer.Log(EventArgs.LogType.Info, null, $"HTTP {response.Request.ID} {response.Request.RemoteIPAddress} get {response.Request.BaseUrl} response gzip {fr.GZIP}");
                }
                HttpToken token = (HttpToken)response.Session.Tag;
                token.File = fr.CreateFileBlock();
                response.SetContentType(fct.ContentType);
                response.Result(token.File);
            }
        }
Esempio n. 2
0
        private void OutputFileResource(FileContentType fct, FileResource fr, HttpResponse response)
        {
            if (!Debug)
            {
                string IfNoneMatch = response.Request.IfNoneMatch;
                if (!string.IsNullOrEmpty(IfNoneMatch) && IfNoneMatch == fr.FileMD5)
                {
                    if (Server.EnableLog(EventArgs.LogType.Info))
                    {
                        Server.BaseServer.Log(EventArgs.LogType.Info, null, "{0} get {1} source no modify ", response.Request.ClientIPAddress, response.Request.Url);
                    }
                    NoModifyResult result = new NoModifyResult();
                    response.Result(result);
                    return;
                }
            }
            if (fr.GZIP)
            {
                SetGZIP(response);
            }
            if (!Debug)
            {
                if (!string.IsNullOrEmpty(fr.FileMD5))
                {
                    response.Header.Add(HeaderTypeFactory.ETAG, fr.FileMD5);
                }
            }
            SetChunked(response);
            EventFileResponseArgs efra = new EventFileResponseArgs();

            efra.Request  = response.Request;
            efra.Response = response;
            efra.Resource = fr;
            FileResponse?.Invoke(this, efra);
            if (Server.EnableLog(EventArgs.LogType.Info))
            {
                Server.BaseServer.Log(EventArgs.LogType.Info, response.Request.Session, "{0} get {1} response gzip {2}",
                                      response.Request.ClientIPAddress, response.Request.BaseUrl, fr.GZIP);
            }
            HttpToken token = (HttpToken)response.Session.Tag;

            token.File = fr.CreateFileBlock();
            response.SetContentType(fct.ContentType);
            response.Result(token.File);
        }
Esempio n. 3
0
        public void OutputFile(FileResult result, HttpRequest request, HttpResponse response)
        {
            var file = result.File;

            if (file.IndexOf(System.IO.Path.DirectorySeparatorChar) == -1)
            {
                var vfile = MatchVirtuslFolder(file);
                if (vfile == null)
                {
                    file = file.Replace('/', System.IO.Path.DirectorySeparatorChar);
                    if (file[0] != System.IO.Path.DirectorySeparatorChar)
                    {
                        file = System.IO.Path.DirectorySeparatorChar + file;
                    }
                    var basePath = Server.Options.StaticResourcePath;
                    if (basePath[basePath.Length - 1] == System.IO.Path.DirectorySeparatorChar)
                    {
                        file = basePath + file.Substring(1);
                    }
                    else
                    {
                        file = basePath + file;
                    }
                }
                else
                {
                    file = vfile;
                }
            }
            var    resource        = new StaticResurce.NoCacheResource(file, "");
            string ext             = System.IO.Path.GetExtension(file).Replace(".", "");
            var    fileContentType = new StaticResurce.FileContentType(ext);

            if (!string.IsNullOrEmpty(result.ContentType))
            {
                fileContentType.ContentType = result.ContentType;
            }
            resource.GZIP = result.GZip;
            EventFileResponseArgs efra = new EventFileResponseArgs();

            efra.Request     = response.Request;
            efra.Response    = response;
            efra.Resource    = resource;
            efra.ContentType = fileContentType;
            FileResponse?.Invoke(this, efra);
            if (!efra.Cancel)
            {
                if (!System.IO.File.Exists(file))
                {
                    NotFoundResult notFound = new NotFoundResult("{0} file not found", request.Url);
                    response.Result(notFound);
                }
                else
                {
                    efra.Resource.Load();
                    if (efra.Resource.GZIP)
                    {
                        SetGZIP(response);
                    }
                    SetChunked(response);
                    if (Server.EnableLog(EventArgs.LogType.Info))
                    {
                        Server.BaseServer.Log(EventArgs.LogType.Info, null, $"HTTP {response.Request.ID} {response.Request.RemoteIPAddress} get {response.Request.BaseUrl} response gzip {efra.Resource.GZIP}");
                    }
                    HttpToken token = (HttpToken)response.Session.Tag;
                    token.File = efra.Resource.CreateFileBlock();
                    response.SetContentType(efra.ContentType.ContentType);
                    response.Result(token.File);
                }
            }
        }