SNode.C
Loading...
Searching...
No Matches
StaticMiddleware.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "express/middleware/StaticMiddleware.h"
43
44#include "core/socket/stream/SocketConnection.h"
45#include "web/http/http_utils.h"
46#include "web/http/server/SocketContext.h"
47
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
50#include "log/Logger.h"
51
52#include <map>
53
54#endif /* DOXYGEN_SHOULD_SKIP_THIS */
55
56namespace express::middleware {
57
58 StaticMiddleware::StaticMiddleware(const std::string& root)
59 : root(root)
60 , index("index.html") {
62
63 use(
64 [&stdHeaders = this->stdHeaders, &stdCookies = this->stdCookies, &connectionState = this->defaultConnectionState] MIDDLEWARE(
65 req, res, next) {
66 LOG(DEBUG) << res->getSocketContext()->getSocketConnection()->getConnectionName() << " Express " << req->method;
67
68 if (req->method != "GET") {
69 res->sendStatus(405, "Unsupported method: " + req->method + "\n");
70 } else {
71 if (connectionState == web::http::ConnectionState::Close) {
72 res->set("Connection", "close");
73 } else if (connectionState == web::http::ConnectionState::Keep) {
74 res->set("Connection", "keep-alive");
75 }
76 res->set(stdHeaders);
77
78 for (auto& [value, options] : stdCookies) {
79 res->cookie(value, options.getValue(), options.getOptions());
80 }
81
82 next();
83 }
84 },
85 [&index = this->index] MIDDLEWARE(req, res, next) {
86 if (req->path == "/") {
87 if (index.empty()) {
88 res->status(404).send("Unsupported resource: " + req->url + "\n");
89 } else {
91 << " Express StaticMiddleware Redirecting: " << req->url << " -> "
92 << req->originalPath +
93 (!req->originalPath.empty() && req->originalPath.back() != '/' && index.front() != '/' ? "/"
94 : "") +
95 index;
96 res->redirect(
97 308,
98 req->originalPath +
99 (!req->originalPath.empty() && req->originalPath.back() != '/' && index.front() != '/' ? "/" : "") + index);
100 }
101 } else {
102 next();
103 }
104 },
105 [&root = this->root] MIDDLEWARE(req, res, next) {
106 const std::string decodedPath = httputils::url_decode(req->path);
107 res->sendFile(root + decodedPath, [&root, decodedPath, req, res, &next](int ret) {
108 if (ret == 0) {
110 << " Express StaticMiddleware: GET " << req->url + " -> " << root + decodedPath;
111 } else {
112 PLOG(ERROR) << res->getSocketContext()->getSocketConnection()->getConnectionName() << " Express StaticMiddleware "
113 << req->url + " -> " << root + decodedPath;
114
115 next();
116 }
117 });
118 });
119 }
120
121 class StaticMiddleware& StaticMiddleware::setIndex(const std::string& index) {
122 this->index = index;
123
124 return *this;
125 }
126
128 this->stdHeaders.clear();
129
130 return *this;
131 }
132
133 class StaticMiddleware& StaticMiddleware::setStdHeaders(const std::map<std::string, std::string>& stdHeaders) {
134 this->stdHeaders = stdHeaders;
135
136 return *this;
137 }
138
139 class StaticMiddleware& StaticMiddleware::appendStdHeaders(const std::map<std::string, std::string>& stdHeaders) {
140 this->stdHeaders.insert(stdHeaders.begin(), stdHeaders.end());
141
142 return *this;
143 }
144
145 class StaticMiddleware& StaticMiddleware::appendStdHeaders(const std::string& field, const std::string& value) {
146 this->stdHeaders[field] = value;
147
148 return *this;
149 }
150
151 class StaticMiddleware& StaticMiddleware::appendStdCookie(const std::string& name,
152 const std::string& value,
153 const std::map<std::string, std::string>& options) {
154 this->stdCookies.insert({name, web::http::CookieOptions(value, options)});
155
156 return *this;
157 }
158
160 this->defaultConnectionState = connectionState;
161
162 return *this;
163 }
164
165 class StaticMiddleware& StaticMiddleware::instance(const std::string& root) {
166 // Keep all created static middlewares alive
167 static std::map<const std::string, std::shared_ptr<class StaticMiddleware>> staticMiddlewares;
168
169 if (!staticMiddlewares.contains(root)) {
170 staticMiddlewares[root] = std::shared_ptr<StaticMiddleware>(new StaticMiddleware(root));
171 }
172
173 return *staticMiddlewares[root];
174 }
175
176 // "Constructor" of StaticMiddleware
177 class StaticMiddleware& StaticMiddleware(const std::string& root) {
179 }
180
181} // namespace express::middleware
#define MIDDLEWARE(req, res, next)
Definition Router.h:63
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
void operator()(const std::string &how="") const
Definition Next.cpp:56
std::string url
Definition Request.h:98
std::string originalPath
Definition Request.h:78
std::string method
Definition Request.h:97
std::string path
Definition Request.h:79
Response & cookie(const std::string &name, const std::string &value, const std::map< std::string, std::string > &options={})
Definition Response.cpp:147
void redirect(int state, const std::string &loc, const std::string &html={})
Definition Response.cpp:90
void sendFile(const std::string &file, const std::function< void(int)> &callback)
Definition Response.cpp:181
void sendStatus(int state, const std::string &html={})
Definition Response.cpp:95
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
Definition Response.cpp:129
void send(const std::string &chunk)
Definition Response.cpp:169
Response & status(int status)
Definition Response.cpp:117
Response & set(const std::map< std::string, std::string > &headers, bool overwrite=true)
Definition Response.cpp:135
web::http::server::SocketContext * getSocketContext() const
Definition Response.cpp:68
const Router & setStrictRouting(bool strictRouting=true) const
Definition Router.cpp:78
std::map< std::string, web::http::CookieOptions > stdCookies
StaticMiddleware & setIndex(const std::string &index)
StaticMiddleware & appendStdHeaders(const std::string &field, const std::string &value)
web::http::ConnectionState defaultConnectionState
std::map< std::string, std::string > stdHeaders
static class StaticMiddleware & instance(const std::string &root)
StaticMiddleware(const std::string &root)
StaticMiddleware & afterResponse(web::http::ConnectionState connectionState)
const std::map< std::string, std::string > & getOptions() const
CookieOptions(const std::string &value, const std::map< std::string, std::string > &options)
const std::string & getValue() const
std::string url_decode(const std::string &text)