Fawkes API  Fawkes Development Version
page_reply.cpp
1 
2 /***************************************************************************
3  * page_reply.h - Web request reply for a normal page
4  *
5  * Created: Thu Oct 23 16:13:48 2008
6  * Copyright 2006-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include <webview/page_reply.h>
24 #include <webview/page_header_generator.h>
25 #include <webview/page_footer_generator.h>
26 #include <utils/system/hostinfo.h>
27 
28 #include <cstdlib>
29 #include <cstring>
30 #include <cstdio>
31 
32 namespace fawkes {
33 #if 0 /* just to make Emacs auto-indent happy */
34 }
35 #endif
36 
37 /** @class WebPageReply <webview/page_reply.h>
38  * Basic page reply.
39  * This reply adds header and footer as appropriate to form a HTML document
40  * with logo and navigation.
41  * @author Tim Niemueller
42  */
43 
44 /** Page header template. */
45 const char * WebPageReply::PAGE_HEADER =
46  "<html>\n"
47  " <head>\n"
48  " <title>%s</title>\n"
49  " <link rel=\"stylesheet\" type=\"text/css\" href=\"/static/webview.css\" />\n"
50  " </head>\n"
51  " <body>\n";
52 
53 /** Page footer template. */
54 const char * WebPageReply::PAGE_FOOTER =
55  "\n </body>\n"
56  "</html>\n";
57 
58 /** Constructor.
59  * @param title title of the page
60  * @param body Optional initial body of the page
61  */
62 WebPageReply::WebPageReply(std::string title, std::string body)
63  : StaticWebReply(WebReply::HTTP_OK, body)
64 {
65  _title = title;
66 }
67 
68 
69 /** Base constructor.
70  * Constructor that does not set a title or anything. Use for sub-classes.
71  * @param code HTTP code for this reply
72  */
74  : StaticWebReply(code)
75 {
76 }
77 
78 
79 /** Set HTML header text.
80  * The given text is placed in the head section of the HTML page. You can use it
81  * for example to add custom stylesheets or JavaScript.
82  * @param h header to set
83  */
84 void
86 {
87  __html_header = h;
88 }
89 
90 
91 /** Pack web page reply.
92  * This method creates the final page by calling the header and footer generators
93  * if supplied (otherwise a standard header is chosen) and the body.
94  * @param active_baseurl the active navigation URL, can be used for instance
95  * to high-light the current section in the navigation.
96  * @param headergen header generator
97  * @param footergen footer generator
98  */
99 void
100 WebPageReply::pack(std::string active_baseurl,
101  WebPageHeaderGenerator *headergen,
102  WebPageFooterGenerator *footergen)
103 {
104  if (headergen)
105  __merged_body += headergen->html_header(_title, active_baseurl, __html_header);
106  else {
107  fawkes::HostInfo hi;
108  char *s;
109  if ( asprintf(&s, PAGE_HEADER, _title.c_str(), hi.short_name()) != -1 ) {
110  __merged_body += s;
111  free(s);
112  }
113  }
114 
115  __merged_body += _body;
116 
117  if (footergen) __merged_body += footergen->html_footer();
118  else __merged_body += PAGE_FOOTER;
119 }
120 
121 std::string::size_type
123 {
124  return __merged_body.length();
125 }
126 
127 
128 const std::string &
130 {
131  return __merged_body;
132 }
133 
134 } // end namespace fawkes
virtual void set_html_header(std::string h)
Set HTML header text.
Definition: page_reply.cpp:85
const char * short_name()
Get short hostname (up to first dot).
Definition: hostinfo.cpp:114
std::string _title
Title of the page.
Definition: page_reply.h:55
Fawkes library namespace.
virtual const std::string & body()
Get body.
Definition: page_reply.cpp:129
virtual void pack()
Pack the data.
Definition: page_reply.h:43
std::string _body
Body of the reply.
Definition: reply.h:139
Interface for HTML header generator.
Host information.
Definition: hostinfo.h:31
virtual std::string html_header(std::string &title, std::string &active_baseurl, std::string &html_header)=0
Generate HTML header.
response_code_t
HTTP response code.
Definition: reply.h:38
Basic web reply.
Definition: reply.h:34
Interface for HTML footer generator.
virtual std::string::size_type body_length()
Get length of body.
Definition: page_reply.cpp:122
virtual std::string html_footer()=0
Generate HTML footer.
WebPageReply(std::string title, std::string page="")
Constructor.
Definition: page_reply.cpp:62
Static web reply.
Definition: reply.h:125