@@ -579,7 +579,8 @@ class Server {
579
579
Server &Options (const char *pattern, Handler handler);
580
580
581
581
bool set_base_dir (const char *dir, const char *mount_point = nullptr );
582
- bool set_mount_point (const char *mount_point, const char *dir);
582
+ bool set_mount_point (const char *mount_point, const char *dir,
583
+ Headers headers = Headers());
583
584
bool remove_mount_point (const char *mount_point);
584
585
void set_file_extension_and_mimetype_mapping (const char *ext,
585
586
const char *mime);
@@ -663,9 +664,15 @@ class Server {
663
664
ContentReceiver multipart_receiver);
664
665
665
666
virtual bool process_and_close_socket (socket_t sock);
667
+
668
+ struct MountPointEntry {
669
+ std::string mount_point;
670
+ std::string base_dir;
671
+ Headers headers;
672
+ };
673
+ std::vector<MountPointEntry> base_dirs_;
666
674
667
675
std::atomic<bool > is_running_;
668
- std::vector<std::pair<std::string, std::string>> base_dirs_;
669
676
std::map<std::string, std::string> file_extension_and_mimetype_map_;
670
677
Handler file_request_handler_;
671
678
Handlers get_handlers_;
@@ -3815,11 +3822,12 @@ inline bool Server::set_base_dir(const char *dir, const char *mount_point) {
3815
3822
return set_mount_point (mount_point, dir);
3816
3823
}
3817
3824
3818
- inline bool Server::set_mount_point (const char *mount_point, const char *dir) {
3825
+ inline bool Server::set_mount_point (const char *mount_point, const char *dir,
3826
+ Headers headers) {
3819
3827
if (detail::is_dir (dir)) {
3820
3828
std::string mnt = mount_point ? mount_point : " /" ;
3821
3829
if (!mnt.empty () && mnt[0 ] == ' /' ) {
3822
- base_dirs_.emplace_back ( mnt, dir);
3830
+ base_dirs_.push_back ({ mnt, dir, std::move (headers)} );
3823
3831
return true ;
3824
3832
}
3825
3833
}
@@ -3828,7 +3836,7 @@ inline bool Server::set_mount_point(const char *mount_point, const char *dir) {
3828
3836
3829
3837
inline bool Server::remove_mount_point (const char *mount_point) {
3830
3838
for (auto it = base_dirs_.begin (); it != base_dirs_.end (); ++it) {
3831
- if (it->first == mount_point) {
3839
+ if (it->mount_point == mount_point) {
3832
3840
base_dirs_.erase (it);
3833
3841
return true ;
3834
3842
}
@@ -4250,22 +4258,22 @@ inline bool Server::read_content_core(Stream &strm, Request &req, Response &res,
4250
4258
4251
4259
inline bool Server::handle_file_request (Request &req, Response &res,
4252
4260
bool head) {
4253
- for (const auto &kv : base_dirs_) {
4254
- const auto &mount_point = kv.first ;
4255
- const auto &base_dir = kv.second ;
4256
-
4261
+ for (const auto &entry : base_dirs_) {
4257
4262
// Prefix match
4258
- if (!req.path .compare (0 , mount_point.size (), mount_point)) {
4259
- std::string sub_path = " /" + req.path .substr (mount_point.size ());
4263
+ if (!req.path .compare (0 , entry. mount_point .size (), entry. mount_point )) {
4264
+ std::string sub_path = " /" + req.path .substr (entry. mount_point .size ());
4260
4265
if (detail::is_valid_path (sub_path)) {
4261
- auto path = base_dir + sub_path;
4266
+ auto path = entry. base_dir + sub_path;
4262
4267
if (path.back () == ' /' ) { path += " index.html" ; }
4263
4268
4264
4269
if (detail::is_file (path)) {
4265
4270
detail::read_file (path, res.body );
4266
4271
auto type =
4267
4272
detail::find_content_type (path, file_extension_and_mimetype_map_);
4268
4273
if (type) { res.set_header (" Content-Type" , type); }
4274
+ for (const auto & kv : entry.headers ) {
4275
+ res.set_header (kv.first .c_str (), kv.second );
4276
+ }
4269
4277
res.status = 200 ;
4270
4278
if (!head && file_request_handler_) {
4271
4279
file_request_handler_ (req, res);
0 commit comments