public http_exception (http_request req,String msg,Exception innerex,int http_status):base(msg,innerex) { _res = http_response.CreateBasic(req,http_status); if(http_status >= 200 && http_status != 204 && http_status != 304) { byte[] messagebytes = Encoding.Default.GetBytes(msg); _res.Body.Write(messagebytes,0,messagebytes.Length); _res.Header.ContentLength = (int)_res.Body.Length; _res.Header.ContentType = "text/plain; charset=" +Encoding.Default.WebName.ToUpper(); _res.Header.SetValue("Cache-Control","no-cache"); } }
public static void SendBasicHtml(Stream stream, http_response r) { try { byte[] bytes = r.ToByteArray(); stream.Write(bytes, 0, bytes.Length); } catch(Exception ex) { #if DEBUG Console.Error.WriteLine(ex.Message); Console.Error.WriteLine(ex.StackTrace); #endif } }
public static http_response CreateBasic(http_request req,int http_status) { http_response res = new http_response (); res.Request = req; switch (http_status) { case 200: res.Header.Statusmessage = "OK"; break; case 204: res.Header.Statusmessage = "No Content"; break; case 206: res.Header.Statusmessage = "Partial Content"; break; case 301: res.Header.Statusmessage = "Moved Permanently"; break; case 302: res.Header.Statusmessage = "Found"; break; case 304: res.Header.Statusmessage = "Not Modified"; break; case 400: res.Header.Statusmessage = "Bad Request"; break; case 401: res.Header.Statusmessage = "Unauthorized"; break; case 403: res.Header.Statusmessage = "Forbidden"; break; case 404: res.Header.Statusmessage = "Not Found"; break; case 406: res.Header.Statusmessage = "Not Acceptable"; break; case 407: res.Header.Statusmessage = "Proxy Authentication Required"; break; case 408: res.Header.Statusmessage = "Request Timeout"; break; case 411: res.Header.Statusmessage = "Length Required"; break; case 500: res.Header.Statusmessage = "Internal Server Error"; break; case 501: res.Header.Statusmessage = "Not Implemented"; break; case 502: res.Header.Statusmessage = "Bad Gateway"; break; case 504: res.Header.Statusmessage = "Gateway Timeout"; break; case 505: res.Header.Statusmessage = "HTTP Version Not Supported"; break; default: throw new Exception("Status: " + http_status + " not defined!"); } res.Header.Statuscode = http_status.ToString(); res.Header.SetValue("Date", DateTime.Now.ToUniversalTime().ToString("r")); res.Header.SetValue ( "Server", String.Format("{0}/{1}",Assembly.GetEntryAssembly().GetName().Name,Assembly.GetEntryAssembly().GetName().Version)); if (req.Header.Values.ContainsKey("Connection") && !req.Header.GetValue("Connection").ToLower().Equals("keep-alive")) res.Header.SetValue("Connection", "close"); return res; }