Debug websocket frame parsing....something is wrong

This commit is contained in:
antao 2018-08-22 19:20:34 +08:00
parent 0e95e10c2b
commit 414e9b5fa6
3 changed files with 1035 additions and 26 deletions

File diff suppressed because it is too large Load Diff

View File

@ -438,7 +438,13 @@ std::string parseWebsockFrame(trantor::MsgBuffer *buffer)
if(buffer->readableBytes()>=2)
{
auto secondByte=(*buffer)[1];
int length=secondByte & 127;
unsigned int length=secondByte & 127;
int isMasked=(secondByte & 0x80);
if(isMasked!=0)
{
LOG_TRACE<<"data encoded!";
} else
LOG_TRACE<<"plain data";
int indexFirstMask = 2;
if (length == 126)
@ -453,22 +459,23 @@ std::string parseWebsockFrame(trantor::MsgBuffer *buffer)
{
if(indexFirstMask==4)
{
length=(*buffer)[2];
length=(length<<8)+(*buffer)[3];
length=(unsigned int)(*buffer)[2];
length=(length<<8)+(unsigned int)(*buffer)[3];
} else if(indexFirstMask==10)
{
length=(*buffer)[2];
length=(length<<8)+(*buffer)[3];
length=(length<<8)+(*buffer)[4];
length=(length<<8)+(*buffer)[5];
length=(length<<8)+(*buffer)[6];
length=(length<<8)+(*buffer)[7];
length=(length<<8)+(*buffer)[8];
length=(length<<8)+(*buffer)[9];
length=(unsigned int)(*buffer)[2];
length=(length<<8)+(unsigned int)(*buffer)[3];
length=(length<<8)+(unsigned int)(*buffer)[4];
length=(length<<8)+(unsigned int)(*buffer)[5];
length=(length<<8)+(unsigned int)(*buffer)[6];
length=(length<<8)+(unsigned int)(*buffer)[7];
length=(length<<8)+(unsigned int)(*buffer)[8];
length=(length<<8)+(unsigned int)(*buffer)[9];
} else{
assert(0);
}
}
LOG_TRACE<<"websocket message len="<<length;
if(buffer->readableBytes()>=(indexFirstMask+4+length))
{
auto masks=buffer->peek()+indexFirstMask;
@ -478,9 +485,10 @@ std::string parseWebsockFrame(trantor::MsgBuffer *buffer)
message.resize(length);
for(int i=0;i<length;i++)
{
message[i]=rawData[i]^masks[i%4];
message[i]=(rawData[i]^masks[i%4]);
}
buffer->retrieve(indexFirstMask+4+length);
LOG_TRACE<<"got message len="<<message.length();
return message;
}
}

View File

@ -11,6 +11,7 @@ WebSocketConnectionImpl::WebSocketConnectionImpl(const trantor::TcpConnectionPtr
}
void WebSocketConnectionImpl::send(const char *msg,uint64_t len)
{
LOG_TRACE<<"send "<<len<<" bytes";
auto conn=_tcpConn.lock();
if(conn)
{
@ -30,29 +31,30 @@ void WebSocketConnectionImpl::send(const char *msg,uint64_t len)
else if(len>= 126 && len <= 65535)
{
bytesFormatted[1] = 126;
bytesFormatted[2] = ( len >> 8 ) & 255;
bytesFormatted[3] = ( len ) & 255;
bytesFormatted[2] = (( len >> 8 ) & 255);
bytesFormatted[3] = (( len ) & 255);
indexStartRawData = 4;
}
else
{
bytesFormatted[1] = 127;
bytesFormatted[2] = ( len >> 56 ) & 255;
bytesFormatted[3] = ( len >> 48 ) & 255;
bytesFormatted[4] = ( len >> 40 ) & 255;
bytesFormatted[5] = ( len >> 32 ) & 255;
bytesFormatted[6] = ( len >> 24 ) & 255;
bytesFormatted[7] = ( len >> 16 ) & 255;
bytesFormatted[8] = ( len >> 8 ) & 255;
bytesFormatted[9] = ( len ) & 255;
bytesFormatted[2] = (( len >> 56 ) & 255);
bytesFormatted[3] = (( len >> 48 ) & 255);
bytesFormatted[4] = (( len >> 40 ) & 255);
bytesFormatted[5] = (( len >> 32 ) & 255);
bytesFormatted[6] = (( len >> 24 ) & 255);
bytesFormatted[7] = (( len >> 16 ) & 255);
bytesFormatted[8] = (( len >> 8 ) & 255);
bytesFormatted[9] = (( len ) & 255);
indexStartRawData = 10;
}
bytesFormatted.resize(indexStartRawData);
LOG_TRACE<<"fheadlen="<<bytesFormatted.length();
bytesFormatted.append(msg,len);
LOG_TRACE<<"send formatted frame len="<<bytesFormatted.length();
LOG_TRACE<<"send formatted frame len="<<len<<" flen="<<bytesFormatted.length();
conn->send(bytesFormatted);
}
}