Skip to content

Commit a976f7a

Browse files
committed
Minor fixes
1 parent 85189f7 commit a976f7a

File tree

9 files changed

+59
-63
lines changed

9 files changed

+59
-63
lines changed

projects/qt-creator/httpserver.pro.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 3.5.1, 2015-12-22T18:57:36. -->
3+
<!-- Written by QtCreator 3.5.1, 2016-01-09T00:56:47. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>

src/ConfigParser.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ namespace HttpServer
4242

4343
if (file_size)
4444
{
45-
std::vector<std::string::value_type> buf(file_size);
46-
file.read(reinterpret_cast<char *>(buf.data() ), file_size);
45+
std::vector<char> buf(file_size);
46+
file.read(buf.data(), file_size);
4747

4848
strBuf.insert(strBuf.begin() + offset, buf.cbegin(), buf.cend() );
4949
}
@@ -299,9 +299,9 @@ namespace HttpServer
299299

300300
const std::string whitespace(" \t\v\f\r");
301301

302-
std::vector<std::string::value_type> buf(file_size);
302+
std::vector<char> buf(file_size);
303303

304-
file.read(reinterpret_cast<char *>(buf.data() ), file_size);
304+
file.read(buf.data(), file_size);
305305

306306
const std::string str_buf(buf.cbegin(), buf.cend() );
307307

