Skip to content

Commit fa3d1e6

Browse files
committed
Updating internals
1 parent 940023a commit fa3d1e6

File tree

6 files changed

+61
-16
lines changed

6 files changed

+61
-16
lines changed

Test/main.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ void wssautobahntest() {
3939
});
4040

4141
listener.startlistening();
42-
43-
SL::WS_LITE::WSContext tlsctx(SL::WS_LITE::ThreadCount(1));
44-
42+
43+
SL::WS_LITE::WSContext tlsctx(SL::WS_LITE::ThreadCount(1));
44+
4545
auto tlslistener = tlsctx.CreateTLSListener(SL::WS_LITE::PortNumber(3001), TEST_CERTIFICATE_PRIVATE_PASSWORD, TEST_CERTIFICATE_PRIVATE_PATH, TEST_CERTIFICATE_PUBLIC_PATH, TEST_DH_PATH);
4646
tlslistener.set_ReadTimeout(std::chrono::seconds(100));
4747
tlslistener.set_WriteTimeout(std::chrono::seconds(100));
@@ -199,7 +199,7 @@ void multithreadtest() {
199199
});
200200
listener.onDisconnection([&](const SL::WS_LITE::WSocket& socket, unsigned short code, const std::string& msg) {
201201
lastheard = std::chrono::high_resolution_clock::now();
202-
});
202+
});
203203
listener.onMessage([&](const SL::WS_LITE::WSocket& socket, const SL::WS_LITE::WSMessage& message) {
204204
lastheard = std::chrono::high_resolution_clock::now();
205205
SL::WS_LITE::WSMessage msg;
@@ -239,7 +239,7 @@ void multithreadtest() {
239239
lastheard = std::chrono::high_resolution_clock::now();
240240
});
241241
clients[i].connect("localhost", port);
242-
242+
243243
}
244244

