Skip to content

Commit

Permalink
Merge pull request #18 from m4x1m1l14n/improv/websocket
Browse files Browse the repository at this point in the history
WebSocket HTTP request constructed using HttpRequest class
  • Loading branch information
m4x1m1l14n authored Jul 19, 2023
2 parents 005abca + 44aefff commit 652228a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
11 changes: 10 additions & 1 deletion System/src/Net/Sockets/TlsSocket.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <System/Net/Sockets/TlsSocket.hpp>

#include <openssl/err.h>
#include <assert.h>

namespace System
Expand Down Expand Up @@ -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
Expand Down
27 changes: 15 additions & 12 deletions System/src/Net/WebSockets/WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

#include <System/Net/WebSockets/WebSocketException.hpp>
#include <System/Net/WebSockets/WebSocketOpCode.hpp>

#include <System/Net/Http/HttpRequest.hpp>
#include <System/Net/Http/HttpResponse.hpp>
#include <System/Net/Http/HttpHeaders.hpp>

#include <Crypto/Random.hpp>
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 652228a

Please sign in to comment.