From cdb0ed6d1a9d6c12d74f83a1a1ab2424d2771081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20=C5=A0epental?= Date: Mon, 6 Feb 2023 15:28:38 +0100 Subject: [PATCH 1/2] Handling SSL errors in TlsSocket --- System/src/Net/Sockets/TlsSocket.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/System/src/Net/Sockets/TlsSocket.cpp b/System/src/Net/Sockets/TlsSocket.cpp index 086a0ba..0673baa 100644 --- a/System/src/Net/Sockets/TlsSocket.cpp +++ b/System/src/Net/Sockets/TlsSocket.cpp @@ -1,5 +1,5 @@ #include - +#include #include namespace System @@ -47,6 +47,15 @@ namespace System { throw SocketException(::WSAGetLastError(), "socket operation failed"); } + else if (err == SSL_ERROR_SSL) + { + // TODO Fill exception with appropriate info + char buffer[120]; + + ERR_error_string(ERR_get_error(), buffer); + + throw SocketException(::WSAGetLastError(), std::string(buffer)); + } else { // TODO throw unhandled From 44aefff4b6d9303f38b2ab67886742c7624afe5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20=C5=A0epental?= Date: Mon, 6 Feb 2023 15:30:00 +0100 Subject: [PATCH 2/2] Improved HTTP headers handling in WebSocket class --- System/src/Net/WebSockets/WebSocket.cpp | 27 ++++++++++++++----------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/System/src/Net/WebSockets/WebSocket.cpp b/System/src/Net/WebSockets/WebSocket.cpp index 69eed4d..d9134cd 100644 --- a/System/src/Net/WebSockets/WebSocket.cpp +++ b/System/src/Net/WebSockets/WebSocket.cpp @@ -5,6 +5,9 @@ #include #include + +#include +#include #include #include @@ -42,21 +45,21 @@ namespace System const auto& secWebSocketKey = WebSocket::GenerateSecWebsocketKey(); - // TODO Create using http headers class - std::stringstream ss; + // TODO Pass path as argument! + HttpRequest request(HttpMethod::Get, "/client"); - ss - << "GET /chat HTTP/1.1\r\n" - << "Host: " << host << ":" << port << "\r\n" - << "Connection: Upgrade\r\n" - << "Upgrade: websocket\r\n" - << "Sec-WebSocket-Version: 13\r\n" - << "Sec-WebSocket-Key: " << secWebSocketKey << "\r\n" - << "\r\n"; + request.Headers() + .Add("Host", host + ":" + std::to_string(port)) + .Add("Connection", "Upgrade") + .Add("Upgrade", "websocket") + .Add("Sec-WebSocket-Version", "13") + .Add("Sec-WebSocket-Key", secWebSocketKey) + .Add("Xclbr-id", "1234567890") + .Add("Xclbr-type", "client"); - const auto& request = ss.str(); + auto requestData = request.ToString(); - m_socket->Write(request, timeout, terminateEvent); + m_socket->Write(requestData, timeout, terminateEvent); // TODO Receive full http header until \r\n const auto& response = m_socket->Read();