Skip to content

Commit 913be66

Browse files
committed
Optimized some copy operations.
Fixed many warnings in code. Changed code style.
1 parent 0fb04b1 commit 913be66

38 files changed

+1366
-538
lines changed

src/Init.cpp

Lines changed: 95 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@
1313
#include <locale>
1414
#include <codecvt>
1515

16-
Socket::Adapter *createSocketAdapter(Transfer::app_request *request, void *addr)
17-
{
16+
Socket::Adapter *createSocketAdapter(
17+
Transfer::app_request *request,
18+
void *addr
19+
) {
1820
if (request->tls_session) {
1921
return new (addr) Socket::AdapterTls(request->tls_session);
2022
}
2123

2224
return new (addr) Socket::AdapterDefault(request->socket);
2325
}
2426

25-
void destroySocketAdapter(Socket::Adapter *adapter)
26-
{
27+
void destroySocketAdapter(Socket::Adapter *adapter) {
2728
if (adapter) {
2829
adapter->~Adapter();
2930
}
@@ -37,7 +38,13 @@ std::string utf8ToLocal(const std::string &u8str)
3738
std::wstring wstr = conv.from_bytes(u8str);
3839

3940
std::string str(wstr.size(), 0);
40-
std::use_facet<std::ctype<wchar_t> >(loc).narrow(wstr.data(), wstr.data() + wstr.size(), '?', &str.front() );
41+
42+
std::use_facet<std::ctype<wchar_t> >(loc).narrow(
43+
wstr.data(),
44+
wstr.data() + wstr.size(),
45+
'?',
46+
&str.front()
47+
);
4148

4249
return str;
4350
}
@@ -46,7 +53,11 @@ std::string getClearPath(const std::string &path)
4653
{
4754
const size_t pos = path.find_first_of("?#");
4855

49-
const std::string clean = Utils::urlDecode(std::string::npos == pos ? path : path.substr(0, pos) );
56+
const std::string clean = Utils::urlDecode(
57+
std::string::npos == pos
58+
? path
59+
: path.substr(0, pos)
60+
);
5061

5162
#ifdef WIN32
5263
return utf8ToLocal(clean);
@@ -55,8 +66,10 @@ std::string getClearPath(const std::string &path)
5566
#endif
5667
}
5768

58-
static void getIncomingVars(std::unordered_multimap<std::string, std::string> &params, const std::string &uri)
59-
{
69+
static void getIncomingVars(
70+
std::unordered_multimap<std::string, std::string> &params,
71+
const std::string &uri
72+
) {
6073
const size_t start = uri.find('?');
6174

6275
if (std::string::npos == start) {
@@ -69,8 +82,11 @@ static void getIncomingVars(std::unordered_multimap<std::string, std::string> &p
6982
return;
7083
}
7184

72-
for (size_t var_pos = start + 1, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1)
73-
{
85+
for (
86+
size_t var_pos = start + 1, var_end = 0;
87+
std::string::npos != var_end;
88+
var_pos = var_end + 1
89+
) {
7490
var_end = uri.find('&', var_pos);
7591

7692
if (var_end > finish) {
@@ -79,27 +95,53 @@ static void getIncomingVars(std::unordered_multimap<std::string, std::string> &p
7995

8096
size_t delimiter = uri.find('=', var_pos);
8197

82-
if (delimiter >= var_end)
83-
{
84-
std::string var_name = Utils::urlDecode(uri.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos) );
85-
86-
params.emplace(std::move(var_name), std::string() );
87-
}
88-
else
89-
{
90-
std::string var_name = Utils::urlDecode(uri.substr(var_pos, delimiter - var_pos) );
98+
if (delimiter >= var_end) {
99+
std::string var_name = Utils::urlDecode(
100+
uri.substr(
101+
var_pos,
102+
std::string::npos != var_end
103+
? var_end - var_pos
104+
: std::string::npos
105+
)
106+
);
107+
108+
params.emplace(
109+
std::move(var_name),
110+
std::string()
111+
);
112+
} else {
113+
std::string var_name = Utils::urlDecode(
114+
uri.substr(
115+
var_pos,
116+
delimiter - var_pos
117+
)
118+
);
91119

92120
++delimiter;
93121

94-
std::string var_value = Utils::urlDecode(uri.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos) );
95-
96-
params.emplace(std::move(var_name), std::move(var_value) );
122+
std::string var_value = Utils::urlDecode(
123+
uri.substr(
124+
delimiter,
125+
std::string::npos != var_end
126+
? var_end - delimiter
127+
: std::string::npos
128+
)
129+
);
130+
131+
params.emplace(
132+
std::move(var_name),
133+
std::move(var_value)
134+
);
97135
}
98136
}
99137
}
100138

101-
bool initServerObjects(HttpServer::Request *procRequest, HttpServer::Response *procResponse, const Transfer::app_request *request, Socket::Adapter *socket_adapter)
102-
{
139+
bool initServerObjects(
140+
HttpServer::Request *procRequest,
141+
HttpServer::Response *procResponse,
142+
const Transfer::app_request *request,
143+
Socket::Adapter *socket_adapter
144+
) {
103145
const uint8_t *src = reinterpret_cast<const uint8_t *>(request->request_data);
104146

105147
size_t protocol_number;
@@ -151,26 +193,25 @@ bool initServerObjects(HttpServer::Request *procRequest, HttpServer::Response *p
151193
src = Utils::unpackString(path, src);
152194
src = Utils::unpackString(method, src);
153195

154-
size_t stream_id;
196+
size_t number;
155197

156-
src = Utils::unpackNumber(&stream_id, src);
198+
src = Utils::unpackNumber(&number, src);
199+
const uint32_t stream_id = static_cast<uint32_t>(number);
157200

158201
Http2::ConnectionSettings settings;
159202

160-
size_t number;
161-
162203
src = Utils::unpackNumber(&number, src);
163-
settings.header_table_size = number;
204+
settings.header_table_size = static_cast<uint32_t>(number);
164205
src = Utils::unpackNumber(&number, src);
165-
settings.enable_push = number;
206+
settings.enable_push = static_cast<uint32_t>(number);
166207
src = Utils::unpackNumber(&number, src);
167-
settings.max_concurrent_streams = number;
208+
settings.max_concurrent_streams = static_cast<uint32_t>(number);
168209
src = Utils::unpackNumber(&number, src);
169-
settings.initial_window_size = number;
210+
settings.initial_window_size = static_cast<uint32_t>(number);
170211
src = Utils::unpackNumber(&number, src);
171-
settings.max_frame_size = number;
212+
settings.max_frame_size = static_cast<uint32_t>(number);
172213
src = Utils::unpackNumber(&number, src);
173-
settings.max_header_list_size = number;
214+
settings.max_header_list_size = static_cast<uint32_t>(number);
174215

175216
std::deque<std::pair<std::string, std::string> > dynamic_table;
176217
src = Utils::unpackVector(dynamic_table, src);
@@ -190,7 +231,16 @@ bool initServerObjects(HttpServer::Request *procRequest, HttpServer::Response *p
190231

191232
getIncomingVars(params, path);
192233

193-
Http2::OutStream *stream = new Http2::OutStream(stream_id, settings, Http2::DynamicTable(settings.header_table_size, settings.max_header_list_size, std::move(dynamic_table) ), mtx);
234+
Http2::OutStream *stream = new Http2::OutStream(
235+
stream_id,
236+
settings,
237+
Http2::DynamicTable(
238+
settings.header_table_size,
239+
settings.max_header_list_size,
240+
std::move(dynamic_table)
241+
),
242+
mtx
243+
);
194244

195245
prot = new HttpServer::ServerHttp2(socket_adapter, stream);
196246

@@ -227,17 +277,18 @@ bool initServerObjects(HttpServer::Request *procRequest, HttpServer::Response *p
227277
return success;
228278
}
229279

230-
void freeProtocolData(HttpServer::Response *response)
231-
{
280+
void freeProtocolData(HttpServer::Response *response) {
232281
if (response) {
233282
delete response->prot;
234283
}
235284
}
236285

237-
bool isSwitchingProtocols(const HttpServer::Request &request, HttpServer::Response &response)
238-
{
286+
bool isSwitchingProtocols(
287+
const HttpServer::Request &request,
288+
HttpServer::Response &response
289+
) {
239290
// Check for https is not set
240-
if (request.prot->getSocket()->get_tls_session() != 0) {
291+
if (request.prot->getSocket()->get_tls_session() != nullptr) {
241292
return false;
242293
}
243294

@@ -305,7 +356,10 @@ bool isSwitchingProtocols(const HttpServer::Request &request, HttpServer::Respon
305356

306357
const std::string headers = "HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: h2c\r\n\r\n";
307358

308-
response.prot->getSocket()->nonblock_send(headers, std::chrono::milliseconds(5000) );
359+
response.prot->getSocket()->nonblock_send(
360+
headers,
361+
std::chrono::milliseconds(5000)
362+
);
309363

310364
return true;
311365
}

src/Init.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,25 @@
55
#include "transfer/AppRequest.h"
66
#include "transfer/AppResponse.h"
77

8-
Socket::Adapter *createSocketAdapter(Transfer::app_request *request, void *addr);
8+
Socket::Adapter *createSocketAdapter(
9+
Transfer::app_request *request,
10+
void *addr
11+
);
12+
913
void destroySocketAdapter(Socket::Adapter *adapter);
1014

1115
std::string getClearPath(const std::string &path);
1216

13-
bool initServerObjects(HttpServer::Request *procRequest, HttpServer::Response *procResponse, const Transfer::app_request *request, Socket::Adapter *socket_adapter);
17+
bool initServerObjects(
18+
HttpServer::Request *procRequest,
19+
HttpServer::Response *procResponse,
20+
const Transfer::app_request *request,
21+
Socket::Adapter *socket_adapter
22+
);
23+
1424
void freeProtocolData(HttpServer::Response *response);
1525

16-
bool isSwitchingProtocols(const HttpServer::Request &request, HttpServer::Response &response);
26+
bool isSwitchingProtocols(
27+
const HttpServer::Request &request,
28+
HttpServer::Response &response
29+
);

src/Main.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ EXPORT bool application_init(const char *root)
1111
return true;
1212
}
1313

14-
EXPORT int application_call(Transfer::app_request *request, Transfer::app_response *response)
15-
{
14+
EXPORT int application_call(
15+
Transfer::app_request *request,
16+
Transfer::app_response *response
17+
) {
1618
// Allocate memory on the stack
1719
uint8_t addr[sizeof(Socket::AdapterTls)];
1820

@@ -30,22 +32,20 @@ EXPORT int application_call(Transfer::app_request *request, Transfer::app_respon
3032

3133
int result = EXIT_SUCCESS;
3234

33-
if (isSwitchingProtocols(proc_request, proc_response) )
34-
{
35+
if (isSwitchingProtocols(proc_request, proc_response) ) {
3536

36-
}
37-
else if (std::string::npos == absolute_path.find("/../") && System::isFileExists(absolute_path) )
38-
{
37+
} else if (
38+
std::string::npos == absolute_path.find("/../") &&
39+
System::isFileExists(absolute_path)
40+
) {
3941
auto it_connection = proc_request.headers.find("connection");
4042

4143
if (proc_request.headers.cend() != it_connection) {
4244
proc_response.headers["connection"] = it_connection->second;
4345
}
4446

4547
proc_response.headers["x-sendfile"] = absolute_path;
46-
}
47-
else
48-
{
48+
} else {
4949
// Call application
5050
result = Application::test(proc_request, proc_response);
5151
}
@@ -55,7 +55,11 @@ EXPORT int application_call(Transfer::app_request *request, Transfer::app_respon
5555
if (proc_response.headers.size() ) {
5656
response->data_size = Utils::getPackContainerSize(proc_response.headers);
5757
response->response_data = new uint8_t[response->data_size];
58-
Utils::packContainer(reinterpret_cast<uint8_t *>(response->response_data), proc_response.headers);
58+
59+
Utils::packContainer(
60+
reinterpret_cast<uint8_t *>(response->response_data),
61+
proc_response.headers
62+
);
5963
}
6064

6165
freeProtocolData(&proc_response);

src/application/Test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
namespace Application
77
{
8-
bool test(HttpServer::Request &request, HttpServer::Response &response)
9-
{
8+
bool test(
9+
HttpServer::Request &request,
10+
HttpServer::Response &response
11+
) {
1012
// Output incoming headers
1113

1214
std::string s = R"(<table>

src/application/Test.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55

66
namespace Application
77
{
8-
bool test(HttpServer::Request &, HttpServer::Response &);
8+
bool test(
9+
HttpServer::Request &,
10+
HttpServer::Response &
11+
);
912
}

0 commit comments

Comments
 (0)