added return value to set_base_dir.
This commit is contained in:
parent
eef74af19b
commit
20fa4ba3b4
55
httplib.h
55
httplib.h
@ -21,11 +21,9 @@
|
|||||||
#ifndef snprintf
|
#ifndef snprintf
|
||||||
#define snprintf _snprintf_s
|
#define snprintf _snprintf_s
|
||||||
#endif
|
#endif
|
||||||
#ifndef getcwd
|
|
||||||
#define getcwd _getcwd
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)
|
#define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)
|
||||||
|
#define S_ISDIR(m) (((m)&S_IFDIR)==S_IFDIR)
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
@ -100,7 +98,7 @@ public:
|
|||||||
void get(const char* pattern, Handler handler);
|
void get(const char* pattern, Handler handler);
|
||||||
void post(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_error_handler(Handler handler);
|
||||||
void set_logger(Logger logger);
|
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)
|
inline bool is_file(const std::string& s)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (stat(s.c_str(), &st) < 0) {
|
return stat(s.c_str(), &st) >= 0 && S_ISREG(st.st_mode);
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
return 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)
|
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()
|
inline Server::Server()
|
||||||
: svr_sock_(-1)
|
: 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)
|
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));
|
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)
|
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)
|
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() == '/') {
|
if (!path.empty() && path.back() == '/') {
|
||||||
path += "index.html";
|
path += "index.html";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (detail::is_file(path)) {
|
if (detail::is_file(path)) {
|
||||||
detail::read_file(path, res.body);
|
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",
|
||||||
res.set_header("Content-Type", type);
|
detail::get_content_type_from_file_extention(
|
||||||
res.status = 200;
|
detail::get_file_extention(path)));
|
||||||
return true;
|
res.status = 200;
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -267,6 +267,12 @@ TEST_F(ServerTest, GetMethodDirTest)
|
|||||||
EXPECT_EQ("test.html", res->body);
|
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
|
#ifdef _WIN32
|
||||||
TEST(CleanupTest, WSACleanup)
|
TEST(CleanupTest, WSACleanup)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user