/// <summary> /// Load an HTML file, taking into account missing extensions and a file-less IP/domain, which should default to index.html. /// </summary> protected ResponsePacket PageLoader(Route routeHandler, Session session, Dictionary<string, object> kvParams, string fullPath, string ext, ExtensionInfo extInfo) { ResponsePacket ret; if (fullPath == WebsitePath) // If nothing follows the domain name or IP, then default to loading index.html. { ret = Route(session, GET, "/index.html", null); } else { if (String.IsNullOrEmpty(ext)) { // No extension, so we make it ".html" fullPath = fullPath + ".html"; } // Inject the "Pages" folder into the path // fullPath = WebsitePath +"\\Pages" + fullPath.RightOf(WebsitePath); // Custom, for page not found error. if (!File.Exists(fullPath)) { ret = new ResponsePacket() { Error = Server.ServerError.PageNotFound }; Console.WriteLine("!!! File not found: " + fullPath); } else { string text = File.ReadAllText(fullPath); // TODO: We put the route custom post process last because of how content is merged in the application's process, // but this might cause problems if the route post processor adds something that the app's post processor needs to replace. // How do we handle this? A before/after process? CSRF tokens are a great example! // Do the application global post process replacement. text = server.PostProcess(session, fullPath, text); // If a custom post process callback exists, call it. routeHandler.IfNotNull((r) => r.PostProcess.IfNotNull((p) => text = p(session, kvParams, text))); // Do our default post process to catch any final CSRF stuff in the fully merged document. text = server.DefaultPostProcess(session, fullPath, text); ret = new ResponsePacket() { Data = Encoding.UTF8.GetBytes(text), ContentType = extInfo.ContentType, Encoding = Encoding.UTF8 }; } } return ret; }
/// <summary> /// Read in an image file and returns a ResponsePacket with the raw data. /// </summary> protected ResponsePacket ImageLoader(Route routeHandler, Session session, Dictionary<string, object> kvParams, string fullPath, string ext, ExtensionInfo extInfo) { ResponsePacket ret; if (!File.Exists(fullPath)) { ret = new ResponsePacket() { Error = Server.ServerError.FileNotFound }; Console.WriteLine("!!! File not found: " + fullPath); } else { FileStream fStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fStream); ret = new ResponsePacket() { Data = br.ReadBytes((int)fStream.Length), ContentType = extInfo.ContentType }; br.Close(); fStream.Close(); } return ret; }
/// <summary> /// Read in what is basically a text file and return a ResponsePacket with the text UTF8 encoded. /// </summary> protected ResponsePacket FileLoader(Route routeHandler, Session session, Dictionary<string, object> kvParams, string fullPath, string ext, ExtensionInfo extInfo) { ResponsePacket ret; if (!File.Exists(fullPath)) { ret = new ResponsePacket() { Error = Server.ServerError.FileNotFound }; Console.WriteLine("!!! File not found: " + fullPath); } else { string text = File.ReadAllText(fullPath); ret = new ResponsePacket() { Data = Encoding.UTF8.GetBytes(text), ContentType = extInfo.ContentType, Encoding = Encoding.UTF8 }; } return ret; }
/// <summary> /// Load an HTML file, taking into account missing extensions and a file-less IP/domain, which should default to index.html. /// </summary> protected ResponsePacket PageLoader(Route routeHandler, Session session, Dictionary <string, object> kvParams, string fullPath, string ext, ExtensionInfo extInfo) { ResponsePacket ret; if (fullPath == WebsitePath) // If nothing follows the domain name or IP, then default to loading index.html. { ret = Route(session, GET, "/index.html", null); } else { if (String.IsNullOrEmpty(ext)) { // No extension, so we make it ".html" fullPath = fullPath + ".html"; } // Inject the "Pages" folder into the path // fullPath = WebsitePath +"\\Pages" + fullPath.RightOf(WebsitePath); // Custom, for page not found error. if (!File.Exists(fullPath)) { ret = new ResponsePacket() { Error = Server.ServerError.PageNotFound }; Console.WriteLine("!!! File not found: " + fullPath); } else { string text = File.ReadAllText(fullPath); // TODO: We put the route custom post process last because of how content is merged in the application's process, // but this might cause problems if the route post processor adds something that the app's post processor needs to replace. // How do we handle this? A before/after process? CSRF tokens are a great example! // Do the application global post process replacement. text = server.PostProcess(session, fullPath, text); // If a custom post process callback exists, call it. routeHandler.IfNotNull((r) => r.PostProcess.IfNotNull((p) => text = p(session, kvParams, text))); // Do our default post process to catch any final CSRF stuff in the fully merged document. text = server.DefaultPostProcess(session, fullPath, text); ret = new ResponsePacket() { Data = Encoding.UTF8.GetBytes(text), ContentType = extInfo.ContentType, Encoding = Encoding.UTF8 }; } } return(ret); }
/// <summary> /// Read in an image file and returns a ResponsePacket with the raw data. /// </summary> protected ResponsePacket ImageLoader(Route routeHandler, Session session, Dictionary <string, object> kvParams, string fullPath, string ext, ExtensionInfo extInfo) { ResponsePacket ret; if (!File.Exists(fullPath)) { ret = new ResponsePacket() { Error = Server.ServerError.FileNotFound }; Console.WriteLine("!!! File not found: " + fullPath); } else { FileStream fStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fStream); ret = new ResponsePacket() { Data = br.ReadBytes((int)fStream.Length), ContentType = extInfo.ContentType }; br.Close(); fStream.Close(); } return(ret); }
/// <summary> /// Read in what is basically a text file and return a ResponsePacket with the text UTF8 encoded. /// </summary> protected ResponsePacket FileLoader(Route routeHandler, Session session, Dictionary <string, object> kvParams, string fullPath, string ext, ExtensionInfo extInfo) { ResponsePacket ret; if (!File.Exists(fullPath)) { ret = new ResponsePacket() { Error = Server.ServerError.FileNotFound }; Console.WriteLine("!!! File not found: " + fullPath); } else { string text = File.ReadAllText(fullPath); ret = new ResponsePacket() { Data = Encoding.UTF8.GetBytes(text), ContentType = extInfo.ContentType, Encoding = Encoding.UTF8 }; } return(ret); }