245245
while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - lastheard).count() < 2000) {
@@ -295,9 +295,9 @@ void multithreadthroughputtest() {
295295
msg.Buffer = std::shared_ptr<unsigned char>(new unsigned char[bufferesize], [&](unsigned char* p) {
296296
mbssent += bufferesize;
297297
if (mbssent == bufferesize * clients.capacity()) {
298-
std::cout << "Took " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()- sendtimer).count() << "ms to send " << bufferesize * clients.capacity() <<" bytes" << std::endl;
298+
std::cout << "Took " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - sendtimer).count() << "ms to send " << bufferesize * clients.capacity() << " bytes" << std::endl;
299299
}
300-
delete[] p;
300+
delete[] p;
301301
});
302302
msg.len = bufferesize;//10MB
303303
msg.code = SL::WS_LITE::OpCode::BINARY;
@@ -316,7 +316,7 @@ void multithreadthroughputtest() {
316316
while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - lastheard).count() < 5000) {
317317
std::this_thread::sleep_for(200ms);
318318
}
319-
std::cout << "Received " << mbsreceived<<" bytes"<< std::endl;
319+
std::cout << "Received " << mbsreceived << " bytes" << std::endl;
320320
}
321321
int main(int argc, char* argv[]) {
322322
wssautobahntest();

include/internal/DataStructures.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
namespace SL {
2222
namespace WS_LITE {
23-
2423
struct HandshakeContainer {
2524
asio::streambuf Read;
2625
asio::streambuf Write;

include/internal/Utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ namespace SL {
232232
}
233233
std::string url_decode(const std::string& in);
234234

235-
bool Parse_Handshake(std::string defaultheaderversion, std::istream& stream, std::unordered_map<std::string, std::string>& header);
235+
bool Parse_ClientHandshake(std::istream& stream, std::unordered_map<std::string, std::string>& header);
236+
bool Parse_ServerHandshake(std::istream& stream, std::unordered_map<std::string, std::string>& header);
236237

237238
const std::string ws_magic_string = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
238239
const int LARGE_BUFFER_SIZE = 300 * 1024;

src/internal/ClientImpl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ namespace SL {
5959
if (!ec) {
6060
SL_WS_LITE_LOG(Logging_Levels::INFO_log_level, "Read Handshake bytes " << bytes_transferred);
6161
std::istream stream(read_buffer.get());
62+
6263
std::unordered_map<std::string, std::string> header;
63-
Parse_Handshake("1.1", stream, header);
64-
if (Base64decode(header[HTTP_SECWEBSOCKETACCEPT]) == accept_sha1) {
64+
if (Parse_ServerHandshake(stream, header) && Base64decode(header[HTTP_SECWEBSOCKETACCEPT]) == accept_sha1) {
6565

6666

6767
SL_WS_LITE_LOG(Logging_Levels::INFO_log_level, "Connected ");

src/internal/ListenerImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace SL {
1414

1515
std::istream headerdata(&handshakecontainer->Read);
1616

17-
if (!Parse_Handshake("1.1", headerdata, handshakecontainer->Header)) {
17+
if (!Parse_ClientHandshake(headerdata, handshakecontainer->Header)) {
1818
SL_WS_LITE_LOG(Logging_Levels::INFO_log_level, "Parse Handshake failed ");
1919
return;
2020
}

src/internal/Utils.cpp

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "internal/Utils.h"
2+
#include "Logging.h"
23

34
#include <random>
45
#include <fstream>
@@ -52,7 +53,46 @@ namespace SL {
5253
}
5354
return out;
5455
}
55-
bool Parse_Handshake(std::string defaultheaderversion, std::istream& stream, std::unordered_map<std::string, std::string>& header)
56+
57+
bool Parse_ServerHandshake(std::istream& stream, std::unordered_map<std::string, std::string>& header)
58+
{
59+
std::string line;
60+
std::getline(stream, line);
61+
62+
size_t path_end;
63+
if ((path_end = line.find(' ')) != std::string::npos) {
64+
65+
if (path_end == 8)
66+
header[HTTP_VERSION] = line.substr(path_end - 3, 3);
67+
else
68+
{
69+
SL_WS_LITE_LOG(Logging_Levels::INFO_log_level, "Parse_ServerHandshake failed due to no http version in header");
70+
return false;
71+
}
72+
if (line.size() > path_end + 4) {
73+
header[HTTP_METHOD] = line.substr(path_end + 1, line.size() - path_end -1);
74+
}
75+
76+
getline(stream, line);
77+
size_t param_end;
78+
while ((param_end = line.find(':')) != std::string::npos) {
79+
size_t value_start = param_end + 1;
80+
if ((value_start) < line.size()) {
81+
if (line[value_start] == ' ')
82+
value_start++;
83+
if (value_start < line.size())
84+
header.insert(std::make_pair(line.substr(0, param_end), line.substr(value_start, line.size() - value_start - 1)));
85+
}
86+
87+
getline(stream, line);
88+
}
89+
90+
}
91+
return true;
92+
}
93+
94+
95+
bool Parse_ClientHandshake(std::istream& stream, std::unordered_map<std::string, std::string>& header)
5696
{
5797
std::string line;
5898
std::getline(stream, line);
@@ -64,8 +104,10 @@ namespace SL {
64104
header[HTTP_PATH] = url_decode(line.substr(method_end + 1, path_end - method_end - 1));
65105
if ((path_end + 6) < line.size())
66106
header[HTTP_VERSION] = line.substr(path_end + 6, line.size() - (path_end + 6) - 1);
67-
else
68-
header[HTTP_VERSION] = defaultheaderversion;
107+
else {
108+
SL_WS_LITE_LOG(Logging_Levels::INFO_log_level, "Parse_ClientHandshake failed due to no http version in header");
109+
return false;
110+
}
69111

70112
getline(stream, line);
71113
size_t param_end;
@@ -99,6 +141,9 @@ namespace SL {
99141
if (header_it != header.end() && header_it->second.find(PERMESSAGEDEFLATE) != std::string::npos) {
100142
handshake << HTTP_SECWEBSOCKETEXTENSIONS << HTTP_KEYVALUEDELIM << PERMESSAGEDEFLATE << HTTP_ENDLINE;
101143
}
144+
else {
145+
handshake << HTTP_SECWEBSOCKETEXTENSIONS << HTTP_KEYVALUEDELIM << HTTP_ENDLINE;
146+
}
102147
handshake << HTTP_SECWEBSOCKETACCEPT << HTTP_KEYVALUEDELIM << Base64encode(sha1) << HTTP_ENDLINE << HTTP_ENDLINE;
103148

104149
return true;

0 commit comments

Comments
 (0)