set_frame_masking_key() should be called in http_aclient::ws_handshake() in client mode.

This commit is contained in:
zhengshuxin 2019-09-17 16:51:43 +08:00
parent 99c79c5254
commit 4ac0748114
6 changed files with 38 additions and 39 deletions

View File

@ -1,5 +1,9 @@
修改历史列表:
590) 2019.9.17
590.1) bugfix: 在客户端模式下,需要在 http_aclient::ws_handshake() 中需要调用
set_frame_masking_key() 设置客户端发送数据的掩码
589) 2019.9.16
589.1) bugfix: aio_stream 类中 get_peer() / get_local() 返回有误
589.2) bugfix: redis_pubsub::get_message() 添加读超时时间

View File

@ -103,6 +103,7 @@ public:
*/
virtual void on_connected(const check_client&, double cost)
{
(void) cost;
}
/**

View File

@ -124,7 +124,7 @@ public:
websocket& set_frame_payload_len(unsigned long long len);
/**
*
*
* @param mask {unsigned int}
* @return {websocket&}
*/

View File

@ -9,15 +9,13 @@ static bool handshake(acl::socket_stream& conn)
.set_upgrade("websocket")
.set_keep_alive(true);
if (req.request(NULL, 0) == false)
{
if (!req.request(NULL, 0)) {
printf("request error\r\n");
return false;
}
int status = req.http_status();
if (status != 101)
{
if (status != 101) {
printf("invalid http status: %d\r\n", status);
return false;
}
@ -28,15 +26,13 @@ static bool handshake(acl::socket_stream& conn)
static bool send_file(acl::websocket& ws, const char* filepath)
{
acl::ifstream in;
if (in.open_read(filepath) == false)
{
if (!in.open_read(filepath)) {
printf("open %s error %s\r\n", filepath, acl::last_serror());
return false;
}
long long size = in.fsize();
if (size <= 0)
{
if (size <= 0) {
printf("filename: %s, invalid size: %lld\r\n", filepath, size);
return false;
}
@ -44,12 +40,13 @@ static bool send_file(acl::websocket& ws, const char* filepath)
acl::string buf;
buf.basename(filepath);
unsigned mask = ~0;
ws.set_frame_fin(true)
.set_frame_opcode(acl::FRAME_TEXT)
.set_frame_payload_len(buf.size());
.set_frame_payload_len(buf.size())
.set_frame_masking_key(mask);
if (ws.send_frame_data(buf, buf.size()) == false)
{
if (!ws.send_frame_data(buf, buf.size())) {
printf("send filenam error %s\r\n", acl::last_serror());
return false;
}
@ -58,32 +55,31 @@ static bool send_file(acl::websocket& ws, const char* filepath)
ws.reset().set_frame_fin(true)
.set_frame_opcode(acl::FRAME_TEXT)
.set_frame_payload_len(buf.size());
if (ws.send_frame_data(buf, buf.size()) == false)
{
if (!ws.send_frame_data(buf, buf.size())) {
printf("send file size error %s\r\n", acl::last_serror());
return false;
}
long long total = 0;
char cbuf[128000];
while (!in.eof())
{
while (!in.eof()) {
int ret = in.read(cbuf, sizeof(cbuf), false);
if (ret == -1)
if (ret == -1) {
break;
}
printf(">>send %d\r\n", ret);
ws.reset().set_frame_fin(true)
.set_frame_opcode(acl::FRAME_BINARY)
.set_frame_payload_len(ret);
if (ws.send_frame_data(cbuf, ret) == false)
{
if (!ws.send_frame_data(cbuf, ret)) {
printf("send data error %s\r\n", acl::last_serror());
return false;
}
total += ret;
if (total % 10240000 == 0)
if (total % 10240000 == 0) {
sleep(1);
}
}
printf(">>total send: %lld\r\n", total);
@ -92,16 +88,14 @@ static bool send_file(acl::websocket& ws, const char* filepath)
static bool read_reply(acl::websocket& ws)
{
if (ws.read_frame_head() == false)
{
if (!ws.read_frame_head()) {
printf("read_frame_head error %s\r\n", acl::last_serror());
return false;
}
char cbuf[1024];
unsigned char opcode = ws.get_frame_opcode();
switch (opcode)
{
switch (opcode) {
case acl::FRAME_TEXT:
case acl::FRAME_BINARY:
break;
@ -111,8 +105,7 @@ static bool read_reply(acl::websocket& ws)
}
int ret = ws.read_frame_data(cbuf, sizeof(cbuf) - 1);
if (ret <= 0)
{
if (ret <= 0) {
printf("read_frame_data error\r\n");
return false;
}
@ -125,22 +118,24 @@ static bool read_reply(acl::websocket& ws)
static bool upload(const char* addr, const char* filepath)
{
acl::socket_stream conn;
if (conn.open(addr, 30, 30) == false)
{
if (!conn.open(addr, 30, 30)) {
printf("connect %s error %s\r\n", addr, acl::last_serror());
return false;
}
if (handshake(conn) == false)
if (!handshake(conn)) {
return false;
}
acl::websocket ws(conn);
if (send_file(ws, filepath) == false)
if (!send_file(ws, filepath)) {
return false;
}
if (read_reply(ws) == false)
if (!read_reply(ws)) {
return false;
}
return true;
}
@ -158,10 +153,8 @@ int main(int argc, char* argv[])
acl::string addr, filename;
while ((ch = getopt(argc, argv, "hf:s:")) > 0)
{
switch (ch)
{
while ((ch = getopt(argc, argv, "hf:s:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
@ -176,13 +169,11 @@ int main(int argc, char* argv[])
}
}
if (addr.empty() || filename.empty())
{
if (addr.empty() || filename.empty()) {
usage(argv[0]);
return 1;
}
upload(addr, filename);
return 0;
}

View File

@ -662,6 +662,9 @@ void http_aclient::ws_handshake(const void* key, size_t len)
ws_in_ = NEW websocket(*stream_);
ws_out_ = NEW websocket(*stream_);
unsigned mask = ~0;
ws_out_->set_frame_masking_key(mask);
status_ = HTTP_ACLIENT_STATUS_WS_HANDSHAKE;
send_request(NULL, 0);
}

View File

@ -105,7 +105,7 @@ websocket& websocket::set_frame_payload_len(unsigned long long len)
websocket& websocket::set_frame_masking_key(unsigned int mask)
{
header_.masking_key = mask;
header_.mask = true;
header_.mask = mask != 0 ? true : false;
return *this;
}