@@ -129,8 +129,9 @@ using socket_t = int;
129129#define INVALID_SOCKET (-1 )
130130#endif // _WIN32
131131
132- #include < cassert >
132+ #include < array >
133133#include < atomic>
134+ #include < cassert>
134135#include < condition_variable>
135136#include < errno.h>
136137#include < fcntl.h>
@@ -145,7 +146,6 @@ using socket_t = int;
145146#include < string>
146147#include < sys/stat.h>
147148#include < thread>
148- #include < array>
149149
150150#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
151151#include < openssl/err.h>
@@ -507,7 +507,7 @@ class Server {
507507 Server &Delete (const char *pattern, Handler handler);
508508 Server &Options (const char *pattern, Handler handler);
509509
510- bool set_base_dir (const char *path );
510+ bool set_base_dir (const char *dir, const char *mount_point = nullptr );
511511 void set_file_request_handler (Handler handler);
512512
513513 void set_error_handler (Handler handler);
@@ -570,7 +570,7 @@ class Server {
570570
571571 std::atomic<bool > is_running_;
572572 std::atomic<socket_t > svr_sock_;
573- std::string base_dir_ ;
573+ std::vector<std::pair<std:: string, std::string>> base_dirs_ ;
574574 Handler file_request_handler_;
575575 Handlers get_handlers_;
576576 Handlers post_handlers_;
@@ -2408,10 +2408,13 @@ inline Server &Server::Options(const char *pattern, Handler handler) {
24082408 return *this ;
24092409}
24102410
2411- inline bool Server::set_base_dir (const char *path) {
2412- if (detail::is_dir (path)) {
2413- base_dir_ = path;
2414- return true ;
2411+ inline bool Server::set_base_dir (const char *dir, const char *mount_point) {
2412+ if (detail::is_dir (dir)) {
2413+ std::string mnt = mount_point ? mount_point : " /" ;
2414+ if (!mnt.empty () && mnt[0 ] == ' /' ) {
2415+ base_dirs_.emplace_back (mnt, dir);
2416+ return true ;
2417+ }
24152418 }
24162419 return false ;
24172420}
@@ -2684,21 +2687,28 @@ Server::read_content_with_content_receiver(Stream &strm, bool last_connection,
26842687}
26852688
26862689inline bool Server::handle_file_request (Request &req, Response &res) {
2687- if (!base_dir_.empty () && detail::is_valid_path (req.path )) {
2688- std::string path = base_dir_ + req.path ;
2689-
2690- if (!path.empty () && path.back () == ' /' ) { path += " index.html" ; }
2691-
2692- if (detail::is_file (path)) {
2693- detail::read_file (path, res.body );
2694- auto type = detail::find_content_type (path);
2695- if (type) { res.set_header (" Content-Type" , type); }
2696- res.status = 200 ;
2697- if (file_request_handler_) { file_request_handler_ (req, res); }
2698- return true ;
2690+ for (const auto & kv: base_dirs_) {
2691+ const auto & mount_point = kv.first ;
2692+ const auto & base_dir = kv.second ;
2693+
2694+ // Prefix match
2695+ if (!req.path .find (mount_point)) {
2696+ std::string sub_path = " /" + req.path .substr (mount_point.size ());
2697+ if (detail::is_valid_path (sub_path)) {
2698+ auto path = base_dir + sub_path;
2699+ if (path.back () == ' /' ) { path += " index.html" ; }
2700+
2701+ if (detail::is_file (path)) {
2702+ detail::read_file (path, res.body );
2703+ auto type = detail::find_content_type (path);
2704+ if (type) { res.set_header (" Content-Type" , type); }
2705+ res.status = 200 ;
2706+ if (file_request_handler_) { file_request_handler_ (req, res); }
2707+ return true ;
2708+ }
2709+ }
26992710 }
27002711 }
2701-
27022712 return false ;
27032713}
27042714
0 commit comments