httplib.h: use boost::regex, for some reasons as in 044ee8b32c87375fbd59735a13f082c8a037494e

This commit is contained in:
Albert S. 2018-12-09 23:58:45 +01:00
parent f992f4d20b
commit f5b594449d

View File

@ -1,6 +1,6 @@
// //
// httplib.h // httplib.h
// // Copyright (c) 2018 Albert S. All rights reserved.
// Copyright (c) 2017 Yuji Hirose. All rights reserved. // Copyright (c) 2017 Yuji Hirose. All rights reserved.
// MIT License // MIT License
// //
@ -59,7 +59,7 @@ typedef int socket_t;
#include <map> #include <map>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <regex> #include <boost/regex.hpp>
#include <string> #include <string>
#include <thread> #include <thread>
#include <sys/stat.h> #include <sys/stat.h>
@ -109,7 +109,7 @@ template <typename uint64_t, typename... Args>
std::pair<std::string, std::string> make_range_header(uint64_t value, Args... args); std::pair<std::string, std::string> make_range_header(uint64_t value, Args... args);
typedef std::multimap<std::string, std::string> Params; typedef std::multimap<std::string, std::string> Params;
typedef std::smatch Match; typedef boost::smatch Match;
typedef std::function<bool(uint64_t current, uint64_t total)> Progress; typedef std::function<bool(uint64_t current, uint64_t total)> Progress;
struct MultipartFile struct MultipartFile
@ -236,7 +236,7 @@ class Server
size_t keep_alive_max_count_; size_t keep_alive_max_count_;
private: private:
typedef std::vector<std::pair<std::regex, Handler>> Handlers; typedef std::vector<std::pair<boost::regex, Handler>> Handlers;
socket_t create_server_socket(const char *host, int port, int socket_flags) const; socket_t create_server_socket(const char *host, int port, int socket_flags) const;
int bind_internal(const char *host, int port, int socket_flags); int bind_internal(const char *host, int port, int socket_flags);
@ -744,9 +744,9 @@ inline void read_file(const std::string &path, std::string &out)
inline std::string file_extension(const std::string &path) inline std::string file_extension(const std::string &path)
{ {
std::smatch m; boost::smatch m;
auto pat = std::regex("\\.([a-zA-Z0-9]+)$"); auto pat = boost::regex("\\.([a-zA-Z0-9]+)$");
if(std::regex_search(path, m, pat)) if(boost::regex_search(path, m, pat))
{ {
return m[1].str(); return m[1].str();
} }
@ -866,7 +866,7 @@ inline int get_header_value_int(const Headers &headers, const char *key, int def
inline bool read_headers(Stream &strm, Headers &headers) inline bool read_headers(Stream &strm, Headers &headers)
{ {
static std::regex re(R"((.+?):\s*(.+?)\s*\r\n)"); static boost::regex re(R"((.+?):\s*(.+?)\s*\r\n)");
const auto bufsiz = 2048; const auto bufsiz = 2048;
char buf[bufsiz]; char buf[bufsiz];
@ -883,8 +883,8 @@ inline bool read_headers(Stream &strm, Headers &headers)
{ {
break; break;
} }
std::cmatch m; boost::cmatch m;
if(std::regex_match(reader.ptr(), m, re)) if(boost::regex_match(reader.ptr(), m, re))
{ {
auto key = std::string(m[1]); auto key = std::string(m[1]);
auto val = std::string(m[2]); auto val = std::string(m[2]);
@ -1267,10 +1267,10 @@ inline bool parse_multipart_formdata(const std::string &boundary, const std::str
static std::string dash = "--"; static std::string dash = "--";
static std::string crlf = "\r\n"; static std::string crlf = "\r\n";
static std::regex re_content_type("Content-Type: (.*?)", std::regex_constants::icase); static boost::regex re_content_type("Content-Type: (.*?)", boost::regex_constants::icase);
static std::regex re_content_disposition("Content-Disposition: form-data; name=\"(.*?)\"(?:; filename=\"(.*?)\")?", static boost::regex re_content_disposition(
std::regex_constants::icase); "Content-Disposition: form-data; name=\"(.*?)\"(?:; filename=\"(.*?)\")?", boost::regex_constants::icase);
auto dash_boundary = dash + boundary; auto dash_boundary = dash + boundary;
@ -1305,12 +1305,12 @@ inline bool parse_multipart_formdata(const std::string &boundary, const std::str
while(pos != next_pos) while(pos != next_pos)
{ {
std::smatch m; boost::smatch m;
if(std::regex_match(header, m, re_content_type)) if(boost::regex_match(header, m, re_content_type))
{ {
file.content_type = m[1]; file.content_type = m[1];
} }
else if(std::regex_match(header, m, re_content_disposition)) else if(boost::regex_match(header, m, re_content_disposition))
{ {
name = m[1]; name = m[1];
file.filename = m[2]; file.filename = m[2];
@ -1658,31 +1658,31 @@ inline Server::~Server()
inline Server &Server::Get(const char *pattern, Handler handler) inline Server &Server::Get(const char *pattern, Handler handler)
{ {
get_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); get_handlers_.push_back(std::make_pair(boost::regex(pattern), handler));
return *this; return *this;
} }
inline Server &Server::Post(const char *pattern, Handler handler) inline Server &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(boost::regex(pattern), handler));
return *this; return *this;
} }
inline Server &Server::Put(const char *pattern, Handler handler) inline Server &Server::Put(const char *pattern, Handler handler)
{ {
put_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); put_handlers_.push_back(std::make_pair(boost::regex(pattern), handler));
return *this; return *this;
} }
inline Server &Server::Delete(const char *pattern, Handler handler) inline Server &Server::Delete(const char *pattern, Handler handler)
{ {
delete_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); delete_handlers_.push_back(std::make_pair(boost::regex(pattern), handler));
return *this; return *this;
} }
inline Server &Server::Options(const char *pattern, Handler handler) inline Server &Server::Options(const char *pattern, Handler handler)
{ {
options_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); options_handlers_.push_back(std::make_pair(boost::regex(pattern), handler));
return *this; return *this;
} }
@ -1747,10 +1747,10 @@ inline void Server::stop()
inline bool Server::parse_request_line(const char *s, Request &req) inline bool Server::parse_request_line(const char *s, Request &req)
{ {
static std::regex re("(GET|HEAD|POST|PUT|DELETE|OPTIONS) (([^?]+)(?:\\?(.+?))?) (HTTP/1\\.[01])\r\n"); static boost::regex re("(GET|HEAD|POST|PUT|DELETE|OPTIONS) (([^?]+)(?:\\?(.+?))?) (HTTP/1\\.[01])\r\n");
std::cmatch m; boost::cmatch m;
if(std::regex_match(s, m, re)) if(boost::regex_match(s, m, re))
{ {
req.version = std::string(m[5]); req.version = std::string(m[5]);
req.method = std::string(m[1]); req.method = std::string(m[1]);
@ -2046,7 +2046,7 @@ inline bool Server::dispatch_request(Request &req, Response &res, Handlers &hand
const auto &pattern = x.first; const auto &pattern = x.first;
const auto &handler = x.second; const auto &handler = x.second;
if(std::regex_match(req.path, req.matches, pattern)) if(boost::regex_match(req.path, req.matches, pattern))
{ {
handler(req, res); handler(req, res);
return true; return true;
@ -2204,10 +2204,10 @@ inline bool Client::read_response_line(Stream &strm, Response &res)
return false; return false;
} }
const static std::regex re("(HTTP/1\\.[01]) (\\d+?) .+\r\n"); const static boost::regex re("(HTTP/1\\.[01]) (\\d+?) .+\r\n");
std::cmatch m; boost::cmatch m;
if(std::regex_match(reader.ptr(), m, re)) if(boost::regex_match(reader.ptr(), m, re))
{ {
res.version = std::string(m[1]); res.version = std::string(m[1]);
res.status = std::stoi(std::string(m[2])); res.status = std::stoi(std::string(m[2]));