Skip to content

Commit 8801e51

Browse files
committed
1 parent 89740a8 commit 8801e51

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

example/simplesvr.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ int main(int argc, const char **argv) {
122122
auto base_dir = "./";
123123
if (argc > 2) { base_dir = argv[2]; }
124124

125-
if (!svr.set_base_dir(base_dir)) {
125+
if (!svr.set_mount_point(base_dir, "/")) {
126126
cout << "The specified base directory doesn't exist...";
127127
return 1;
128128
}

httplib.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,9 @@ class Server {
465465
Server &Delete(const char *pattern, Handler handler);
466466
Server &Options(const char *pattern, Handler handler);
467467

468-
bool set_base_dir(const char *dir, const char *mount_point = nullptr);
468+
[[deprecated]] bool set_base_dir(const char *dir, const char *mount_point = nullptr);
469+
bool set_mount_point(const char *dir, const char *mount_point);
470+
bool remove_mount_point(const char *mount_point);
469471
void set_file_extension_and_mimetype_mapping(const char *ext,
470472
const char *mime);
471473
void set_file_request_handler(Handler handler);
@@ -2889,6 +2891,10 @@ inline Server &Server::Options(const char *pattern, Handler handler) {
28892891
}
28902892

28912893
inline bool Server::set_base_dir(const char *dir, const char *mount_point) {
2894+
return set_mount_point(dir, mount_point);
2895+
}
2896+
2897+
inline bool Server::set_mount_point(const char *dir, const char *mount_point) {
28922898
if (detail::is_dir(dir)) {
28932899
std::string mnt = mount_point ? mount_point : "/";
28942900
if (!mnt.empty() && mnt[0] == '/') {
@@ -2899,6 +2905,16 @@ inline bool Server::set_base_dir(const char *dir, const char *mount_point) {
28992905
return false;
29002906
}
29012907

2908+
inline bool Server::remove_mount_point(const char *mount_point) {
2909+
for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) {
2910+
if (it->first == mount_point) {
2911+
base_dirs_.erase(it);
2912+
return true;
2913+
}
2914+
}
2915+
return false;
2916+
}
2917+
29022918
inline void Server::set_file_extension_and_mimetype_mapping(const char *ext,
29032919
const char *mime) {
29042920
file_extension_and_mimetype_map_[ext] = mime;

test/test.cc

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ class ServerTest : public ::testing::Test {
662662
}
663663

664664
virtual void SetUp() {
665-
svr_.set_base_dir("./www");
666-
svr_.set_base_dir("./www2", "/mount");
665+
svr_.set_mount_point("./www", "/");
666+
svr_.set_mount_point("./www2", "/mount");
667667
svr_.set_file_extension_and_mimetype_mapping("abcde", "text/abcde");
668668

669669
svr_.Get("/hi",
@@ -1245,7 +1245,7 @@ TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
12451245
}
12461246

12471247
TEST_F(ServerTest, InvalidBaseDirMount) {
1248-
EXPECT_EQ(false, svr_.set_base_dir("./www3", "invalid_mount_point"));
1248+
EXPECT_EQ(false, svr_.set_mount_point("./www3", "invalid_mount_point"));
12491249
}
12501250

12511251
TEST_F(ServerTest, EmptyRequest) {
@@ -2069,6 +2069,50 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) {
20692069
ASSERT_FALSE(svr.is_running());
20702070
}
20712071

2072+
TEST(MountTest, Unmount) {
2073+
Server svr;
2074+
2075+
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
2076+
while (!svr.is_running()) {
2077+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
2078+
}
2079+
2080+
// Give GET time to get a few messages.
2081+
std::this_thread::sleep_for(std::chrono::seconds(1));
2082+
2083+
Client cli("localhost", PORT);
2084+
2085+
svr.set_mount_point("./www2", "/mount2");
2086+
2087+
auto res = cli.Get("/");
2088+
ASSERT_TRUE(res != nullptr);
2089+
EXPECT_EQ(404, res->status);
2090+
2091+
res = cli.Get("/mount2/dir/test.html");
2092+
ASSERT_TRUE(res != nullptr);
2093+
EXPECT_EQ(200, res->status);
2094+
2095+
svr.set_mount_point("./www", "/");
2096+
2097+
res = cli.Get("/dir/");
2098+
ASSERT_TRUE(res != nullptr);
2099+
EXPECT_EQ(200, res->status);
2100+
2101+
svr.remove_mount_point("/");
2102+
res = cli.Get("/dir/");
2103+
ASSERT_TRUE(res != nullptr);
2104+
EXPECT_EQ(404, res->status);
2105+
2106+
svr.remove_mount_point("/mount2");
2107+
res = cli.Get("/mount2/dir/test.html");
2108+
ASSERT_TRUE(res != nullptr);
2109+
EXPECT_EQ(404, res->status);
2110+
2111+
svr.stop();
2112+
listen_thread.join();
2113+
ASSERT_FALSE(svr.is_running());
2114+
}
2115+
20722116
class ServerTestWithAI_PASSIVE : public ::testing::Test {
20732117
protected:
20742118
ServerTestWithAI_PASSIVE()

0 commit comments

Comments
 (0)