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"
65 [&stdHeaders =
this->stdHeaders,
66 &stdCookies =
this->stdCookies,
67 &connectionState =
this->defaultConnectionState,
68 &fallThrough
= this->fallThrough]
MIDDLEWARE(req, res, next) {
71 if (req->method !=
"GET") {
79 res->
set("Connection", "close");
81 res->
set("Connection", "keep-alive");
85 for (
auto& [value, options] : stdCookies) {
93 if (req->
path ==
"/") {
98 <<
" Express StaticMiddleware Redirecting: " << req->
url <<
" -> "
112 [&root =
this->root, &fallThrough
= this->fallThrough]
MIDDLEWARE(req, res, next) {
114 res->
sendFile(root + decodedPath
, [&root, decodedPath, req, res, &next, &fallThrough](
int ret) {
117 <<
" Express StaticMiddleware: GET " << req->
url +
" -> " << root + decodedPath;
120 << req->
url +
" -> " << root + decodedPath;
151 this->stdHeaders.insert(stdHeaders.begin(), stdHeaders.end());
163 const std::string& value,
164 const std::map<std::string, std::string>& options) {
178 static std::map<
const std::string, std::shared_ptr<
class StaticMiddleware>> staticMiddlewares;
180 if (!staticMiddlewares.contains(root)) {
184 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
StaticMiddleware(const std::string &root, bool fallThrough)
static class StaticMiddleware & instance(const std::string &root, bool fallThrough)
StaticMiddleware & afterResponse(web::http::ConnectionState connectionState)
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
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)