diff options
-rw-r--r-- | TODO | 1 | ||||
-rw-r--r-- | include/Action.h | 6 | ||||
-rw-r--r-- | include/HtmlSkinner.h | 17 | ||||
-rw-r--r-- | lib/Action.cc | 20 | ||||
-rw-r--r-- | lib/HtmlSkinner.cc | 33 | ||||
-rw-r--r-- | lib/HttpServ.cc | 15 |
6 files changed, 68 insertions, 24 deletions
@@ -1,3 +1,2 @@ -) Add timeouts in HttpServ. -) Add logging support in HttpServ. --) Remove hardcoded skin, and create the HtmlSkin class. diff --git a/include/Action.h b/include/Action.h index 6110945..329592c 100644 --- a/include/Action.h +++ b/include/Action.h @@ -5,6 +5,7 @@ #include <Handle.h> #include <Variables.h> #include <Exceptions.h> +#include <HtmlSkinner.h> //! This is the basic HTTP action. /*! @@ -16,7 +17,7 @@ class Action : public Base { /*! If no URL is provided, one will be generated on the fly. */ - Action(const String & url = ""); + Action(const String & url = "", HtmlSkinner * = 0); virtual ~Action(); //! Will recursively resolve an URL, and return the corresponding action, or NULL if not found. Action * Look4URL(const String &); @@ -42,6 +43,8 @@ class Action : public Base { String GetURL(void); //! Mark this action to be erased as soon as the Do method ends. void CleanUp(void); + //! Skinner accessor. + HtmlSkinner * GetSkinner(); protected: //! Mark this action as beeing accessed. @@ -52,6 +55,7 @@ class Action : public Base { Action * next, * prev; String URL; bool hastoclean, accessed; + HtmlSkinner * skin; }; #endif diff --git a/include/HtmlSkinner.h b/include/HtmlSkinner.h new file mode 100644 index 0000000..6603cde --- /dev/null +++ b/include/HtmlSkinner.h @@ -0,0 +1,17 @@ +#ifndef __HTMLSKINNER_H__ +#define __HTMLSKINNER_H__ + +#include <Exceptions.h> +#include <BString.h> + +class HtmlSkinner : public Base { + public: + HtmlSkinner(); + virtual ~HtmlSkinner(); + virtual String Redirect(const String & location); + virtual String Error(const String & location); + virtual String Header(const String & title); + virtual String Footer(); +}; + +#endif diff --git a/lib/Action.cc b/lib/Action.cc index 23f96d0..2fb0e2a 100644 --- a/lib/Action.cc +++ b/lib/Action.cc @@ -8,6 +8,7 @@ Action * Action::start = 0; static int counter = 0; +static HtmlSkinner default_skinner; static String genurl(const String & u) { if (u.strlen()) { @@ -19,9 +20,12 @@ static String genurl(const String & u) { } } -Action::Action(const String & u) : next(start), prev(0), URL(genurl(u)), hastoclean(false), accessed(false) { +Action::Action(const String & u, HtmlSkinner * _skin) : next(start), prev(0), URL(genurl(u)), hastoclean(false), accessed(false), skin(_skin) { start = this; if (next) next->prev = this; + if (!skin) { + skin = &default_skinner; + } } Action::~Action() { @@ -57,17 +61,15 @@ Action * Action::Look4URL(const String & URL) { */ void Action::SendHead(Handle * h) { - (*h) << "<HTML><HEAD><TITLE>" << GetTitle() << "</TITLE>" << endnl << - "<META http-equiv=\"Content-Style-Type\" content=\"text/css\">" << endnl << - "<LINK REL=STYLESHEET HREF=\"/image/style.css\" TYPE=\"text/css\">" << endnl << - "</HEAD><BODY BGCOLOR=\"#aaaaaa\" BACKGROUND=\"/image/grain.png\">" << endnl << - "<center><TABLE WIDTH=\"50%\" BORDER=3 cellspacing=1 BGCOLOR=\"#ffffff\"><TR><TD>" << endnl << - "<center><b><h1>" << GetTitle() << "</h1></b></center></TD></TR></TABLE><P>" << endnl << - "<TABLE WIDTH=\"80%\" BORDER=2 cellspacing=1 BGCOLOR=\"#ffffff\"><TR><TD>" << endnl; + (*h) << skin->Header(GetTitle()); } void Action::SendFoot(Handle * h) { - (*h) << "</TABLE></TD></TR></BODY></HTML>" << endnl; + (*h) << skin->Footer(); +} + +HtmlSkinner * Action::GetSkinner() { + return skin; } String Action::GetURL(void) { diff --git a/lib/HtmlSkinner.cc b/lib/HtmlSkinner.cc new file mode 100644 index 0000000..31ed083 --- /dev/null +++ b/lib/HtmlSkinner.cc @@ -0,0 +1,33 @@ +#include "HtmlSkinner.h" + +HtmlSkinner::HtmlSkinner() { +} + +HtmlSkinner::~HtmlSkinner() { +} + +String HtmlSkinner::Redirect(const String & location) { + return "<HTML><HEAD><TITLE>301 - Moved Permanently</TITLE></HEAD>\n" + "<BODY><center><b><h2>You should be redirected <a href=\"" + location + "\">here</a> shortly</h2></b></center>\n" + "</BODY></HTML>\n"; +} + +String HtmlSkinner::Error(const String & location) { + return "<HTML><HEAD><TITLE>404 - Error</TITLE></HEAD>\n" + "<BODY><center><b><h2>The server was unable to process your query</h2></b></center>\n" + "Click <A HREF=\"/\">here</A> to go the main page.</BODY></HTML>\n"; +} + +String HtmlSkinner::Header(const String & title) { + return "<HTML><HEAD><TITLE>" + title + "</TITLE>\n" + "<META http-equiv=\"Content-Style-Type\" content=\"text/css\">\n" + "<LINK REL=STYLESHEET HREF=\"/image/style.css\" TYPE=\"text/css\">\n" + "</HEAD><BODY BGCOLOR=\"#aaaaaa\" BACKGROUND=\"/image/grain.png\">\n" + "<center><TABLE WIDTH=\"50%\" BORDER=3 cellspacing=1 BGCOLOR=\"#ffffff\"><TR><TD>\n" + "<center><b><h1>" + title + "</h1></b></center></TD></TR></TABLE><P>\n" + "<TABLE WIDTH=\"80%\" BORDER=2 cellspacing=1 BGCOLOR=\"#ffffff\"><TR><TD>\n"; +} + +String HtmlSkinner::Footer() { + return "</TABLE></TD></TR></BODY></HTML>"; +} diff --git a/lib/HttpServ.cc b/lib/HttpServ.cc index 1105745..9a59dc2 100644 --- a/lib/HttpServ.cc +++ b/lib/HttpServ.cc @@ -359,10 +359,7 @@ void ProcessRequest::SendRedirect(Handle * s) { "Cache-Control: no-cache" << endhl << "Connection: closed" << endhl << "Content-Type: text/html" << endhl << endhl << - "<HTML><HEAD><TITLE>301 - Moved Permanently</TITLE></HEAD>" << endnl << - "<BODY><center><b><h2>You should be redirected to the " << endnl << endnl << - "<a href=\"http://" << host << "/bin/start\">start page</a></h2></b></center>" << endnl << - "</BODY></HTML>" << endnl; + p->GetSkinner()->Redirect("http://" + host + "/bin/start"); } /* @@ -391,15 +388,7 @@ void ProcessRequest::SendHeads(Handle * s, const String & mime, const String & e */ void ProcessRequest::ShowError(Handle * s) { - *s << "HTTP/1.1 404 Not Found" << endhl << - "Server: " << name << endhl << - "Cache-Control: no-cache" << endhl << - "Connection: closed" << endhl << - "Content-Type: text/html" << endhl << endhl << - "<HTML><HEAD><TITLE>404 - Error</TITLE></HEAD>" << endnl << - "<BODY><center><b><h2>The server was unable to process your query</h2></b></center>" << endnl << - "Click <A HREF=\"/\">here</A> to go the main page." << - "</BODY></HTML>" << endnl; + *s << p->GetSkinner()->Error(""); } /* |