Skip to content

Commit 3f2922b

Browse files
committed
1 parent 509f583 commit 3f2922b

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

httplib.h

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,9 +2253,15 @@ make_basic_authentication_header(const std::string &username,
22532253

22542254
namespace detail {
22552255

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;
22572260

2258-
bool is_dir(const std::string &path);
2261+
private:
2262+
struct stat st_;
2263+
int ret_ = -1;
2264+
};
22592265

22602266
std::string encode_query_param(const std::string &value);
22612267

@@ -2626,14 +2632,14 @@ inline bool is_valid_path(const std::string &path) {
26262632
return true;
26272633
}
26282634

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_);
26322637
}
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);
26372643
}
26382644

26392645
inline std::string encode_query_param(const std::string &value) {
@@ -6085,7 +6091,8 @@ inline bool Server::set_base_dir(const std::string &dir,
60856091

60866092
inline bool Server::set_mount_point(const std::string &mount_point,
60876093
const std::string &dir, Headers headers) {
6088-
if (detail::is_dir(dir)) {
6094+
detail::FileStat stat(dir);
6095+
if (stat.is_dir()) {
60896096
std::string mnt = !mount_point.empty() ? mount_point : "/";
60906097
if (!mnt.empty() && mnt[0] == '/') {
60916098
base_dirs_.push_back({mnt, dir, std::move(headers)});
@@ -6569,12 +6576,14 @@ inline bool Server::handle_file_request(const Request &req, Response &res,
65696576
auto path = entry.base_dir + sub_path;
65706577
if (path.back() == '/') { path += "index.html"; }
65716578

6572-
if (detail::is_dir(path)) {
6579+
detail::FileStat stat(path);
6580+
6581+
if (stat.is_dir()) {
65736582
res.set_redirect(sub_path + "/", StatusCode::MovedPermanently_301);
65746583
return true;
65756584
}
65766585

6577-
if (detail::is_file(path)) {
6586+
if (stat.is_file()) {
65786587
for (const auto &kv : entry.headers) {
65796588
res.set_header(kv.first, kv.second);
65806589
}

test/test.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7676,11 +7676,13 @@ TEST(FileSystemTest, FileAndDirExistenceCheck) {
76767676
auto file_path = "./www/dir/index.html";
76777677
auto dir_path = "./www/dir";
76787678

7679-
EXPECT_TRUE(detail::is_file(file_path));
7680-
EXPECT_FALSE(detail::is_dir(file_path));
7679+
detail::FileStat stat_file(file_path);
7680+
EXPECT_TRUE(stat_file.is_file());
7681+
EXPECT_FALSE(stat_file.is_dir());
76817682

7682-
EXPECT_FALSE(detail::is_file(dir_path));
7683-
EXPECT_TRUE(detail::is_dir(dir_path));
7683+
detail::FileStat stat_dir(dir_path);
7684+
EXPECT_FALSE(stat_dir.is_file());
7685+
EXPECT_TRUE(stat_dir.is_dir());
76847686
}
76857687

76867688
TEST(DirtyDataRequestTest, HeadFieldValueContains_CR_LF_NUL) {

0 commit comments

Comments
 (0)