From 20fa4ba3b49662a78d8b19e93874657cf34d6078 Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 4 Jul 2013 22:08:06 -0400 Subject: [PATCH] added return value to set_base_dir. --- httplib.h | 55 +++++++++++++++++++++++++++------------------------- test/test.cc | 6 ++++++ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/httplib.h b/httplib.h index 53624d2..3924f07 100644 --- a/httplib.h +++ b/httplib.h @@ -21,11 +21,9 @@ #ifndef snprintf #define snprintf _snprintf_s #endif -#ifndef getcwd -#define getcwd _getcwd -#endif #define S_ISREG(m) (((m)&S_IFREG)==S_IFREG) +#define S_ISDIR(m) (((m)&S_IFDIR)==S_IFDIR) #include #include @@ -100,7 +98,7 @@ public: void get(const char* pattern, Handler handler); void post(const char* pattern, Handler handler); - void set_base_dir(const char* path); + bool set_base_dir(const char* path); void set_error_handler(Handler handler); void set_logger(Logger logger); @@ -255,10 +253,13 @@ inline socket_t create_client_socket(const char* host, int port) inline bool is_file(const std::string& s) { struct stat st; - if (stat(s.c_str(), &st) < 0) { - return false; - } - return S_ISREG(st.st_mode); + return stat(s.c_str(), &st) >= 0 && S_ISREG(st.st_mode); +} + +inline bool is_dir(const std::string& s) +{ + struct stat st; + return stat(s.c_str(), &st) >= 0 && S_ISDIR(st.st_mode); } inline void read_file(const std::string& path, std::string& out) @@ -623,11 +624,6 @@ inline void Response::set_content(const std::string& s, const char* content_type inline Server::Server() : svr_sock_(-1) { - char curr_dir[FILENAME_MAX]; - if (getcwd(curr_dir, sizeof(curr_dir))) { - curr_dir[sizeof(curr_dir) - 1] = '\0'; - base_dir_ = curr_dir; - } } inline void Server::get(const char* pattern, Handler handler) @@ -640,9 +636,13 @@ inline void Server::post(const char* pattern, Handler handler) post_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); } -inline void Server::set_base_dir(const char* path) +inline bool Server::set_base_dir(const char* path) { - base_dir_ = path; + if (detail::is_dir(path)) { + base_dir_ = path; + return true; + } + return false; } inline void Server::set_error_handler(Handler handler) @@ -722,19 +722,22 @@ inline bool Server::read_request_line(FILE* fp, Request& req) inline bool Server::handle_file_request(Request& req, Response& res) { - std::string path = base_dir_ + req.url; + if (!base_dir_.empty()) { + std::string path = base_dir_ + req.url; - if (!path.empty() && path.back() == '/') { - path += "index.html"; - } + if (!path.empty() && path.back() == '/') { + path += "index.html"; + } - if (detail::is_file(path)) { - detail::read_file(path, res.body); - auto type = detail::get_content_type_from_file_extention(detail::get_file_extention(path)); - res.set_header("Content-Type", type); - res.status = 200; - return true; - } + if (detail::is_file(path)) { + detail::read_file(path, res.body); + res.set_header("Content-Type", + detail::get_content_type_from_file_extention( + detail::get_file_extention(path))); + res.status = 200; + return true; + } + } return false; } diff --git a/test/test.cc b/test/test.cc index 5609602..2934719 100644 --- a/test/test.cc +++ b/test/test.cc @@ -267,6 +267,12 @@ TEST_F(ServerTest, GetMethodDirTest) EXPECT_EQ("test.html", res->body); } +TEST_F(ServerTest, InvalidBaseDir) +{ + EXPECT_EQ(false, svr_.set_base_dir("invalid_dir")); + EXPECT_EQ(true, svr_.set_base_dir("./")); +} + #ifdef _WIN32 TEST(CleanupTest, WSACleanup) {