Compare commits

..

13 Commits

Author SHA1 Message Date
yhirose faa5f1d802 Additional changes for #889 2021-04-05 16:13:41 -04:00
yhirose 9d3365df54 Fix #889 2021-04-05 11:40:53 -04:00
yhirose 6ff84d34d1 Another simpler implementation of #890 (#891) 2021-04-02 18:25:04 -04:00
yhirose b845425cd0 Fix #878 2021-03-16 19:42:44 -04:00
yhirose 89519c88e2 Fix #874 2021-03-10 15:57:56 -05:00
yhirose ff813bf99d Fix #863 2021-02-17 15:36:56 -05:00
yhirose cf475bcb50 Fix #860 2021-02-12 12:21:43 -05:00
yhirose bc80d7c789 Fixed ClientStop test problem 2021-02-06 20:12:30 -05:00
yhirose b7566f6961 Resolve #852 2021-02-02 22:09:35 -05:00
Nikolas 0542fdb8e4 Add exception handler (#845)
* Add exception handler

* revert content reader changes

* Add test for and fix exception handler

* Fix warning in test

* Readd exception test, improve readme note, don't rethrow errors, remove exception handler response
2021-01-28 17:19:11 -05:00
yhirose 78c474c744 Update README 2021-01-27 11:59:42 -05:00
yhirose 88411a1f52 Fix #846 2021-01-27 14:35:32 +00:00
yhirose ae6cf70bc4 Updated README 2021-01-26 08:38:28 -05:00
3 changed files with 575 additions and 200 deletions
+16 -3
View File
@@ -7,7 +7,7 @@ A C++11 single-file header-only cross platform HTTP/HTTPS library.
It's extremely easy to setup. Just include the **httplib.h** file in your code!
NOTE: This is a 'blocking' HTTP library. If you are looking for a 'non-blocking' library, this is not the one that you want.
NOTE: This is a multi-threaded 'blocking' HTTP library. If you are looking for a 'non-blocking' library, this is not the one that you want.
Simple examples
---------------
@@ -177,15 +177,28 @@ svr.set_error_handler([](const auto& req, auto& res) {
});
```
### Exception handler
The exception handler gets called if a user routing handler throws an error.
```cpp
svr.set_exception_handler([](const auto& req, auto& res, std::exception &e) {
res.status = 500;
auto fmt = "<h1>Error 500</h1><p>%s</p>";
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), fmt, e.what());
res.set_content(buf, "text/html");
});
```
### Pre routing handler
```cpp
svr.set_pre_routing_handler([](const auto& req, auto& res) -> bool {
if (req.path == "/hello") {
res.set_content("world", "text/html");
return true; // This request is handled
return Server::HandlerResponse::Handled;
}
return false; // Let the router handle this request
return Server::HandlerResponse::Unhandled;
});
```
+289 -108
View File
@@ -204,6 +204,7 @@ using socket_t = int;
#include <string>
#include <sys/stat.h>
#include <thread>
#include <set>
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
#include <openssl/err.h>
@@ -390,6 +391,9 @@ struct Request {
Match matches;
// for client
ResponseHandler response_handler;
ContentReceiverWithProgress content_receiver;
Progress progress;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
const SSL *ssl;
#endif
@@ -413,12 +417,9 @@ struct Request {
// private members...
size_t redirect_count_ = CPPHTTPLIB_REDIRECT_MAX_COUNT;
ResponseHandler response_handler_;
ContentReceiverWithProgress content_receiver_;
size_t content_length_ = 0;
ContentProvider content_provider_;
bool is_chunked_content_provider_ = false;
Progress progress_;
size_t authorization_count_ = 0;
};
@@ -598,6 +599,9 @@ class Server {
public:
using Handler = std::function<void(const Request &, Response &)>;
using ExceptionHandler =
std::function<void(const Request &, Response &, std::exception &e)>;
enum class HandlerResponse {
Handled,
Unhandled,
@@ -652,6 +656,7 @@ public:
Server &set_error_handler(HandlerWithResponse handler);
Server &set_error_handler(Handler handler);
Server &set_exception_handler(ExceptionHandler handler);
Server &set_pre_routing_handler(HandlerWithResponse handler);
Server &set_post_routing_handler(Handler handler);
@@ -663,9 +668,18 @@ public:
Server &set_keep_alive_max_count(size_t count);
Server &set_keep_alive_timeout(time_t sec);
Server &set_read_timeout(time_t sec, time_t usec = 0);
template <class Rep, class Period>
Server &set_read_timeout(const std::chrono::duration<Rep, Period> &duration);
Server &set_write_timeout(time_t sec, time_t usec = 0);
template <class Rep, class Period>
Server &set_write_timeout(const std::chrono::duration<Rep, Period> &duration);
Server &set_idle_interval(time_t sec, time_t usec = 0);
template <class Rep, class Period>
Server &set_idle_interval(const std::chrono::duration<Rep, Period> &duration);
Server &set_payload_max_length(size_t length);
@@ -762,6 +776,7 @@ private:
HandlersForContentReader delete_handlers_for_content_reader_;
Handlers options_handlers_;
HandlerWithResponse error_handler_;
ExceptionHandler exception_handler_;
HandlerWithResponse pre_routing_handler_;
Handler post_routing_handler_;
Logger logger_;
@@ -789,8 +804,11 @@ enum Error {
class Result {
public:
Result(std::unique_ptr<Response> res, Error err)
: res_(std::move(res)), err_(err) {}
Result(std::unique_ptr<Response> &&res, Error err,
Headers &&request_headers = Headers{})
: res_(std::move(res)), err_(err),
request_headers_(std::move(request_headers)) {}
// Response
operator bool() const { return res_ != nullptr; }
bool operator==(std::nullptr_t) const { return res_ == nullptr; }
bool operator!=(std::nullptr_t) const { return res_ != nullptr; }
@@ -800,11 +818,21 @@ public:
Response &operator*() { return *res_; }
const Response *operator->() const { return res_.get(); }
Response *operator->() { return res_.get(); }
// Error
Error error() const { return err_; }
// Request Headers
bool has_request_header(const char *key) const;
std::string get_request_header_value(const char *key, size_t id = 0) const;
template <typename T>
T get_request_header_value(const char *key, size_t id = 0) const;
size_t get_request_header_value_count(const char *key) const;
private:
std::unique_ptr<Response> res_;
Error err_;
Headers request_headers_;
};
class ClientImpl {
@@ -934,7 +962,7 @@ public:
Result Options(const char *path);
Result Options(const char *path, const Headers &headers);
bool send(const Request &req, Response &res, Error &error);
bool send(Request &req, Response &res, Error &error);
Result send(const Request &req);
size_t is_socket_open() const;
@@ -947,8 +975,16 @@ public:
void set_socket_options(SocketOptions socket_options);
void set_connection_timeout(time_t sec, time_t usec = 0);
template <class Rep, class Period>
void set_connection_timeout(const std::chrono::duration<Rep, Period> &duration);
void set_read_timeout(time_t sec, time_t usec = 0);
template <class Rep, class Period>
void set_read_timeout(const std::chrono::duration<Rep, Period> &duration);
void set_write_timeout(time_t sec, time_t usec = 0);
template <class Rep, class Period>
void set_write_timeout(const std::chrono::duration<Rep, Period> &duration);
void set_basic_auth(const char *username, const char *password);
void set_bearer_token_auth(const char *token);
@@ -988,6 +1024,8 @@ protected:
bool is_open() const { return sock != INVALID_SOCKET; }
};
Result send_(Request &&req);
virtual bool create_and_connect_socket(Socket &socket, Error &error);
// All of:
@@ -1005,7 +1043,7 @@ protected:
// concurrently with a DIFFERENT thread sending requests from the socket
void lock_socket_and_shutdown_and_close();
bool process_request(Stream &strm, const Request &req, Response &res,
bool process_request(Stream &strm, Request &req, Response &res,
bool close_connection, Error &error);
bool write_content_with_provider(Stream &strm, const Request &req,
@@ -1081,13 +1119,14 @@ protected:
private:
socket_t create_client_socket(Error &error) const;
bool read_response_line(Stream &strm, const Request &req, Response &res);
bool write_request(Stream &strm, const Request &req, bool close_connection,
bool write_request(Stream &strm, Request &req, bool close_connection,
Error &error);
bool redirect(const Request &req, Response &res, Error &error);
bool handle_request(Stream &strm, const Request &req, Response &res,
bool redirect(Request &req, Response &res, Error &error);
bool handle_request(Stream &strm, Request &req, Response &res,
bool close_connection, Error &error);
std::unique_ptr<Response> send_with_content_provider(
const char *method, const char *path, const Headers &headers,
Request &req,
// const char *method, const char *path, const Headers &headers,
const char *body, size_t content_length, ContentProvider content_provider,
ContentProviderWithoutLength content_provider_without_length,
const char *content_type, Error &error);
@@ -1233,7 +1272,7 @@ public:
Result Options(const char *path);
Result Options(const char *path, const Headers &headers);
bool send(const Request &req, Response &res, Error &error);
bool send(Request &req, Response &res, Error &error);
Result send(const Request &req);
size_t is_socket_open() const;
@@ -1246,8 +1285,16 @@ public:
void set_socket_options(SocketOptions socket_options);
void set_connection_timeout(time_t sec, time_t usec = 0);
template <class Rep, class Period>
void set_connection_timeout(const std::chrono::duration<Rep, Period> &duration);
void set_read_timeout(time_t sec, time_t usec = 0);
template <class Rep, class Period>
void set_read_timeout(const std::chrono::duration<Rep, Period> &duration);
void set_write_timeout(time_t sec, time_t usec = 0);
template <class Rep, class Period>
void set_write_timeout(const std::chrono::duration<Rep, Period> &duration);
void set_basic_auth(const char *username, const char *password);
void set_bearer_token_auth(const char *token);
@@ -2484,7 +2531,7 @@ public:
strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
ret = deflate(&strm_, flush);
assert(ret != Z_STREAM_ERROR);
if (ret == Z_STREAM_ERROR) { return false; }
if (!callback(buff.data(), buff.size() - strm_.avail_out)) {
return false;
@@ -2917,17 +2964,8 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
});
}
template <typename T>
inline ssize_t write_headers(Stream &strm, const T &info,
const Headers &headers) {
inline ssize_t write_headers(Stream &strm, const Headers &headers) {
ssize_t write_len = 0;
for (const auto &x : info.headers) {
if (x.first == "EXCEPTION_WHAT") { continue; }
auto len =
strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str());
if (len < 0) { return len; }
write_len += len;
}
for (const auto &x : headers) {
auto len =
strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str());
@@ -3114,7 +3152,7 @@ inline bool write_content_chunked(Stream &strm,
}
template <typename T>
inline bool redirect(T &cli, const Request &req, Response &res,
inline bool redirect(T &cli, Request &req, Response &res,
const std::string &path, const std::string &location,
Error &error) {
Request new_req = req;
@@ -3131,8 +3169,9 @@ inline bool redirect(T &cli, const Request &req, Response &res,
auto ret = cli.send(new_req, new_res, error);
if (ret) {
new_res.location = location;
req = new_req;
res = new_res;
res.location = location;
}
return ret;
}
@@ -3158,7 +3197,14 @@ inline std::string append_query_params(const char *path, const Params &params) {
}
inline void parse_query_text(const std::string &s, Params &params) {
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) {
@@ -3762,7 +3808,7 @@ inline std::string random_string(size_t length) {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[static_cast<size_t>(rand()) % max_index];
return charset[static_cast<size_t>(std::rand()) % max_index];
};
std::string str(length, 0);
std::generate_n(str.begin(), length, randchar);
@@ -3783,6 +3829,15 @@ private:
ContentProviderWithoutLength content_provider_;
};
template <typename T, typename U>
inline void duration_to_sec_and_usec(const T &duration, U callback) {
auto sec = std::chrono::duration_cast<std::chrono::seconds>(duration).count();
auto usec = std::chrono::duration_cast<std::chrono::microseconds>(
duration - std::chrono::seconds(sec))
.count();
callback(sec, usec);
}
} // namespace detail
// Header utilities
@@ -3973,7 +4028,27 @@ inline void Response::set_chunked_content_provider(
is_chunked_content_provider_ = true;
}
// Rstream implementation
// Result implementation
inline bool Result::has_request_header(const char *key) const {
return request_headers_.find(key) != request_headers_.end();
}
inline std::string Result::get_request_header_value(const char *key,
size_t id) const {
return detail::get_header_value(request_headers_, key, id, "");
}
template <typename T>
inline T Result::get_request_header_value(const char *key, size_t id) const {
return detail::get_header_value<T>(request_headers_, key, id, 0);
}
inline size_t Result::get_request_header_value_count(const char *key) const {
auto r = request_headers_.equal_range(key);
return static_cast<size_t>(std::distance(r.first, r.second));
}
// Stream implementation
inline ssize_t Stream::write(const char *ptr) {
return write(ptr, strlen(ptr));
}
@@ -4281,6 +4356,11 @@ inline Server &Server::set_error_handler(Handler handler) {
return *this;
}
inline Server &Server::set_exception_handler(ExceptionHandler handler) {
exception_handler_ = std::move(handler);
return *this;
}
inline Server &Server::set_pre_routing_handler(HandlerWithResponse handler) {
pre_routing_handler_ = std::move(handler);
return *this;
@@ -4335,6 +4415,15 @@ inline Server &Server::set_read_timeout(time_t sec, time_t usec) {
return *this;
}
template <class Rep, class Period>
inline Server &Server::set_read_timeout(
const std::chrono::duration<Rep, Period> &duration) {
detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) {
set_read_timeout(sec, usec);
});
return *this;
}
inline Server &Server::set_write_timeout(time_t sec, time_t usec) {
write_timeout_sec_ = sec;
write_timeout_usec_ = usec;
@@ -4342,6 +4431,15 @@ inline Server &Server::set_write_timeout(time_t sec, time_t usec) {
return *this;
}
template <class Rep, class Period>
inline Server &Server::set_write_timeout(
const std::chrono::duration<Rep, Period> &duration) {
detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) {
set_write_timeout(sec, usec);
});
return *this;
}
inline Server &Server::set_idle_interval(time_t sec, time_t usec) {
idle_interval_sec_ = sec;
idle_interval_usec_ = usec;
@@ -4349,6 +4447,15 @@ inline Server &Server::set_idle_interval(time_t sec, time_t usec) {
return *this;
}
template <class Rep, class Period>
inline Server &Server::set_idle_interval(
const std::chrono::duration<Rep, Period> &duration) {
detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) {
set_idle_interval(sec, usec);
});
return *this;
}
inline Server &Server::set_payload_max_length(size_t length) {
payload_max_length_ = length;
@@ -4463,7 +4570,7 @@ inline bool Server::write_response_core(Stream &strm, bool close_connection,
return false;
}
if (!detail::write_headers(bstrm, res, Headers())) { return false; }
if (!detail::write_headers(bstrm, res.headers)) { return false; }
// Flush buffer
auto &data = bstrm.get_buffer();
@@ -4835,22 +4942,14 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm) {
inline bool Server::dispatch_request(Request &req, Response &res,
const Handlers &handlers) {
try {
for (const auto &x : handlers) {
const auto &pattern = x.first;
const auto &handler = x.second;
for (const auto &x : handlers) {
const auto &pattern = x.first;
const auto &handler = x.second;
if (std::regex_match(req.path, req.matches, pattern)) {
handler(req, res);
return true;
}
if (std::regex_match(req.path, req.matches, pattern)) {
handler(req, res);
return true;
}
} catch (const std::exception &ex) {
res.status = 500;
res.set_header("EXCEPTION_WHAT", ex.what());
} catch (...) {
res.status = 500;
res.set_header("EXCEPTION_WHAT", "UNKNOWN");
}
return false;
}
@@ -5064,7 +5163,23 @@ Server::process_request(Stream &strm, bool close_connection,
}
// Rounting
if (routing(req, res, strm)) {
bool routed = false;
try {
routed = routing(req, res, strm);
} catch (std::exception &e) {
if (exception_handler_) {
exception_handler_(req, res, e);
routed = true;
} else {
res.status = 500;
res.set_header("EXCEPTION_WHAT", e.what());
}
} catch (...) {
res.status = 500;
res.set_header("EXCEPTION_WHAT", "UNKNOWN");
}
if (routed) {
if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; }
return write_response_with_content(strm, close_connection, req, res);
} else {
@@ -5211,7 +5326,7 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req,
if (!line_reader.getline()) { return false; }
const static std::regex re("(HTTP/1\\.[01]) (\\d{3}) (.*?)\r\n");
const static std::regex re("(HTTP/1\\.[01]) (\\d{3})(?: (.*?))?\r\n");
std::cmatch m;
if (!std::regex_match(line_reader.ptr(), m, re)) {
@@ -5235,7 +5350,7 @@ inline bool ClientImpl::read_response_line(Stream &strm, const Request &req,
return true;
}
inline bool ClientImpl::send(const Request &req, Response &res, Error &error) {
inline bool ClientImpl::send(Request &req, Response &res, Error &error) {
std::lock_guard<std::recursive_mutex> request_mutex_guard(request_mutex_);
{
@@ -5288,6 +5403,12 @@ inline bool ClientImpl::send(const Request &req, Response &res, Error &error) {
socket_requests_are_from_thread_ = std::this_thread::get_id();
}
for (const auto &header : default_headers_) {
if (req.headers.find(header.first) == req.headers.end()) {
req.headers.insert(header);
}
}
auto close_connection = !keep_alive_;
auto ret = process_socket(socket_, [&](Stream &strm) {
return handle_request(strm, req, res, close_connection, error);
@@ -5318,13 +5439,18 @@ inline bool ClientImpl::send(const Request &req, Response &res, Error &error) {
}
inline Result ClientImpl::send(const Request &req) {
auto req2 = req;
return send_(std::move(req2));
}
inline Result ClientImpl::send_(Request &&req) {
auto res = detail::make_unique<Response>();
auto error = Error::Success;
auto ret = send(req, *res, error);
return Result{ret ? std::move(res) : nullptr, error};
return Result{ret ? std::move(res) : nullptr, error, std::move(req.headers)};
}
inline bool ClientImpl::handle_request(Stream &strm, const Request &req,
inline bool ClientImpl::handle_request(Stream &strm, Request &req,
Response &res, bool close_connection,
Error &error) {
if (req.path.empty()) {
@@ -5332,12 +5458,16 @@ inline bool ClientImpl::handle_request(Stream &strm, const Request &req,
return false;
}
auto req_save = req;
bool ret;
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, error);
req = req2;
req.path = req_save.path;
} else {
ret = process_request(strm, req, res, close_connection, error);
}
@@ -5345,6 +5475,7 @@ inline bool ClientImpl::handle_request(Stream &strm, const Request &req,
if (!ret) { return false; }
if (300 < res.status && res.status < 400 && follow_location_) {
req = req_save;
ret = redirect(req, res, error);
}
@@ -5380,8 +5511,7 @@ inline bool ClientImpl::handle_request(Stream &strm, const Request &req,
return ret;
}
inline bool ClientImpl::redirect(const Request &req, Response &res,
Error &error) {
inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) {
if (req.redirect_count_ == 0) {
error = Error::ExceedRedirectCount;
return false;
@@ -5458,75 +5588,74 @@ inline bool ClientImpl::write_content_with_provider(Stream &strm,
}
} // namespace httplib
inline bool ClientImpl::write_request(Stream &strm, const Request &req,
inline bool ClientImpl::write_request(Stream &strm, Request &req,
bool close_connection, Error &error) {
// Prepare additional headers
Headers headers;
if (close_connection) { headers.emplace("Connection", "close"); }
if (close_connection) { req.headers.emplace("Connection", "close"); }
if (!req.has_header("Host")) {
if (is_ssl()) {
if (port_ == 443) {
headers.emplace("Host", host_);
req.headers.emplace("Host", host_);
} else {
headers.emplace("Host", host_and_port_);
req.headers.emplace("Host", host_and_port_);
}
} else {
if (port_ == 80) {
headers.emplace("Host", host_);
req.headers.emplace("Host", host_);
} else {
headers.emplace("Host", host_and_port_);
req.headers.emplace("Host", host_and_port_);
}
}
}
if (!req.has_header("Accept")) { headers.emplace("Accept", "*/*"); }
if (!req.has_header("Accept")) { req.headers.emplace("Accept", "*/*"); }
if (!req.has_header("User-Agent")) {
headers.emplace("User-Agent", "cpp-httplib/0.7");
req.headers.emplace("User-Agent", "cpp-httplib/0.7");
}
if (req.body.empty()) {
if (req.content_provider_) {
if (!req.is_chunked_content_provider_) {
auto length = std::to_string(req.content_length_);
headers.emplace("Content-Length", length);
req.headers.emplace("Content-Length", length);
}
} else {
if (req.method == "POST" || req.method == "PUT" ||
req.method == "PATCH") {
headers.emplace("Content-Length", "0");
req.headers.emplace("Content-Length", "0");
}
}
} else {
if (!req.has_header("Content-Type")) {
headers.emplace("Content-Type", "text/plain");
req.headers.emplace("Content-Type", "text/plain");
}
if (!req.has_header("Content-Length")) {
auto length = std::to_string(req.body.size());
headers.emplace("Content-Length", length);
req.headers.emplace("Content-Length", length);
}
}
if (!basic_auth_password_.empty()) {
headers.insert(make_basic_authentication_header(
req.headers.insert(make_basic_authentication_header(
basic_auth_username_, basic_auth_password_, false));
}
if (!proxy_basic_auth_username_.empty() &&
!proxy_basic_auth_password_.empty()) {
headers.insert(make_basic_authentication_header(
req.headers.insert(make_basic_authentication_header(
proxy_basic_auth_username_, proxy_basic_auth_password_, true));
}
if (!bearer_token_auth_token_.empty()) {
headers.insert(make_bearer_token_authentication_header(
req.headers.insert(make_bearer_token_authentication_header(
bearer_token_auth_token_, false));
}
if (!proxy_bearer_token_auth_token_.empty()) {
headers.insert(make_bearer_token_authentication_header(
req.headers.insert(make_bearer_token_authentication_header(
proxy_bearer_token_auth_token_, true));
}
@@ -5537,7 +5666,7 @@ inline bool ClientImpl::write_request(Stream &strm, const Request &req,
const auto &path = detail::encode_url(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);
detail::write_headers(bstrm, req.headers);
// Flush buffer
auto &data = bstrm.get_buffer();
@@ -5558,16 +5687,16 @@ inline bool ClientImpl::write_request(Stream &strm, const Request &req,
}
inline std::unique_ptr<Response> ClientImpl::send_with_content_provider(
const char *method, const char *path, const Headers &headers,
Request &req,
// const char *method, const char *path, const Headers &headers,
const char *body, size_t content_length, ContentProvider content_provider,
ContentProviderWithoutLength content_provider_without_length,
const char *content_type, Error &error) {
Request req;
req.method = method;
req.headers = default_headers_;
req.headers.insert(headers.begin(), headers.end());
req.path = path;
// Request req;
// req.method = method;
// req.headers = headers;
// req.path = path;
if (content_type) { req.headers.emplace("Content-Type", content_type); }
@@ -5649,14 +5778,23 @@ inline Result ClientImpl::send_with_content_provider(
const char *body, size_t content_length, ContentProvider content_provider,
ContentProviderWithoutLength content_provider_without_length,
const char *content_type) {
Request req;
req.method = method;
req.headers = headers;
req.path = path;
auto error = Error::Success;
auto res = send_with_content_provider(
method, path, headers, body, content_length, std::move(content_provider),
req,
// method, path, headers,
body, content_length, std::move(content_provider),
std::move(content_provider_without_length), content_type, error);
return Result{std::move(res), error};
return Result{std::move(res), error, std::move(req.headers)};
}
inline bool ClientImpl::process_request(Stream &strm, const Request &req,
inline bool ClientImpl::process_request(Stream &strm, Request &req,
Response &res, bool close_connection,
Error &error) {
// Send request
@@ -5669,20 +5807,23 @@ inline bool ClientImpl::process_request(Stream &strm, const Request &req,
return false;
}
if (req.response_handler_) {
if (!req.response_handler_(res)) {
error = Error::Canceled;
return false;
}
}
// Body
if ((res.status != 204) && req.method != "HEAD" && req.method != "CONNECT") {
auto redirect = 300 < res.status && res.status < 400 && follow_location_;
if (req.response_handler && !redirect) {
if (!req.response_handler(res)) {
error = Error::Canceled;
return false;
}
}
auto out =
req.content_receiver_
req.content_receiver
? static_cast<ContentReceiverWithProgress>(
[&](const char *buf, size_t n, uint64_t off, uint64_t len) {
auto ret = req.content_receiver_(buf, n, off, len);
if (redirect) { return true; }
auto ret = req.content_receiver(buf, n, off, len);
if (!ret) { error = Error::Canceled; }
return ret;
})
@@ -5697,8 +5838,8 @@ inline bool ClientImpl::process_request(Stream &strm, const Request &req,
});
auto progress = [&](uint64_t current, uint64_t total) {
if (!req.progress_) { return true; }
auto ret = req.progress_(current, total);
if (!req.progress || redirect) { return true; }
auto ret = req.progress(current, total);
if (!ret) { error = Error::Canceled; }
return ret;
};
@@ -5760,11 +5901,10 @@ inline Result ClientImpl::Get(const char *path, const Headers &headers,
Request req;
req.method = "GET";
req.path = path;
req.headers = default_headers_;
req.headers.insert(headers.begin(), headers.end());
req.progress_ = std::move(progress);
req.headers = headers;
req.progress = std::move(progress);
return send(req);
return send_(std::move(req));
}
inline Result ClientImpl::Get(const char *path,
@@ -5820,17 +5960,16 @@ inline Result ClientImpl::Get(const char *path, const Headers &headers,
Request req;
req.method = "GET";
req.path = path;
req.headers = default_headers_;
req.headers.insert(headers.begin(), headers.end());
req.response_handler_ = std::move(response_handler);
req.content_receiver_ =
req.headers = headers;
req.response_handler = std::move(response_handler);
req.content_receiver =
[content_receiver](const char *data, size_t data_length,
uint64_t /*offset*/, uint64_t /*total_length*/) {
return content_receiver(data, data_length);
};
req.progress_ = std::move(progress);
req.progress = std::move(progress);
return send(req);
return send_(std::move(req));
}
inline Result ClientImpl::Get(const char *path, const Params &params,
@@ -5858,7 +5997,7 @@ inline Result ClientImpl::Get(const char *path, const Params &params,
}
std::string path_with_query = detail::append_query_params(path, params);
return Get(path_with_query.c_str(), params, headers, response_handler,
return Get(path_with_query.c_str(), headers, response_handler,
content_receiver, progress);
}
@@ -5869,11 +6008,10 @@ inline Result ClientImpl::Head(const char *path) {
inline Result ClientImpl::Head(const char *path, const Headers &headers) {
Request req;
req.method = "HEAD";
req.headers = default_headers_;
req.headers.insert(headers.begin(), headers.end());
req.headers = headers;
req.path = path;
return send(req);
return send_(std::move(req));
}
inline Result ClientImpl::Post(const char *path) {
@@ -6133,14 +6271,13 @@ inline Result ClientImpl::Delete(const char *path, const Headers &headers,
const char *content_type) {
Request req;
req.method = "DELETE";
req.headers = default_headers_;
req.headers.insert(headers.begin(), headers.end());
req.headers = headers;
req.path = path;
if (content_type) { req.headers.emplace("Content-Type", content_type); }
req.body.assign(body, content_length);
return send(req);
return send_(std::move(req));
}
inline Result ClientImpl::Delete(const char *path, const std::string &body,
@@ -6161,11 +6298,10 @@ inline Result ClientImpl::Options(const char *path) {
inline Result ClientImpl::Options(const char *path, const Headers &headers) {
Request req;
req.method = "OPTIONS";
req.headers = default_headers_;
req.headers.insert(headers.begin(), headers.end());
req.headers = headers;
req.path = path;
return send(req);
return send_(std::move(req));
}
inline size_t ClientImpl::is_socket_open() const {
@@ -6201,16 +6337,40 @@ inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) {
connection_timeout_usec_ = usec;
}
template <class Rep, class Period>
inline void ClientImpl::set_connection_timeout(
const std::chrono::duration<Rep, Period> &duration) {
detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) {
set_connection_timeout(sec, usec);
});
}
inline void ClientImpl::set_read_timeout(time_t sec, time_t usec) {
read_timeout_sec_ = sec;
read_timeout_usec_ = usec;
}
template <class Rep, class Period>
inline void ClientImpl::set_read_timeout(
const std::chrono::duration<Rep, Period> &duration) {
detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) {
set_read_timeout(sec, usec);
});
}
inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) {
write_timeout_sec_ = sec;
write_timeout_usec_ = usec;
}
template <class Rep, class Period>
inline void ClientImpl::set_write_timeout(
const std::chrono::duration<Rep, Period> &duration) {
detail::duration_to_sec_and_usec(duration, [&](time_t sec, time_t usec) {
set_write_timeout(sec, usec);
});
}
inline void ClientImpl::set_basic_auth(const char *username,
const char *password) {
basic_auth_username_ = username;
@@ -7285,7 +7445,7 @@ inline Result Client::Options(const char *path, const Headers &headers) {
return cli_->Options(path, headers);
}
inline bool Client::send(const Request &req, Response &res, Error &error) {
inline bool Client::send(Request &req, Response &res, Error &error) {
return cli_->send(req, res, error);
}
@@ -7300,6 +7460,7 @@ inline void Client::set_default_headers(Headers headers) {
}
inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); }
inline void Client::set_socket_options(SocketOptions socket_options) {
cli_->set_socket_options(std::move(socket_options));
}
@@ -7307,13 +7468,33 @@ inline void Client::set_socket_options(SocketOptions socket_options) {
inline void Client::set_connection_timeout(time_t sec, time_t usec) {
cli_->set_connection_timeout(sec, usec);
}
template <class Rep, class Period>
inline void Client::set_connection_timeout(
const std::chrono::duration<Rep, Period> &duration) {
cli_->set_connection_timeout(duration);
}
inline void Client::set_read_timeout(time_t sec, time_t usec) {
cli_->set_read_timeout(sec, usec);
}
template <class Rep, class Period>
inline void Client::set_read_timeout(
const std::chrono::duration<Rep, Period> &duration) {
cli_->set_read_timeout(duration);
}
inline void Client::set_write_timeout(time_t sec, time_t usec) {
cli_->set_write_timeout(sec, usec);
}
template <class Rep, class Period>
inline void Client::set_write_timeout(
const std::chrono::duration<Rep, Period> &duration) {
cli_->set_write_timeout(duration);
}
inline void Client::set_basic_auth(const char *username, const char *password) {
cli_->set_basic_auth(username, password);
}
+270 -89
View File
@@ -5,6 +5,7 @@
#include <atomic>
#include <chrono>
#include <future>
#include <stdexcept>
#include <thread>
#define SERVER_CERT_FILE "./cert.pem"
@@ -416,16 +417,16 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {
cli.set_connection_timeout(2);
std::string body;
auto res = cli.Get(
"/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
[&](const Response &response) {
EXPECT_EQ(200, response.status);
return true;
},
[&](const char *data, size_t data_length) {
body.append(data, data_length);
return true;
});
auto res =
cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
[&](const Response &response) {
EXPECT_EQ(200, response.status);
return true;
},
[&](const char *data, size_t data_length) {
body.append(data, data_length);
return true;
});
ASSERT_TRUE(res);
std::string out;
@@ -524,7 +525,7 @@ TEST(ConnectionErrorTest, InvalidHost) {
auto port = 80;
Client cli(host, port);
#endif
cli.set_connection_timeout(2);
cli.set_connection_timeout(std::chrono::seconds(2));
auto res = cli.Get("/");
ASSERT_TRUE(!res);
@@ -539,7 +540,7 @@ TEST(ConnectionErrorTest, InvalidHost2) {
#else
Client cli(host);
#endif
cli.set_connection_timeout(2);
cli.set_connection_timeout(std::chrono::seconds(2));
auto res = cli.Get("/");
ASSERT_TRUE(!res);
@@ -548,15 +549,14 @@ TEST(ConnectionErrorTest, InvalidHost2) {
TEST(ConnectionErrorTest, InvalidPort) {
auto host = "localhost";
auto port = 44380;
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 44380;
SSLClient cli(host, port);
#else
auto port = 8080;
Client cli(host, port);
#endif
cli.set_connection_timeout(2);
cli.set_connection_timeout(std::chrono::seconds(2));
auto res = cli.Get("/");
ASSERT_TRUE(!res);
@@ -573,7 +573,7 @@ TEST(ConnectionErrorTest, Timeout) {
auto port = 8080;
Client cli(host, port);
#endif
cli.set_connection_timeout(2);
cli.set_connection_timeout(std::chrono::seconds(2));
auto res = cli.Get("/");
ASSERT_TRUE(!res);
@@ -590,7 +590,7 @@ TEST(CancelTest, NoCancel) {
auto port = 80;
Client cli(host, port);
#endif
cli.set_connection_timeout(5);
cli.set_connection_timeout(std::chrono::seconds(5));
auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return true; });
ASSERT_TRUE(res);
@@ -610,7 +610,7 @@ TEST(CancelTest, WithCancelSmallPayload) {
#endif
auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return false; });
cli.set_connection_timeout(5);
cli.set_connection_timeout(std::chrono::seconds(5));
ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error());
}
@@ -625,7 +625,7 @@ TEST(CancelTest, WithCancelLargePayload) {
auto port = 80;
Client cli(host, port);
#endif
cli.set_connection_timeout(5);
cli.set_connection_timeout(std::chrono::seconds(5));
uint32_t count = 0;
auto res = cli.Get("/range/65536",
@@ -889,6 +889,64 @@ TEST(UrlWithSpace, Redirect) {
EXPECT_EQ(200, res->status);
EXPECT_EQ(18527, res->get_header_value<uint64_t>("Content-Length"));
}
TEST(RedirectFromPageWithContent, Redirect) {
Server svr;
svr.Get("/1", [&](const Request & /*req*/, Response &res) {
res.set_content("___", "text/plain");
res.set_redirect("/2");
});
svr.Get("/2", [&](const Request & /*req*/, Response &res) {
res.set_content("Hello World!", "text/plain");
});
auto th = std::thread([&]() { svr.listen("localhost", PORT); });
while (!svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
{
Client cli("localhost", PORT);
cli.set_follow_location(true);
std::string body;
auto res = cli.Get("/1",
[&](const char *data, size_t data_length) {
body.append(data, data_length);
return true;
});
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
EXPECT_EQ("Hello World!", body);
}
{
Client cli("localhost", PORT);
std::string body;
auto res = cli.Get("/1",
[&](const char *data, size_t data_length) {
body.append(data, data_length);
return true;
});
ASSERT_TRUE(res);
EXPECT_EQ(302, res->status);
EXPECT_EQ("___", body);
}
svr.stop();
th.join();
ASSERT_FALSE(svr.is_running());
}
#endif
TEST(BindServerTest, BindDualStack) {
@@ -978,6 +1036,42 @@ TEST(ErrorHandlerTest, ContentLength) {
ASSERT_FALSE(svr.is_running());
}
TEST(ExceptionHandlerTest, ContentLength) {
Server svr;
svr.set_exception_handler(
[](const Request & /*req*/, Response &res, std::exception & /*e*/) {
res.status = 500;
res.set_content("abcdefghijklmnopqrstuvwxyz",
"text/html"); // <= Content-Length still 13
});
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
res.set_content("Hello World!\n", "text/plain");
throw std::runtime_error("abc");
});
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));
{
Client cli(HOST, PORT);
auto res = cli.Get("/hi");
ASSERT_TRUE(res);
EXPECT_EQ(500, res->status);
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
EXPECT_EQ("26", res->get_header_value("Content-Length"));
EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body);
}
svr.stop();
thread.join();
ASSERT_FALSE(svr.is_running());
}
TEST(NoContentTest, ContentLength) {
Server svr;
@@ -1608,6 +1702,7 @@ TEST_F(ServerTest, GetMethod200) {
ASSERT_TRUE(res);
EXPECT_EQ("HTTP/1.1", res->version);
EXPECT_EQ(200, res->status);
EXPECT_EQ("OK", res->reason);
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
EXPECT_EQ("Hello World!", res->body);
@@ -2279,7 +2374,8 @@ TEST_F(ServerTest, ClientStop) {
auto res = cli_.Get("/streamed-cancel",
[&](const char *, uint64_t) { return true; });
ASSERT_TRUE(!res);
EXPECT_TRUE(res.error() == Error::Canceled || res.error() == Error::Read);
EXPECT_TRUE(res.error() == Error::Canceled ||
res.error() == Error::Read || res.error() == Error::Write);
}));
}
@@ -2424,13 +2520,13 @@ TEST_F(ServerTest, SlowPost) {
char buffer[64 * 1024];
memset(buffer, 0x42, sizeof(buffer));
auto res = cli_.Post(
"/slowpost", 64 * 1024 * 1024,
[&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
sink.write(buffer, sizeof(buffer));
return true;
},
"text/plain");
auto res =
cli_.Post("/slowpost", 64 * 1024 * 1024,
[&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
sink.write(buffer, sizeof(buffer));
return true;
},
"text/plain");
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
@@ -2440,14 +2536,14 @@ TEST_F(ServerTest, SlowPostFail) {
char buffer[64 * 1024];
memset(buffer, 0x42, sizeof(buffer));
cli_.set_write_timeout(0, 0);
auto res = cli_.Post(
"/slowpost", 64 * 1024 * 1024,
[&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
sink.write(buffer, sizeof(buffer));
return true;
},
"text/plain");
cli_.set_write_timeout(std::chrono::seconds(0));
auto res =
cli_.Post("/slowpost", 64 * 1024 * 1024,
[&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
sink.write(buffer, sizeof(buffer));
return true;
},
"text/plain");
ASSERT_TRUE(!res);
EXPECT_EQ(Error::Write, res.error());
@@ -2461,14 +2557,13 @@ TEST_F(ServerTest, Put) {
}
TEST_F(ServerTest, PutWithContentProvider) {
auto res = cli_.Put(
"/put", 3,
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
return true;
},
"text/plain");
auto res = cli_.Put("/put", 3,
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
return true;
},
"text/plain");
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
@@ -2476,27 +2571,24 @@ TEST_F(ServerTest, PutWithContentProvider) {
}
TEST_F(ServerTest, PostWithContentProviderAbort) {
auto res = cli_.Post(
"/post", 42,
[](size_t /*offset*/, size_t /*length*/, DataSink & /*sink*/) {
return false;
},
"text/plain");
auto res = cli_.Post("/post", 42,
[](size_t /*offset*/, size_t /*length*/,
DataSink & /*sink*/) { return false; },
"text/plain");
ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error());
}
TEST_F(ServerTest, PutWithContentProviderWithoutLength) {
auto res = cli_.Put(
"/put",
[](size_t /*offset*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
sink.done();
return true;
},
"text/plain");
auto res = cli_.Put("/put",
[](size_t /*offset*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
sink.done();
return true;
},
"text/plain");
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
@@ -2515,14 +2607,13 @@ TEST_F(ServerTest, PostWithContentProviderWithoutLengthAbort) {
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
TEST_F(ServerTest, PutWithContentProviderWithGzip) {
cli_.set_compress(true);
auto res = cli_.Put(
"/put", 3,
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
return true;
},
"text/plain");
auto res = cli_.Put("/put", 3,
[](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
return true;
},
"text/plain");
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
@@ -2531,12 +2622,10 @@ TEST_F(ServerTest, PutWithContentProviderWithGzip) {
TEST_F(ServerTest, PostWithContentProviderWithGzipAbort) {
cli_.set_compress(true);
auto res = cli_.Post(
"/post", 42,
[](size_t /*offset*/, size_t /*length*/, DataSink & /*sink*/) {
return false;
},
"text/plain");
auto res = cli_.Post("/post", 42,
[](size_t /*offset*/, size_t /*length*/,
DataSink & /*sink*/) { return false; },
"text/plain");
ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error());
@@ -2544,15 +2633,14 @@ TEST_F(ServerTest, PostWithContentProviderWithGzipAbort) {
TEST_F(ServerTest, PutWithContentProviderWithoutLengthWithGzip) {
cli_.set_compress(true);
auto res = cli_.Put(
"/put",
[](size_t /*offset*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
sink.done();
return true;
},
"text/plain");
auto res = cli_.Put("/put",
[](size_t /*offset*/, DataSink &sink) {
EXPECT_TRUE(sink.is_writable());
sink.os << "PUT";
sink.done();
return true;
},
"text/plain");
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
@@ -2578,6 +2666,24 @@ TEST_F(ServerTest, PutLargeFileWithGzip) {
EXPECT_EQ(LARGE_DATA, res->body);
}
TEST_F(ServerTest, PutLargeFileWithGzip2) {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
Client cli("https://localhost:1234");
cli.enable_server_certificate_verification(false);
#else
Client cli("http://localhost:1234");
#endif
cli.set_compress(true);
auto res = cli.Put("/put-large", LARGE_DATA, "text/plain");
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("gzip", res.get_request_header_value("Content-Encoding"));
}
TEST_F(ServerTest, PutContentWithDeflate) {
cli_.set_compress(false);
Headers headers;
@@ -2854,9 +2960,8 @@ TEST_F(ServerTest, KeepAlive) {
EXPECT_EQ("empty", res->body);
EXPECT_EQ("close", res->get_header_value("Connection"));
res = cli_.Post(
"/empty", 0, [&](size_t, size_t, DataSink &) { return true; },
"text/plain");
res = cli_.Post("/empty", 0, [&](size_t, size_t, DataSink &) { return true; },
"text/plain");
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
@@ -3099,7 +3204,7 @@ static void test_raw_request(const std::string &req,
// bug to reproduce, probably to force the server to process a request
// without a trailing blank line.
const time_t client_read_timeout_sec = 1;
svr.set_read_timeout(client_read_timeout_sec + 1, 0);
svr.set_read_timeout(std::chrono::seconds(client_read_timeout_sec + 1));
bool listen_thread_ok = false;
thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); });
while (!svr.is_running()) {
@@ -3369,7 +3474,8 @@ TEST(ExceptionTest, ThrowExceptionInHandler) {
auto res = cli.Get("/hi");
ASSERT_TRUE(res);
EXPECT_EQ(500, res->status);
ASSERT_FALSE(res->has_header("EXCEPTION_WHAT"));
ASSERT_TRUE(res->has_header("EXCEPTION_WHAT"));
EXPECT_EQ("exception...", res->get_header_value("EXCEPTION_WHAT"));
svr.stop();
listen_thread.join();
@@ -3398,7 +3504,7 @@ TEST(KeepAliveTest, ReadTimeout) {
Client cli("localhost", PORT);
cli.set_keep_alive(true);
cli.set_read_timeout(1);
cli.set_read_timeout(std::chrono::seconds(1));
auto resa = cli.Get("/a");
ASSERT_TRUE(!resa);
@@ -3447,6 +3553,69 @@ TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) {
ASSERT_FALSE(svr.is_running());
}
TEST(GetWithParametersTest, GetWithParameters) {
Server svr;
svr.Get("/", [&](const Request & req, Response &res) {
auto text = req.get_param_value("hello");
res.set_content(text, "text/plain");
});
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
while (!svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli("localhost", PORT);
Params params;
params.emplace("hello", "world");
auto res = cli.Get("/", params, Headers{});
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
EXPECT_EQ("world", res->body);
svr.stop();
listen_thread.join();
ASSERT_FALSE(svr.is_running());
}
TEST(GetWithParametersTest, GetWithParameters2) {
Server svr;
svr.Get("/", [&](const Request & req, Response &res) {
auto text = req.get_param_value("hello");
res.set_content(text, "text/plain");
});
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
while (!svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
std::this_thread::sleep_for(std::chrono::seconds(1));
Client cli("localhost", PORT);
Params params;
params.emplace("hello", "world");
std::string body;
auto res = cli.Get("/", params, Headers{}, [&](const char *data, size_t data_length) {
body.append(data, data_length);
return true;
});
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
EXPECT_EQ("world", body);
svr.stop();
listen_thread.join();
ASSERT_FALSE(svr.is_running());
}
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
TEST(KeepAliveTest, ReadTimeoutSSL) {
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
@@ -3472,7 +3641,7 @@ TEST(KeepAliveTest, ReadTimeoutSSL) {
SSLClient cli("localhost", PORT);
cli.enable_server_certificate_verification(false);
cli.set_keep_alive(true);
cli.set_read_timeout(1);
cli.set_read_timeout(std::chrono::seconds(1));
auto resa = cli.Get("/a");
ASSERT_TRUE(!resa);
@@ -3927,7 +4096,6 @@ TEST(NoSSLSupport, SimpleInterface) {
}
#endif
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
TEST(InvalidScheme, SimpleInterface) {
ASSERT_ANY_THROW(Client cli("scheme://yahoo.com"));
}
@@ -3937,6 +4105,19 @@ TEST(NoScheme, SimpleInterface) {
ASSERT_TRUE(cli.is_valid());
}
TEST(SendAPI, SimpleInterface) {
Client cli("http://yahoo.com");
Request req;
req.method = "GET";
req.path = "/";
auto res = cli.send(req);
ASSERT_TRUE(res);
EXPECT_EQ(301, res->status);
}
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
TEST(YahooRedirectTest2, SimpleInterface) {
Client cli("http://yahoo.com");