Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Feb 1, 2020
1 parent 89740a8 commit 8801e51
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion example/simplesvr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ int main(int argc, const char **argv) {
auto base_dir = "./";
if (argc > 2) { base_dir = argv[2]; }

if (!svr.set_base_dir(base_dir)) {
if (!svr.set_mount_point(base_dir, "/")) {
cout << "The specified base directory doesn't exist...";
return 1;
}
Expand Down
18 changes: 17 additions & 1 deletion httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,9 @@ class Server {
Server &Delete(const char *pattern, Handler handler);
Server &Options(const char *pattern, Handler handler);

bool set_base_dir(const char *dir, const char *mount_point = nullptr);
[[deprecated]] bool set_base_dir(const char *dir, const char *mount_point = nullptr);
bool set_mount_point(const char *dir, const char *mount_point);
bool remove_mount_point(const char *mount_point);
void set_file_extension_and_mimetype_mapping(const char *ext,
const char *mime);
void set_file_request_handler(Handler handler);
Expand Down Expand Up @@ -2889,6 +2891,10 @@ inline Server &Server::Options(const char *pattern, Handler handler) {
}

inline bool Server::set_base_dir(const char *dir, const char *mount_point) {
return set_mount_point(dir, mount_point);
}

inline bool Server::set_mount_point(const char *dir, const char *mount_point) {
if (detail::is_dir(dir)) {
std::string mnt = mount_point ? mount_point : "/";
if (!mnt.empty() && mnt[0] == '/') {
Expand All @@ -2899,6 +2905,16 @@ inline bool Server::set_base_dir(const char *dir, const char *mount_point) {
return false;
}

inline bool Server::remove_mount_point(const char *mount_point) {
for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) {
if (it->first == mount_point) {
base_dirs_.erase(it);
return true;
}
}
return false;
}

inline void Server::set_file_extension_and_mimetype_mapping(const char *ext,
const char *mime) {
file_extension_and_mimetype_map_[ext] = mime;
Expand Down
50 changes: 47 additions & 3 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,8 @@ class ServerTest : public ::testing::Test {
}

virtual void SetUp() {
svr_.set_base_dir("./www");
svr_.set_base_dir("./www2", "/mount");
svr_.set_mount_point("./www", "/");
svr_.set_mount_point("./www2", "/mount");
svr_.set_file_extension_and_mimetype_mapping("abcde", "text/abcde");

svr_.Get("/hi",
Expand Down Expand Up @@ -1245,7 +1245,7 @@ TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
}

TEST_F(ServerTest, InvalidBaseDirMount) {
EXPECT_EQ(false, svr_.set_base_dir("./www3", "invalid_mount_point"));
EXPECT_EQ(false, svr_.set_mount_point("./www3", "invalid_mount_point"));
}

TEST_F(ServerTest, EmptyRequest) {
Expand Down Expand Up @@ -2069,6 +2069,50 @@ TEST(ServerStopTest, StopServerWithChunkedTransmission) {
ASSERT_FALSE(svr.is_running());
}

TEST(MountTest, Unmount) {
Server svr;

auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
while (!svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));

Client cli("localhost", PORT);

svr.set_mount_point("./www2", "/mount2");

auto res = cli.Get("/");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(404, res->status);

res = cli.Get("/mount2/dir/test.html");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);

svr.set_mount_point("./www", "/");

res = cli.Get("/dir/");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);

svr.remove_mount_point("/");
res = cli.Get("/dir/");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(404, res->status);

svr.remove_mount_point("/mount2");
res = cli.Get("/mount2/dir/test.html");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(404, res->status);

svr.stop();
listen_thread.join();
ASSERT_FALSE(svr.is_running());
}

class ServerTestWithAI_PASSIVE : public ::testing::Test {
protected:
ServerTestWithAI_PASSIVE()
Expand Down

0 comments on commit 8801e51

Please sign in to comment.