Exemplo n.º 1
0
        public void ProcessFile(HttpRequest request, HttpResponse response)
        {
            string url = HttpParse.CharToLower(request.BaseUrl);

            if (url == "/")
            {
                for (int i = 0; i < mDefaultPages.Count; i++)
                {
                    string          defaultpage = url + mDefaultPages[i];
                    string          ext         = HttpParse.GetBaseUrlExt(defaultpage);
                    FileContentType fct         = null;
                    if (!mExts.TryGetValue(ext, out fct))
                    {
                        continue;
                    }
                    FileResource fr = GetFileResource(defaultpage);

                    if (fr != null)
                    {
                        OutputFileResource(fct, fr, response);
                        return;
                    }
                }
                response.NotFound();
                Server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "http {1} {0} not found", request.BaseUrl, request.ClientIPAddress);
                return;
            }

            if (ExtSupport(request.Ext))
            {
                FileContentType fct = mExts[request.Ext];
                FileResource    fr  = GetFileResource(url);
                if (fr != null)
                {
                    OutputFileResource(fct, fr, response);
                }
                else
                {
                    if (ExistsFile(request.BaseUrl))
                    {
                        string file = GetFile(url);
                        fr = CreateResource(file);
                        if (fr != null)
                        {
                            OutputFileResource(fct, fr, response);
                        }
                    }
                    else
                    {
                        response.NotFound();
                        Server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "http {1} {0} not found", request.BaseUrl, request.ClientIPAddress);
                    }
                }
            }
            else
            {
                response.NotSupport();
                Server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "http {1} {0} not support", request.BaseUrl, request.ClientIPAddress);
            }
        }
Exemplo n.º 2
0
        private void OutputFileResource(FileContentType fct, FileResource fr, HttpResponse response)
        {
            if (!Debug)
            {
                if (response.Request.IfNoneMatch == fr.FileMD5)
                {
                    response.NoModify();
                    return;
                }
            }

            if (fr.GZIP)
            {
                SetGZIP(response);
            }
            if (!Debug)
            {
                if (fr.Cached)
                {
                    response.Header.Add(HeaderType.CACHE_CONTROL, "private, max-age=31536000");
                }
                else
                {
                    response.Header.Add(HeaderType.ETAG, fr.FileMD5);
                }
            }
            SetChunked(response);
            HttpToken token = (HttpToken)response.Session.Tag;

            token.File = new FileBlock(fr);
            response.SetContentType(fct.ContentType);
            response.Result(token.File);
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
 public void SetFileExts(string exts)
 {
     if (exts != null)
     {
         Server.Options.StaticResurceType = exts;
         foreach (string item in exts.ToLower().Split(';'))
         {
             FileContentType fct = new FileContentType(item);
             mExts[fct.Ext] = fct;
         }
     }
 }
Exemplo n.º 5
0
        public ResourceCenter(HttpApiServer server)
        {
            Server = server;
            Path   = Server.Options.StaticResourcePath;
            if (Path[Path.Length - 1] != System.IO.Path.DirectorySeparatorChar)
            {
                Path += System.IO.Path.DirectorySeparatorChar;
            }

            foreach (string item in server.Options.StaticResurceType.ToLower().Split(';'))
            {
                FileContentType fct = new FileContentType(item);
                mExts[fct.Ext] = fct;
            }
            mDefaultPages.AddRange(Server.Options.DefaultPage.Split(";"));
        }
Exemplo n.º 6
0
        private void OutputFileResource(FileContentType fct, FileResource fr, HttpResponse response)
        {
            if (!Debug)
            {
                if (response.Request.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);
                    }
                    response.NoModify();
                    return;
                }
            }
            else
            {
                fr.Load();
            }

            if (fr.GZIP)
            {
                SetGZIP(response);
            }
            if (!Debug)
            {
                if (fr.Cached)
                {
                    response.Header.Add(HeaderType.CACHE_CONTROL, "private, max-age=31536000");
                }
                else
                {
                    response.Header.Add(HeaderType.ETAG, fr.FileMD5);
                }
            }
            SetChunked(response);
            HttpToken token = (HttpToken)response.Session.Tag;

            token.File = fr.CreateFileBlock();
            response.SetContentType(fct.ContentType);
            response.Result(token.File);
            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);
            }
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        public void ProcessFile(HttpRequest request, HttpResponse response)
        {
            FileContentType fct    = null;
            FileResource    fr     = null;
            string          url    = request.BaseUrl;
            var             result = MatchVirtuslFolder(request.BaseUrl);

            if (result != null)
            {
                FileResult fileResult = new FileResult(result);
                OutputFile(fileResult, request, response);
                return;
            }

            if (url[url.Length - 1] == '/')
            {
                for (int i = 0; i < mDefaultPages.Count; i++)
                {
                    string defaultpage = url + mDefaultPages[i];
                    string ext         = HttpParse.GetBaseUrlExt(defaultpage);

                    if (!mExts.TryGetValue(ext, out fct))
                    {
                        continue;
                    }
                    fr = GetFileResource(defaultpage);
                    if (fr != null)
                    {
                        OutputFileResource(fct, fr, response);
                        return;
                    }
                }
                if (Server.EnableLog(EventArgs.LogType.Warring))
                {
                    Server.BaseServer.Log(EventArgs.LogType.Warring, null, $"HTTP {request.ID} {request.RemoteIPAddress} get {request.Url} file not found");
                }
                if (!Server.OnHttpRequesNotfound(request, response).Cancel)
                {
                    NotFoundResult notFound = new NotFoundResult("{0} file not found", request.Url);
                    response.Result(notFound);
                }
                return;
            }
            if (ExtSupport(request.Ext))
            {
                url = System.Net.WebUtility.UrlDecode(url);
                fct = mExts[request.Ext];
                fr  = GetFileResource(url);
                if (!Server.Options.Debug && fr != null)
                {
                    OutputFileResource(fct, fr, response);
                }
                else
                {
                    string file;
                    string fileurl = HttpParse.GetBaseUrl(request.Url);
                    fileurl = System.Net.WebUtility.UrlDecode(fileurl);
                    if (ExistsFile(request, fileurl, out file))
                    {
                        fr = CreateResource(file, false);
                        if (fr != null)
                        {
                            OutputFileResource(fct, fr, response);
                        }
                    }
                    else
                    {
                        if (Server.EnableLog(EventArgs.LogType.Warring))
                        {
                            Server.BaseServer.Log(EventArgs.LogType.Warring, null, $"HTTP {request.ID} {request.RemoteIPAddress} get {request.Url} file not found");
                        }
                        if (!Server.OnHttpRequesNotfound(request, response).Cancel)
                        {
                            NotFoundResult notFound = new NotFoundResult("{0} file not found", request.Url);
                            response.Result(notFound);
                        }
                    }
                }
            }
            else
            {
                if (Server.EnableLog(EventArgs.LogType.Warring))
                {
                    Server.BaseServer.Log(EventArgs.LogType.Warring, null, $"HTTP {request.ID} {request.RemoteIPAddress} get { request.BaseUrl} file ext not support");
                }
                NotSupportResult notSupport = new NotSupportResult("get {0} file {1} ext not support", request.Url, request.Ext);
                response.Result(notSupport);
            }
        }
