Debian Codegraph @

Server/Web/ResponseHtml.cpp

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #include <sstream>
22 #include <string>
23
24 #include "Server/Web/ResponseHtml.h"
25
26 namespace Server { namespace Web
27 {
28 ResponseHtml::ResponseHtml(const ResponseCode responseCode, const std::string& title, const HtmlTag body)
29 : Response(responseCode, body),
30 title(title)
31 {
32 AddHeader("Cache-Control", "no-cache, must-revalidate");
33 AddHeader("Pragma", "no-cache");
34 AddHeader("Expires", "Sun, 12 Feb 2016 00:00:00 GMT");
35 AddHeader("Content-Type", "text/html; charset=utf-8");
36 AddHeader("Connection", "keep-alive");
37 }
38
39 void ResponseHtml::AddAttribute(const std::string name, const std::string value)
40 {
41 this->content.AddAttribute(name, value);
42 }
43
44 void ResponseHtml::AddChildTag(HtmlTag content)
45 {
46 this->content.AddChildTag(content);
47 }
48
49 ResponseHtml::operator std::string()
50 {
51 std::stringstream reply;
52 reply << *this;
53 return reply.str();
54 }
55
56 std::ostream& operator<<(std::ostream& stream, const ResponseHtml& response)
57 {
58 stream << "HTTP/1.1 " << std::to_string(response.responseCode) << " " << ResponseHtml::responseTexts.at(response.responseCode) << "\r\n";
59 for (auto& header : response.headers)
60 {
61 stream << header.first << ": " << header.second << "\r\n";
62 }
63
64 std::stringstream body;
65 body << "<!DOCTYPE html>";
66
67 HtmlTag html("html");
68 if (response.title.length() > 0)
69 {
70 HtmlTag head("head");
71 head.AddChildTag(HtmlTag("title").AddContent(response.title));
72 html.AddChildTag(head);
73 }
74 html.AddChildTag(response.content);
75 body << html;
76
77 std::string bodyString(body.str());
78 stream << "Content-Length: " << bodyString.size();
79 stream << "\r\n\r\n";
80 stream << bodyString;
81 return stream;
82 }
83 }} // namespace Server::Web