httplib.h: use boost::regex, for some reasons as in 044ee8b32c87375fbd59735a13f082c8a037494e
This commit is contained in:
parent
ebbea6c4f7
commit
05e5353501
@ -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>
|
||||||
@ -106,7 +106,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 {
|
||||||
@ -224,7 +224,7 @@ protected:
|
|||||||
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);
|
||||||
@ -688,9 +688,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();
|
||||||
}
|
}
|
||||||
return std::string();
|
return std::string();
|
||||||
@ -772,7 +772,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];
|
||||||
@ -786,8 +786,8 @@ inline bool read_headers(Stream& strm, Headers& headers)
|
|||||||
if (!strcmp(reader.ptr(), "\r\n")) {
|
if (!strcmp(reader.ptr(), "\r\n")) {
|
||||||
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]);
|
||||||
headers.emplace(key, val);
|
headers.emplace(key, val);
|
||||||
@ -1092,12 +1092,12 @@ inline bool parse_multipart_formdata(
|
|||||||
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(
|
static boost::regex re_content_type(
|
||||||
"Content-Type: (.*?)", std::regex_constants::icase);
|
"Content-Type: (.*?)", boost::regex_constants::icase);
|
||||||
|
|
||||||
static std::regex re_content_disposition(
|
static boost::regex re_content_disposition(
|
||||||
"Content-Disposition: form-data; name=\"(.*?)\"(?:; filename=\"(.*?)\")?",
|
"Content-Disposition: form-data; name=\"(.*?)\"(?:; filename=\"(.*?)\")?",
|
||||||
std::regex_constants::icase);
|
boost::regex_constants::icase);
|
||||||
|
|
||||||
auto dash_boundary = dash + boundary;
|
auto dash_boundary = dash + boundary;
|
||||||
|
|
||||||
@ -1127,10 +1127,10 @@ inline bool parse_multipart_formdata(
|
|||||||
auto header = body.substr(pos, (next_pos - pos));
|
auto header = body.substr(pos, (next_pos - pos));
|
||||||
|
|
||||||
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];
|
||||||
}
|
}
|
||||||
@ -1462,31 +1462,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1548,10 +1548,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]);
|
||||||
req.target = std::string(m[2]);
|
req.target = std::string(m[2]);
|
||||||
@ -1797,7 +1797,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;
|
||||||
}
|
}
|
||||||
@ -1945,10 +1945,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]));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user