Exemplo n.º 9
0
        public void ProcessFile(HttpRequest request, HttpResponse response)
        {
            string url = HttpParse.CharToLower(request.BaseUrl);

            if (url == "/")
            {
                for (int i = 0; i < mDefaultPages.Count; i++)
                {
                    string          defaultpage = url + mDefaultPages[i];
                    string          ext         = HttpParse.GetBaseUrlExt(defaultpage);
                    FileContentType fct         = null;
                    if (!mExts.TryGetValue(ext, out fct))
                    {
                        continue;
                    }
                    FileResource fr = GetFileResource(defaultpage);
                    if (fr != null)
                    {
                        OutputFileResource(fct, fr, response);
                        return;
                    }
                }
                if (Server.EnableLog(EventArgs.LogType.Warring))
                {
                    Server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "{0} get {1} file not found", request.ClientIPAddress, request.BaseUrl);
                }
                if (!Server.OnHttpRequesNotfound(request, response).Cancel)
                {
                    NotFoundResult notFound = new NotFoundResult("{0} file not found", request.Url);
                    response.Result(notFound);
                }
                return;
            }

            if (ExtSupport(request.Ext))
            {
                FileContentType fct = mExts[request.Ext];
                FileResource    fr  = GetFileResource(url);
                if (fr != null)
                {
                    OutputFileResource(fct, fr, response);
                }
                else
                {
                    string file;
                    string fileurl = HttpParse.GetBaseUrl(request.Url);
                    if (ExistsFile(request, fileurl, out file))
                    {
                        fr = CreateResource(file, false);
                        if (fr != null)
                        {
                            OutputFileResource(fct, fr, response);
                        }
                    }
                    else
                    {
                        if (Server.EnableLog(EventArgs.LogType.Warring))
                        {
                            Server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "{0} get {1} file not found", request.ClientIPAddress, request.BaseUrl);
                        }
                        if (!Server.OnHttpRequesNotfound(request, response).Cancel)
                        {
                            NotFoundResult notFound = new NotFoundResult("{0} file not found", request.Url);
                            response.Result(notFound);
                        }
                    }
                }
            }
            else
            {
                if (Server.EnableLog(EventArgs.LogType.Warring))
                {
                    Server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "{0} get {1} file ext not support", request.ClientIPAddress, request.BaseUrl);
                }
                NotSupportResult notSupport = new NotSupportResult("get {0} file {1} ext not support", request.Url, request.Ext);
                response.Result(notSupport);
            }
        }