@@ -2253,9 +2253,15 @@ make_basic_authentication_header(const std::string &username,
2253
2253
2254
2254
namespace detail {
2255
2255
2256
- bool is_file (const std::string &path);
2256
+ struct FileStat {
2257
+ FileStat (const std::string &path);
2258
+ bool is_file () const ;
2259
+ bool is_dir () const ;
2257
2260
2258
- bool is_dir (const std::string &path);
2261
+ private:
2262
+ struct stat st_;
2263
+ int ret_ = -1 ;
2264
+ };
2259
2265
2260
2266
std::string encode_query_param (const std::string &value);
2261
2267
@@ -2626,14 +2632,14 @@ inline bool is_valid_path(const std::string &path) {
2626
2632
return true ;
2627
2633
}
2628
2634
2629
- inline bool is_file (const std::string &path) {
2630
- struct stat st;
2631
- return stat (path.c_str (), &st) >= 0 && S_ISREG (st.st_mode );
2635
+ inline FileStat::FileStat (const std::string &path) {
2636
+ ret_ = stat (path.c_str (), &st_);
2632
2637
}
2633
-
2634
- inline bool is_dir (const std::string &path) {
2635
- struct stat st;
2636
- return stat (path.c_str (), &st) >= 0 && S_ISDIR (st.st_mode );
2638
+ inline bool FileStat::is_file () const {
2639
+ return ret_ >= 0 && S_ISREG (st_.st_mode );
2640
+ }
2641
+ inline bool FileStat::is_dir () const {
2642
+ return ret_ >= 0 && S_ISDIR (st_.st_mode );
2637
2643
}
2638
2644
2639
2645
inline std::string encode_query_param (const std::string &value) {
@@ -6085,7 +6091,8 @@ inline bool Server::set_base_dir(const std::string &dir,
6085
6091
6086
6092
inline bool Server::set_mount_point (const std::string &mount_point,
6087
6093
const std::string &dir, Headers headers) {
6088
- if (detail::is_dir (dir)) {
6094
+ detail::FileStat stat (dir);
6095
+ if (stat.is_dir ()) {
6089
6096
std::string mnt = !mount_point.empty () ? mount_point : " /" ;
6090
6097
if (!mnt.empty () && mnt[0 ] == ' /' ) {
6091
6098
base_dirs_.push_back ({mnt, dir, std::move (headers)});
@@ -6569,12 +6576,14 @@ inline bool Server::handle_file_request(const Request &req, Response &res,
6569
6576
auto path = entry.base_dir + sub_path;
6570
6577
if (path.back () == ' /' ) { path += " index.html" ; }
6571
6578
6572
- if (detail::is_dir (path)) {
6579
+ detail::FileStat stat (path);
6580
+
6581
+ if (stat.is_dir ()) {
6573
6582
res.set_redirect (sub_path + " /" , StatusCode::MovedPermanently_301);
6574
6583
return true ;
6575
6584
}
6576
6585
6577
- if (detail:: is_file (path )) {
6586
+ if (stat. is_file ()) {
6578
6587
for (const auto &kv : entry.headers ) {
6579
6588
res.set_header (kv.first , kv.second );
6580
6589
}
0 commit comments