add ssl enable/disable switch for http_aclient

This commit is contained in:
zhengshuxin 2019-10-18 10:02:22 +08:00
parent 2998681abc
commit 76ba04380d
2 changed files with 57 additions and 2 deletions

View File

@ -69,6 +69,43 @@ public:
return unzip_;
}
/**
* SSL conf
* ssl_conf NULL ssl_enable_
* false ssl_conf enable_ssl()
* ssl
* @param ssl_conf {polarssl_conf*} NULL SSL功能
* @return {http_aclient&}
*/
http_aclient& set_ssl_conf(polarssl_conf* ssl_conf);
/**
* SSL
* @return {polarssl_conf*} NULL
*/
polarssl_conf* get_ssl_conf(void) const
{
return ssl_conf_;
}
/**
* ssl_conf SSL
* ssl_conf ssl_enable_ true
* ssl
* @param yes {bool}
* @return {http_aclient&}
*/
http_aclient& enable_ssl(bool yes);
/**
* ssl
* @return {bool}
*/
bool is_enable_ssl(void) const
{
return ssl_enable_ && !ssl_conf_;
}
/**
* WEB
* @param addr {const char*} WEB
@ -265,6 +302,8 @@ public:
/**
* WEBSOCKET
* @param key {const void*} key
* @param len {size_t} key
*/
void ws_handshake(const void* key, size_t len);
void ws_handshake(const char* key = "123456789xxx");
@ -345,7 +384,8 @@ protected:
bool keep_alive_;
bool unzip_; // 是否自动解压响应体数据
aio_handle& handle_;
polarssl_conf* ssl_conf_;
polarssl_conf* ssl_conf_; // 非空时才允许启用 SSL 功能
bool ssl_enable_; // 是否启用 SSL 功能
aio_socket_stream* conn_;
socket_stream* stream_;
http_header* header_;

View File

@ -44,6 +44,9 @@ http_aclient::http_aclient(aio_handle& handle, polarssl_conf* ssl_conf /* NULL *
memset(&serv_addr_, 0, sizeof(serv_addr_));
#if !defined(HAS_POLARSSL_DLL) && !defined(HAS_POLARSSL)
(void) ssl_conf_; // just avoiding compiling warning
ssl_enable_ = false;
#else
ssl_enable_ = true;
#endif
}
@ -78,6 +81,18 @@ http_aclient& http_aclient::unzip_body(bool on)
return *this;
}
http_aclient& http_aclient::set_ssl_conf(polarssl_conf* ssl_conf)
{
ssl_conf_ = ssl_conf;
return *this;
}
http_aclient& http_aclient::enable_ssl(bool yes)
{
ssl_enable_ = yes;
return *this;
}
bool http_aclient::open(const char* addr, int conn_timeout, int rw_timeout)
{
ACL_AIO* aio = handle_.get_handle();
@ -163,7 +178,7 @@ bool http_aclient::handle_connect(const ACL_ASTREAM_CTX *ctx)
conn_->add_timeout_callback(this);
#if defined(HAS_POLARSSL_DLL) || defined(HAS_POLARSSL)
if (!ssl_conf_) {
if (!ssl_conf_ || !ssl_enable_) {
return this->on_connect();
}