Fawkes API
Fawkes Development Version
|
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 /** Set HTML header text. 00080 * The given text is placed in the head section of the HTML page. You can use it 00081 * for example to add custom stylesheets or JavaScript. 00082 * @param h header to set 00083 */ 00084 void 00085 WebPageReply::set_html_header(std::string h) 00086 { 00087 __html_header = h; 00088 } 00089 00090 00091 /** Pack web page reply. 00092 * This method creates the final page by calling the header and footer generators 00093 * if supplied (otherwise a standard header is chosen) and the body. 00094 * @param active_baseurl the active navigation URL, can be used for instance 00095 * to high-light the current section in the navigation. 00096 * @param headergen header generator 00097 * @param footergen footer generator 00098 */ 00099 void 00100 WebPageReply::pack(std::string active_baseurl, 00101 WebPageHeaderGenerator *headergen, 00102 WebPageFooterGenerator *footergen) 00103 { 00104 if (headergen) 00105 __merged_body += headergen->html_header(_title, active_baseurl, __html_header); 00106 else { 00107 fawkes::HostInfo hi; 00108 char *s; 00109 if ( asprintf(&s, PAGE_HEADER, _title.c_str(), hi.short_name()) != -1 ) { 00110 __merged_body += s; 00111 free(s); 00112 } 00113 } 00114 00115 __merged_body += _body; 00116 00117 if (footergen) __merged_body += footergen->html_footer(); 00118 else __merged_body += PAGE_FOOTER; 00119 } 00120 00121 std::string::size_type 00122 WebPageReply::body_length() 00123 { 00124 return __merged_body.length(); 00125 } 00126 00127 00128 const std::string & 00129 WebPageReply::body() 00130 { 00131 return __merged_body; 00132 } 00133 00134 } // end namespace fawkes