mirror of
https://github.com/yhirose/cpp-httplib
synced 2026-06-08 18:30:49 +00:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a609330e4c | |||
| c029597a5a | |||
| 30b7732565 | |||
| 6650632e7f | |||
| afe627e7af | |||
| 67f6ff7fa9 | |||
| c7ed1796a7 | |||
| 44c62d838e | |||
| 6bb580cda8 | |||
| 00a8cb8e5d | |||
| 2e34a39673 | |||
| 01b90829bc | |||
| e699bd0730 | |||
| 961a9379d5 | |||
| ec87b04aff | |||
| aabf752a51 | |||
| afb0674ccb | |||
| ee625232a4 | |||
| 52d8dd41f1 | |||
| be07d2d7a9 | |||
| 0f1b62c2b3 | |||
| c30906a541 | |||
| 3533503323 |
+2
-2
@@ -256,13 +256,13 @@ configure_package_config_file("${PROJECT_NAME}Config.cmake.in"
|
||||
if(HTTPLIB_COMPILE)
|
||||
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
|
||||
# Example: if you find_package(httplib 0.5.4)
|
||||
# then anything >= 0.5 and <= 1.0 is accepted
|
||||
# then anything >= 0.5.4 and < 0.6 is accepted
|
||||
COMPATIBILITY SameMinorVersion
|
||||
)
|
||||
else()
|
||||
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
|
||||
# Example: if you find_package(httplib 0.5.4)
|
||||
# then anything >= 0.5 and <= 1.0 is accepted
|
||||
# then anything >= 0.5.4 and < 0.6 is accepted
|
||||
COMPATIBILITY SameMinorVersion
|
||||
# Tells Cmake that it's a header-only lib
|
||||
# Mildly useful for end-users :)
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#ifndef CPPHTTPLIB_HTTPLIB_H
|
||||
#define CPPHTTPLIB_HTTPLIB_H
|
||||
|
||||
#define CPPHTTPLIB_VERSION "0.13.0"
|
||||
#define CPPHTTPLIB_VERSION "0.14.0"
|
||||
|
||||
/*
|
||||
* Configuration
|
||||
@@ -193,6 +193,7 @@ using socket_t = SOCKET;
|
||||
#endif
|
||||
#include <csignal>
|
||||
#include <pthread.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
@@ -486,8 +487,7 @@ struct Request {
|
||||
|
||||
bool has_header(const std::string &key) const;
|
||||
std::string get_header_value(const std::string &key, size_t id = 0) const;
|
||||
template <typename T>
|
||||
T get_header_value(const std::string &key, size_t id = 0) const;
|
||||
uint64_t get_header_value_u64(const std::string &key, size_t id = 0) const;
|
||||
size_t get_header_value_count(const std::string &key) const;
|
||||
void set_header(const std::string &key, const std::string &val);
|
||||
|
||||
@@ -519,8 +519,7 @@ struct Response {
|
||||
|
||||
bool has_header(const std::string &key) const;
|
||||
std::string get_header_value(const std::string &key, size_t id = 0) const;
|
||||
template <typename T>
|
||||
T get_header_value(const std::string &key, size_t id = 0) const;
|
||||
uint64_t get_header_value_u64(const std::string &key, size_t id = 0) const;
|
||||
size_t get_header_value_count(const std::string &key) const;
|
||||
void set_header(const std::string &key, const std::string &val);
|
||||
|
||||
@@ -668,6 +667,8 @@ using SocketOptions = std::function<void(socket_t sock)>;
|
||||
|
||||
void default_socket_options(socket_t sock);
|
||||
|
||||
const char *status_message(int status);
|
||||
|
||||
namespace detail {
|
||||
|
||||
class MatcherBase {
|
||||
@@ -736,6 +737,8 @@ private:
|
||||
std::regex regex_;
|
||||
};
|
||||
|
||||
ssize_t write_headers(Stream &strm, const Headers &headers);
|
||||
|
||||
} // namespace detail
|
||||
|
||||
class Server {
|
||||
@@ -782,6 +785,7 @@ public:
|
||||
bool remove_mount_point(const std::string &mount_point);
|
||||
Server &set_file_extension_and_mimetype_mapping(const std::string &ext,
|
||||
const std::string &mime);
|
||||
Server &set_default_file_mimetype(const std::string &mime);
|
||||
Server &set_file_request_handler(Handler handler);
|
||||
|
||||
Server &set_error_handler(HandlerWithResponse handler);
|
||||
@@ -798,6 +802,8 @@ public:
|
||||
Server &set_socket_options(SocketOptions socket_options);
|
||||
|
||||
Server &set_default_headers(Headers headers);
|
||||
Server &
|
||||
set_header_writer(std::function<ssize_t(Stream &, Headers &)> const &writer);
|
||||
|
||||
Server &set_keep_alive_max_count(size_t count);
|
||||
Server &set_keep_alive_timeout(time_t sec);
|
||||
@@ -905,6 +911,7 @@ private:
|
||||
};
|
||||
std::vector<MountPointEntry> base_dirs_;
|
||||
std::map<std::string, std::string> file_extension_and_mimetype_map_;
|
||||
std::string default_file_mimetype_ = "application/octet-stream";
|
||||
Handler file_request_handler_;
|
||||
|
||||
Handlers get_handlers_;
|
||||
@@ -931,6 +938,8 @@ private:
|
||||
SocketOptions socket_options_ = default_socket_options;
|
||||
|
||||
Headers default_headers_;
|
||||
std::function<ssize_t(Stream &, Headers &)> header_writer_ =
|
||||
detail::write_headers;
|
||||
};
|
||||
|
||||
enum class Error {
|
||||
@@ -948,6 +957,7 @@ enum class Error {
|
||||
UnsupportedMultipartBoundaryChars,
|
||||
Compression,
|
||||
ConnectionTimeout,
|
||||
ProxyConnection,
|
||||
|
||||
// For internal use only
|
||||
SSLPeerCouldBeClosed_,
|
||||
@@ -982,8 +992,8 @@ public:
|
||||
bool has_request_header(const std::string &key) const;
|
||||
std::string get_request_header_value(const std::string &key,
|
||||
size_t id = 0) const;
|
||||
template <typename T>
|
||||
T get_request_header_value(const std::string &key, size_t id = 0) const;
|
||||
uint64_t get_request_header_value_u64(const std::string &key,
|
||||
size_t id = 0) const;
|
||||
size_t get_request_header_value_count(const std::string &key) const;
|
||||
|
||||
private:
|
||||
@@ -1160,6 +1170,9 @@ public:
|
||||
|
||||
void set_default_headers(Headers headers);
|
||||
|
||||
void
|
||||
set_header_writer(std::function<ssize_t(Stream &, Headers &)> const &writer);
|
||||
|
||||
void set_address_family(int family);
|
||||
void set_tcp_nodelay(bool on);
|
||||
void set_socket_options(SocketOptions socket_options);
|
||||
@@ -1269,6 +1282,10 @@ protected:
|
||||
// Default headers
|
||||
Headers default_headers_;
|
||||
|
||||
// Header writer
|
||||
std::function<ssize_t(Stream &, Headers &)> header_writer_ =
|
||||
detail::write_headers;
|
||||
|
||||
// Settings
|
||||
std::string client_cert_path_;
|
||||
std::string client_key_path_;
|
||||
@@ -1535,6 +1552,9 @@ public:
|
||||
|
||||
void set_default_headers(Headers headers);
|
||||
|
||||
void
|
||||
set_header_writer(std::function<ssize_t(Stream &, Headers &)> const &writer);
|
||||
|
||||
void set_address_family(int family);
|
||||
void set_tcp_nodelay(bool on);
|
||||
void set_socket_options(SocketOptions socket_options);
|
||||
@@ -1704,15 +1724,9 @@ inline void duration_to_sec_and_usec(const T &duration, U callback) {
|
||||
callback(static_cast<time_t>(sec), static_cast<time_t>(usec));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T get_header_value(const Headers & /*headers*/,
|
||||
const std::string & /*key*/, size_t /*id*/ = 0,
|
||||
uint64_t /*def*/ = 0) {}
|
||||
|
||||
template <>
|
||||
inline uint64_t get_header_value<uint64_t>(const Headers &headers,
|
||||
const std::string &key, size_t id,
|
||||
uint64_t def) {
|
||||
inline uint64_t get_header_value_u64(const Headers &headers,
|
||||
const std::string &key, size_t id,
|
||||
uint64_t def) {
|
||||
auto rng = headers.equal_range(key);
|
||||
auto it = rng.first;
|
||||
std::advance(it, static_cast<ssize_t>(id));
|
||||
@@ -1724,14 +1738,14 @@ inline uint64_t get_header_value<uint64_t>(const Headers &headers,
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename T>
|
||||
inline T Request::get_header_value(const std::string &key, size_t id) const {
|
||||
return detail::get_header_value<T>(headers, key, id, 0);
|
||||
inline uint64_t Request::get_header_value_u64(const std::string &key,
|
||||
size_t id) const {
|
||||
return detail::get_header_value_u64(headers, key, id, 0);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T Response::get_header_value(const std::string &key, size_t id) const {
|
||||
return detail::get_header_value<T>(headers, key, id, 0);
|
||||
inline uint64_t Response::get_header_value_u64(const std::string &key,
|
||||
size_t id) const {
|
||||
return detail::get_header_value_u64(headers, key, id, 0);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
@@ -1776,6 +1790,76 @@ inline void default_socket_options(socket_t sock) {
|
||||
#endif
|
||||
}
|
||||
|
||||
inline const char *status_message(int status) {
|
||||
switch (status) {
|
||||
case 100: return "Continue";
|
||||
case 101: return "Switching Protocol";
|
||||
case 102: return "Processing";
|
||||
case 103: return "Early Hints";
|
||||
case 200: return "OK";
|
||||
case 201: return "Created";
|
||||
case 202: return "Accepted";
|
||||
case 203: return "Non-Authoritative Information";
|
||||
case 204: return "No Content";
|
||||
case 205: return "Reset Content";
|
||||
case 206: return "Partial Content";
|
||||
case 207: return "Multi-Status";
|
||||
case 208: return "Already Reported";
|
||||
case 226: return "IM Used";
|
||||
case 300: return "Multiple Choice";
|
||||
case 301: return "Moved Permanently";
|
||||
case 302: return "Found";
|
||||
case 303: return "See Other";
|
||||
case 304: return "Not Modified";
|
||||
case 305: return "Use Proxy";
|
||||
case 306: return "unused";
|
||||
case 307: return "Temporary Redirect";
|
||||
case 308: return "Permanent Redirect";
|
||||
case 400: return "Bad Request";
|
||||
case 401: return "Unauthorized";
|
||||
case 402: return "Payment Required";
|
||||
case 403: return "Forbidden";
|
||||
case 404: return "Not Found";
|
||||
case 405: return "Method Not Allowed";
|
||||
case 406: return "Not Acceptable";
|
||||
case 407: return "Proxy Authentication Required";
|
||||
case 408: return "Request Timeout";
|
||||
case 409: return "Conflict";
|
||||
case 410: return "Gone";
|
||||
case 411: return "Length Required";
|
||||
case 412: return "Precondition Failed";
|
||||
case 413: return "Payload Too Large";
|
||||
case 414: return "URI Too Long";
|
||||
case 415: return "Unsupported Media Type";
|
||||
case 416: return "Range Not Satisfiable";
|
||||
case 417: return "Expectation Failed";
|
||||
case 418: return "I'm a teapot";
|
||||
case 421: return "Misdirected Request";
|
||||
case 422: return "Unprocessable Entity";
|
||||
case 423: return "Locked";
|
||||
case 424: return "Failed Dependency";
|
||||
case 425: return "Too Early";
|
||||
case 426: return "Upgrade Required";
|
||||
case 428: return "Precondition Required";
|
||||
case 429: return "Too Many Requests";
|
||||
case 431: return "Request Header Fields Too Large";
|
||||
case 451: return "Unavailable For Legal Reasons";
|
||||
case 501: return "Not Implemented";
|
||||
case 502: return "Bad Gateway";
|
||||
case 503: return "Service Unavailable";
|
||||
case 504: return "Gateway Timeout";
|
||||
case 505: return "HTTP Version Not Supported";
|
||||
case 506: return "Variant Also Negotiates";
|
||||
case 507: return "Insufficient Storage";
|
||||
case 508: return "Loop Detected";
|
||||
case 510: return "Not Extended";
|
||||
case 511: return "Network Authentication Required";
|
||||
|
||||
default:
|
||||
case 500: return "Internal Server Error";
|
||||
}
|
||||
}
|
||||
|
||||
template <class Rep, class Period>
|
||||
inline Server &
|
||||
Server::set_read_timeout(const std::chrono::duration<Rep, Period> &duration) {
|
||||
@@ -1816,6 +1900,7 @@ inline std::string to_string(const Error error) {
|
||||
return "Unsupported HTTP multipart boundary characters";
|
||||
case Error::Compression: return "Compression failed";
|
||||
case Error::ConnectionTimeout: return "Connection timed out";
|
||||
case Error::ProxyConnection: return "Proxy connection failed";
|
||||
case Error::Unknown: return "Unknown";
|
||||
default: break;
|
||||
}
|
||||
@@ -1829,10 +1914,9 @@ inline std::ostream &operator<<(std::ostream &os, const Error &obj) {
|
||||
return os;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T Result::get_request_header_value(const std::string &key,
|
||||
size_t id) const {
|
||||
return detail::get_header_value<T>(request_headers_, key, id, 0);
|
||||
inline uint64_t Result::get_request_header_value_u64(const std::string &key,
|
||||
size_t id) const {
|
||||
return detail::get_header_value_u64(request_headers_, key, id, 0);
|
||||
}
|
||||
|
||||
template <class Rep, class Period>
|
||||
@@ -2068,6 +2152,29 @@ private:
|
||||
std::string glowable_buffer_;
|
||||
};
|
||||
|
||||
class mmap {
|
||||
public:
|
||||
mmap(const char *path);
|
||||
~mmap();
|
||||
|
||||
bool open(const char *path);
|
||||
void close();
|
||||
|
||||
bool is_open() const;
|
||||
size_t size() const;
|
||||
const char *data() const;
|
||||
|
||||
private:
|
||||
#if defined(_WIN32)
|
||||
HANDLE hFile_;
|
||||
HANDLE hMapping_;
|
||||
#else
|
||||
int fd_;
|
||||
#endif
|
||||
size_t size_;
|
||||
void *addr_;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -2356,6 +2463,13 @@ inline std::string trim_copy(const std::string &s) {
|
||||
return s.substr(r.first, r.second - r.first);
|
||||
}
|
||||
|
||||
inline std::string trim_double_quotes_copy(const std::string &s) {
|
||||
if (s.length() >= 2 && s.front() == '"' && s.back() == '"') {
|
||||
return s.substr(1, s.size() - 2);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
inline void split(const char *b, const char *e, char d,
|
||||
std::function<void(const char *, const char *)> fn) {
|
||||
size_t i = 0;
|
||||
@@ -2441,6 +2555,95 @@ inline void stream_line_reader::append(char c) {
|
||||
}
|
||||
}
|
||||
|
||||
inline mmap::mmap(const char *path)
|
||||
#if defined(_WIN32)
|
||||
: hFile_(NULL), hMapping_(NULL)
|
||||
#else
|
||||
: fd_(-1)
|
||||
#endif
|
||||
,
|
||||
size_(0), addr_(nullptr) {
|
||||
if (!open(path)) { std::runtime_error(""); }
|
||||
}
|
||||
|
||||
inline mmap::~mmap() { close(); }
|
||||
|
||||
inline bool mmap::open(const char *path) {
|
||||
close();
|
||||
|
||||
#if defined(_WIN32)
|
||||
hFile_ = ::CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL,
|
||||
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
|
||||
if (hFile_ == INVALID_HANDLE_VALUE) { return false; }
|
||||
|
||||
size_ = ::GetFileSize(hFile_, NULL);
|
||||
|
||||
hMapping_ = ::CreateFileMapping(hFile_, NULL, PAGE_READONLY, 0, 0, NULL);
|
||||
|
||||
if (hMapping_ == NULL) {
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
|
||||
addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0);
|
||||
#else
|
||||
fd_ = ::open(path, O_RDONLY);
|
||||
if (fd_ == -1) { return false; }
|
||||
|
||||
struct stat sb;
|
||||
if (fstat(fd_, &sb) == -1) {
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
size_ = static_cast<size_t>(sb.st_size);
|
||||
|
||||
addr_ = ::mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0);
|
||||
#endif
|
||||
|
||||
if (addr_ == nullptr) {
|
||||
close();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool mmap::is_open() const { return addr_ != nullptr; }
|
||||
|
||||
inline size_t mmap::size() const { return size_; }
|
||||
|
||||
inline const char *mmap::data() const { return (const char *)addr_; }
|
||||
|
||||
inline void mmap::close() {
|
||||
#if defined(_WIN32)
|
||||
if (addr_) {
|
||||
::UnmapViewOfFile(addr_);
|
||||
addr_ = nullptr;
|
||||
}
|
||||
|
||||
if (hMapping_) {
|
||||
::CloseHandle(hMapping_);
|
||||
hMapping_ = NULL;
|
||||
}
|
||||
|
||||
if (hFile_ != INVALID_HANDLE_VALUE) {
|
||||
::CloseHandle(hFile_);
|
||||
hFile_ = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
#else
|
||||
if (addr_ != nullptr) {
|
||||
munmap(addr_, size_);
|
||||
addr_ = nullptr;
|
||||
}
|
||||
|
||||
if (fd_ != -1) {
|
||||
::close(fd_);
|
||||
fd_ = -1;
|
||||
}
|
||||
#endif
|
||||
size_ = 0;
|
||||
}
|
||||
inline int close_socket(socket_t sock) {
|
||||
#ifdef _WIN32
|
||||
return closesocket(sock);
|
||||
@@ -3116,9 +3319,10 @@ inline constexpr unsigned int operator"" _t(const char *s, size_t l) {
|
||||
|
||||
} // namespace udl
|
||||
|
||||
inline const char *
|
||||
inline std::string
|
||||
find_content_type(const std::string &path,
|
||||
const std::map<std::string, std::string> &user_data) {
|
||||
const std::map<std::string, std::string> &user_data,
|
||||
const std::string &default_content_type) {
|
||||
auto ext = file_extension(path);
|
||||
|
||||
auto it = user_data.find(ext);
|
||||
@@ -3127,7 +3331,8 @@ find_content_type(const std::string &path,
|
||||
using udl::operator""_t;
|
||||
|
||||
switch (str2tag(ext)) {
|
||||
default: return nullptr;
|
||||
default: return default_content_type;
|
||||
|
||||
case "css"_t: return "text/css";
|
||||
case "csv"_t: return "text/csv";
|
||||
case "htm"_t:
|
||||
@@ -3180,76 +3385,6 @@ find_content_type(const std::string &path,
|
||||
}
|
||||
}
|
||||
|
||||
inline const char *status_message(int status) {
|
||||
switch (status) {
|
||||
case 100: return "Continue";
|
||||
case 101: return "Switching Protocol";
|
||||
case 102: return "Processing";
|
||||
case 103: return "Early Hints";
|
||||
case 200: return "OK";
|
||||
case 201: return "Created";
|
||||
case 202: return "Accepted";
|
||||
case 203: return "Non-Authoritative Information";
|
||||
case 204: return "No Content";
|
||||
case 205: return "Reset Content";
|
||||
case 206: return "Partial Content";
|
||||
case 207: return "Multi-Status";
|
||||
case 208: return "Already Reported";
|
||||
case 226: return "IM Used";
|
||||
case 300: return "Multiple Choice";
|
||||
case 301: return "Moved Permanently";
|
||||
case 302: return "Found";
|
||||
case 303: return "See Other";
|
||||
case 304: return "Not Modified";
|
||||
case 305: return "Use Proxy";
|
||||
case 306: return "unused";
|
||||
case 307: return "Temporary Redirect";
|
||||
case 308: return "Permanent Redirect";
|
||||
case 400: return "Bad Request";
|
||||
case 401: return "Unauthorized";
|
||||
case 402: return "Payment Required";
|
||||
case 403: return "Forbidden";
|
||||
case 404: return "Not Found";
|
||||
case 405: return "Method Not Allowed";
|
||||
case 406: return "Not Acceptable";
|
||||
case 407: return "Proxy Authentication Required";
|
||||
case 408: return "Request Timeout";
|
||||
case 409: return "Conflict";
|
||||
case 410: return "Gone";
|
||||
case 411: return "Length Required";
|
||||
case 412: return "Precondition Failed";
|
||||
case 413: return "Payload Too Large";
|
||||
case 414: return "URI Too Long";
|
||||
case 415: return "Unsupported Media Type";
|
||||
case 416: return "Range Not Satisfiable";
|
||||
case 417: return "Expectation Failed";
|
||||
case 418: return "I'm a teapot";
|
||||
case 421: return "Misdirected Request";
|
||||
case 422: return "Unprocessable Entity";
|
||||
case 423: return "Locked";
|
||||
case 424: return "Failed Dependency";
|
||||
case 425: return "Too Early";
|
||||
case 426: return "Upgrade Required";
|
||||
case 428: return "Precondition Required";
|
||||
case 429: return "Too Many Requests";
|
||||
case 431: return "Request Header Fields Too Large";
|
||||
case 451: return "Unavailable For Legal Reasons";
|
||||
case 501: return "Not Implemented";
|
||||
case 502: return "Bad Gateway";
|
||||
case 503: return "Service Unavailable";
|
||||
case 504: return "Gateway Timeout";
|
||||
case 505: return "HTTP Version Not Supported";
|
||||
case 506: return "Variant Also Negotiates";
|
||||
case 507: return "Insufficient Storage";
|
||||
case 508: return "Loop Detected";
|
||||
case 510: return "Not Extended";
|
||||
case 511: return "Network Authentication Required";
|
||||
|
||||
default:
|
||||
case 500: return "Internal Server Error";
|
||||
}
|
||||
}
|
||||
|
||||
inline bool can_compress_content_type(const std::string &content_type) {
|
||||
using udl::operator""_t;
|
||||
|
||||
@@ -3384,16 +3519,12 @@ inline bool gzip_decompressor::decompress(const char *data, size_t data_length,
|
||||
data += strm_.avail_in;
|
||||
|
||||
std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
|
||||
while (strm_.avail_in > 0) {
|
||||
while (strm_.avail_in > 0 && ret == Z_OK) {
|
||||
strm_.avail_out = static_cast<uInt>(buff.size());
|
||||
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
|
||||
|
||||
auto prev_avail_in = strm_.avail_in;
|
||||
|
||||
ret = inflate(&strm_, Z_NO_FLUSH);
|
||||
|
||||
if (prev_avail_in - strm_.avail_in == 0) { return false; }
|
||||
|
||||
assert(ret != Z_STREAM_ERROR);
|
||||
switch (ret) {
|
||||
case Z_NEED_DICT:
|
||||
@@ -3770,7 +3901,7 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
|
||||
} else if (!has_header(x.headers, "Content-Length")) {
|
||||
ret = read_content_without_length(strm, out);
|
||||
} else {
|
||||
auto len = get_header_value<uint64_t>(x.headers, "Content-Length");
|
||||
auto len = get_header_value_u64(x.headers, "Content-Length", 0, 0);
|
||||
if (len > payload_max_length) {
|
||||
exceed_payload_max_length = true;
|
||||
skip_content_with_length(strm, len);
|
||||
@@ -4066,14 +4197,34 @@ inline bool parse_multipart_boundary(const std::string &content_type,
|
||||
if (pos == std::string::npos) { return false; }
|
||||
auto end = content_type.find(';', pos);
|
||||
auto beg = pos + strlen(boundary_keyword);
|
||||
boundary = content_type.substr(beg, end - beg);
|
||||
if (boundary.length() >= 2 && boundary.front() == '"' &&
|
||||
boundary.back() == '"') {
|
||||
boundary = boundary.substr(1, boundary.size() - 2);
|
||||
}
|
||||
boundary = trim_double_quotes_copy(content_type.substr(beg, end - beg));
|
||||
return !boundary.empty();
|
||||
}
|
||||
|
||||
inline void parse_disposition_params(const std::string &s, Params ¶ms) {
|
||||
std::set<std::string> cache;
|
||||
split(s.data(), s.data() + s.size(), ';', [&](const char *b, const char *e) {
|
||||
std::string kv(b, e);
|
||||
if (cache.find(kv) != cache.end()) { return; }
|
||||
cache.insert(kv);
|
||||
|
||||
std::string key;
|
||||
std::string val;
|
||||
split(b, e, '=', [&](const char *b2, const char *e2) {
|
||||
if (key.empty()) {
|
||||
key.assign(b2, e2);
|
||||
} else {
|
||||
val.assign(b2, e2);
|
||||
}
|
||||
});
|
||||
|
||||
if (!key.empty()) {
|
||||
params.emplace(trim_double_quotes_copy((key)),
|
||||
trim_double_quotes_copy((val)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_NO_EXCEPTIONS
|
||||
inline bool parse_range_header(const std::string &s, Ranges &ranges) {
|
||||
#else
|
||||
@@ -4084,7 +4235,7 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) try {
|
||||
if (std::regex_match(s, m, re_first_range)) {
|
||||
auto pos = static_cast<size_t>(m.position(1));
|
||||
auto len = static_cast<size_t>(m.length(1));
|
||||
bool all_valid_ranges = true;
|
||||
auto all_valid_ranges = true;
|
||||
split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) {
|
||||
if (!all_valid_ranges) return;
|
||||
static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))");
|
||||
@@ -4131,11 +4282,6 @@ public:
|
||||
bool parse(const char *buf, size_t n, const ContentReceiver &content_callback,
|
||||
const MultipartContentHeader &header_callback) {
|
||||
|
||||
// TODO: support 'filename*'
|
||||
static const std::regex re_content_disposition(
|
||||
R"~(^Content-Disposition:\s*form-data;\s*name="(.*?)"(?:;\s*filename="(.*?)")?(?:;\s*filename\*=\S+)?\s*$)~",
|
||||
std::regex_constants::icase);
|
||||
|
||||
buf_append(buf, n);
|
||||
|
||||
while (buf_size() > 0) {
|
||||
@@ -4173,10 +4319,40 @@ public:
|
||||
if (start_with_case_ignore(header, header_name)) {
|
||||
file_.content_type = trim_copy(header.substr(header_name.size()));
|
||||
} else {
|
||||
static const std::regex re_content_disposition(
|
||||
R"~(^Content-Disposition:\s*form-data;\s*(.*)$)~",
|
||||
std::regex_constants::icase);
|
||||
|
||||
std::smatch m;
|
||||
if (std::regex_match(header, m, re_content_disposition)) {
|
||||
file_.name = m[1];
|
||||
file_.filename = m[2];
|
||||
Params params;
|
||||
parse_disposition_params(m[1], params);
|
||||
|
||||
auto it = params.find("name");
|
||||
if (it != params.end()) {
|
||||
file_.name = it->second;
|
||||
} else {
|
||||
is_valid_ = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
it = params.find("filename");
|
||||
if (it != params.end()) { file_.filename = it->second; }
|
||||
|
||||
it = params.find("filename*");
|
||||
if (it != params.end()) {
|
||||
// Only allow UTF-8 enconnding...
|
||||
static const std::regex re_rfc5987_encoding(
|
||||
R"~(^UTF-8''(.+?)$)~", std::regex_constants::icase);
|
||||
|
||||
std::smatch m2;
|
||||
if (std::regex_match(it->second, m2, re_rfc5987_encoding)) {
|
||||
file_.filename = decode_url(m2[1], false); // override...
|
||||
} else {
|
||||
is_valid_ = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
is_valid_ = false;
|
||||
return false;
|
||||
@@ -4217,9 +4393,9 @@ public:
|
||||
buf_erase(crlf_.size());
|
||||
state_ = 1;
|
||||
} else {
|
||||
if (dash_crlf_.size() > buf_size()) { return true; }
|
||||
if (buf_start_with(dash_crlf_)) {
|
||||
buf_erase(dash_crlf_.size());
|
||||
if (dash_.size() > buf_size()) { return true; }
|
||||
if (buf_start_with(dash_)) {
|
||||
buf_erase(dash_.size());
|
||||
is_valid_ = true;
|
||||
buf_erase(buf_size()); // Remove epilogue
|
||||
} else {
|
||||
@@ -4252,7 +4428,6 @@ private:
|
||||
|
||||
const std::string dash_ = "--";
|
||||
const std::string crlf_ = "\r\n";
|
||||
const std::string dash_crlf_ = "--\r\n";
|
||||
std::string boundary_;
|
||||
std::string dash_boundary_crlf_;
|
||||
std::string crlf_dash_boundary_;
|
||||
@@ -4438,12 +4613,13 @@ get_range_offset_and_length(const Request &req, size_t content_length,
|
||||
return std::make_pair(r.first, static_cast<size_t>(r.second - r.first) + 1);
|
||||
}
|
||||
|
||||
inline std::string make_content_range_header_field(size_t offset, size_t length,
|
||||
size_t content_length) {
|
||||
inline std::string
|
||||
make_content_range_header_field(const std::pair<ssize_t, ssize_t> &range,
|
||||
size_t content_length) {
|
||||
std::string field = "bytes ";
|
||||
field += std::to_string(offset);
|
||||
if (range.first != -1) { field += std::to_string(range.first); }
|
||||
field += "-";
|
||||
field += std::to_string(offset + length - 1);
|
||||
if (range.second != -1) { field += std::to_string(range.second); }
|
||||
field += "/";
|
||||
field += std::to_string(content_length);
|
||||
return field;
|
||||
@@ -4465,21 +4641,22 @@ bool process_multipart_ranges_data(const Request &req, Response &res,
|
||||
ctoken("\r\n");
|
||||
}
|
||||
|
||||
auto offsets = get_range_offset_and_length(req, res.body.size(), i);
|
||||
ctoken("Content-Range: ");
|
||||
const auto &range = req.ranges[i];
|
||||
stoken(make_content_range_header_field(range, res.content_length_));
|
||||
ctoken("\r\n");
|
||||
ctoken("\r\n");
|
||||
|
||||
auto offsets = get_range_offset_and_length(req, res.content_length_, i);
|
||||
auto offset = offsets.first;
|
||||
auto length = offsets.second;
|
||||
|
||||
ctoken("Content-Range: ");
|
||||
stoken(make_content_range_header_field(offset, length, res.body.size()));
|
||||
ctoken("\r\n");
|
||||
ctoken("\r\n");
|
||||
if (!content(offset, length)) { return false; }
|
||||
ctoken("\r\n");
|
||||
}
|
||||
|
||||
ctoken("--");
|
||||
stoken(boundary);
|
||||
ctoken("--\r\n");
|
||||
ctoken("--");
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -4816,7 +4993,7 @@ inline bool parse_www_authenticate(const Response &res,
|
||||
s = s.substr(pos + 1);
|
||||
auto beg = std::sregex_iterator(s.begin(), s.end(), re);
|
||||
for (auto i = beg; i != std::sregex_iterator(); ++i) {
|
||||
auto m = *i;
|
||||
const auto &m = *i;
|
||||
auto key = s.substr(static_cast<size_t>(m.position(1)),
|
||||
static_cast<size_t>(m.length(1)));
|
||||
auto val = m.length(2) > 0
|
||||
@@ -5275,7 +5452,7 @@ inline PathParamsMatcher::PathParamsMatcher(const std::string &pattern) {
|
||||
}
|
||||
|
||||
inline bool PathParamsMatcher::match(Request &request) const {
|
||||
request.matches = {};
|
||||
request.matches = std::smatch();
|
||||
request.path_params.clear();
|
||||
request.path_params.reserve(param_names_.size());
|
||||
|
||||
@@ -5442,6 +5619,11 @@ Server::set_file_extension_and_mimetype_mapping(const std::string &ext,
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Server &Server::set_default_file_mimetype(const std::string &mime) {
|
||||
default_file_mimetype_ = mime;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Server &Server::set_file_request_handler(Handler handler) {
|
||||
file_request_handler_ = std::move(handler);
|
||||
return *this;
|
||||
@@ -5506,6 +5688,12 @@ inline Server &Server::set_default_headers(Headers headers) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Server &Server::set_header_writer(
|
||||
std::function<ssize_t(Stream &, Headers &)> const &writer) {
|
||||
header_writer_ = writer;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline Server &Server::set_keep_alive_max_count(size_t count) {
|
||||
keep_alive_max_count_ = count;
|
||||
return *this;
|
||||
@@ -5696,11 +5884,11 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection,
|
||||
detail::BufferStream bstrm;
|
||||
|
||||
if (!bstrm.write_format("HTTP/1.1 %d %s\r\n", res.status,
|
||||
detail::status_message(res.status))) {
|
||||
status_message(res.status))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!detail::write_headers(bstrm, res.headers)) { return false; }
|
||||
if (!header_writer_(bstrm, res.headers)) { return false; }
|
||||
|
||||
// Flush buffer
|
||||
auto &data = bstrm.get_buffer();
|
||||
@@ -5893,17 +6081,26 @@ inline bool Server::handle_file_request(const Request &req, Response &res,
|
||||
if (path.back() == '/') { path += "index.html"; }
|
||||
|
||||
if (detail::is_file(path)) {
|
||||
detail::read_file(path, res.body);
|
||||
auto type =
|
||||
detail::find_content_type(path, file_extension_and_mimetype_map_);
|
||||
if (type) { res.set_header("Content-Type", type); }
|
||||
for (const auto &kv : entry.headers) {
|
||||
res.set_header(kv.first.c_str(), kv.second);
|
||||
}
|
||||
res.status = req.has_header("Range") ? 206 : 200;
|
||||
|
||||
auto mm = std::make_shared<detail::mmap>(path.c_str());
|
||||
if (!mm->is_open()) { return false; }
|
||||
|
||||
res.set_content_provider(
|
||||
mm->size(),
|
||||
detail::find_content_type(path, file_extension_and_mimetype_map_,
|
||||
default_file_mimetype_),
|
||||
[mm](size_t offset, size_t length, DataSink &sink) -> bool {
|
||||
sink.write(mm->data() + offset, length);
|
||||
return true;
|
||||
});
|
||||
|
||||
if (!head && file_request_handler_) {
|
||||
file_request_handler_(req, res);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -6151,10 +6348,10 @@ inline void Server::apply_ranges(const Request &req, Response &res,
|
||||
} else if (req.ranges.size() == 1) {
|
||||
auto offsets =
|
||||
detail::get_range_offset_and_length(req, res.content_length_, 0);
|
||||
auto offset = offsets.first;
|
||||
length = offsets.second;
|
||||
|
||||
auto content_range = detail::make_content_range_header_field(
|
||||
offset, length, res.content_length_);
|
||||
req.ranges[0], res.content_length_);
|
||||
res.set_header("Content-Range", content_range);
|
||||
} else {
|
||||
length = detail::get_multipart_ranges_data_length(req, res, boundary,
|
||||
@@ -6177,13 +6374,15 @@ inline void Server::apply_ranges(const Request &req, Response &res,
|
||||
if (req.ranges.empty()) {
|
||||
;
|
||||
} else if (req.ranges.size() == 1) {
|
||||
auto content_range = detail::make_content_range_header_field(
|
||||
req.ranges[0], res.body.size());
|
||||
res.set_header("Content-Range", content_range);
|
||||
|
||||
auto offsets =
|
||||
detail::get_range_offset_and_length(req, res.body.size(), 0);
|
||||
auto offset = offsets.first;
|
||||
auto length = offsets.second;
|
||||
auto content_range = detail::make_content_range_header_field(
|
||||
offset, length, res.body.size());
|
||||
res.set_header("Content-Range", content_range);
|
||||
|
||||
if (offset < res.body.size()) {
|
||||
res.body = res.body.substr(offset, length);
|
||||
} else {
|
||||
@@ -6262,15 +6461,10 @@ Server::process_request(Stream &strm, bool close_connection,
|
||||
if (!line_reader.getline()) { return false; }
|
||||
|
||||
Request req;
|
||||
|
||||
Response res;
|
||||
|
||||
res.version = "HTTP/1.1";
|
||||
|
||||
for (const auto &header : default_headers_) {
|
||||
if (res.headers.find(header.first) == res.headers.end()) {
|
||||
res.headers.insert(header);
|
||||
}
|
||||
}
|
||||
res.headers = default_headers_;
|
||||
|
||||
#ifdef _WIN32
|
||||
// TODO: Increase FD_SETSIZE statically (libzmq), dynamically (MySQL).
|
||||
@@ -6337,14 +6531,14 @@ Server::process_request(Stream &strm, bool close_connection,
|
||||
case 100:
|
||||
case 417:
|
||||
strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status,
|
||||
detail::status_message(status));
|
||||
status_message(status));
|
||||
break;
|
||||
default: return write_response(strm, close_connection, req, res);
|
||||
}
|
||||
}
|
||||
|
||||
// Rounting
|
||||
bool routed = false;
|
||||
auto routed = false;
|
||||
#ifdef CPPHTTPLIB_NO_EXCEPTIONS
|
||||
routed = routing(req, res, strm);
|
||||
#else
|
||||
@@ -6542,9 +6736,9 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req,
|
||||
if (!line_reader.getline()) { return false; }
|
||||
|
||||
#ifdef CPPHTTPLIB_ALLOW_LF_AS_LINE_TERMINATOR
|
||||
const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n");
|
||||
#else
|
||||
const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r?\n");
|
||||
#else
|
||||
const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n");
|
||||
#endif
|
||||
|
||||
std::cmatch m;
|
||||
@@ -6704,6 +6898,21 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req,
|
||||
|
||||
if (!ret) { return false; }
|
||||
|
||||
if (res.get_header_value("Connection") == "close" ||
|
||||
(res.version == "HTTP/1.0" && res.reason != "Connection established")) {
|
||||
// TODO this requires a not-entirely-obvious chain of calls to be correct
|
||||
// for this to be safe.
|
||||
|
||||
// This is safe to call because handle_request is only called by send_
|
||||
// which locks the request mutex during the process. It would be a bug
|
||||
// to call it from a different thread since it's a thread-safety issue
|
||||
// to do these things to the socket if another thread is using the socket.
|
||||
std::lock_guard<std::mutex> guard(socket_mutex_);
|
||||
shutdown_ssl(socket_, true);
|
||||
shutdown_socket(socket_);
|
||||
close_socket(socket_);
|
||||
}
|
||||
|
||||
if (300 < res.status && res.status < 400 && follow_location_) {
|
||||
req = req_save;
|
||||
ret = redirect(req, res, error);
|
||||
@@ -6918,7 +7127,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
|
||||
const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path;
|
||||
bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str());
|
||||
|
||||
detail::write_headers(bstrm, req.headers);
|
||||
header_writer_(bstrm, req.headers);
|
||||
|
||||
// Flush buffer
|
||||
auto &data = bstrm.get_buffer();
|
||||
@@ -7118,24 +7327,6 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
|
||||
}
|
||||
}
|
||||
|
||||
if (res.get_header_value("Connection") == "close" ||
|
||||
(res.version == "HTTP/1.0" && res.reason != "Connection established")) {
|
||||
// TODO this requires a not-entirely-obvious chain of calls to be correct
|
||||
// for this to be safe. Maybe a code refactor (such as moving this out to
|
||||
// the send function and getting rid of the recursiveness of the mutex)
|
||||
// could make this more obvious.
|
||||
|
||||
// This is safe to call because process_request is only called by
|
||||
// handle_request which is only called by send, which locks the request
|
||||
// mutex during the process. It would be a bug to call it from a different
|
||||
// thread since it's a thread-safety issue to do these things to the socket
|
||||
// if another thread is using the socket.
|
||||
std::lock_guard<std::mutex> guard(socket_mutex_);
|
||||
shutdown_ssl(socket_, true);
|
||||
shutdown_socket(socket_);
|
||||
close_socket(socket_);
|
||||
}
|
||||
|
||||
// Log
|
||||
if (logger_) { logger_(req, res); }
|
||||
|
||||
@@ -7163,7 +7354,7 @@ inline ContentProviderWithoutLength ClientImpl::get_multipart_content_provider(
|
||||
}
|
||||
|
||||
DataSink cur_sink;
|
||||
bool has_data = true;
|
||||
auto has_data = true;
|
||||
cur_sink.write = sink.write;
|
||||
cur_sink.done = [&]() { has_data = false; };
|
||||
|
||||
@@ -7747,6 +7938,11 @@ inline void ClientImpl::set_default_headers(Headers headers) {
|
||||
default_headers_ = std::move(headers);
|
||||
}
|
||||
|
||||
inline void ClientImpl::set_header_writer(
|
||||
std::function<ssize_t(Stream &, Headers &)> const &writer) {
|
||||
header_writer_ = writer;
|
||||
}
|
||||
|
||||
inline void ClientImpl::set_address_family(int family) {
|
||||
address_family_ = family;
|
||||
}
|
||||
@@ -8243,14 +8439,14 @@ inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) {
|
||||
inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res,
|
||||
bool &success, Error &error) {
|
||||
success = true;
|
||||
Response res2;
|
||||
Response proxy_res;
|
||||
if (!detail::process_client_socket(
|
||||
socket.sock, read_timeout_sec_, read_timeout_usec_,
|
||||
write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) {
|
||||
Request req2;
|
||||
req2.method = "CONNECT";
|
||||
req2.path = host_and_port_;
|
||||
return process_request(strm, req2, res2, false, error);
|
||||
return process_request(strm, req2, proxy_res, false, error);
|
||||
})) {
|
||||
// Thread-safe to close everything because we are assuming there are no
|
||||
// requests in flight
|
||||
@@ -8261,12 +8457,12 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res,
|
||||
return false;
|
||||
}
|
||||
|
||||
if (res2.status == 407) {
|
||||
if (proxy_res.status == 407) {
|
||||
if (!proxy_digest_auth_username_.empty() &&
|
||||
!proxy_digest_auth_password_.empty()) {
|
||||
std::map<std::string, std::string> auth;
|
||||
if (detail::parse_www_authenticate(res2, auth, true)) {
|
||||
Response res3;
|
||||
if (detail::parse_www_authenticate(proxy_res, auth, true)) {
|
||||
proxy_res = Response();
|
||||
if (!detail::process_client_socket(
|
||||
socket.sock, read_timeout_sec_, read_timeout_usec_,
|
||||
write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) {
|
||||
@@ -8277,7 +8473,7 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res,
|
||||
req3, auth, 1, detail::random_string(10),
|
||||
proxy_digest_auth_username_, proxy_digest_auth_password_,
|
||||
true));
|
||||
return process_request(strm, req3, res3, false, error);
|
||||
return process_request(strm, req3, proxy_res, false, error);
|
||||
})) {
|
||||
// Thread-safe to close everything because we are assuming there are
|
||||
// no requests in flight
|
||||
@@ -8288,17 +8484,28 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res,
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
res = res2;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// If status code is not 200, proxy request is failed.
|
||||
// Set error to ProxyConnection and return proxy response
|
||||
// as the response of the request
|
||||
if (proxy_res.status != 200) {
|
||||
error = Error::ProxyConnection;
|
||||
res = std::move(proxy_res);
|
||||
// Thread-safe to close everything because we are assuming there are
|
||||
// no requests in flight
|
||||
shutdown_ssl(socket, true);
|
||||
shutdown_socket(socket);
|
||||
close_socket(socket);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool SSLClient::load_certs() {
|
||||
bool ret = true;
|
||||
auto ret = true;
|
||||
|
||||
std::call_once(initialize_cert_, [&]() {
|
||||
std::lock_guard<std::mutex> guard(ctx_mutex_);
|
||||
@@ -8930,6 +9137,11 @@ inline void Client::set_default_headers(Headers headers) {
|
||||
cli_->set_default_headers(std::move(headers));
|
||||
}
|
||||
|
||||
inline void Client::set_header_writer(
|
||||
std::function<ssize_t(Stream &, Headers &)> const &writer) {
|
||||
cli_->set_header_writer(writer);
|
||||
}
|
||||
|
||||
inline void Client::set_address_family(int family) {
|
||||
cli_->set_address_family(family);
|
||||
}
|
||||
|
||||
+8
-1
@@ -86,6 +86,12 @@ subdir(join_paths('www', 'dir'))
|
||||
subdir(join_paths('www2', 'dir'))
|
||||
subdir(join_paths('www3', 'dir'))
|
||||
|
||||
# GoogleTest 1.13.0 requires C++14
|
||||
test_options = []
|
||||
if gtest_dep.version().version_compare('>=1.13.0')
|
||||
test_options += 'cpp_std=c++14'
|
||||
endif
|
||||
|
||||
test(
|
||||
'main',
|
||||
executable(
|
||||
@@ -94,7 +100,8 @@ test(
|
||||
dependencies: [
|
||||
cpp_httplib_dep,
|
||||
gtest_dep
|
||||
]
|
||||
],
|
||||
override_options: test_options
|
||||
),
|
||||
depends: [
|
||||
key_pem,
|
||||
|
||||
+265
-11
@@ -211,8 +211,7 @@ TEST(GetHeaderValueTest, DefaultValue) {
|
||||
|
||||
TEST(GetHeaderValueTest, DefaultValueInt) {
|
||||
Headers headers = {{"Dummy", "Dummy"}};
|
||||
auto val =
|
||||
detail::get_header_value<uint64_t>(headers, "Content-Length", 0, 100);
|
||||
auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 100);
|
||||
EXPECT_EQ(100ull, val);
|
||||
}
|
||||
|
||||
@@ -241,8 +240,7 @@ TEST(GetHeaderValueTest, SetContent) {
|
||||
|
||||
TEST(GetHeaderValueTest, RegularValueInt) {
|
||||
Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
|
||||
auto val =
|
||||
detail::get_header_value<uint64_t>(headers, "Content-Length", 0, 0);
|
||||
auto val = detail::get_header_value_u64(headers, "Content-Length", 0, 0);
|
||||
EXPECT_EQ(100ull, val);
|
||||
}
|
||||
|
||||
@@ -444,7 +442,7 @@ TEST(HostnameToIPConversionTest, HTTPWatch_Online) {
|
||||
auto host = "www.httpwatch.com";
|
||||
|
||||
auto ip = hosted_at(host);
|
||||
EXPECT_EQ("191.236.16.12", ip);
|
||||
EXPECT_EQ("23.96.13.243", ip);
|
||||
|
||||
std::vector<std::string> addrs;
|
||||
hosted_at(host, addrs);
|
||||
@@ -1016,7 +1014,7 @@ TEST(UrlWithSpace, Redirect_Online) {
|
||||
auto res = cli.Get("/files/2595/310/Neat 1.4-17.jar");
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ(18527U, res->get_header_value<uint64_t>("Content-Length"));
|
||||
EXPECT_EQ(18527U, res->get_header_value_u64("Content-Length"));
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1594,6 +1592,46 @@ TEST(URLFragmentTest, WithFragment) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(HeaderWriter, SetHeaderWriter) {
|
||||
Server svr;
|
||||
|
||||
svr.set_header_writer([](Stream &strm, Headers &hdrs) {
|
||||
hdrs.emplace("CustomServerHeader", "CustomServerValue");
|
||||
return detail::write_headers(strm, hdrs);
|
||||
});
|
||||
svr.Get("/hi", [](const Request &req, Response &res) {
|
||||
auto it = req.headers.find("CustomClientHeader");
|
||||
EXPECT_TRUE(it != req.headers.end());
|
||||
EXPECT_EQ(it->second, "CustomClientValue");
|
||||
res.set_content("Hello World!\n", "text/plain");
|
||||
});
|
||||
|
||||
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
thread.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
});
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
|
||||
{
|
||||
Client cli(HOST, PORT);
|
||||
cli.set_header_writer([](Stream &strm, Headers &hdrs) {
|
||||
hdrs.emplace("CustomClientHeader", "CustomClientValue");
|
||||
return detail::write_headers(strm, hdrs);
|
||||
});
|
||||
|
||||
auto res = cli.Get("/hi");
|
||||
EXPECT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
|
||||
auto it = res->headers.find("CustomServerHeader");
|
||||
EXPECT_TRUE(it != res->headers.end());
|
||||
EXPECT_EQ(it->second, "CustomServerValue");
|
||||
}
|
||||
}
|
||||
|
||||
class ServerTest : public ::testing::Test {
|
||||
protected:
|
||||
ServerTest()
|
||||
@@ -2501,6 +2539,55 @@ TEST_F(ServerTest, StaticFileRange) {
|
||||
EXPECT_EQ(std::string("cd"), res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, StaticFileRanges) {
|
||||
auto res =
|
||||
cli_.Get("/dir/test.abcde", {{make_range_header({{1, 2}, {4, -1}})}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(206, res->status);
|
||||
EXPECT_TRUE(
|
||||
res->get_header_value("Content-Type")
|
||||
.find(
|
||||
"multipart/byteranges; boundary=--cpp-httplib-multipart-data-") ==
|
||||
0);
|
||||
EXPECT_EQ("265", res->get_header_value("Content-Length"));
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, StaticFileRangeHead) {
|
||||
auto res = cli_.Head("/dir/test.abcde", {{make_range_header({{2, 3}})}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(206, res->status);
|
||||
EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
|
||||
EXPECT_EQ("2", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ(true, res->has_header("Content-Range"));
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, StaticFileRangeBigFile) {
|
||||
auto res = cli_.Get("/dir/1MB.txt", {{make_range_header({{-1, 5}})}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(206, res->status);
|
||||
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
|
||||
EXPECT_EQ("5", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ(true, res->has_header("Content-Range"));
|
||||
EXPECT_EQ("LAST\n", res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, StaticFileRangeBigFile2) {
|
||||
auto res = cli_.Get("/dir/1MB.txt", {{make_range_header({{1, 4097}})}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(206, res->status);
|
||||
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
|
||||
EXPECT_EQ("4097", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ(true, res->has_header("Content-Range"));
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, StaticFileBigFile) {
|
||||
auto res = cli_.Get("/dir/1MB.txt");
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
|
||||
EXPECT_EQ("1048576", res->get_header_value("Content-Length"));
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, InvalidBaseDirMount) {
|
||||
EXPECT_EQ(false, svr_.set_mount_point("invalid_mount_point", "./www3"));
|
||||
}
|
||||
@@ -2886,9 +2973,9 @@ TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
|
||||
cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(206, res->status);
|
||||
EXPECT_EQ("269", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ("267", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ(false, res->has_header("Content-Range"));
|
||||
EXPECT_EQ(269U, res->body.size());
|
||||
EXPECT_EQ(267U, res->body.size());
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetStreamedEndless) {
|
||||
@@ -2978,9 +3065,9 @@ TEST_F(ServerTest, GetWithRangeMultipart) {
|
||||
auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(206, res->status);
|
||||
EXPECT_EQ("269", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ("267", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ(false, res->has_header("Content-Range"));
|
||||
EXPECT_EQ(269U, res->body.size());
|
||||
EXPECT_EQ(267U, res->body.size());
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) {
|
||||
@@ -3247,7 +3334,7 @@ TEST_F(ServerTest, PutLargeFileWithGzip2) {
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ(LARGE_DATA, res->body);
|
||||
EXPECT_EQ(101942u, res.get_request_header_value<uint64_t>("Content-Length"));
|
||||
EXPECT_EQ(101942u, res.get_request_header_value_u64("Content-Length"));
|
||||
EXPECT_EQ("gzip", res.get_request_header_value("Content-Encoding"));
|
||||
}
|
||||
|
||||
@@ -3330,6 +3417,59 @@ TEST(GzipDecompressor, ChunkedDecompression) {
|
||||
ASSERT_EQ(data, decompressed_data);
|
||||
}
|
||||
|
||||
TEST(GzipDecompressor, DeflateDecompression) {
|
||||
std::string original_text = "Raw deflate without gzip";
|
||||
unsigned char data[32] = {0x78, 0x9C, 0x0B, 0x4A, 0x2C, 0x57, 0x48, 0x49,
|
||||
0x4D, 0xCB, 0x49, 0x2C, 0x49, 0x55, 0x28, 0xCF,
|
||||
0x2C, 0xC9, 0xC8, 0x2F, 0x2D, 0x51, 0x48, 0xAF,
|
||||
0xCA, 0x2C, 0x00, 0x00, 0x6F, 0x98, 0x09, 0x2E};
|
||||
std::string compressed_data(data, data + sizeof(data) / sizeof(data[0]));
|
||||
|
||||
std::string decompressed_data;
|
||||
{
|
||||
httplib::detail::gzip_decompressor decompressor;
|
||||
|
||||
bool result = decompressor.decompress(
|
||||
compressed_data.data(), compressed_data.size(),
|
||||
[&](const char *decompressed_data_chunk,
|
||||
size_t decompressed_data_chunk_size) {
|
||||
decompressed_data.insert(decompressed_data.size(),
|
||||
decompressed_data_chunk,
|
||||
decompressed_data_chunk_size);
|
||||
return true;
|
||||
});
|
||||
ASSERT_TRUE(result);
|
||||
}
|
||||
ASSERT_EQ(original_text, decompressed_data);
|
||||
}
|
||||
|
||||
TEST(GzipDecompressor, DeflateDecompressionTrailingBytes) {
|
||||
std::string original_text = "Raw deflate without gzip";
|
||||
unsigned char data[40] = {0x78, 0x9C, 0x0B, 0x4A, 0x2C, 0x57, 0x48, 0x49,
|
||||
0x4D, 0xCB, 0x49, 0x2C, 0x49, 0x55, 0x28, 0xCF,
|
||||
0x2C, 0xC9, 0xC8, 0x2F, 0x2D, 0x51, 0x48, 0xAF,
|
||||
0xCA, 0x2C, 0x00, 0x00, 0x6F, 0x98, 0x09, 0x2E,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
std::string compressed_data(data, data + sizeof(data) / sizeof(data[0]));
|
||||
|
||||
std::string decompressed_data;
|
||||
{
|
||||
httplib::detail::gzip_decompressor decompressor;
|
||||
|
||||
bool result = decompressor.decompress(
|
||||
compressed_data.data(), compressed_data.size(),
|
||||
[&](const char *decompressed_data_chunk,
|
||||
size_t decompressed_data_chunk_size) {
|
||||
decompressed_data.insert(decompressed_data.size(),
|
||||
decompressed_data_chunk,
|
||||
decompressed_data_chunk_size);
|
||||
return true;
|
||||
});
|
||||
ASSERT_TRUE(result);
|
||||
}
|
||||
ASSERT_EQ(original_text, decompressed_data);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
TEST(GzipDecompressor, LargeRandomData) {
|
||||
|
||||
@@ -6052,6 +6192,120 @@ TEST(MultipartFormDataTest, PutInvalidBoundaryChars) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(MultipartFormDataTest, AlternateFilename) {
|
||||
auto handled = false;
|
||||
|
||||
Server svr;
|
||||
svr.Post("/test", [&](const Request &req, Response &res) {
|
||||
ASSERT_EQ(3u, req.files.size());
|
||||
|
||||
auto it = req.files.begin();
|
||||
ASSERT_EQ("file1", it->second.name);
|
||||
ASSERT_EQ("A.txt", it->second.filename);
|
||||
ASSERT_EQ("text/plain", it->second.content_type);
|
||||
ASSERT_EQ("Content of a.txt.\r\n", it->second.content);
|
||||
|
||||
++it;
|
||||
ASSERT_EQ("file2", it->second.name);
|
||||
ASSERT_EQ("a.html", it->second.filename);
|
||||
ASSERT_EQ("text/html", it->second.content_type);
|
||||
ASSERT_EQ("<!DOCTYPE html><title>Content of a.html.</title>\r\n",
|
||||
it->second.content);
|
||||
|
||||
++it;
|
||||
ASSERT_EQ("text", it->second.name);
|
||||
ASSERT_EQ("", it->second.filename);
|
||||
ASSERT_EQ("", it->second.content_type);
|
||||
ASSERT_EQ("text default", it->second.content);
|
||||
|
||||
res.set_content("ok", "text/plain");
|
||||
|
||||
handled = true;
|
||||
});
|
||||
|
||||
thread t = thread([&] { svr.listen(HOST, PORT); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
t.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
ASSERT_TRUE(handled);
|
||||
});
|
||||
|
||||
svr.wait_until_ready();
|
||||
|
||||
auto req = "POST /test HTTP/1.1\r\n"
|
||||
"Content-Type: multipart/form-data;boundary=--------\r\n"
|
||||
"Content-Length: 399\r\n"
|
||||
"\r\n"
|
||||
"----------\r\n"
|
||||
"Content-Disposition: form-data; name=\"text\"\r\n"
|
||||
"\r\n"
|
||||
"text default\r\n"
|
||||
"----------\r\n"
|
||||
"Content-Disposition: form-data; filename*=\"UTF-8''\%41.txt\"; "
|
||||
"filename=\"a.txt\"; name=\"file1\"\r\n"
|
||||
"Content-Type: text/plain\r\n"
|
||||
"\r\n"
|
||||
"Content of a.txt.\r\n"
|
||||
"\r\n"
|
||||
"----------\r\n"
|
||||
"Content-Disposition: form-data; name=\"file2\" ;filename = "
|
||||
"\"a.html\"\r\n"
|
||||
"Content-Type: text/html\r\n"
|
||||
"\r\n"
|
||||
"<!DOCTYPE html><title>Content of a.html.</title>\r\n"
|
||||
"\r\n"
|
||||
"------------\r\n";
|
||||
|
||||
ASSERT_TRUE(send_request(1, req));
|
||||
}
|
||||
|
||||
TEST(MultipartFormDataTest, CloseDelimiterWithoutCRLF) {
|
||||
auto handled = false;
|
||||
|
||||
Server svr;
|
||||
svr.Post("/test", [&](const Request &req, Response &) {
|
||||
ASSERT_EQ(2u, req.files.size());
|
||||
|
||||
auto it = req.files.begin();
|
||||
ASSERT_EQ("text1", it->second.name);
|
||||
ASSERT_EQ("text1", it->second.content);
|
||||
|
||||
++it;
|
||||
ASSERT_EQ("text2", it->second.name);
|
||||
ASSERT_EQ("text2", it->second.content);
|
||||
|
||||
handled = true;
|
||||
});
|
||||
|
||||
thread t = thread([&] { svr.listen(HOST, PORT); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
t.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
ASSERT_TRUE(handled);
|
||||
});
|
||||
|
||||
svr.wait_until_ready();
|
||||
|
||||
auto req = "POST /test HTTP/1.1\r\n"
|
||||
"Content-Type: multipart/form-data;boundary=--------\r\n"
|
||||
"Content-Length: 146\r\n"
|
||||
"\r\n----------\r\n"
|
||||
"Content-Disposition: form-data; name=\"text1\"\r\n"
|
||||
"\r\n"
|
||||
"text1"
|
||||
"\r\n----------\r\n"
|
||||
"Content-Disposition: form-data; name=\"text2\"\r\n"
|
||||
"\r\n"
|
||||
"text2"
|
||||
"\r\n------------";
|
||||
|
||||
std::string resonse;
|
||||
ASSERT_TRUE(send_request(1, req, &resonse));
|
||||
ASSERT_EQ("200", resonse.substr(9, 3));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user