/// <summary> /// This function return the default error page as Application Response. /// </summary> /// <param name="request"></param> /// <param name="ex"></param> /// <returns></returns> public static ApplicationResponse Generate404Page(HttpRequest request, string bodyMessage,string pageHeader,string title="Error Page") { //Check if the defaultPage exist if (!File.Exists(HttpApplicationManager.RootDirectory + "\\" + HttpApplicationManager.DefaultPage)) { //No data to sent back so the connection will be close. return new ApplicationResponse(request) { Action = ResponseAction.Disconnect }; } //Get the file byte[] page = Helper.GetFile(HttpApplicationManager.RootDirectory + "\\" + HttpApplicationManager.DefaultPage); string page_str = new String(Encoding.UTF8.GetChars(page)); //fill the page with exception information page_str = page_str.Replace("<%ws_title%>", title); page_str = page_str.Replace("<%ws_domain%>", HttpApplicationManager.CurrentDomain + ":" + HttpApplicationManager.ServicePort); page_str = page_str.Replace("<%ws_header%>", pageHeader); page_str = page_str.Replace("<%ws_message%>", bodyMessage); page = Encoding.UTF8.GetBytes(page_str); //Get the pageHeader byte[] binheader = GetHeader(page.Length,MimeType.text_html,true,false); //build the response byte[] completeResponse = binheader.Concat(page); ApplicationResponse response = new HttpResponse(completeResponse, request); return response; }
/// <summary> /// This method try to find a resource in a ResourceDirectory path and return a response. /// </summary> /// <param name="request"></param> /// <returns></returns> public ApplicationResponse ResponseStaticResource(HttpRequest request) { string resource = request.Path; ApplicationResponse output = null; if (resource[0] != '/') resource = "/" + resource; //Get resource mime type MimeType type = HttpHelper.GetResourceMime(resource); //Check if Gzip compression is enable bool gzip = request.isGZIPSupported(); //Get a file byte[] file = Helper.GetFile(RootDirectory + request.Path); if (file != null) { //compress data id Gzip is supported file = gzip ? HttpHelper.CompressGZIP(file) : file; //get a pageHeader byte[] header = HttpHelper.GetHeader(file.Length, type, true, gzip); //build the complete response byte[] response = header.Concat(file); //create the response output = new HttpResponse(response, request); } else { //file not found: return 404 response output = new HttpResponse(HttpHelper.GetHtml404Header(0, type), request); } return output; }