NAWA 0.9
Web Application Framework for C++
multipage.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 Tobias Flaig.
3 *
4 * This file is part of nawa.
5 *
6 * nawa is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License,
8 * version 3, as published by the Free Software Foundation.
9 *
10 * nawa is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with nawa. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25#include <nawa/application.h>
26
27using namespace std;
28using namespace nawa;
29
30int init(AppInit& appInit) {
31
32 // enable access filtering
33 appInit.accessFilters().filtersEnabled() = true;
34
35 // apply forward filters for images below '/test/static/images' and '/test2/static/images ...
36 ForwardFilter imageFilter;
37 imageFilter.pathFilter({{"test", "static", "images"},
38 {"test2", "static", "images"}});
39 imageFilter.extensionFilter({"png", "jpeg", "jpg", "gif"});
40 imageFilter.basePath("/var/www/multipage/images");
41 appInit.accessFilters().forwardFilters().push_back(imageFilter);
42
43 // and block everything else in these directories
44 BlockFilter blockNonImages;
45 blockNonImages.pathFilter({{"test", "static", "images"},
46 {"test2", "static", "images"}});
47 blockNonImages.extensionFilter({"png", "jpeg", "jpg", "gif"});
48 blockNonImages.invertExtensionFilter(true);
49 blockNonImages.status(404);
50 appInit.accessFilters().blockFilters().push_back(blockNonImages);
51
52 // ... and html below '/test/static/html'
53 ForwardFilter htmlFilter;
54 htmlFilter.pathFilter({{"test", "static", "html"}});
55 htmlFilter.extensionFilter({"html", "htm"});
56 htmlFilter.basePath("/var/www/multipage/html");
57 appInit.accessFilters().forwardFilters().push_back(htmlFilter);
58
59 // and also block everything else there
60 BlockFilter blockNonHtml;
61 blockNonHtml.pathFilter({{"test", "static", "html"}});
62 blockNonHtml.extensionFilter({"html", "htm"});
63 blockNonHtml.invertExtensionFilter(true);
64 blockNonHtml.status(404);
65 appInit.accessFilters().blockFilters().push_back(blockNonHtml);
66
67 // authenticate access to all static resources
68 AuthFilter authFilter;
69 authFilter.pathFilter({{"test", "static"},
70 {"test2", "static"}});
71 authFilter.authName("Not for everyone!");
72 authFilter.authFunction() = [](std::string user, std::string password) -> bool {
73 return (user == "test" && password == "supersecure");
74 };
75 appInit.accessFilters().authFilters().push_back(authFilter);
76
77 // for an example of regex-based filtering see tests/apps/nawatest.cpp
78
79 return 0;
80}
81
82int handleRequest(Connection& connection) {
83
84 // we do not have to care about requests for static resources -- the filters are doing that for us!
85
86 // shortcuts
87 auto& resp = connection.responseStream();
88 auto requestPath = connection.request().env().getRequestPath();
89
90 resp << "<!DOCTYPE html><html><head>"
91 "<title>nawa Multipage Example</title>"
92 "</head><body><p>Request Path Elements: ";
93 for (auto const& e : requestPath) {
94 resp << e << ", ";
95 }
96 resp << "</p>";
97
98 if (requestPath.size() > 1) {
99
100 // if the request path starts with "/test/page1", show the following page
101 if (requestPath.at(0) == "test" && requestPath.at(1) == "page1") {
102 resp << "<h1>First Page</h1>"
103 "<p>Lorem ipsum sit dolor</p>"
104 "</body></html>";
105
106 return 0;
107 }
108 }
109
110 // otherwise, the index will be displayed
111 resp << "<h1>Index</h1>"
112 "<ul>"
113 "<li><a href=\"/test/page1\">First Page</a></li>"
114 "<li><a href=\"/test/static/html/somedocument.html\">A static HTML document</a></li>"
115 "</ul></body></html>";
116
117 return 0;
118}
This file will be configured by CMake and contains the necessary properties to ensure that a loaded a...
std::vector< BlockFilter > & blockFilters() noexcept
bool & filtersEnabled() noexcept
std::vector< AuthFilter > & authFilters() noexcept
std::vector< ForwardFilter > & forwardFilters() noexcept
std::vector< std::string > & extensionFilter() noexcept
std::vector< std::vector< std::string > > & pathFilter() noexcept
bool & invertExtensionFilter() noexcept
AccessFilterList & accessFilters()
Definition: AppInit.cpp:47
std::string & authName() noexcept
std::function< bool(std::string, std::string)> & authFunction() noexcept
unsigned int & status() noexcept
std::ostream & responseStream() noexcept
Definition: Connection.cpp:377
nawa::Request const & request() const noexcept
Definition: Connection.cpp:357
std::string & basePath() noexcept
request::Env const & env() const noexcept
Definition: Request.cpp:48
std::vector< std::string > getRequestPath() const
Definition: Env.cpp:51
int handleRequest(Connection &connection)
Definition: multipage.cpp:82
int init(AppInit &appInit)
Definition: multipage.cpp:30
Definition: AppInit.h:31