Skip to content

Commit 5289618

Browse files
committed
Fixed bug in HTTPS: interrupt receiving data. Code styling
1 parent f5f0cb9 commit 5289618

25 files changed

+319
-648
lines changed

src/SignalHandlers.cpp

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,35 @@ static HttpServer::Server *globalServerPtr = nullptr;
1717
/**
1818
* Terminate signal
1919
*/
20-
static void handlerSigTerm(const int) noexcept
21-
{
22-
if (globalServerPtr)
23-
{
20+
static void handlerSigTerm(const int) noexcept {
21+
if (globalServerPtr) {
2422
globalServerPtr->stop();
2523
}
2624
}
2725

2826
/**
2927
* Interrupt signal
3028
*/
31-
static void handlerSigInt(const int) noexcept
32-
{
33-
if (globalServerPtr)
34-
{
29+
static void handlerSigInt(const int) noexcept {
30+
if (globalServerPtr) {
3531
globalServerPtr->stop();
3632
}
3733
}
3834

3935
/**
4036
* Signal to restart
4137
*/
42-
static void handlerSigUsr1(const int) noexcept
43-
{
44-
if (globalServerPtr)
45-
{
38+
static void handlerSigUsr1(const int) noexcept {
39+
if (globalServerPtr) {
4640
globalServerPtr->restart();
4741
}
4842
}
4943

5044
/**
5145
* Signal to update modules
5246
*/
53-
static void handlerSigUsr2(const int) noexcept
54-
{
55-
if (globalServerPtr)
56-
{
47+
static void handlerSigUsr2(const int) noexcept {
48+
if (globalServerPtr) {
5749
globalServerPtr->update();
5850
}
5951
}
@@ -68,51 +60,44 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const
6860
{
6961
switch (message)
7062
{
71-
case SIGTERM:
72-
{
63+
case SIGTERM: {
7364
handlerSigTerm(message);
7465
::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0);
7566

7667
break;
7768
}
7869

79-
case SIGINT:
80-
{
70+
case SIGINT: {
8171
handlerSigInt(message);
8272
::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0);
8373

8474
break;
8575
}
8676

87-
case SIGUSR1:
88-
{
77+
case SIGUSR1: {
8978
handlerSigUsr1(message);
9079
break;
9180
}
9281

93-
case SIGUSR2:
94-
{
82+
case SIGUSR2: {
9583
handlerSigUsr2(message);
9684
break;
9785
}
9886

9987
// Cases WM_QUERYENDSESSION and WM_ENDSESSION run before shutting down the system (or ending user session)
100-
case WM_QUERYENDSESSION:
101-
{
88+
case WM_QUERYENDSESSION: {
10289
handlerSigTerm(message);
10390
break;
10491
}
10592

106-
case WM_ENDSESSION:
107-
{
93+
case WM_ENDSESSION: {
10894
const ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId);
10995
::WaitForSingleObject(hThread, INFINITE);
11096
::CloseHandle(hThread);
11197
break;
11298
}
11399

114-
default:
115-
{
100+
default: {
116101
return ::DefWindowProc(hWnd, message, wParam, lParam);
117102
}
118103
}
@@ -126,15 +111,13 @@ static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, Utils::Event * cons
126111

127112
eventWindowCreation->notify(); // After this action, eventWindowCreation will be destroyed (in the other thread)
128113

129-
if (0 == hWnd)
130-
{
114+
if (0 == hWnd) {
131115
return 0;
132116
}
133117

134118
::MSG msg;
135119

136-
while (::GetMessage(&msg, hWnd, 0, 0) )
137-
{
120+
while (::GetMessage(&msg, hWnd, 0, 0) ) {
138121
::TranslateMessage(&msg);
139122
::DispatchMessage(&msg);
140123
}
@@ -152,18 +135,18 @@ static ::BOOL consoleSignalHandler(const ::DWORD ctrlType) noexcept
152135
// @see my function WndProc -> cases WM_QUERYENDSESSION and WM_ENDSESSION. Only they happen in this program, because the library user32.dll is connected.
153136
// @prooflink: https://msdn.microsoft.com/library/windows/desktop/ms686016(v=vs.85).aspx
154137
case CTRL_LOGOFF_EVENT:
155-
case CTRL_SHUTDOWN_EVENT:
156-
{
138+
case CTRL_SHUTDOWN_EVENT: {
157139
handlerSigTerm(ctrlType);
158140
const ::HANDLE hThread = ::OpenThread(SYNCHRONIZE, false, gMainThreadId);
159141
::WaitForSingleObject(hThread, INFINITE);
160142
::CloseHandle(hThread);
161143
return true;
162144
}
163145

164-
case CTRL_C_EVENT:
146+
case CTRL_C_EVENT: {
165147
handlerSigInt(ctrlType);
166148
return true;
149+
}
167150

168151
default:
169152
return false;
@@ -194,8 +177,7 @@ bool bindSignalHandlers(HttpServer::Server *server) noexcept
194177
wcex.hInstance = hInstance;
195178
wcex.lpszClassName = myWndClassName;
196179

197-
if (0 == ::RegisterClassEx(&wcex) )
198-
{
180+
if (::RegisterClassEx(&wcex) == 0) {
199181
return false;
200182
}
201183

src/socket/Adapter.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,19 @@
33

44
namespace Socket
55
{
6-
long Adapter::nonblock_recv(std::vector<std::string::value_type> &buf, const std::chrono::milliseconds &timeout) const noexcept
7-
{
6+
long Adapter::nonblock_recv(std::vector<std::string::value_type> &buf, const std::chrono::milliseconds &timeout) const noexcept {
87
return this->nonblock_recv(buf.data(), buf.size(), timeout);
98
}
109

11-
long Adapter::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept
12-
{
10+
long Adapter::nonblock_send(const std::string &buf, const std::chrono::milliseconds &timeout) const noexcept {
1311
return this->nonblock_send(buf.data(), buf.length(), timeout);
1412
}
1513

16-
bool Adapter::operator ==(const Adapter &obj) const noexcept
17-
{
14+
bool Adapter::operator ==(const Adapter &obj) const noexcept {
1815
return this->get_handle() == obj.get_handle();
1916
}
2017

21-
bool Adapter::operator !=(const Adapter &obj) const noexcept
22-
{
18+
bool Adapter::operator !=(const Adapter &obj) const noexcept {
2319
return this->get_handle() != obj.get_handle();
2420
}
25-
};
21+
}

src/socket/Adapter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ namespace Socket
3030
bool operator ==(const Adapter &obj) const noexcept;
3131
bool operator !=(const Adapter &obj) const noexcept;
3232
};
33-
};
33+
}

src/socket/AdapterDefault.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,33 @@
33

44
namespace Socket
55
{
6-
AdapterDefault::AdapterDefault(const Socket &_sock) noexcept : sock(_sock)
7-
{
6+
AdapterDefault::AdapterDefault(const Socket &_sock) noexcept : sock(_sock) {}
87

9-
}
10-
11-
System::native_socket_type AdapterDefault::get_handle() const noexcept
12-
{
8+
System::native_socket_type AdapterDefault::get_handle() const noexcept {
139
return sock.get_handle();
1410
}
1511

16-
::gnutls_session_t AdapterDefault::get_tls_session() const noexcept
17-
{
12+
::gnutls_session_t AdapterDefault::get_tls_session() const noexcept {
1813
return 0;
1914
}
2015

21-
Adapter *AdapterDefault::copy() const noexcept
22-
{
16+
Adapter *AdapterDefault::copy() const noexcept {
2317
return new AdapterDefault(this->sock);
2418
}
2519

26-
long AdapterDefault::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept
27-
{
20+
long AdapterDefault::nonblock_recv(void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept {
2821
return sock.nonblock_recv(buf, length, timeout);
2922
}
3023

31-
long AdapterDefault::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept
32-
{
24+
long AdapterDefault::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept {
3325
return sock.nonblock_send(buf, length, timeout);
3426
}
3527

36-
void AdapterDefault::close() noexcept
37-
{
28+
void AdapterDefault::close() noexcept {
3829
// Wait for send all data to client
3930
sock.nonblock_send_sync();
4031

4132
sock.shutdown();
4233
sock.close();
4334
}
44-
};
35+
}

src/socket/AdapterDefault.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ namespace Socket
2323

2424
virtual void close() noexcept override;
2525
};
26-
};
26+
}

src/socket/AdapterTls.cpp

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,18 @@ namespace Socket
3333
::gnutls_alpn_set_protocols(this->session, protocols, sizeof(protocols) / sizeof(::gnutls_datum_t), 0);
3434
}
3535

36-
AdapterTls::AdapterTls(const ::gnutls_session_t session) noexcept : session(session)
37-
{
38-
39-
}
36+
AdapterTls::AdapterTls(const ::gnutls_session_t session) noexcept : session(session) {}
4037

4138
bool AdapterTls::handshake() noexcept
4239
{
4340
int ret;
4441

45-
do
46-
{
42+
do {
4743
ret = ::gnutls_handshake(this->session);
4844
}
4945
while (ret < 0 && ::gnutls_error_is_fatal(ret) == 0);
5046

51-
if (ret < 0)
52-
{
47+
if (ret < 0) {
5348
Socket sock(this->get_handle() );
5449

5550
sock.close();
@@ -66,8 +61,7 @@ namespace Socket
6661
// size_t record_size = ::gnutls_record_get_max_size(this->session);
6762
size_t record_size = length;
6863

69-
if (0 == record_size)
70-
{
64+
if (0 == record_size) {
7165
return -1;
7266
}
7367

@@ -77,25 +71,21 @@ namespace Socket
7771

7872
size_t total = 0;
7973

80-
while (total < length)
81-
{
82-
if (record_size > length - total)
83-
{
74+
while (total < length) {
75+
if (record_size > length - total) {
8476
record_size = length - total;
8577
}
8678

8779
// const long send_size = ::gnutls_record_send(this->session, reinterpret_cast<const uint8_t *>(buf) + total, record_size);
8880

8981
long send_size = 0;
9082

91-
do
92-
{
83+
do {
9384
sock.nonblock_send_sync();
9485
}
9586
while (GNUTLS_E_AGAIN == (send_size = ::gnutls_record_send(this->session, reinterpret_cast<const uint8_t *>(buf) + total, record_size) ) );
9687

97-
if (send_size < 0)
98-
{
88+
if (send_size < 0) {
9989
return send_size;
10090
}
10191

@@ -105,18 +95,15 @@ namespace Socket
10595
return static_cast<long>(total);
10696
}
10797

108-
System::native_socket_type AdapterTls::get_handle() const noexcept
109-
{
98+
System::native_socket_type AdapterTls::get_handle() const noexcept {
11099
return static_cast<System::native_socket_type>(::gnutls_transport_get_int(this->session) );
111100
}
112101

113-
::gnutls_session_t AdapterTls::get_tls_session() const noexcept
114-
{
102+
::gnutls_session_t AdapterTls::get_tls_session() const noexcept {
115103
return this->session;
116104
}
117105

118-
Adapter *AdapterTls::copy() const noexcept
119-
{
106+
Adapter *AdapterTls::copy() const noexcept {
120107
return new AdapterTls(this->session);
121108
}
122109

@@ -125,18 +112,23 @@ namespace Socket
125112
// ::gnutls_record_set_timeout(this->session, static_cast<const unsigned int>(timeout.count() ) );
126113

127114
Socket sock(this->get_handle() );
128-
sock.nonblock_recv_sync();
129115

130-
return ::gnutls_record_recv(this->session, buf, length);
116+
long result;
117+
118+
do {
119+
sock.nonblock_recv_sync();
120+
result = ::gnutls_record_recv(this->session, buf, length);
121+
}
122+
while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED);
123+
124+
return result;
131125
}
132126

133-
long AdapterTls::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept
134-
{
127+
long AdapterTls::nonblock_send(const void *buf, const size_t length, const std::chrono::milliseconds &timeout) const noexcept {
135128
return this->nonblock_send_all(buf, length, timeout);
136129
}
137130

138-
void AdapterTls::close() noexcept
139-
{
131+
void AdapterTls::close() noexcept {
140132
Socket sock(this->get_handle() );
141133

142134
// Wait for send all data to client
@@ -148,4 +140,4 @@ namespace Socket
148140

149141
::gnutls_deinit(this->session);
150142
}
151-
};
143+
}

src/socket/AdapterTls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ namespace Socket
2727

2828
virtual void close() noexcept override;
2929
};
30-
};
30+
}

0 commit comments

Comments
 (0)