mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-30 02:47:56 +08:00
ws key should be base64 encoded in http_header::set_ws_key()
This commit is contained in:
parent
c55d1badc7
commit
6eba8bbafd
@ -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 支持值为空的场景
|
||||
|
||||
|
@ -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 数据
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user