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 <utils/system/hostinfo.h>
24#include <webview/page_footer_generator.h>
25#include <webview/page_header_generator.h>
26#include <webview/page_reply.h>
27
28#include <cstdio>
29#include <cstdlib>
30#include <cstring>
31
32namespace fawkes {
33
34/** @class WebPageReply <webview/page_reply.h>
35 * Basic page reply.
36 * This reply adds header and footer as appropriate to form a HTML document
37 * with logo and navigation.
38 * @author Tim Niemueller
39 */
40
41/** Page header template. */
42const char *WebPageReply::PAGE_HEADER =
43 "<html>\n"
44 " <head>\n"
45 " <title>%s</title>\n"
46 " <link rel=\"stylesheet\" type=\"text/css\" href=\"/static/css/webview.css\" />\n"
47 "%s"
48 " </head>\n"
49 " <body>\n";
50
51/** Page footer template. */
52const char *WebPageReply::PAGE_FOOTER = "\n </body>\n"
53 "</html>\n";
54
55/** Constructor.
56 * @param title title of the page
57 * @param body Optional initial body of the page
58 */
59WebPageReply::WebPageReply(std::string title, std::string body)
60: StaticWebReply(WebReply::HTTP_OK, body)
61{
62 _title = title;
63 navbar_enabled_ = true;
64 footer_enabled_ = true;
65
66 add_header("Content-type", "text/html");
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{
75 navbar_enabled_ = true;
76 footer_enabled_ = true;
77
78 add_header("Content-type", "text/html");
79}
80
81/** Set HTML header text.
82 * The given text is placed in the head section of the HTML page. You can use it
83 * for example to add custom stylesheets or JavaScript.
84 * @param h header to set
85 */
86void
88{
89 html_header_ = h;
90}
91
92/** Pack web page reply.
93 * This method creates the final page by calling the header and footer generators
94 * if supplied (otherwise a standard header is chosen) and the body.
95 * @param active_baseurl the active navigation URL, can be used for instance
96 * to high-light the current section in the navigation.
97 * @param headergen header generator
98 * @param footergen footer generator
99 */
100void
101WebPageReply::pack(std::string active_baseurl,
102 WebPageHeaderGenerator *headergen,
103 WebPageFooterGenerator *footergen)
104{
105 if (headergen && navbar_enabled_)
106 merged_body_ += headergen->html_header(_title, active_baseurl, html_header_);
107 else {
109 char * s;
110 if (asprintf(&s, PAGE_HEADER, _title.c_str(), html_header_.c_str(), hi.short_name()) != -1) {
111 merged_body_ += s;
112 free(s);
113 }
114 }
115
116 merged_body_ += _body;
117
118 if (footergen && footer_enabled_)
119 merged_body_ += footergen->html_footer();
120 else
121 merged_body_ += PAGE_FOOTER;
122}
123
124std::string::size_type
126{
127 return merged_body_.length();
128}
129
130const std::string &
132{
133 return merged_body_;
134}
135
136/**
137 * Enable or disable the Fawkes Navigationbar (default enabled)
138 * @param enabled enabled
139 */
140void
142{
143 navbar_enabled_ = enabled;
144}
145/**
146 * Is the Fawkes Navigation bar enabled?
147 * @return enabled
148 */
149bool
151{
152 return navbar_enabled_;
153}
154/**
155 * Enable or disable the Fawkes Webview footer (default enabled)
156 * @param enabled enabled
157 */
158void
160{
161 footer_enabled_ = enabled;
162}
163/**
164 * Is the Fawkes Webview footer enabled?
165 * @return enabled
166 */
167bool
169{
170 return footer_enabled_;
171}
172
173} // end namespace fawkes
Host information.
Definition: hostinfo.h:32
const char * short_name()
Get short hostname (up to first dot).
Definition: hostinfo.cpp:109
Static web reply.
Definition: reply.h:136
std::string _body
Body of the reply.
Definition: reply.h:151
Interface for HTML footer generator.
virtual std::string html_footer()=0
Generate HTML footer.
Interface for HTML header generator.
virtual std::string html_header(std::string &title, std::string &active_baseurl, std::string &html_header)=0
Generate HTML header.
void set_navbar_enabled(bool enabled)
Enable or disable the Fawkes Navigationbar (default enabled)
Definition: page_reply.cpp:141
virtual std::string::size_type body_length()
Get length of body.
Definition: page_reply.cpp:125
virtual void pack()
Pack the data.
Definition: page_reply.h:41
bool get_navbar_enabled()
Is the Fawkes Navigation bar enabled?
Definition: page_reply.cpp:150
void set_footer_enabled(bool enabled)
Enable or disable the Fawkes Webview footer (default enabled)
Definition: page_reply.cpp:159
std::string _title
Title of the page.
Definition: page_reply.h:61
virtual void set_html_header(std::string h)
Set HTML header text.
Definition: page_reply.cpp:87
virtual const std::string & body()
Get body.
Definition: page_reply.cpp:131
WebPageReply(std::string title, std::string page="")
Constructor.
Definition: page_reply.cpp:59
bool get_footer_enabled()
Is the Fawkes Webview footer enabled?
Definition: page_reply.cpp:168
Basic web reply.
Definition: reply.h:34
void add_header(const std::string &header, const std::string &content)
Add a HTTP header.
Definition: reply.cpp:123
Code
HTTP response code.
Definition: reply.h:37
Fawkes library namespace.