Compare commits

..

27 Commits

Author SHA1 Message Date
yhirose cc14855ba0 Fix #661 2020-09-26 04:50:09 -04:00
yhirose 56c418745f Fixed conction close problem with HTTP 1.0 client 2020-09-25 20:58:49 -04:00
yhirose 4ce9911837 Add <sstream> 2020-09-25 18:17:32 -04:00
yhirose 559c407552 Adjusted SlowRequest test 2020-09-25 18:13:10 -04:00
yhirose a2f4e29a7b Add set_keep_alive_timeout 2020-09-25 17:57:33 -04:00
mi01 b8cf739d27 Add cctype header (#656) 2020-09-16 16:32:49 -04:00
yhirose aec2f9521d Updated documentation 2020-09-15 10:11:46 -04:00
yhirose 7b55ecdc59 Fixed #650 2020-09-12 16:11:14 -04:00
tmahring e9575bcb78 don't replace plus with space in headers (#649)
* don't replace plus with space in headers

* fixed forward handling with changed header parsing

* add test for boundaries containing plus chars
2020-09-10 20:27:01 -04:00
Jonas Minnberg 308aeb187b Undefined if2ip() also on Android since getifaddrs() does not exist. (#648)
Co-authored-by: Jonas Minnberg <jonas@minnberg.se>
2020-09-10 07:52:01 -04:00
Jodi the Tigger 05d18f2bc5 Fix -D build flags containing escaped quotes (#645)
Fixes #638

Done by removed unneeded quotes from cmake's add_compiler_definitions()
2020-09-08 21:35:50 -04:00
Ivan Fefer 3da4a0ac69 Add compression buffer size customization (#644)
* add compression buffer size customization and small brotli refactor

* allocat brotli buffer once

* add init to brotli decoder buffer
2020-09-08 12:18:14 -04:00
yhirose 9d12b3f20e Fixed warnings and refactoring 2020-09-03 20:33:30 -04:00
Omkar Jadhav 852a374748 Fix server crash caused due to regex complexity while matching headers. (#632)
* Fix parsing to parse query string with single space char.

When passed ' ' as a query string, the server crashes cause of illegal memory access done in httplib::detail::split. Have added checks to make sure the split function has a valid string with length > 0.

* Fix parsing to parse query string with single space char.

* Fix server crash caused due to regex complexity while matching headers.

While parsing content-type header in multipart form request the server crashes due to the exhaustion of max iterations performed while matching the input string with content-type regex.
Have removed the regex which might use backtracking while matching and replaced it with manual string processing. Have added tests as well.

* Remove magic number

Co-authored-by: Ivan Fefer <fefer.ivan@gmail.com>

Co-authored-by: yhirose <yhirose@users.noreply.github.com>
Co-authored-by: Ivan Fefer <fefer.ivan@gmail.com>
2020-09-03 13:17:52 -04:00
Ivan Fefer 3b5bab3308 Fix gzip_decompressor in case of one chunk being exactly equal to buffer size (#636)
* add larget chunks test

* revert test

* Fix gzip decoder in case of chunk being equal to buffer size

* add test
2020-09-03 12:20:02 -04:00
yhirose 69e75f4a67 Fix #635. HTTPS request stucked with proxy (#637) 2020-09-03 12:17:53 -04:00
Omkar Jadhav b0fd4befb1 Fix query parsing issues (#629)
* Fix parsing to parse query string with single space char.

When passed ' ' as a query string, the server crashes cause of illegal memory access done in httplib::detail::split. Have added checks to make sure the split function has a valid string with length > 0.

* Fix parsing to parse query string with single space char.
2020-08-28 09:43:28 -04:00
yhirose 3e80666a74 Fix #628 2020-08-27 19:45:28 -04:00
yhirose 3e4567bae8 Updated Repl.it examples 2020-08-27 14:16:35 -04:00
yhirose db075d8cf9 Added Repl.it examples 2020-08-27 14:06:57 -04:00
yhirose 16df0ef37e Code cleanup 2020-08-26 12:18:49 -04:00
Ivan Fefer f1a2ac5108 Avoid copying of content provider if possible (#627) 2020-08-26 08:56:51 -04:00
yhirose e5743b358d Updated README 2020-08-25 20:26:53 -04:00
Ivan Fefer 1184bbe4cb Fix typo in README (#625) 2020-08-25 06:38:16 -04:00
Felix Kästner 9dcffda7ae Fix cmake execute_process working directory (#622)
Correct the working directory of the execute_process call, to get the version from git tags.
The working directory should be the the directory of this libary. Otherwise, if you include httplib
as a git submodule and call add_subdirectory, the execute_process command will be run in a wrong
directory leading to error.
2020-08-24 17:23:01 -04:00
yhirose e5903635e2 Fix #619 2020-08-22 12:54:43 -04:00
yhirose 510b4eaaae Fix #613 2020-08-17 13:40:06 -04:00
4 changed files with 431 additions and 219 deletions
+4 -3
View File
@@ -66,6 +66,7 @@ if(Git_FOUND)
# Gets the latest tag as a string like "v0.6.6"
# Can silently fail if git isn't on the system
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE _raw_version_string
ERROR_VARIABLE _git_tag_error
)
@@ -235,9 +236,9 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
# Set the definitions to enable optional features
target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:"CPPHTTPLIB_BROTLI_SUPPORT">
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:"CPPHTTPLIB_ZLIB_SUPPORT">
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:"CPPHTTPLIB_OPENSSL_SUPPORT">
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:CPPHTTPLIB_BROTLI_SUPPORT>
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:CPPHTTPLIB_ZLIB_SUPPORT>
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:CPPHTTPLIB_OPENSSL_SUPPORT>
)
# Cmake's find_package search path is different based on the system
+47 -9
View File
@@ -7,8 +7,41 @@ A C++11 single-file header-only cross platform HTTP/HTTPS library.
It's extremely easy to setup. Just include **httplib.h** file in your code!
Server Example
--------------
NOTE: This is a 'blocking' HTTP library. If you are looking for a 'non-blocking' library, this is not the one that you want.
Simple examples
---------------
#### Server
```c++
httplib::Server svr;
svr.Get("/hi", [](const httplib::Request &, httplib::Response &res) {
res.set_content("Hello World!", "text/plain");
});
svr.listen("0.0.0.0", 8080);
```
#### Client
```c++
httplib::Client cli("http://cpp-httplib-server.yhirose.repl.co");
auto res = cli.Get("/hi");
res->status; // 200
res->body; // "Hello World!"
```
### Try out the examples on Repl.it!
1. Run server at https://repl.it/@yhirose/cpp-httplib-server
2. Run client at https://repl.it/@yhirose/cpp-httplib-client
Server
------
```c++
#include <httplib.h>
@@ -197,7 +230,7 @@ svr.Get("/stream", [&](const Request &req, Response &res) {
// prepare data...
sink.write(data.data(), data.size());
} else {
done(); // No more data
sink.done(); // No more data
}
return true; // return 'false' if you want to cancel the process.
});
@@ -264,13 +297,18 @@ Please see [Server example](https://github.com/yhirose/cpp-httplib/blob/master/e
### Default thread pool support
`ThreadPool` is used as a **default** task queue, and the default thread count is 8, or `std::thread::hardware_concurrency()`. You can change it with `CPPHTTPLIB_THREAD_POOL_COUNT`.
`ThreadPool` is used as a default task queue, and the default thread count is set to value from `std::thread::hardware_concurrency()`.
If you want to set the thread count at runtime, there is no convenient way... But here is how.
You can change the thread count by setting `CPPHTTPLIB_THREAD_POOL_COUNT`.
```cpp
svr.new_task_queue = [] { return new ThreadPool(12); };
```
### Override the default thread pool with yours
You can supply your own thread pool implementation according to your need.
```cpp
class YourThreadPoolTaskQueue : public TaskQueue {
public:
@@ -295,8 +333,8 @@ svr.new_task_queue = [] {
};
```
Client Example
--------------
Client
------
```c++
#include <httplib.h>
@@ -564,9 +602,9 @@ NOTE: cpp-httplib currently supports only version 1.1.1.
```c++
#define CPPHTTPLIB_OPENSSL_SUPPORT
SSLServer svr("./cert.pem", "./key.pem");
httplib::SSLServer svr("./cert.pem", "./key.pem");
SSLClient cli("localhost", 8080);
httplib::SSLClient cli("localhost", 1234); // or `httplib::Client cli("https://localhost:1234");`
cli.set_ca_cert_path("./ca-bundle.crt");
cli.enable_server_certificate_verification(true);
```
+259 -193
View File
@@ -16,10 +16,6 @@
#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5
#endif
#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND
#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND 0
#endif
#ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT
#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5
#endif
@@ -80,6 +76,10 @@
#define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u)
#endif
#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ
#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u)
#endif
#ifndef CPPHTTPLIB_THREAD_POOL_COUNT
#define CPPHTTPLIB_THREAD_POOL_COUNT \
((std::max)(8u, std::thread::hardware_concurrency() > 0 \
@@ -176,6 +176,7 @@ using socket_t = int;
#include <array>
#include <atomic>
#include <cassert>
#include <cctype>
#include <climits>
#include <condition_variable>
#include <errno.h>
@@ -189,6 +190,7 @@ using socket_t = int;
#include <mutex>
#include <random>
#include <regex>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <thread>
@@ -281,7 +283,7 @@ public:
private:
class data_sink_streambuf : public std::streambuf {
public:
data_sink_streambuf(DataSink &sink) : sink_(sink) {}
explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {}
protected:
std::streamsize xsputn(const char *s, std::streamsize n) {
@@ -384,6 +386,7 @@ struct Request {
struct Response {
std::string version;
int status = -1;
std::string reason;
Headers headers;
std::string body;
@@ -402,15 +405,15 @@ struct Response {
void set_content_provider(
size_t length, const char *content_type, ContentProvider provider,
std::function<void()> resource_releaser = [] {});
const std::function<void()> &resource_releaser = nullptr);
void set_content_provider(
const char *content_type, ContentProviderWithoutLength provider,
std::function<void()> resource_releaser = [] {});
const std::function<void()> &resource_releaser = nullptr);
void set_chunked_content_provider(
const char *content_type, ContentProviderWithoutLength provider,
std::function<void()> resource_releaser = [] {});
const std::function<void()> &resource_releaser = nullptr);
Response() = default;
Response(const Response &) = default;
@@ -590,6 +593,7 @@ public:
void set_socket_options(SocketOptions socket_options);
void set_keep_alive_max_count(size_t count);
void set_keep_alive_timeout(time_t sec);
void set_read_timeout(time_t sec, time_t usec = 0);
void set_write_timeout(time_t sec, time_t usec = 0);
void set_idle_interval(time_t sec, time_t usec = 0);
@@ -614,6 +618,7 @@ protected:
std::atomic<socket_t> svr_sock_;
size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT;
time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND;
time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND;
time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND;
time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND;
@@ -634,10 +639,11 @@ private:
bool routing(Request &req, Response &res, Stream &strm);
bool handle_file_request(Request &req, Response &res, bool head = false);
bool dispatch_request(Request &req, Response &res, Handlers &handlers);
bool dispatch_request_for_content_reader(Request &req, Response &res,
ContentReader content_reader,
HandlersForContentReader &handlers);
bool dispatch_request(Request &req, Response &res, const Handlers &handlers);
bool
dispatch_request_for_content_reader(Request &req, Response &res,
ContentReader content_reader,
const HandlersForContentReader &handlers);
bool parse_request_line(const char *s, Request &req);
bool write_response(Stream &strm, bool close_connection, const Request &req,
@@ -696,14 +702,15 @@ enum Error {
class Result {
public:
Result(std::shared_ptr<Response> res, Error err) : res_(res), err_(err) {}
operator bool() { return res_ != nullptr; }
Result(const std::shared_ptr<Response> &res, Error err)
: res_(res), err_(err) {}
operator bool() const { return res_ != nullptr; }
bool operator==(std::nullptr_t) const { return res_ == nullptr; }
bool operator!=(std::nullptr_t) const { return res_ != nullptr; }
const Response &value() { return *res_; }
const Response &operator*() { return *res_; }
const Response *operator->() { return res_.get(); }
Error error() { return err_; }
const Response &value() const { return *res_; }
const Response &operator*() const { return *res_; }
const Response *operator->() const { return res_.get(); }
Error error() const { return err_; }
private:
std::shared_ptr<Response> res_;
@@ -899,7 +906,7 @@ protected:
std::string interface_;
std::string proxy_host_;
int proxy_port_;
int proxy_port_ = -1;
std::string proxy_basic_auth_username_;
std::string proxy_basic_auth_password_;
@@ -1245,6 +1252,14 @@ inline std::string from_i_to_hex(size_t n) {
return ret;
}
inline bool start_with(const std::string &a, const std::string &b) {
if (a.size() < b.size()) { return false; }
for (size_t i = 0; i < b.size(); i++) {
if (std::tolower(a[i]) != std::tolower(b[i])) { return false; }
}
return true;
}
inline size_t to_utf8(int code, char *buff) {
if (code < 0x0080) {
buff[0] = (code & 0x7F);
@@ -1438,25 +1453,32 @@ inline std::string file_extension(const std::string &path) {
return std::string();
}
inline std::pair<int, int> trim(const char *b, const char *e, int left,
int right) {
while (b + left < e && b[left] == ' ') {
inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; }
inline std::pair<size_t, size_t> trim(const char *b, const char *e, size_t left,
size_t right) {
while (b + left < e && is_space_or_tab(b[left])) {
left++;
}
while (right - 1 >= 0 && b[right - 1] == ' ') {
while (right > 0 && is_space_or_tab(b[right - 1])) {
right--;
}
return std::make_pair(left, right);
}
template <class Fn> void split(const char *b, const char *e, char d, Fn fn) {
int i = 0;
int beg = 0;
inline std::string trim_copy(const std::string &s) {
auto r = trim(s.data(), s.data() + s.size(), 0, s.size());
return s.substr(r.first, r.second - r.first);
}
while (e ? (b + i != e) : (b[i] != '\0')) {
template <class Fn> void split(const char *b, const char *e, char d, Fn fn) {
size_t i = 0;
size_t beg = 0;
while (e ? (b + i < e) : (b[i] != '\0')) {
if (b[i] == d) {
auto r = trim(b, e, beg, i);
fn(&b[r.first], &b[r.second]);
if (r.first < r.second) { fn(&b[r.first], &b[r.second]); }
beg = i + 1;
}
i++;
@@ -1464,7 +1486,7 @@ template <class Fn> void split(const char *b, const char *e, char d, Fn fn) {
if (i) {
auto r = trim(b, e, beg, i);
fn(&b[r.first], &b[r.second]);
if (r.first < r.second) { fn(&b[r.first], &b[r.second]); }
}
}
@@ -1717,7 +1739,7 @@ private:
size_t position = 0;
};
inline bool keep_alive(socket_t sock) {
inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) {
using namespace std::chrono;
auto start = steady_clock::now();
while (true) {
@@ -1727,8 +1749,7 @@ inline bool keep_alive(socket_t sock) {
} else if (val == 0) {
auto current = steady_clock::now();
auto duration = duration_cast<milliseconds>(current - start);
auto timeout = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND * 1000 +
CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND / 1000;
auto timeout = keep_alive_timeout_sec * 1000;
if (duration.count() > timeout) { return false; }
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} else {
@@ -1738,13 +1759,13 @@ inline bool keep_alive(socket_t sock) {
}
template <typename T>
inline bool process_server_socket_core(socket_t sock,
size_t keep_alive_max_count,
T callback) {
inline bool
process_server_socket_core(socket_t sock, size_t keep_alive_max_count,
time_t keep_alive_timeout_sec, T callback) {
assert(keep_alive_max_count > 0);
auto ret = false;
auto count = keep_alive_max_count;
while (count > 0 && keep_alive(sock)) {
while (count > 0 && keep_alive(sock, keep_alive_timeout_sec)) {
auto close_connection = count == 1;
auto connection_closed = false;
ret = callback(close_connection, connection_closed);
@@ -1755,14 +1776,14 @@ inline bool process_server_socket_core(socket_t sock,
}
template <typename T>
inline bool process_server_socket(socket_t sock, size_t keep_alive_max_count,
time_t read_timeout_sec,
time_t read_timeout_usec,
time_t write_timeout_sec,
time_t write_timeout_usec, T callback) {
inline bool
process_server_socket(socket_t sock, size_t keep_alive_max_count,
time_t keep_alive_timeout_sec, time_t read_timeout_sec,
time_t read_timeout_usec, time_t write_timeout_sec,
time_t write_timeout_usec, T callback) {
return process_server_socket_core(
sock, keep_alive_max_count,
[&](bool close_connection, bool connection_closed) {
sock, keep_alive_max_count, keep_alive_timeout_sec,
[&](bool close_connection, bool &connection_closed) {
SocketStream strm(sock, read_timeout_sec, read_timeout_usec,
write_timeout_sec, write_timeout_usec);
return callback(strm, close_connection, connection_closed);
@@ -1911,7 +1932,11 @@ inline bool bind_ip_address(socket_t sock, const char *host) {
return ret;
}
#ifndef _WIN32
#if !defined _WIN32 && !defined ANDROID
#define USE_IF2IP
#endif
#ifdef USE_IF2IP
inline std::string if2ip(const std::string &ifn) {
struct ifaddrs *ifap;
getifaddrs(&ifap);
@@ -1941,7 +1966,7 @@ inline socket_t create_client_socket(const char *host, int port,
host, port, 0, tcp_nodelay, socket_options,
[&](socket_t sock, struct addrinfo &ai) -> bool {
if (!intf.empty()) {
#ifndef _WIN32
#ifdef USE_IF2IP
auto ip = if2ip(intf);
if (ip.empty()) { ip = intf; }
if (!bind_ip_address(sock, ip.c_str())) {
@@ -1971,7 +1996,7 @@ inline socket_t create_client_socket(const char *host, int port,
});
if (sock != INVALID_SOCKET) {
if (error != Error::Success) { error = Error::Success; }
error = Error::Success;
} else {
if (error == Error::Success) { error = Error::Connection; }
}
@@ -2208,7 +2233,7 @@ public:
int ret = Z_OK;
std::array<char, 16384> buff{};
std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
do {
strm_.avail_out = buff.size();
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
@@ -2259,8 +2284,8 @@ public:
strm_.avail_in = static_cast<decltype(strm_.avail_in)>(data_length);
strm_.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data));
std::array<char, 16384> buff{};
do {
std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
while (strm_.avail_in > 0) {
strm_.avail_out = buff.size();
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
@@ -2275,7 +2300,7 @@ public:
if (!callback(buff.data(), buff.size() - strm_.avail_out)) {
return false;
}
} while (strm_.avail_out == 0);
}
return ret == Z_OK || ret == Z_STREAM_END;
}
@@ -2297,7 +2322,7 @@ public:
bool compress(const char *data, size_t data_length, bool last,
Callback callback) override {
std::array<uint8_t, 16384> buff{};
std::array<uint8_t, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS;
auto available_in = data_length;
@@ -2359,20 +2384,18 @@ public:
decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
char output[1024];
char *next_out = output;
size_t avail_out = sizeof(output);
char *next_out = buff.data();
size_t avail_out = buff.size();
decoder_r = BrotliDecoderDecompressStream(
decoder_s, &avail_in, &next_in, &avail_out,
reinterpret_cast<unsigned char **>(&next_out), &total_out);
reinterpret_cast<uint8_t **>(&next_out), &total_out);
if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; }
if (!callback((const char *)output, sizeof(output) - avail_out)) {
return false;
}
if (!callback(buff.data(), buff.size() - avail_out)) { return false; }
}
return decoder_r == BROTLI_DECODER_RESULT_SUCCESS ||
@@ -2415,26 +2438,34 @@ inline uint64_t get_header_value<uint64_t>(const Headers &headers,
return def;
}
inline void parse_header(const char *beg, const char *end, Headers &headers) {
template <typename T>
inline bool parse_header(const char *beg, const char *end, T fn) {
// Skip trailing spaces and tabs.
while (beg < end && is_space_or_tab(end[-1])) {
end--;
}
auto p = beg;
while (p < end && *p != ':') {
p++;
}
if (p < end) {
auto key_end = p;
p++; // skip ':'
while (p < end && (*p == ' ' || *p == '\t')) {
p++;
}
if (p < end) {
auto val_begin = p;
while (p < end) {
p++;
}
headers.emplace(std::string(beg, key_end),
decode_url(std::string(val_begin, end), true));
}
if (p == end) { return false; }
auto key_end = p;
if (*p++ != ':') { return false; }
while (p < end && is_space_or_tab(*p)) {
p++;
}
if (p < end) {
fn(std::string(beg, key_end), decode_url(std::string(p, end), false));
return true;
}
return false;
}
inline bool read_headers(Stream &strm, Headers &headers) {
@@ -2453,13 +2484,13 @@ inline bool read_headers(Stream &strm, Headers &headers) {
continue; // Skip invalid line.
}
// Skip trailing spaces and tabs.
// Exclude CRLF
auto end = line_reader.ptr() + line_reader.size() - 2;
while (line_reader.ptr() < end && (end[-1] == ' ' || end[-1] == '\t')) {
end--;
}
parse_header(line_reader.ptr(), end, headers);
parse_header(line_reader.ptr(), end,
[&](std::string &&key, std::string &&val) {
headers.emplace(std::move(key), std::move(val));
});
}
return true;
@@ -2607,7 +2638,7 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
Progress progress, ContentReceiver receiver,
bool decompress) {
return prepare_content_receiver(
x, status, receiver, decompress, [&](ContentReceiver &out) {
x, status, receiver, decompress, [&](const ContentReceiver &out) {
auto ret = true;
auto exceed_payload_max_length = false;
@@ -2830,12 +2861,11 @@ inline std::string params_to_query_str(const Params &params) {
query += "=";
query += encode_url(it->second);
}
return query;
}
inline void parse_query_text(const std::string &s, Params &params) {
split(&s[0], &s[s.size()], '&', [&](const char *b, const char *e) {
split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) {
std::string key;
std::string val;
split(b, e, '=', [&](const char *b2, const char *e2) {
@@ -2845,7 +2875,10 @@ inline void parse_query_text(const std::string &s, Params &params) {
val.assign(b2, e2);
}
});
params.emplace(decode_url(key, true), decode_url(val, true));
if (!key.empty()) {
params.emplace(decode_url(key, true), decode_url(val, true));
}
});
}
@@ -2905,8 +2938,6 @@ public:
template <typename T, typename U>
bool parse(const char *buf, size_t n, T content_callback, U header_callback) {
static const std::regex re_content_type(R"(^Content-Type:\s*(.*?)\s*$)",
std::regex_constants::icase);
static const std::regex re_content_disposition(
"^Content-Disposition:\\s*form-data;\\s*name=\"(.*?)\"(?:;\\s*filename="
@@ -2949,12 +2980,13 @@ public:
break;
}
auto header = buf_.substr(0, pos);
{
static const std::string header_name = "content-type:";
const auto header = buf_.substr(0, pos);
if (start_with(header, header_name)) {
file_.content_type = trim_copy(header.substr(header_name.size()));
} else {
std::smatch m;
if (std::regex_match(header, m, re_content_type)) {
file_.content_type = m[1];
} else if (std::regex_match(header, m, re_content_disposition)) {
if (std::regex_match(header, m, re_content_disposition)) {
file_.name = m[1];
file_.filename = m[2];
}
@@ -3019,14 +3051,14 @@ public:
}
case 4: { // Boundary
if (crlf_.size() > buf_.size()) { return true; }
if (buf_.find(crlf_) == 0) {
if (buf_.compare(0, crlf_.size(), crlf_) == 0) {
buf_.erase(0, crlf_.size());
off_ += crlf_.size();
state_ = 1;
} else {
auto pattern = dash_ + crlf_;
if (pattern.size() > buf_.size()) { return true; }
if (buf_.find(pattern) == 0) {
if (buf_.compare(0, pattern.size(), pattern) == 0) {
buf_.erase(0, pattern.size());
off_ += pattern.size();
is_valid_ = true;
@@ -3314,39 +3346,6 @@ public:
static WSInit wsinit_;
#endif
} // namespace detail
// Header utilities
inline std::pair<std::string, std::string> make_range_header(Ranges ranges) {
std::string field = "bytes=";
auto i = 0;
for (auto r : ranges) {
if (i != 0) { field += ", "; }
if (r.first != -1) { field += std::to_string(r.first); }
field += '-';
if (r.second != -1) { field += std::to_string(r.second); }
i++;
}
return std::make_pair("Range", field);
}
inline std::pair<std::string, std::string>
make_basic_authentication_header(const std::string &username,
const std::string &password,
bool is_proxy = false) {
auto field = "Basic " + detail::base64_encode(username + ":" + password);
auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
return std::make_pair(key, field);
}
inline std::pair<std::string, std::string>
make_bearer_token_authentication_header(const std::string &token,
bool is_proxy = false) {
auto field = "Bearer " + token;
auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
return std::make_pair(key, field);
}
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
inline std::pair<std::string, std::string> make_digest_authentication_header(
const Request &req, const std::map<std::string, std::string> &auth,
@@ -3444,6 +3443,53 @@ inline std::string random_string(size_t length) {
return str;
}
class ContentProviderAdapter {
public:
explicit ContentProviderAdapter(
ContentProviderWithoutLength &&content_provider)
: content_provider_(content_provider) {}
bool operator()(size_t offset, size_t, DataSink &sink) {
return content_provider_(offset, sink);
}
private:
ContentProviderWithoutLength content_provider_;
};
} // namespace detail
// Header utilities
inline std::pair<std::string, std::string> make_range_header(Ranges ranges) {
std::string field = "bytes=";
auto i = 0;
for (auto r : ranges) {
if (i != 0) { field += ", "; }
if (r.first != -1) { field += std::to_string(r.first); }
field += '-';
if (r.second != -1) { field += std::to_string(r.second); }
i++;
}
return std::make_pair("Range", field);
}
inline std::pair<std::string, std::string>
make_basic_authentication_header(const std::string &username,
const std::string &password,
bool is_proxy = false) {
auto field = "Basic " + detail::base64_encode(username + ":" + password);
auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
return std::make_pair(key, field);
}
inline std::pair<std::string, std::string>
make_bearer_token_authentication_header(const std::string &token,
bool is_proxy = false) {
auto field = "Bearer " + token;
auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
return std::make_pair(key, field);
}
// Request implementation
inline bool Request::has_header(const char *key) const {
return detail::has_header(headers, key);
@@ -3568,37 +3614,32 @@ inline void Response::set_content(std::string s, const char *content_type) {
inline void
Response::set_content_provider(size_t in_length, const char *content_type,
ContentProvider provider,
std::function<void()> resource_releaser) {
const std::function<void()> &resource_releaser) {
assert(in_length > 0);
set_header("Content-Type", content_type);
content_length_ = in_length;
content_provider_ = [provider](size_t offset, size_t length, DataSink &sink) {
return provider(offset, length, sink);
};
content_provider_ = std::move(provider);
content_provider_resource_releaser_ = resource_releaser;
is_chunked_content_provider = false;
}
inline void Response::set_content_provider(
const char *content_type, ContentProviderWithoutLength provider,
std::function<void()> resource_releaser) {
inline void
Response::set_content_provider(const char *content_type,
ContentProviderWithoutLength provider,
const std::function<void()> &resource_releaser) {
set_header("Content-Type", content_type);
content_length_ = 0;
content_provider_ = [provider](size_t offset, size_t, DataSink &sink) {
return provider(offset, sink);
};
content_provider_ = detail::ContentProviderAdapter(std::move(provider));
content_provider_resource_releaser_ = resource_releaser;
is_chunked_content_provider = false;
}
inline void Response::set_chunked_content_provider(
const char *content_type, ContentProviderWithoutLength provider,
std::function<void()> resource_releaser) {
const std::function<void()> &resource_releaser) {
set_header("Content-Type", content_type);
content_length_ = 0;
content_provider_ = [provider](size_t offset, size_t, DataSink &sink) {
return provider(offset, sink);
};
content_provider_ = detail::ContentProviderAdapter(std::move(provider));
content_provider_resource_releaser_ = resource_releaser;
is_chunked_content_provider = true;
}
@@ -3727,11 +3768,13 @@ inline const std::string &BufferStream::get_buffer() const { return buffer; }
} // namespace detail
// HTTP server implementation
inline Server::Server() : svr_sock_(INVALID_SOCKET), is_running_(false) {
inline Server::Server()
: new_task_queue(
[] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }),
svr_sock_(INVALID_SOCKET), is_running_(false) {
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
#endif
new_task_queue = [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); };
}
inline Server::~Server() {}
@@ -3849,6 +3892,10 @@ inline void Server::set_keep_alive_max_count(size_t count) {
keep_alive_max_count_ = count;
}
inline void Server::set_keep_alive_timeout(time_t sec) {
keep_alive_timeout_sec_ = sec;
}
inline void Server::set_read_timeout(time_t sec, time_t usec) {
read_timeout_sec_ = sec;
read_timeout_usec_ = usec;
@@ -3932,10 +3979,11 @@ inline bool Server::write_response(Stream &strm, bool close_connection,
// Headers
if (close_connection || req.get_header_value("Connection") == "close") {
res.set_header("Connection", "close");
}
if (!close_connection && req.get_header_value("Connection") == "Keep-Alive") {
res.set_header("Connection", "Keep-Alive");
} else {
std::stringstream ss;
ss << "timeout=" << keep_alive_timeout_sec_
<< ", max=" << keep_alive_max_count_;
res.set_header("Keep-Alive", ss.str());
}
if (!res.has_header("Content-Type") &&
@@ -4233,7 +4281,7 @@ inline bool Server::handle_file_request(Request &req, Response &res,
const auto &base_dir = kv.second;
// Prefix match
if (!req.path.find(mount_point)) {
if (!req.path.compare(0, mount_point.size(), mount_point)) {
std::string sub_path = "/" + req.path.substr(mount_point.size());
if (detail::is_valid_path(sub_path)) {
auto path = base_dir + sub_path;
@@ -4417,7 +4465,7 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) {
}
inline bool Server::dispatch_request(Request &req, Response &res,
Handlers &handlers) {
const Handlers &handlers) {
try {
for (const auto &x : handlers) {
@@ -4441,7 +4489,7 @@ inline bool Server::dispatch_request(Request &req, Response &res,
inline bool Server::dispatch_request_for_content_reader(
Request &req, Response &res, ContentReader content_reader,
HandlersForContentReader &handlers) {
const HandlersForContentReader &handlers) {
for (const auto &x : handlers) {
const auto &pattern = x.first;
const auto &handler = x.second;
@@ -4536,8 +4584,8 @@ inline bool Server::is_valid() const { return true; }
inline bool Server::process_and_close_socket(socket_t sock) {
auto ret = detail::process_server_socket(
sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_,
write_timeout_sec_, write_timeout_usec_,
sock, keep_alive_max_count_, keep_alive_timeout_sec_, read_timeout_sec_,
read_timeout_usec_, write_timeout_sec_, write_timeout_usec_,
[this](Stream &strm, bool close_connection, bool &connection_closed) {
return process_request(strm, close_connection, connection_closed,
nullptr);
@@ -4569,7 +4617,7 @@ inline bool ClientImpl::is_valid() const { return true; }
inline Error ClientImpl::get_last_error() const { return error_; }
inline socket_t ClientImpl::create_client_socket() const {
if (!proxy_host_.empty()) {
if (!proxy_host_.empty() && proxy_port_ != -1) {
return detail::create_client_socket(
proxy_host_.c_str(), proxy_port_, tcp_nodelay_, socket_options_,
connection_timeout_sec_, connection_timeout_usec_, interface_, error_);
@@ -4602,12 +4650,23 @@ inline bool ClientImpl::read_response_line(Stream &strm, Response &res) {
if (!line_reader.getline()) { return false; }
const static std::regex re("(HTTP/1\\.[01]) (\\d+).*?\r\n");
const static std::regex re("(HTTP/1\\.[01]) (\\d+) (.*?)\r\n");
std::cmatch m;
if (std::regex_match(line_reader.ptr(), m, re)) {
if (!std::regex_match(line_reader.ptr(), m, re)) { return false; }
res.version = std::string(m[1]);
res.status = std::stoi(std::string(m[2]));
res.reason = std::string(m[3]);
// Ignore '100 Continue'
while (res.status == 100) {
if (!line_reader.getline()) { return false; } // CRLF
if (!line_reader.getline()) { return false; } // next response line
if (!std::regex_match(line_reader.ptr(), m, re)) { return false; }
res.version = std::string(m[1]);
res.status = std::stoi(std::string(m[2]));
res.reason = std::string(m[3]);
}
return true;
@@ -4632,7 +4691,7 @@ inline bool ClientImpl::send(const Request &req, Response &res) {
// TODO: refactoring
if (is_ssl()) {
auto &scli = static_cast<SSLClient &>(*this);
if (!proxy_host_.empty()) {
if (!proxy_host_.empty() && proxy_port_ != -1) {
bool success = false;
if (!scli.connect_with_proxy(socket_, res, success)) {
return success;
@@ -4669,7 +4728,7 @@ inline bool ClientImpl::handle_request(Stream &strm, const Request &req,
bool ret;
if (!is_ssl() && !proxy_host_.empty()) {
if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) {
auto req2 = req;
req2.path = "http://" + host_and_port_ + req.path;
ret = process_request(strm, req2, res, close_connection);
@@ -4694,13 +4753,13 @@ inline bool ClientImpl::handle_request(Stream &strm, const Request &req,
if (!username.empty() && !password.empty()) {
std::map<std::string, std::string> auth;
if (parse_www_authenticate(res, auth, is_proxy)) {
if (detail::parse_www_authenticate(res, auth, is_proxy)) {
Request new_req = req;
new_req.authorization_count_ += 1;
auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
new_req.headers.erase(key);
new_req.headers.insert(make_digest_authentication_header(
req, auth, new_req.authorization_count_, random_string(10),
new_req.headers.insert(detail::make_digest_authentication_header(
req, auth, new_req.authorization_count_, detail::random_string(10),
username, password, is_proxy));
Response new_res;
@@ -4721,7 +4780,7 @@ inline bool ClientImpl::redirect(const Request &req, Response &res) {
return false;
}
auto location = res.get_header_value("location");
auto location = detail::decode_url(res.get_header_value("location"), true);
if (location.empty()) { return false; }
const static std::regex re(
@@ -4824,7 +4883,7 @@ inline bool ClientImpl::write_request(Stream &strm, const Request &req,
}
}
if (!basic_auth_username_.empty() && !basic_auth_password_.empty()) {
if (!basic_auth_password_.empty()) {
headers.insert(make_basic_authentication_header(
basic_auth_username_, basic_auth_password_, false));
}
@@ -5016,7 +5075,7 @@ inline bool ClientImpl::process_request(Stream &strm, const Request &req,
}
if (res.get_header_value("Connection") == "close" ||
res.version == "HTTP/1.0") {
(res.version == "HTTP/1.0" && res.reason != "Connection established")) {
stop_core();
}
@@ -5058,7 +5117,8 @@ inline Result ClientImpl::Get(const char *path, const Headers &headers,
req.progress = std::move(progress);
auto res = std::make_shared<Response>();
return Result{send(req, *res) ? res : nullptr, get_last_error()};
auto ret = send(req, *res);
return Result{ret ? res : nullptr, get_last_error()};
}
inline Result ClientImpl::Get(const char *path,
@@ -5121,7 +5181,8 @@ inline Result ClientImpl::Get(const char *path, const Headers &headers,
req.progress = std::move(progress);
auto res = std::make_shared<Response>();
return Result{send(req, *res) ? res : nullptr, get_last_error()};
auto ret = send(req, *res);
return Result{ret ? res : nullptr, get_last_error()};
}
inline Result ClientImpl::Head(const char *path) {
@@ -5136,7 +5197,8 @@ inline Result ClientImpl::Head(const char *path, const Headers &headers) {
req.path = path;
auto res = std::make_shared<Response>();
return Result{send(req, *res) ? res : nullptr, get_last_error()};
auto ret = send(req, *res);
return Result{ret ? res : nullptr, get_last_error()};
}
inline Result ClientImpl::Post(const char *path) {
@@ -5151,9 +5213,9 @@ inline Result ClientImpl::Post(const char *path, const std::string &body,
inline Result ClientImpl::Post(const char *path, const Headers &headers,
const std::string &body,
const char *content_type) {
return Result{send_with_content_provider("POST", path, headers, body, 0,
nullptr, content_type),
get_last_error()};
auto ret = send_with_content_provider("POST", path, headers, body, 0, nullptr,
content_type);
return Result{ret, get_last_error()};
}
inline Result ClientImpl::Post(const char *path, const Params &params) {
@@ -5170,10 +5232,10 @@ inline Result ClientImpl::Post(const char *path, const Headers &headers,
size_t content_length,
ContentProvider content_provider,
const char *content_type) {
return Result{send_with_content_provider("POST", path, headers, std::string(),
content_length, content_provider,
content_type),
get_last_error()};
auto ret = send_with_content_provider("POST", path, headers, std::string(),
content_length, content_provider,
content_type);
return Result{ret, get_last_error()};
}
inline Result ClientImpl::Post(const char *path, const Headers &headers,
@@ -5225,9 +5287,9 @@ inline Result ClientImpl::Put(const char *path, const std::string &body,
inline Result ClientImpl::Put(const char *path, const Headers &headers,
const std::string &body,
const char *content_type) {
return Result{send_with_content_provider("PUT", path, headers, body, 0,
nullptr, content_type),
get_last_error()};
auto ret = send_with_content_provider("PUT", path, headers, body, 0, nullptr,
content_type);
return Result{ret, get_last_error()};
}
inline Result ClientImpl::Put(const char *path, size_t content_length,
@@ -5240,10 +5302,10 @@ inline Result ClientImpl::Put(const char *path, const Headers &headers,
size_t content_length,
ContentProvider content_provider,
const char *content_type) {
return Result{send_with_content_provider("PUT", path, headers, std::string(),
content_length, content_provider,
content_type),
get_last_error()};
auto ret = send_with_content_provider("PUT", path, headers, std::string(),
content_length, content_provider,
content_type);
return Result{ret, get_last_error()};
}
inline Result ClientImpl::Put(const char *path, const Params &params) {
@@ -5264,9 +5326,9 @@ inline Result ClientImpl::Patch(const char *path, const std::string &body,
inline Result ClientImpl::Patch(const char *path, const Headers &headers,
const std::string &body,
const char *content_type) {
return Result{send_with_content_provider("PATCH", path, headers, body, 0,
nullptr, content_type),
get_last_error()};
auto ret = send_with_content_provider("PATCH", path, headers, body, 0,
nullptr, content_type);
return Result{ret, get_last_error()};
}
inline Result ClientImpl::Patch(const char *path, size_t content_length,
@@ -5279,10 +5341,10 @@ inline Result ClientImpl::Patch(const char *path, const Headers &headers,
size_t content_length,
ContentProvider content_provider,
const char *content_type) {
return Result{send_with_content_provider("PATCH", path, headers,
std::string(), content_length,
content_provider, content_type),
get_last_error()};
auto ret = send_with_content_provider("PATCH", path, headers, std::string(),
content_length, content_provider,
content_type);
return Result{ret, get_last_error()};
}
inline Result ClientImpl::Delete(const char *path) {
@@ -5311,7 +5373,8 @@ inline Result ClientImpl::Delete(const char *path, const Headers &headers,
req.body = body;
auto res = std::make_shared<Response>();
return Result{send(req, *res) ? res : nullptr, get_last_error()};
auto ret = send(req, *res);
return Result{ret ? res : nullptr, get_last_error()};
}
inline Result ClientImpl::Options(const char *path) {
@@ -5326,7 +5389,8 @@ inline Result ClientImpl::Options(const char *path, const Headers &headers) {
req.path = path;
auto res = std::make_shared<Response>();
return Result{send(req, *res) ? res : nullptr, get_last_error()};
auto ret = send(req, *res);
return Result{ret ? res : nullptr, get_last_error()};
}
inline size_t ClientImpl::is_socket_open() const {
@@ -5474,12 +5538,13 @@ inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl,
template <typename T>
inline bool
process_server_socket_ssl(SSL *ssl, socket_t sock, size_t keep_alive_max_count,
time_t keep_alive_timeout_sec,
time_t read_timeout_sec, time_t read_timeout_usec,
time_t write_timeout_sec, time_t write_timeout_usec,
T callback) {
return process_server_socket_core(
sock, keep_alive_max_count,
[&](bool close_connection, bool connection_closed) {
sock, keep_alive_max_count, keep_alive_timeout_sec,
[&](bool close_connection, bool &connection_closed) {
SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec,
write_timeout_sec, write_timeout_usec);
return callback(strm, close_connection, connection_closed);
@@ -5685,8 +5750,9 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
if (ssl) {
auto ret = detail::process_server_socket_ssl(
ssl, sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_,
write_timeout_sec_, write_timeout_usec_,
ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_,
read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
write_timeout_usec_,
[this, ssl](Stream &strm, bool close_connection,
bool &connection_closed) {
return process_request(strm, close_connection, connection_closed,
@@ -5799,7 +5865,7 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res,
if (!proxy_digest_auth_username_.empty() &&
!proxy_digest_auth_password_.empty()) {
std::map<std::string, std::string> auth;
if (parse_www_authenticate(res2, auth, true)) {
if (detail::parse_www_authenticate(res2, auth, true)) {
Response res3;
if (!detail::process_client_socket(
socket.sock, read_timeout_sec_, read_timeout_usec_,
@@ -5807,8 +5873,8 @@ inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res,
Request req3;
req3.method = "CONNECT";
req3.path = host_and_port_;
req3.headers.insert(make_digest_authentication_header(
req3, auth, 1, random_string(10),
req3.headers.insert(detail::make_digest_authentication_header(
req3, auth, 1, detail::random_string(10),
proxy_digest_auth_username_, proxy_digest_auth_password_,
true));
return process_request(strm, req3, res3, false);
+121 -14
View File
@@ -44,6 +44,12 @@ TEST(StartupTest, WSAStartup) {
}
#endif
TEST(TrimTests, TrimStringTests) {
EXPECT_EQ("abc", detail::trim_copy("abc"));
EXPECT_EQ("abc", detail::trim_copy(" abc "));
EXPECT_TRUE(detail::trim_copy("").empty());
}
TEST(SplitTest, ParseQueryString) {
string s = "key1=val1&key2=val2&key3=val3";
Params dic;
@@ -66,6 +72,23 @@ TEST(SplitTest, ParseQueryString) {
EXPECT_EQ("val3", dic.find("key3")->second);
}
TEST(SplitTest, ParseInvalidQueryTests) {
{
string s = " ";
Params dict;
detail::parse_query_text(s, dict);
EXPECT_TRUE(dict.empty());
}
{
string s = " = =";
Params dict;
detail::parse_query_text(s, dict);
EXPECT_TRUE(dict.empty());
}
}
TEST(ParseQueryTest, ParseQueryString) {
string s = "key1=val1&key2=val2&key3=val3";
Params dic;
@@ -1064,7 +1087,7 @@ protected:
})
.Post("/multipart",
[&](const Request &req, Response & /*res*/) {
EXPECT_EQ(5u, req.files.size());
EXPECT_EQ(6u, req.files.size());
ASSERT_TRUE(!req.has_file("???"));
ASSERT_TRUE(req.body.empty());
@@ -1093,6 +1116,13 @@ protected:
EXPECT_EQ("application/octet-stream", file.content_type);
EXPECT_EQ(0u, file.content.size());
}
{
const auto &file = req.get_file_value("file4");
EXPECT_TRUE(file.filename.empty());
EXPECT_EQ(0u, file.content.size());
EXPECT_EQ("application/json tmp-string", file.content_type);
}
})
.Post("/empty",
[&](const Request &req, Response &res) {
@@ -1303,8 +1333,11 @@ protected:
virtual void TearDown() {
svr_.stop();
for (auto &t : request_threads_) {
t.join();
if (!request_threads_.empty()) {
std::this_thread::sleep_for(std::chrono::seconds(1));
for (auto &t : request_threads_) {
t.join();
}
}
t_.join();
}
@@ -1785,7 +1818,7 @@ TEST_F(ServerTest, MultipartFormData) {
{"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
{"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
{"file3", "", "", "application/octet-stream"},
};
{"file4", "", "", " application/json tmp-string "}};
auto res = cli_.Post("/multipart", items);
@@ -2021,7 +2054,6 @@ TEST_F(ServerTest, SlowRequest) {
std::thread([=]() { auto res = cli_.Get("/slow"); }));
request_threads_.push_back(
std::thread([=]() { auto res = cli_.Get("/slow"); }));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
TEST_F(ServerTest, SlowPost) {
@@ -2157,6 +2189,47 @@ TEST_F(ServerTest, GetStreamedChunkedWithGzip2) {
EXPECT_EQ(200, res->status);
EXPECT_EQ(std::string("123456789"), res->body);
}
TEST(GzipDecompressor, ChunkedDecompression) {
std::string data;
for (size_t i = 0; i < 32 * 1024; ++i) {
data.push_back(static_cast<char>('a' + i % 26));
}
std::string compressed_data;
{
httplib::detail::gzip_compressor compressor;
bool result = compressor.compress(
data.data(), data.size(),
/*last=*/true, [&](const char *data, size_t size) {
compressed_data.insert(compressed_data.size(), data, size);
return true;
});
ASSERT_TRUE(result);
}
std::string decompressed_data;
{
httplib::detail::gzip_decompressor decompressor;
// Chunk size is chosen specificaly to have a decompressed chunk size equal
// to 16384 bytes 16384 bytes is the size of decompressor output buffer
size_t chunk_size = 130;
for (size_t chunk_begin = 0; chunk_begin < compressed_data.size();
chunk_begin += chunk_size) {
size_t current_chunk_size =
std::min(compressed_data.size() - chunk_begin, chunk_size);
bool result = decompressor.decompress(
compressed_data.data() + chunk_begin, current_chunk_size,
[&](const char *data, size_t size) {
decompressed_data.insert(decompressed_data.size(), data, size);
return true;
});
ASSERT_TRUE(result);
}
}
ASSERT_EQ(data, decompressed_data);
}
#endif
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
@@ -2252,6 +2325,41 @@ TEST_F(ServerTest, PostMulitpartFilsContentReceiver) {
EXPECT_EQ(200, res->status);
}
TEST_F(ServerTest, PostMulitpartPlusBoundary) {
MultipartFormDataItems items = {
{"text1", "text default", "", ""},
{"text2", "aωb", "", ""},
{"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
{"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
{"file3", "", "", "application/octet-stream"},
};
auto boundary = std::string("+++++");
std::string body;
for (const auto &item : items) {
body += "--" + boundary + "\r\n";
body += "Content-Disposition: form-data; name=\"" + item.name + "\"";
if (!item.filename.empty()) {
body += "; filename=\"" + item.filename + "\"";
}
body += "\r\n";
if (!item.content_type.empty()) {
body += "Content-Type: " + item.content_type + "\r\n";
}
body += "\r\n";
body += item.content + "\r\n";
}
body += "--" + boundary + "--\r\n";
std::string content_type = "multipart/form-data; boundary=" + boundary;
auto res = cli_.Post("/content_receiver", body, content_type.c_str());
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
}
TEST_F(ServerTest, PostContentReceiverGzip) {
cli_.set_compress(true);
auto res = cli_.Post("/content_receiver", "content", "text/plain");
@@ -2733,15 +2841,14 @@ TEST(StreamingTest, NoContentLengthStreaming) {
Server svr;
svr.Get("/stream", [](const Request & /*req*/, Response &res) {
res.set_content_provider(
"text/plain", [](size_t offset, DataSink &sink) {
if (offset < 6) {
sink.os << (offset < 3 ? "a" : "b");
} else {
sink.done();
}
return true;
});
res.set_content_provider("text/plain", [](size_t offset, DataSink &sink) {
if (offset < 6) {
sink.os << (offset < 3 ? "a" : "b");
} else {
sink.done();
}
return true;
});
});
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });