NAWA  0.8
Web Application Framework for C++
gpctest.cpp
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2019-2021 Tobias Flaig.
8  *
9  * This file is part of nawa.
10  *
11  * nawa is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License,
13  * version 3, as published by the Free Software Foundation.
14  *
15  * nawa 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with nawa. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
24 #include <nawa/application.h>
25 #include <nawa/util/encoding.h>
26 
27 using namespace nawa;
28 using namespace std;
29 
30 int init(AppInit& appInit) {
31  return 0;
32 }
33 
34 int handleRequest(Connection& connection) {
35 
36  auto& resp = connection.responseStream();
37  auto& req = connection.request();
38  auto& session = connection.session();
39 
40  session.start();
41 
42  if (req.get().count("download") == 1 && session.isSet(req.get()["download"])) {
43  try {
44  auto downloadFile = any_cast<File const>(session[req.get()["download"]]);
45  connection.setHeader("content-type", downloadFile.contentType());
46  connection.setHeader("content-disposition", "attachment; filename=\"" + downloadFile.filename() + "\"");
47  connection.setHeader("content-length", to_string(downloadFile.size()));
48  connection.setResponseBody(downloadFile.toString());
49  return 0;
50  } catch (bad_any_cast const& e) {
51  resp << "Bad any cast: " << e.what();
52  return 0;
53  }
54  }
55 
56  resp << "<!DOCTYPE html>\n"
57  "<html><head><title>NAWA GPC Test</title></head><body>\n";
58 
59  auto printEncoded = [&](string const& k, string const& v) {
60  resp << "<li>[" << encoding::htmlEncode(k) << "] = " << encoding::htmlEncode(v) << "</li>";
61  };
62 
63  if (req.cookie()) {
64  resp << "<p>COOKIE vars:</p><ul>";
65  for (auto const& [k, v] : req.cookie()) {
66  printEncoded(k, v);
67  }
68  resp << "</ul>";
69  }
70 
71  if (req.get()) {
72  resp << "<p>GET vars:</p><ul>";
73  for (auto const& [k, v] : req.get()) {
74  printEncoded(k, v);
75  }
76  resp << "</ul>";
77  }
78 
79  if (req.post()) {
80  resp << "<p>POST vars (without files):</p><ul>";
81  for (auto const& [k, v] : req.post()) {
82  printEncoded(k, v);
83  }
84  resp << "</ul>";
85  }
86 
87  if (req.post().hasFiles()) {
88  resp << "<p>POST files:</p><ul>";
89  for (auto const& [k, v] : req.post().getFileMultimap()) {
90  // skip empty files
91  if (v.size() == 0) {
92  continue;
93  }
94 
95  resp << "<li>[" << encoding::htmlEncode(k) << "]: "
96  << R"(<a href="?download=)" << encoding::urlEncode(v.filename()) << R"(">)"
97  << encoding::htmlEncode(v.filename())
98  << "; size: "
99  << v.size()
100  << "; content type: "
101  << encoding::htmlEncode(v.contentType())
102  << "</a></li>";
103 
104  // Saving files in session and not even cleaning them up at some point is something that clearly
105  // shouldn't be done outside of a test app :)
106  session.set(v.filename(), v);
107  }
108  resp << "</ul>";
109  }
110 
111  if (req.post().getRaw()) {
112  resp << "<p>Raw POST data:</p><pre>" << *req.post().getRaw() << "</pre>";
113  }
114 
115  resp << R"(<p>Some POST form:</p><form name="testform" method="post" action="?" enctype="multipart/form-data">)"
116  << R"(<p>Field one (1): <input type="text" name="one"></p>)"
117  << R"(<p>Field one (2): <input type="text" name="one"></p>)"
118  << R"(<p>Field two: <input type="text" name="two"></p>)"
119  << R"(<p>Field fileupload (1): <input type="file" name="fileupload"></p>)"
120  << R"(<p>Field fileupload (2): <input type="file" name="fileupload"></p>)"
121  << R"(<p><input type="submit" value="Go" name="submit"></p></form>)";
122 
123  return 0;
124 }
nawa::Session & session() noexcept
Definition: Connection.cpp:361
void setHeader(std::string key, std::string value)
Definition: Connection.cpp:218
std::ostream & responseStream() noexcept
Definition: Connection.cpp:377
nawa::Request const & request() const noexcept
Definition: Connection.cpp:357
void setResponseBody(std::string content)
Definition: Connection.cpp:134
void start(nawa::Cookie properties=Cookie())
Definition: Session.cpp:127
Namespace containing functions for text encoding and decoding.
int handleRequest(Connection &connection)
Definition: gpctest.cpp:34
int init(AppInit &appInit)
Definition: gpctest.cpp:30
std::string htmlEncode(std::string input, bool encodeAll=false)
Definition: encoding.cpp:133
std::string urlEncode(std::string const &input)
Definition: encoding.cpp:238
Definition: AppInit.h:31