Skip to content

Commit

Permalink
static file support. vc12 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Jul 4, 2013
1 parent a9b3461 commit eef74af
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 223 deletions.
119 changes: 102 additions & 17 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#ifndef _CPPHTTPLIB_HTTPSLIB_H_
#define _CPPHTTPLIB_HTTPSLIB_H_

#ifdef _WIN32
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE

Expand All @@ -21,6 +21,11 @@
#ifndef snprintf
#define snprintf _snprintf_s
#endif
#ifndef getcwd
#define getcwd _getcwd
#endif

#define S_ISREG(m) (((m)&S_IFREG)==S_IFREG)

#include <fcntl.h>
#include <io.h>
Expand All @@ -35,10 +40,12 @@ typedef SOCKET socket_t;
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/stat.h>

typedef int socket_t;
#endif

#include <fstream>
#include <functional>
#include <map>
#include <memory>
Expand Down Expand Up @@ -93,6 +100,8 @@ class Server {
void get(const char* pattern, Handler handler);
void post(const char* pattern, Handler handler);

void set_base_dir(const char* path);

void set_error_handler(Handler handler);
void set_logger(Logger logger);

Expand All @@ -105,13 +114,15 @@ class Server {
void process_request(socket_t sock);
bool read_request_line(FILE* fp, Request& req);
bool routing(Request& req, Response& res);
bool dispatch_request(Request& req, Response& res, Handlers& handlers);

socket_t svr_sock_;
Handlers get_handlers_;
Handlers post_handlers_;
Handler error_handler_;
Logger logger_;
bool handle_file_request(Request& req, Response& res);
bool dispatch_request(Request& req, Response& res, Handlers& handlers);

socket_t svr_sock_;
std::string base_dir_;
Handlers get_handlers_;
Handlers post_handlers_;
Handler error_handler_;
Logger logger_;
};

class Client {
Expand Down Expand Up @@ -156,7 +167,7 @@ void split(const char* b, const char* e, char d, Fn fn)

inline void get_flie_pointers(int fd, FILE*& fp_read, FILE*& fp_write)
{
#ifdef _WIN32
#ifdef _MSC_VER
int osfhandle = _open_osfhandle(fd, _O_RDONLY);
fp_read = _fdopen(osfhandle, "rb");
fp_write = _fdopen(osfhandle, "wb");
Expand All @@ -169,7 +180,7 @@ inline void get_flie_pointers(int fd, FILE*& fp_read, FILE*& fp_write)
template <typename Fn>
socket_t create_socket(const char* host, int port, Fn fn)
{
#ifdef _WIN32
#ifdef _MSC_VER
int opt = SO_SYNCHRONOUS_NONALERT;
setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char*)&opt, sizeof(opt));
#endif
Expand Down Expand Up @@ -215,7 +226,7 @@ inline socket_t create_server_socket(const char* host, int port)

inline int shutdown_socket(socket_t sock)
{
#ifdef _WIN32
#ifdef _MSC_VER
return shutdown(sock, SD_BOTH);
#else
return shutdown(sock, SHUT_RDWR);
Expand All @@ -224,7 +235,7 @@ inline int shutdown_socket(socket_t sock)

inline int close_socket(socket_t sock)
{
#ifdef _WIN32
#ifdef _MSC_VER
return closesocket(sock);
#else
return close(sock);
Expand All @@ -241,6 +252,45 @@ 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);
}

inline void read_file(const std::string& path, std::string& out)
{
auto fs = std::ifstream(path, std::ios_base::binary);
fs.seekg(0, std::ios_base::end);
auto size = fs.tellg();
fs.seekg(0);
out.assign(size, 0);
fs.read(&out[0], size);
}

inline std::string get_file_extention(const std::string& path)
{
std::smatch m;
auto pat = std::regex("\\.([a-zA-Z0-9]+)$");
auto ret = std::regex_search(path, m, pat);
std::string content_type;
if (ret) {
return m[1].str();
}
return std::string();
}

inline const char* get_content_type_from_file_extention(const std::string& ext)
{
if (ext == "html") {
return "text/html";
}
return "text/plain";
}

inline const char* status_message(int status)
{
switch (status) {
Expand Down Expand Up @@ -502,7 +552,7 @@ inline void parse_query_text(const std::string& s, Map& params)
});
}

#ifdef _WIN32
#ifdef _MSC_VER
class WSInit {
public:
WSInit::WSInit() {
Expand Down Expand Up @@ -573,6 +623,11 @@ 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)
Expand All @@ -585,6 +640,11 @@ 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)
{
base_dir_ = path;
}

inline void Server::set_error_handler(Handler handler)
{
error_handler_ = handler;
Expand Down Expand Up @@ -660,8 +720,31 @@ inline bool Server::read_request_line(FILE* fp, Request& req)
return false;
}

inline bool Server::handle_file_request(Request& req, Response& res)
{
std::string path = base_dir_ + req.url;

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

return false;
}

inline bool Server::routing(Request& req, Response& res)
{
if (req.method == "GET" && handle_file_request(req, res)) {
return true;
}

if (req.method == "GET" || req.method == "HEAD") {
return dispatch_request(req, res, get_handlers_);
} else if (req.method == "POST") {
Expand Down Expand Up @@ -723,8 +806,9 @@ inline void Server::process_request(socket_t sock)
detail::write_response(fp_write, req, res);
fflush(fp_write);

fclose(fp_read);
fclose(fp_write);
// NOTE: The following code causes problem on Windows...
//fclose(fp_read);
//fclose(fp_write);

if (logger_) {
logger_(req, res);
Expand Down Expand Up @@ -782,8 +866,9 @@ inline bool Client::send(const Request& req, Response& res)
}
}

fclose(fp_read);
fclose(fp_write);
// NOTE: The following code causes problem on Windows...
//fclose(fp_read);
//fclose(fp_write);

detail::shutdown_socket(sock);
detail::close_socket(sock);
Expand Down
20 changes: 20 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class ServerTest : public ::testing::Test {
}

virtual void SetUp() {
svr_.set_base_dir("./www");

svr_.get("/hi", [&](const Request& req, Response& res) {
res.set_content("Hello World!", "text/plain");
});
Expand Down Expand Up @@ -247,6 +249,24 @@ TEST_F(ServerTest, PostMethod2)
ASSERT_EQ("coder", res->body);
}

TEST_F(ServerTest, GetMethodDir)
{
auto res = cli_.get("/dir/");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
EXPECT_EQ("index.html", res->body);
}

TEST_F(ServerTest, GetMethodDirTest)
{
auto res = cli_.get("/dir/test.html");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
EXPECT_EQ("test.html", res->body);
}

#ifdef _WIN32
TEST(CleanupTest, WSACleanup)
{
Expand Down
28 changes: 28 additions & 0 deletions test/test.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Express 2013 for Windows Desktop
VisualStudioVersion = 12.0.20617.1 PREVIEW
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test.vcxproj", "{6B3E6769-052D-4BC0-9D2C-E9127C3DBB26}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6B3E6769-052D-4BC0-9D2C-E9127C3DBB26}.Debug|Win32.ActiveCfg = Debug|Win32
{6B3E6769-052D-4BC0-9D2C-E9127C3DBB26}.Debug|Win32.Build.0 = Debug|Win32
{6B3E6769-052D-4BC0-9D2C-E9127C3DBB26}.Debug|x64.ActiveCfg = Debug|x64
{6B3E6769-052D-4BC0-9D2C-E9127C3DBB26}.Debug|x64.Build.0 = Debug|x64
{6B3E6769-052D-4BC0-9D2C-E9127C3DBB26}.Release|Win32.ActiveCfg = Release|Win32
{6B3E6769-052D-4BC0-9D2C-E9127C3DBB26}.Release|Win32.Build.0 = Release|Win32
{6B3E6769-052D-4BC0-9D2C-E9127C3DBB26}.Release|x64.ActiveCfg = Release|x64
{6B3E6769-052D-4BC0-9D2C-E9127C3DBB26}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
20 changes: 0 additions & 20 deletions test/test.vc10.sln

This file was deleted.

Loading

0 comments on commit eef74af

Please sign in to comment.