Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Dec 18, 2019
1 parent 39c7bba commit 9c81693
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ res = cli.Options("/resource/foo");
### Connection Timeout

```c++
httplib::Client cli("localhost", 8080, 5); // timeouts in 5 seconds
cli.set_timeout_sec(5); // timeouts in 5 seconds
```
### With Progress Callback

Expand Down
40 changes: 23 additions & 17 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ class Server {

class Client {
public:
explicit Client(const char *host, int port = 80, time_t timeout_sec = 300);
explicit Client(const char *host, int port = 80);

virtual ~Client();

Expand Down Expand Up @@ -734,6 +734,8 @@ class Client {
bool send(const std::vector<Request> &requests,
std::vector<Response> &responses);

void set_timeout_sec(time_t timeout_sec);

void set_keep_alive_max_count(size_t count);

void set_read_timeout(time_t sec, time_t usec);
Expand All @@ -752,15 +754,17 @@ class Client {

const std::string host_;
const int port_;
time_t timeout_sec_;
const std::string host_and_port_;
size_t keep_alive_max_count_;
time_t read_timeout_sec_;
time_t read_timeout_usec_;
bool follow_location_;

// Options
time_t timeout_sec_ = 300;
size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT;
time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND;
time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND;
std::string username_;
std::string password_;
bool compress_;
bool follow_location_ = false;
bool compress_ = false;
std::string interface_;

private:
Expand Down Expand Up @@ -852,7 +856,7 @@ class SSLServer : public Server {

class SSLClient : public Client {
public:
SSLClient(const char *host, int port = 443, time_t timeout_sec = 300,
SSLClient(const char *host, int port = 443,
const char *client_cert_path = nullptr,
const char *client_key_path = nullptr);

Expand Down Expand Up @@ -884,6 +888,8 @@ class SSLClient : public Client {
SSL_CTX *ctx_;
std::mutex ctx_mutex_;
std::vector<std::string> host_components_;

// Options
std::string ca_cert_file_path_;
std::string ca_cert_dir_path_;
bool server_certificate_verification_ = false;
Expand Down Expand Up @@ -3355,13 +3361,9 @@ inline bool Server::process_and_close_socket(socket_t sock) {
}

// HTTP client implementation
inline Client::Client(const char *host, int port, time_t timeout_sec)
: host_(host), port_(port), timeout_sec_(timeout_sec),
host_and_port_(host_ + ":" + std::to_string(port_)),
keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT),
read_timeout_sec_(CPPHTTPLIB_READ_TIMEOUT_SECOND),
read_timeout_usec_(CPPHTTPLIB_READ_TIMEOUT_USECOND),
follow_location_(false), compress_(false) {}
inline Client::Client(const char *host, int port)
: host_(host), port_(port),
host_and_port_(host_ + ":" + std::to_string(port_)) {}

inline Client::~Client() {}

Expand Down Expand Up @@ -3988,6 +3990,10 @@ inline std::shared_ptr<Response> Client::Options(const char *path,
return send(req, *res) ? res : nullptr;
}

inline void Client::set_timeout_sec(time_t timeout_sec) {
timeout_sec_ = timeout_sec;
}

inline void Client::set_keep_alive_max_count(size_t count) {
keep_alive_max_count_ = count;
}
Expand Down Expand Up @@ -4227,10 +4233,10 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
}

// SSL HTTP client implementation
inline SSLClient::SSLClient(const char *host, int port, time_t timeout_sec,
inline SSLClient::SSLClient(const char *host, int port,
const char *client_cert_path,
const char *client_key_path)
: Client(host, port, timeout_sec) {
: Client(host, port) {
ctx_ = SSL_CTX_new(SSLv23_client_method());

detail::split(&host_[0], &host_[host_.size()], '.',
Expand Down
69 changes: 36 additions & 33 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ TEST(ParseHeaderValueTest, Range) {

TEST(ChunkedEncodingTest, FromHTTPWatch) {
auto host = "www.httpwatch.com";
auto sec = 2;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443;
httplib::SSLClient cli(host, port, sec);
httplib::SSLClient cli(host, port);
#else
auto port = 80;
httplib::Client cli(host, port, sec);
httplib::Client cli(host, port);
#endif
cli.set_timeout_sec(2);

auto res =
cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137");
Expand All @@ -227,15 +227,15 @@ TEST(ChunkedEncodingTest, FromHTTPWatch) {

TEST(ChunkedEncodingTest, WithContentReceiver) {
auto host = "www.httpwatch.com";
auto sec = 2;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443;
httplib::SSLClient cli(host, port, sec);
httplib::SSLClient cli(host, port);
#else
auto port = 80;
httplib::Client cli(host, port, sec);
httplib::Client cli(host, port);
#endif
cli.set_timeout_sec(2);

std::string body;
auto res =
Expand All @@ -255,15 +255,15 @@ TEST(ChunkedEncodingTest, WithContentReceiver) {

TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {
auto host = "www.httpwatch.com";
auto sec = 2;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443;
httplib::SSLClient cli(host, port, sec);
httplib::SSLClient cli(host, port);
#else
auto port = 80;
httplib::Client cli(host, port, sec);
httplib::Client cli(host, port);
#endif
cli.set_timeout_sec(2);

std::string body;
auto res = cli.Get(
Expand All @@ -287,15 +287,15 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {

TEST(RangeTest, FromHTTPBin) {
auto host = "httpbin.org";
auto sec = 5;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443;
httplib::SSLClient cli(host, port, sec);
httplib::SSLClient cli(host, port);
#else
auto port = 80;
httplib::Client cli(host, port, sec);
httplib::Client cli(host, port);
#endif
cli.set_timeout_sec(5);

{
httplib::Headers headers;
Expand Down Expand Up @@ -347,63 +347,63 @@ TEST(RangeTest, FromHTTPBin) {

TEST(ConnectionErrorTest, InvalidHost) {
auto host = "-abcde.com";
auto sec = 2;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443;
httplib::SSLClient cli(host, port, sec);
httplib::SSLClient cli(host, port);
#else
auto port = 80;
httplib::Client cli(host, port, sec);
httplib::Client cli(host, port);
#endif
cli.set_timeout_sec(2);

auto res = cli.Get("/");
ASSERT_TRUE(res == nullptr);
}

TEST(ConnectionErrorTest, InvalidPort) {
auto host = "localhost";
auto sec = 2;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 44380;
httplib::SSLClient cli(host, port, sec);
httplib::SSLClient cli(host, port);
#else
auto port = 8080;
httplib::Client cli(host, port, sec);
httplib::Client cli(host, port);
#endif
cli.set_timeout_sec(2);

auto res = cli.Get("/");
ASSERT_TRUE(res == nullptr);
}

TEST(ConnectionErrorTest, Timeout) {
auto host = "google.com";
auto sec = 2;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 44380;
httplib::SSLClient cli(host, port, sec);
httplib::SSLClient cli(host, port);
#else
auto port = 8080;
httplib::Client cli(host, port, sec);
httplib::Client cli(host, port);
#endif
cli.set_timeout_sec(2);

auto res = cli.Get("/");
ASSERT_TRUE(res == nullptr);
}

TEST(CancelTest, NoCancel) {
auto host = "httpbin.org";
auto sec = 5;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443;
httplib::SSLClient cli(host, port, sec);
httplib::SSLClient cli(host, port);
#else
auto port = 80;
httplib::Client cli(host, port, sec);
httplib::Client cli(host, port);
#endif
cli.set_timeout_sec(5);

auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return true; });
ASSERT_TRUE(res != nullptr);
Expand All @@ -413,31 +413,31 @@ TEST(CancelTest, NoCancel) {

TEST(CancelTest, WithCancelSmallPayload) {
auto host = "httpbin.org";
auto sec = 5;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443;
httplib::SSLClient cli(host, port, sec);
httplib::SSLClient cli(host, port);
#else
auto port = 80;
httplib::Client cli(host, port, sec);
httplib::Client cli(host, port);
#endif

auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return false; });
cli.set_timeout_sec(5);
ASSERT_TRUE(res == nullptr);
}

TEST(CancelTest, WithCancelLargePayload) {
auto host = "httpbin.org";
auto sec = 5;

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto port = 443;
httplib::SSLClient cli(host, port, sec);
httplib::SSLClient cli(host, port);
#else
auto port = 80;
httplib::Client cli(host, port, sec);
httplib::Client cli(host, port);
#endif
cli.set_timeout_sec(5);

uint32_t count = 0;
httplib::Headers headers;
Expand Down Expand Up @@ -2090,9 +2090,10 @@ TEST(SSLClientServerTest, ClientCertPresent) {
thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
msleep(1);

httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE,
CLIENT_PRIVATE_KEY_FILE);
auto res = cli.Get("/test");
cli.set_timeout_sec(30);
ASSERT_TRUE(res != nullptr);
ASSERT_EQ(200, res->status);

Expand All @@ -2109,8 +2110,9 @@ TEST(SSLClientServerTest, ClientCertMissing) {
thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
msleep(1);

httplib::SSLClient cli(HOST, PORT, 30);
httplib::SSLClient cli(HOST, PORT);
auto res = cli.Get("/test");
cli.set_timeout_sec(30);
ASSERT_TRUE(res == nullptr);

svr.stop();
Expand All @@ -2130,9 +2132,10 @@ TEST(SSLClientServerTest, TrustDirOptional) {
thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
msleep(1);

httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE,
CLIENT_PRIVATE_KEY_FILE);
auto res = cli.Get("/test");
cli.set_timeout_sec(30);
ASSERT_TRUE(res != nullptr);
ASSERT_EQ(200, res->status);

Expand Down

0 comments on commit 9c81693

Please sign in to comment.