page_reply.cpp

00001 
00002 /***************************************************************************
00003  *  page_reply.h - Web request reply for a normal page
00004  *
00005  *  Created: Thu Oct 23 16:13:48 2008
00006  *  Copyright  2006-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include <webview/page_reply.h>
00024 #include <webview/page_header_generator.h>
00025 #include <webview/page_footer_generator.h>
00026 #include <utils/system/hostinfo.h>
00027 
00028 #include <cstdlib>
00029 #include <cstring>
00030 #include <cstdio>
00031 
00032 namespace fawkes {
00033 #if 0 /* just to make Emacs auto-indent happy */
00034 }
00035 #endif
00036 
00037 /** @class WebPageReply <webview/page_reply.h>
00038  * Basic page reply.
00039  * This reply adds header and footer as appropriate to form a HTML document
00040  * with logo and navigation.
00041  * @author Tim Niemueller
00042  */
00043 
00044 /** Page header template. */
00045 const char *  WebPageReply::PAGE_HEADER =
00046   "<html>\n"
00047   " <head>\n"
00048   "  <title>%s</title>\n"
00049   "  <link rel=\"stylesheet\" type=\"text/css\" href=\"/static/webview.css\" />\n"
00050   " </head>\n"
00051   " <body>\n";
00052 
00053 /** Page footer template. */
00054 const char *  WebPageReply::PAGE_FOOTER =
00055   "\n </body>\n"
00056   "</html>\n";
00057 
00058 /** Constructor.
00059  * @param title title of the page
00060  * @param body Optional initial body of the page
00061  */
00062 WebPageReply::WebPageReply(std::string title, std::string body)
00063   : StaticWebReply(WebReply::HTTP_OK, body)
00064 {
00065   _title = title;
00066 }
00067 
00068 
00069 /** Base constructor.
00070  * Constructor that does not set a title or anything. Use for sub-classes.
00071  * @param code HTTP code for this reply
00072  */
00073 WebPageReply::WebPageReply(response_code_t code)
00074   : StaticWebReply(code)
00075 {
00076 }
00077 
00078 
00079 /** Pack web page reply.
00080  * This method creates the final page by calling the header and footer generators
00081  * if supplied (otherwise a standard header is chosen) and the body.
00082  * @param active_baseurl the active navigation URL, can be used for instance
00083  * to high-light the current section in the navigation.
00084  * @param headergen header generator
00085  * @param footergen footer generator
00086  */
00087 void
00088 WebPageReply::pack(std::string active_baseurl,
00089                    WebPageHeaderGenerator *headergen,
00090                    WebPageFooterGenerator *footergen)
00091 {
00092   if (headergen)  __merged_body += headergen->html_header(_title, active_baseurl);
00093   else {
00094     fawkes::HostInfo hi;
00095     char *s;
00096     if ( asprintf(&s, PAGE_HEADER, _title.c_str(), hi.short_name()) != -1 ) {
00097       __merged_body += s;
00098       free(s);
00099     }
00100   }
00101 
00102   __merged_body  += _body;
00103 
00104   if (footergen)  __merged_body += footergen->html_footer();
00105   else            __merged_body += PAGE_FOOTER;
00106 }
00107 
00108 std::string::size_type
00109 WebPageReply::body_length()
00110 {
00111   return __merged_body.length();
00112 }
00113 
00114 
00115 const std::string &
00116 WebPageReply::body()
00117 {
00118   return __merged_body;
00119 }
00120 
00121 } // end namespace fawkes

Generated on Tue Feb 22 13:32:29 2011 for Fawkes API by  doxygen 1.4.7