diff --git a/a8/websocketclient.cc b/a8/websocketclient.cc index febe053..287bb2a 100644 --- a/a8/websocketclient.cc +++ b/a8/websocketclient.cc @@ -30,11 +30,18 @@ static const unsigned char WEBSOCKET_PAYLOAD_LEN_UINT64 = 127; static const char* WEB_SOCKET_KEY = "Sec-WebSocket-Key: "; static const char* WEB_SOCKET_KEY2 = "Sec-Websocket-Key: "; +static const int DEFAULT_MAX_PACKET_LEN = 1024 * 10; +static const int DEFAULT_MAX_RECV_BUFFERSIZE = 1024 * 64; + namespace a8 { WebSocketClient::WebSocketClient(asio::io_context& io_context, const std::string& remote_ip, int remote_port) { + max_packet_len_ = DEFAULT_MAX_PACKET_LEN; + recv_buff_ = (char *)malloc(max_packet_len_ + 1); + recv_bufflen_ = 0; + tcp_client_ = std::make_shared(io_context, remote_ip, remote_port); decoded_buff_ = (char *)malloc(1024 * 64 + 1); decoded_bufflen_ = 0; @@ -97,6 +104,10 @@ namespace a8 WebSocketClient::~WebSocketClient() { + recv_bufflen_ = 0; + free(recv_buff_); + recv_buff_ = nullptr; + free(decoded_buff_); decoded_buff_ = nullptr; decoded_bufflen_ = 0; @@ -150,11 +161,7 @@ namespace a8 if (!pend) { return; } - if (strstr(buf + offset, WEB_SOCKET_KEY) || strstr(buf + offset, WEB_SOCKET_KEY2)) { - ProcessWsHandShake(buf, offset, buflen); - } else { - abort(); - } + ProcessWsHandShake(buf, offset, buflen); } void WebSocketClient::ProcessWsHandShake(char* buf, int& offset, unsigned int buflen) @@ -163,41 +170,7 @@ namespace a8 if (!pend) { return; } - if (strncmp(buf + offset, "GET ", strlen("GET ")) == 0) { - std::string url; - std::string querystr; - std::string location; - - a8::ParserQueryStr(buf + offset + 4, url, querystr); - } - std::string server_key; - { - char* p1 = strstr(buf + offset, WEB_SOCKET_KEY); - if (!p1) { - p1 = strstr(buf + offset, WEB_SOCKET_KEY2); - } - if (p1) { - p1 += strlen(WEB_SOCKET_KEY); - char* p2 = strstr(p1 , "\r\n"); - if (p2) { - server_key.append(p1, p2-p1); - } - } - } - #if 0 - server_key += "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; - std::string sha1_data = a8::openssl::Sha1Encode(server_key); - std::string hash_data; - a8::openssl::Base64Encode(sha1_data, hash_data); - - std::string response = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" - "Upgrade: websocket\r\n" - "Connection: Upgrade\r\n" - "Sec-WebSocket-Accept: " + hash_data + "\r\n" + - "\r\n"; - SendBuff(response.data(), response.size()); handshook_ = true; - #endif offset += pend - buf - offset + strlen("\r\n\r\n"); }