Format cleanup.
This commit is contained in:
parent
123edd5a29
commit
1ee29f7b2c
82
httplib.h
82
httplib.h
@ -22,8 +22,8 @@
|
|||||||
#define snprintf _snprintf_s
|
#define snprintf _snprintf_s
|
||||||
#endif
|
#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)
|
#define S_ISDIR(m) (((m)&S_IFDIR)==S_IFDIR)
|
||||||
|
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
@ -117,8 +117,8 @@ private:
|
|||||||
void process_request(FILE* fp_read, FILE* fp_write);
|
void process_request(FILE* fp_read, FILE* fp_write);
|
||||||
bool read_request_line(FILE* fp, Request& req);
|
bool read_request_line(FILE* fp, Request& req);
|
||||||
bool routing(Request& req, Response& res);
|
bool routing(Request& req, Response& res);
|
||||||
bool handle_file_request(Request& req, Response& res);
|
bool handle_file_request(Request& req, Response& res);
|
||||||
bool dispatch_request(Request& req, Response& res, Handlers& handlers);
|
bool dispatch_request(Request& req, Response& res, Handlers& handlers);
|
||||||
|
|
||||||
socket_t svr_sock_;
|
socket_t svr_sock_;
|
||||||
std::string base_dir_;
|
std::string base_dir_;
|
||||||
@ -285,44 +285,44 @@ 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;
|
||||||
return stat(s.c_str(), &st) >= 0 && 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)
|
inline bool is_dir(const std::string& s)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
return stat(s.c_str(), &st) >= 0 && S_ISDIR(st.st_mode);
|
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)
|
||||||
{
|
{
|
||||||
auto fs = std::ifstream(path, std::ios_base::binary);
|
auto fs = std::ifstream(path, std::ios_base::binary);
|
||||||
fs.seekg(0, std::ios_base::end);
|
fs.seekg(0, std::ios_base::end);
|
||||||
auto size = fs.tellg();
|
auto size = fs.tellg();
|
||||||
fs.seekg(0);
|
fs.seekg(0);
|
||||||
out.resize(size);
|
out.resize(size);
|
||||||
fs.read(&out[0], size);
|
fs.read(&out[0], size);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string get_file_extention(const std::string& path)
|
inline std::string get_file_extention(const std::string& path)
|
||||||
{
|
{
|
||||||
std::smatch m;
|
std::smatch m;
|
||||||
auto pat = std::regex("\\.([a-zA-Z0-9]+)$");
|
auto pat = std::regex("\\.([a-zA-Z0-9]+)$");
|
||||||
auto ret = std::regex_search(path, m, pat);
|
auto ret = std::regex_search(path, m, pat);
|
||||||
std::string content_type;
|
std::string content_type;
|
||||||
if (ret) {
|
if (ret) {
|
||||||
return m[1].str();
|
return m[1].str();
|
||||||
}
|
}
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const char* get_content_type_from_file_extention(const std::string& ext)
|
inline const char* get_content_type_from_file_extention(const std::string& ext)
|
||||||
{
|
{
|
||||||
if (ext == "html") {
|
if (ext == "html") {
|
||||||
return "text/html";
|
return "text/html";
|
||||||
}
|
}
|
||||||
return "text/plain";
|
return "text/plain";
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const char* status_message(int status)
|
inline const char* status_message(int status)
|
||||||
@ -776,13 +776,13 @@ inline bool Server::handle_file_request(Request& req, Response& res)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Server::routing(Request& req, Response& res)
|
inline bool Server::routing(Request& req, Response& res)
|
||||||
{
|
{
|
||||||
if (req.method == "GET" && handle_file_request(req, res)) {
|
if (req.method == "GET" && handle_file_request(req, res)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method == "GET" || req.method == "HEAD") {
|
if (req.method == "GET" || req.method == "HEAD") {
|
||||||
@ -881,20 +881,20 @@ inline bool Client::send(const Request& req, Response& res)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return detail::read_and_close_socket(sock, [&](FILE* fp_read, FILE* fp_write) {
|
return detail::read_and_close_socket(sock, [&](FILE* fp_read, FILE* fp_write) {
|
||||||
// Send request
|
// Send request
|
||||||
detail::write_request(fp_write, req);
|
detail::write_request(fp_write, req);
|
||||||
fflush(fp_write);
|
fflush(fp_write);
|
||||||
|
|
||||||
// Receive response
|
// Receive response
|
||||||
if (!read_response_line(fp_read, res) ||
|
if (!read_response_line(fp_read, res) ||
|
||||||
!detail::read_headers(fp_read, res.headers)) {
|
!detail::read_headers(fp_read, res.headers)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (req.method != "HEAD") {
|
if (req.method != "HEAD") {
|
||||||
if (!detail::read_content(res, fp_read)) {
|
if (!detail::read_content(res, fp_read)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user