src/DataVariantMultipartFormData.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace HttpServer
1515
(
1616
const Socket &sock,
1717
const std::chrono::milliseconds &timeout,
18-
std::vector<std::string::value_type> &buf,
18+
std::vector<char> &buf,
1919
std::string &str_buf,
2020
const std::string &data_end,
2121
const size_t &leftBytes,
@@ -86,7 +86,7 @@ namespace HttpServer
8686
const size_t buf_len = (leftBytes >= 512 * 1024) ? 512 * 1024 : leftBytes;
8787

8888
// Создание буферов
89-
std::vector<std::string::value_type> buf(buf_len);
89+
std::vector<char> buf(buf_len);
9090

9191
size_t str_cur; // Текущая позиция в буфере
9292

@@ -275,7 +275,7 @@ namespace HttpServer
275275
if (file.is_open() )
276276
{
277277
// Смещение данных в буфере в начало
278-
// str.assign(str.cbegin() + str_cur, str.cend() );
278+
// str.assign(str.cbegin() + str_cur, str.cend() );
279279
str.erase(str.begin(), str.begin() + str_cur);
280280

281281
// Поиск конца блока данных
@@ -287,7 +287,7 @@ namespace HttpServer
287287
// Добавить данные к значению
288288
file.write(str.data(), str.length() - data_end.length() );
289289

290-
// str.assign(str.cend() - data_end.length(), str.cend() );
290+
// str.assign(str.cend() - data_end.length(), str.cend() );
291291
str.erase(str.begin(), str.end() - data_end.length() );
292292

293293
// Получить следующий кусок данных
@@ -331,7 +331,7 @@ namespace HttpServer
331331
std::string value;
332332

333333
// Смещение данных в буфере в начало
334-
// str.assign(str.cbegin() + str_cur, str.cend() );
334+
// str.assign(str.cbegin() + str_cur, str.cend() );
335335
str.erase(str.begin(), str.begin() + str_cur);
336336

337337
// Поиск конца блока данных
@@ -343,7 +343,7 @@ namespace HttpServer
343343
// Добавить данные к значению
344344
value.append(str.cbegin(), str.cend() - data_end.length() );
345345

346-
// str.assign(str.cend() - data_end.length(), str.cend() );
346+
// str.assign(str.cend() - data_end.length(), str.cend() );
347347
str.erase(str.begin(), str.end() - data_end.length() );
348348

349349
// Получить следующий кусок данных
@@ -395,7 +395,7 @@ namespace HttpServer
395395

396396
while (std::string::npos == str_cur)
397397
{
398-
// str.assign(str.cend() - data_end.length(), str.cend() );
398+
// str.assign(str.cend() - data_end.length(), str.cend() );
399399
str.erase(str.begin(), str.end() - data_end.length() );
400400

401401
// Получить следующий кусок данных

src/DataVariantMultipartFormData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace HttpServer
1414
(
1515
const Socket &sock,
1616
const std::chrono::milliseconds &timeout,
17-
std::vector<std::string::value_type> &buf,
17+
std::vector<char> &buf,
1818
std::string &str_buf,
1919
const std::string &data_end,
2020
const size_t &leftBytes,

src/Server.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace HttpServer
2626
std::string file_ext = std::string::npos != ext_pos ? fileName.substr(ext_pos + 1) : "";
2727

2828
std::locale loc;
29-
Utils::tolower(file_ext, loc);
29+
Utils::toLower(file_ext, loc);
3030

3131
auto it_mime = this->mimes_types.find(file_ext);
3232

@@ -225,7 +225,7 @@ namespace HttpServer
225225
{
226226
const size_t length = std::get<1>(range);
227227

228-
std::vector<std::string::value_type> buf(length < 512 * 1024 ? length : 512 * 1024);
228+
std::vector<char> buf(length < 512 * 1024 ? length : 512 * 1024);
229229

230230
const size_t position = std::get<0>(range);
231231

@@ -242,7 +242,7 @@ namespace HttpServer
242242
buf.resize(send_size_left);
243243
}
244244

245-
file.read(reinterpret_cast<char *>(buf.data() ), buf.size() );
245+
file.read(buf.data(), buf.size() );
246246
send_size = clientSocket.nonblock_send(buf, file.gcount(), timeout);
247247

248248
send_size_left -= send_size;
@@ -350,7 +350,7 @@ namespace HttpServer
350350
// Отправить файл
351351
if (false == headersOnly && file_size)
352352
{
353-
std::vector<std::string::value_type> buf(file_size < 512 * 1024 ? file_size : 512 * 1024);
353+
std::vector<char> buf(file_size < 512 * 1024 ? file_size : 512 * 1024);
354354

355355
size_t send_size;
356356

@@ -441,7 +441,7 @@ namespace HttpServer
441441
rp.keep_alive_count = 100;
442442

443443
const size_t buf_len = 4096;
444-
std::vector<std::string::value_type> buf(buf_len);
444+
std::vector<char> buf(buf_len);
445445

446446
std::string str_buf;
447447

@@ -516,7 +516,7 @@ namespace HttpServer
516516
return rp.app_exit_code;
517517
}
518518

519-
bool Server::getRequest(Socket clientSocket, std::vector<std::string::value_type> &buf, std::string &str_buf, struct request_parameters &rp)
519+
bool Server::getRequest(Socket clientSocket, std::vector<char> &buf, std::string &str_buf, struct request_parameters &rp)
520520
{
521521
// Получить данные запроса от клиента
522522
const size_t recv_size = clientSocket.nonblock_recv(buf, rp.timeout);
@@ -897,10 +897,10 @@ namespace HttpServer
897897
std::locale loc;
898898

899899
std::string connection_in = it_in_connection->second;
900-
Utils::tolower(connection_in, loc);
900+
Utils::toLower(connection_in, loc);
901901

902902
std::string connection_out = it_out_connection->second;
903-
Utils::tolower(connection_out, loc);
903+
Utils::toLower(connection_out, loc);
904904

905905
auto const incoming_params = Utils::explode(connection_in, ',');
906906

src/Server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace HttpServer
5353

5454
int threadRequestProc(Socket clientSocket) const;
5555

56-
static bool getRequest(Socket clientSocket, std::vector<std::string::value_type> &buf, std::string &str_buf, struct request_parameters &rp);
56+
static bool getRequest(Socket clientSocket, std::vector<char> &buf, std::string &str_buf, struct request_parameters &rp);
5757

5858
int getRequestHeaders(std::string &str_buf, struct request_parameters &rp) const;
5959

src/SignalsHandles.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ int bindSignalsHandles(HttpServer::Server *server)
127127

128128
::HINSTANCE hInstance = ::GetModuleHandle(nullptr);
129129

130-
::WNDCLASSEX wcex = {
131-
sizeof(::WNDCLASSEX)
132-
};
130+
::WNDCLASSEX wcex = {};
133131

132+
wcex.cbSize = sizeof(::WNDCLASSEX);
134133
wcex.lpfnWndProc = WndProc;
135134
wcex.hInstance = hInstance;
136135
wcex.lpszClassName = myWndClassName;
@@ -148,7 +147,7 @@ int bindSignalsHandles(HttpServer::Server *server)
148147

149148
#elif POSIX
150149

151-
struct ::sigaction act = {0};
150+
struct ::sigaction act = {};
152151

153152
act.sa_handler = handlerSigInt;
154153
::sigaction(SIGINT, &act, nullptr);

src/Utils.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,26 @@
1111

1212
namespace Utils
1313
{
14+
void toLower(std::string &str, const std::locale &loc)
15+
{
16+
for (auto &c : str)
17+
{
18+
c = std::tolower(c, loc);
19+
}
20+
}
21+
1422
void trim(std::string &str)
1523
{
16-
const size_t last = str.find_last_not_of(" \t\n\v\f\r");
24+
static const char whitespace[] = {" \t\n\v\f\r"};
25+
26+
const size_t last = str.find_last_not_of(whitespace);
1727

1828
if (std::string::npos == last)
1929
{
2030
return str.clear();
2131
}
2232

23-
str.assign(str.cbegin() + str.find_first_not_of(" \t\n\v\f\r"), str.cbegin() + last + 1);
33+
str.assign(str.cbegin() + str.find_first_not_of(whitespace), str.cbegin() + last + 1);
2434
}
2535

2636
std::vector<std::string> explode(const std::string &str, const char sep)
@@ -68,7 +78,7 @@ namespace Utils
6878
return buf;
6979
}
7080

71-
std::string binToHexString(const char *binData, const size_t dataSize)
81+
std::string binToHexString(const void *binData, const size_t dataSize)
7282
{
7383
std::string str(dataSize * 2, 0);
7484

@@ -109,8 +119,8 @@ namespace Utils
109119

110120
for (size_t i = 0; i < bin.length(); ++i)
111121
{
112-
char a = hexStr[(i << 1) + 0];
113-
char b = hexStr[(i << 1) + 1];
122+
const char a = hexStr[(i << 1) + 0];
123+
const char b = hexStr[(i << 1) + 1];
114124

115125
bin[i] = (
116126
(hexStringToBinEncodeSymbol(a) << 4) | hexStringToBinEncodeSymbol(b)
@@ -164,7 +174,7 @@ namespace Utils
164174

165175
time = htonll(time);
166176

167-
return binToHexString(reinterpret_cast<const char *>(&time), sizeof(time) );
177+
return binToHexString(&time, sizeof(time) );
168178
}
169179

170180
char *stlStringToPChar(const std::string &str)
@@ -254,24 +264,19 @@ namespace Utils
254264

255265
time_t stringTimeToTimestamp(const std::string &strTime)
256266
{
257-
/* static const std::unordered_map<std::string, int> map_days {
258-
{"Sun", 0}, {"Mon", 1}, {"Tue", 2}, {"Wed", 3}, {"Thu", 4}, {"Fri", 5}, {"Sat", 6}
259-
};*/
260-
261267
static const std::unordered_map<std::string, int> map_months {
262268
{"Jan", 0}, {"Feb", 1}, {"Mar", 2}, {"Apr", 3}, {"May", 4}, {"Jun", 5}, {"Jul", 6}, {"Aug", 7}, {"Sep", 8}, {"Oct", 9}, {"Nov", 10}, {"Dec", 11}
263269
};
264270

265271
if (strTime.length() > 64)
266272
{
267-
return (time_t) ~0;
273+
return ~0;
268274
}
269275

270276
const size_t str_mon_length = 64;
271277
std::vector<char> s_mon(str_mon_length);
272278

273-
struct ::tm tc;
274-
memset(&tc, 0, sizeof(tc) );
279+
struct ::tm tc = {};
275280

276281
// Parse RFC 822
277282
#ifdef WIN32
@@ -301,27 +306,24 @@ namespace Utils
301306

302307
::time_t cur_time = tTime;
303308

304-
if ( (time_t)~0 == tTime)
309+
if (tTime == ~0)
305310
{
306311
::time(&cur_time);
307312
}
308313

309314
#ifdef WIN32
310-
struct ::tm stm = {0};
315+
struct ::tm stm = {};
311316

312-
if (isGmtTime)
313-
{
314-
::localtime_s(&stm, &cur_time);
315-
}
316-
else
317-
{
317+
isGmtTime ?
318+
::localtime_s(&stm, &cur_time) :
318319
::gmtime_s(&stm, &cur_time);
319-
}
320320

321321
// RFC 822
322322
::strftime(buf.data(), buf.size(), "%a, %d %b %Y %H:%M:%S GMT", &stm);
323323
#else
324-
struct ::tm *ptm = isGmtTime ? localtime(&cur_time) : gmtime(&cur_time);
324+
struct ::tm *ptm = isGmtTime ?
325+
::localtime(&cur_time) :
326+
::gmtime(&cur_time);
325327

326328
// RFC 822
327329
::strftime(buf.data(), buf.size(), "%a, %d %b %G %H:%M:%S GMT", ptm);
@@ -350,7 +352,7 @@ namespace Utils
350352
{
351353
if (cookieHeader.empty() )
352354
{
353-
return false;
355+
return true;
354356
}
355357

356358
for (size_t cur_pos = 0, next_value; std::string::npos != cur_pos; cur_pos = next_value)
@@ -385,7 +387,7 @@ namespace Utils
385387
return true;
386388
}
387389

388-
inline bool isUrlAllowed(const std::string::value_type c)
390+
inline bool isUrlAllowed(const char c)
389391
{
390392
static const std::string special("-_.~");
391393

@@ -400,7 +402,7 @@ namespace Utils
400402

401403
for (auto it = str.cbegin(); str.cend() != it; ++it)
402404
{
403-
const std::string::value_type &c = *it;
405+
const char &c = *it;
404406

405407
if (' ' == c)
406408
{
@@ -423,11 +425,12 @@ namespace Utils
423425
{
424426
std::string decoded;
425427

426-
std::string::value_type ch[3] = {0};
428+
// ch length must be >= 3
429+
std::array<char, sizeof(size_t)> ch;
427430

428431
for (auto it = str.cbegin(); str.cend() != it; ++it)
429432
{
430-
const std::string::value_type &c = *it;
433+
const char &c = *it;
431434

432435
if ('%' == c)
433436
{
@@ -449,7 +452,7 @@ namespace Utils
449452

450453
ch[1] = *it;
451454

452-
decoded.push_back(strtoul(ch, nullptr, 16) );
455+
decoded.push_back(strtoul(ch.data(), nullptr, 16) );
453456
}
454457
else if ('+' == c)
455458
{

src/Utils.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,15 @@
99

1010
namespace Utils
1111
{
12-
inline void tolower(std::string &str, const std::locale &loc)
13-
{
14-
for (auto &c : str)
15-
{
16-
c = std::tolower(c, loc);
17-
}
18-
}
12+
void toLower(std::string &str, const std::locale &loc);
1913

2014
void trim(std::string &str);
2115

2216
std::vector<std::string> explode(const std::string &str, const char sep);
2317

2418
std::string encodeHtmlSymbols(const std::string &str);
2519

26-
std::string binToHexString(const char *bin, const size_t size);
20+
std::string binToHexString(const void *bin, const size_t size);
2721

2822
std::string hexStringToBin(const std::string &hexStr);
2923

0 commit comments

Comments
 (0)