public void LoadManifestResource(System.Reflection.Assembly assembly) { string[] files = assembly.GetManifestResourceNames(); string tmpFolder = "_tempview"; if (!System.IO.Directory.Exists(tmpFolder)) { Directory.CreateDirectory(tmpFolder); } foreach (string item in files) { int offset = item.IndexOf(".views"); if (offset > 0) { string url = GetResourceUrl(item.Substring(offset + 6, item.Length - offset - 6)); string ext = System.IO.Path.GetExtension(item).ToLower(); ext = ext.Substring(1, ext.Length - 1); if (mExts.ContainsKey(ext)) { string urlname = url; string filename = tmpFolder + System.IO.Path.DirectorySeparatorChar + item; SaveTempFile(assembly, item, filename); FileResource fr; bool nogzip = !(Server?.Options.NoGzipFiles.IndexOf(ext) >= 0); bool cachefile = Server?.Options.CacheFiles.IndexOf(ext) >= 0; if (Debug) { fr = new NoCacheResource(filename, urlname); if (nogzip) { fr.GZIP = true; } } else { if (cachefile) { fr = new FileResource(filename, urlname); } else { fr = new NoCacheResource(filename, urlname); if (nogzip) { fr.GZIP = true; } } } mResources[urlname] = fr; fr.Load(); if (Server != null && Server.BaseServer != null) { if (Server.BaseServer.EnableLog(EventArgs.LogType.Info)) { Server.BaseServer.Log(EventArgs.LogType.Info, null, "load static resource " + urlname); } } } } } }
public void ProcessFile(HttpRequest request, HttpResponse response) { string url = request.BaseUrl; //if (Server.Options.UrlIgnoreCase) // url = HttpParse.CharToLower(request.BaseUrl); if (url[url.Length - 1] == '/') { 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, 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); 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); 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); } }
private FileResource CreateResource(string file, bool cache) { try { string ext = System.IO.Path.GetExtension(file).ToLower(); ext = ext.Substring(1, ext.Length - 1); if (mExts.ContainsKey(ext)) { FileResource fr; string urlname = ""; urlname = GetUrl(file); if (cache) { if (!Debug) { if (mResources.TryGetValue(urlname, out fr)) { if (Server.BaseServer.GetRunTime() - fr.CreateTime < 2000) { return(fr); } } } } bool nogzip = !(Server.Options.NoGzipFiles.IndexOf(ext) >= 0); bool cachefile = Server.Options.CacheFiles.IndexOf(ext) >= 0; if (Debug) { fr = new NoCacheResource(file, urlname); if (nogzip) { fr.GZIP = true; } } else { FileInfo info = new FileInfo(file); if (cachefile && info.Length < 1024 * Server.Options.CacheFileSize) { fr = new FileResource(file, urlname); } else { fr = new NoCacheResource(file, urlname); if (nogzip) { fr.GZIP = true; } } } fr.Load(); fr.CreateTime = Server.BaseServer.GetRunTime(); if (cache && mResources.Count < 5000) { mResources[urlname] = fr; } if (Server.EnableLog(EventArgs.LogType.Info)) { Server.BaseServer.Log(EventArgs.LogType.Info, null, "update {0} static resource success", urlname); } return(fr); } } catch (Exception e_) { Server.BaseServer.Error(e_, null, "update {0} resource error {1}", file, e_.Message); } return(null); }
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(); if (Server.EnableLog(EventArgs.LogType.Warring)) { Server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "{0} get {1} file not found", request.ClientIPAddress, request.BaseUrl); } 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(); if (Server.EnableLog(EventArgs.LogType.Warring)) { Server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "{0} get {1} file not found", request.ClientIPAddress, request.BaseUrl); } } } } else { response.NotSupport(); if (Server.EnableLog(EventArgs.LogType.Warring)) { Server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "{0} get {1} file not found", request.ClientIPAddress, request.BaseUrl); } } }