2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "express/middleware/StaticMiddleware.h"
44#include "core/socket/stream/SocketConnection.h"
45#include "web/http/http_utils.h"
46#include "web/http/server/SocketContext.h"
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
50#include "log/Logger.h"
60 ,
index(
"index.html") {
64 [&stdHeaders =
this->stdHeaders, &stdCookies =
this->stdCookies, &connectionState
= this->defaultConnectionState]
MIDDLEWARE(
68 if (req->method !=
"GET") {
72 res->
set("Connection", "close");
74 res->
set("Connection", "keep-alive");
78 for (
auto& [value, options] : stdCookies) {
86 if (req->
path ==
"/") {
91 <<
" Express StaticMiddleware Redirecting: " << req->
url <<
" -> "
107 res->
sendFile(root + decodedPath
, [&root, decodedPath, req, res, &next](
int ret) {
110 <<
" Express StaticMiddleware: GET " << req->
url +
" -> " << root + decodedPath;
113 << req->
url +
" -> " << root + decodedPath;
140 this->stdHeaders.insert(stdHeaders.begin(), stdHeaders.end());
152 const std::string& value,
153 const std::map<std::string, std::string>& options) {
167 static std::map<
const std::string, std::shared_ptr<
class StaticMiddleware>> staticMiddlewares;
169 if (!staticMiddlewares.contains(root)) {
173 return *staticMiddlewares[root];
#define MIDDLEWARE(req, res, next)
const std::string & getConnectionName() const
SocketConnection * getSocketConnection() const
void operator()(const std::string &how="") const
Response & cookie(const std::string &name, const std::string &value, const std::map< std::string, std::string > &options={})
void redirect(int state, const std::string &loc, const std::string &html={})
void sendFile(const std::string &file, const std::function< void(int)> &callback)
void sendStatus(int state, const std::string &html={})
Response & set(const std::string &field, const std::string &value, bool overwrite=true)
void send(const std::string &chunk)
Response & status(int status)
Response & set(const std::map< std::string, std::string > &headers, bool overwrite=true)
web::http::server::SocketContext * getSocketContext() const
const Router & setStrictRouting(bool strictRouting=true) const
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
StaticMiddleware & clearStdHeaders()
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)