ws key should be base64 encoded in http_header::set_ws_key()

This commit is contained in:
zhengshuxin 2019-08-09 18:05:51 +08:00
parent c55d1badc7
commit 6eba8bbafd
6 changed files with 61 additions and 7 deletions

View File

@ -1,5 +1,8 @@
修改历史列表:
586) 2019.8.9
586.1) bugfix: http_aclient.cpp 通过 set_ws_key() 设置的 key 应该做 Base64 编码
585) 2019.8.8
585.1) feature: url_coder 支持值为空的场景

View File

@ -12,6 +12,7 @@ struct ACL_ASTREAM;
namespace acl {
class string;
class aio_handle;
class aio_socket_stream;
class socket_stream;
@ -202,6 +203,26 @@ protected:
*/
virtual bool on_ws_frame_finish(void) { return true; }
/**
* ping
* data pong data
* pong
* @param data {string&}
*/
virtual void on_ws_frame_ping(string& data)
{
(void) data;
}
/**
* pong
* @param data {string&}
*/
virtual void on_ws_frame_pong(string& data)
{
(void) data;
}
public:
/**
* WEB HTTP HTTP
@ -214,7 +235,8 @@ public:
/**
* WEBSOCKET
*/
void ws_handshake(void);
void ws_handshake(const void* key, size_t len);
void ws_handshake(const char* key = "123456789");
/**
* websocket

View File

@ -330,6 +330,7 @@ public:
#endif
http_header& set_ws_origin(const char* url);
http_header& set_ws_key(const void* key, size_t len);
http_header& set_ws_key(const char* key);
http_header& set_ws_protocol(const char* proto);
http_header& set_ws_version(int ver);

View File

@ -42,7 +42,8 @@ protected:
// @override
void destroy(void)
{
printf("websocket_client will be deleted!\r\n");
printf("%s(%d): websocket_client will be deleted!\r\n",
__FUNCTION__, __LINE__);
fflush(stdout);
delete this;
@ -62,7 +63,8 @@ protected:
// @override
void on_disconnect(void)
{
printf("disconnect from server\r\n");
printf("%s(%d): disconnect from server\r\n",
__FUNCTION__, __LINE__);
fflush(stdout);
}

View File

@ -173,6 +173,11 @@ bool http_aclient::handle_ws_ping(void)
}
return true;
case 0:
this->on_ws_frame_ping(*buff_);
if (buff_->empty()) {
return true;
}
// 异步发送 pong 数据
res = ws_out_->send_frame_pong(*conn_,
(void*) buff_->c_str(), buff_->size());
@ -200,6 +205,7 @@ bool http_aclient::handle_ws_pong(void)
}
return true;
case 0:
this->on_ws_frame_pong(*buff_);
buff_->clear();
return true;
default:
@ -582,15 +588,23 @@ void http_aclient::send_request(const void* body, size_t len)
http_res_hdr_cllback, this, rw_timeout_);
}
void http_aclient::ws_handshake(void)
void http_aclient::ws_handshake(const char* key)
{
acl_assert(key && *key);
return ws_handshake(key, strlen(key));
}
void http_aclient::ws_handshake(const void* key, size_t len)
{
acl_assert(key && len > 0);
acl_assert(stream_ == NULL);
ACL_VSTREAM* vs = conn_->get_vstream();
stream_ = new socket_stream;
(void) stream_->open(vs);
http_header& hdr = request_header();
hdr.set_ws_key("123456789")
hdr.set_ws_key(key, len)
.set_ws_version(13)
.set_upgrade("websocket")
.set_keep_alive(true);

View File

@ -783,12 +783,24 @@ http_header& http_header::set_ws_origin(const char* url)
return *this;
}
http_header& http_header::set_ws_key(const void* key, size_t len)
{
if (key && len > 0) {
string buf;
buf.base64_encode(key, len);
ws_sec_key_ = dbuf_->dbuf_strdup(buf.c_str());
}
return *this;
}
http_header& http_header::set_ws_key(const char* key)
{
if (key && *key) {
ws_sec_key_ = dbuf_->dbuf_strdup(key);
return set_ws_key(key, strlen(key));
} else {
return *this;
}
return *this;
}
http_header& http_header::set_ws_protocol(const char* proto)