Skip to content

Commit 890627c

Browse files
committed
Safe conversion of strings to numbers, safe parsing dates as strings
1 parent b927c9a commit 890627c

File tree

8 files changed

+44
-84
lines changed

8 files changed

+44
-84
lines changed

httpserver.userprefs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<Properties>
22
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
3-
<MonoDevelop.Ide.Workbench ActiveDocument="httpserver/SocketList.cpp">
3+
<MonoDevelop.Ide.Workbench ActiveDocument="httpserver/Utils.cpp">
44
<Files>
5-
<File FileName="httpserver/Server.cpp" Line="1" Column="1" />
5+
<File FileName="httpserver/Server.cpp" Line="75" Column="75" />
66
<File FileName="httpserver/Module.cpp" Line="1" Column="1" />
7-
<File FileName="httpserver/SignalsHandles.cpp" Line="1" Column="1" />
7+
<File FileName="httpserver/SignalsHandles.cpp" Line="2" Column="2" />
88
<File FileName="httpserver/Utils.h" Line="1" Column="1" />
99
<File FileName="httpserver/Main.cpp" Line="1" Column="1" />
1010
<File FileName="httpserver/Module.h" Line="1" Column="1" />
@@ -18,12 +18,14 @@
1818
<File FileName="httpserver/ServerApplicationSettings.h" Line="1" Column="1" />
1919
<File FileName="httpserver/ServerApplicationDefaultSettings.h" Line="1" Column="1" />
2020
<File FileName="httpserver/SocketList.h" Line="1" Column="1" />
21-
<File FileName="httpserver/SocketList.cpp" Line="51" Column="51" />
21+
<File FileName="httpserver/SocketList.cpp" Line="3" Column="3" />
2222
<File FileName="httpserver/Socket.h" Line="1" Column="1" />
2323
<File FileName="../../../usr/include/c++/4.8/mutex" Line="1" Column="1" />
2424
<File FileName="httpserver/RawData.h" Line="1" Column="1" />
25-
<File FileName="httpserver/Socket.cpp" Line="1" Column="1" />
25+
<File FileName="httpserver/Socket.cpp" Line="28" Column="28" />
2626
<File FileName="../../../usr/include/c++/4.8/bits/hashtable_policy.h" Line="1" Column="1" />
27+
<File FileName="httpserver/Utils.cpp" Line="4" Column="4" />
28+
<File FileName="httpserver/ServerApplicationsTree.cpp" Line="11" Column="11" />
2729
</Files>
2830
</MonoDevelop.Ide.Workbench>
2931
<MonoDevelop.Ide.DebuggingService.Breakpoints>

httpserver/Server.cpp

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ namespace HttpServer
8787

8888
if (false == range_begin_str.empty() )
8989
{
90-
const size_t range_begin = std::stoull(range_begin_str) * range_unit;
90+
const size_t range_begin = std::strtoull(range_begin_str.c_str(), nullptr, 10) * range_unit;
9191

9292
if (range_begin < fileSize)
9393
{
9494
if (false == range_end_str.empty() )
9595
{
96-
size_t range_end = std::stoull(range_end_str) * range_unit;
96+
size_t range_end = std::strtoull(range_end_str.c_str(), nullptr, 10) * range_unit;
9797

9898
if (range_end >= range_begin)
9999
{
@@ -125,7 +125,7 @@ namespace HttpServer
125125
}
126126
else if (false == range_end_str.empty() ) // if range_begin_str empty
127127
{
128-
size_t range_end = std::stoull(range_end_str) * range_unit;
128+
size_t range_end = std::strtoull(range_end_str.c_str(), nullptr, 10) * range_unit;
129129

130130
const size_t length = range_end < fileSize ? fileSize - range_end : fileSize;
131131

@@ -580,7 +580,7 @@ namespace HttpServer
580580
const std::string host = it_host->second.substr(0, delimiter);
581581

582582
// Получить номер порта
583-
const int port = (std::string::npos != delimiter) ? std::stoi(it_host->second.substr(delimiter + 1) ) : 80;
583+
const int port = (std::string::npos != delimiter) ? std::strtol(it_host->second.substr(delimiter + 1).c_str(), nullptr, 10) : 80;
584584

585585
// Поиск настроек приложения по имени
586586
ServerApplicationSettings *app_sets = apps_tree.find(host);
@@ -655,7 +655,7 @@ namespace HttpServer
655655

656656
if (incoming_headers.end() != it_len)
657657
{
658-
data_length = std::stoull(it_len->second);
658+
data_length = std::strtoull(it_len->second.c_str(), nullptr, 10);
659659
}
660660

661661
// Если размер запроса не превышает лимит (если лимит был установлен)
@@ -849,12 +849,19 @@ namespace HttpServer
849849

850850
if (settings.end() != it_option)
851851
{
852-
threads_max_count = std::stoul(it_option->second);
852+
threads_max_count = std::strtoull(it_option->second.c_str(), nullptr, 10);
853853
}
854854

855855
if (0 == threads_max_count)
856856
{
857-
threads_max_count = System::getProcessorsCount();
857+
threads_max_count = std::thread::hardware_concurrency();
858+
859+
if (0 == threads_max_count)
860+
{
861+
threads_max_count = 1;
862+
}
863+
864+
threads_max_count *= 2;
858865
}
859866

860867
std::function<int(Server *, Socket)> serverThreadRequestProc = std::mem_fn(&Server::threadRequestProc);
@@ -1126,7 +1133,7 @@ namespace HttpServer
11261133

11271134
auto it_request_max_size = app.find("request_max_size");
11281135

1129-
const size_t request_max_size = app.end() != it_request_max_size ? std::stoull(it_request_max_size->second) : defaults.request_max_size;
1136+
const size_t request_max_size = app.end() != it_request_max_size ? std::strtoull(it_request_max_size->second.c_str(), nullptr, 10) : defaults.request_max_size;
11301137

11311138
auto it_module_update = app.find("server_module_update");
11321139

@@ -1167,7 +1174,7 @@ namespace HttpServer
11671174

11681175
// Create application settings struct
11691176
ServerApplicationSettings *sets = new ServerApplicationSettings {
1170-
std::stoi(it_port->second.c_str() ),
1177+
std::strtol(it_port->second.c_str(), nullptr, 10),
11711178
root_dir,
11721179
temp_dir,
11731180
request_max_size,
@@ -1319,7 +1326,7 @@ namespace HttpServer
13191326

13201327
auto it_default_request_max_size = settings.find("request_max_size");
13211328

1322-
const size_t default_request_max_size = settings.end() != it_default_request_max_size ? std::stoull(it_default_request_max_size->second) : 0;
1329+
const size_t default_request_max_size = settings.end() != it_default_request_max_size ? std::strtoull(it_default_request_max_size->second.c_str(), nullptr, 10) : 0;
13231330

13241331
ServerApplicationDefaultSettings defaults {
13251332
default_temp_dir,
@@ -1575,13 +1582,13 @@ namespace HttpServer
15751582

15761583
bool Server::init()
15771584
{
1578-
if (0 == Socket::Startup() )
1585+
if (Socket::Startup() && loadConfig() )
15791586
{
15801587
addDataVariant(new DataVariantFormUrlencoded() );
15811588
addDataVariant(new DataVariantMultipartFormData() );
15821589
addDataVariant(new DataVariantTextPlain() );
15831590

1584-
return loadConfig();
1591+
return true;
15851592
}
15861593

15871594
return false;
@@ -1661,45 +1668,6 @@ namespace HttpServer
16611668
}
16621669
}
16631670

1664-
/* void Server::accept(std::vector<Socket> &sockets, const System::native_socket_type max_val) const
1665-
{
1666-
::fd_set readset;
1667-
FD_ZERO(&readset);
1668-
1669-
for (auto &sock : server_sockets)
1670-
{
1671-
FD_SET(sock.get_handle(), &readset);
1672-
}
1673-
1674-
if (0 < ::select(max_val + 1, &readset, nullptr, nullptr, nullptr) )
1675-
{
1676-
for (auto &sock : server_sockets)
1677-
{
1678-
if (FD_ISSET(sock.get_handle(), &readset) )
1679-
{
1680-
System::native_socket_type client_socket = ~0;
1681-
1682-
do
1683-
{
1684-
#ifdef WIN32
1685-
client_socket = ::accept(sock.get_handle(), static_cast<sockaddr *>(nullptr), static_cast<int *>(nullptr) );
1686-
#elif POSIX
1687-
client_socket = ::accept(sock.get_handle(), static_cast<sockaddr *>(nullptr), static_cast<socklen_t *>(nullptr) );
1688-
#else
1689-
#error "Undefine platform"
1690-
#endif
1691-
1692-
if (~0 != client_socket)
1693-
{
1694-
sockets.emplace_back(Socket(client_socket) );
1695-
}
1696-
}
1697-
while (~0 != client_socket);
1698-
}
1699-
}
1700-
}
1701-
}*/
1702-
17031671
int Server::run()
17041672
{
17051673
if (false == init() )
@@ -1901,7 +1869,7 @@ namespace HttpServer
19011869

19021870
if (file.gcount() )
19031871
{
1904-
pid = std::stoull(str_pid);
1872+
pid = std::strtoull(str_pid, nullptr, 10);
19051873
}
19061874
}
19071875

httpserver/Server.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ namespace HttpServer
4949
int transferFile(const Socket &, const std::chrono::milliseconds &, const std::string &, const std::unordered_map<std::string, std::string> &, const std::map<std::string, std::string> &, const std::string &, const bool) const;
5050
bool parseIncomingVars(std::unordered_multimap<std::string, std::string> &, const std::string &) const;
5151

52-
// void accept(std::vector<Socket> &sockets, const System::native_socket_type max_val) const;
53-
5452
bool loadConfig();
5553
bool includeConfigFile(const std::string &, std::string &, const size_t);
5654
bool addApplication(const std::unordered_map<std::string, std::string> &, const ServerApplicationDefaultSettings &);

httpserver/ServerApplicationSettings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace HttpServer
1111
{
1212
struct ServerApplicationSettings
1313
{
14-
int port;
14+
long int port;
1515
std::string root_dir;
1616
std::string temp_dir;
1717
size_t request_max_size;

httpserver/Socket.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33

44
namespace HttpServer
55
{
6-
int Socket::Startup()
6+
bool Socket::Startup()
77
{
88
#ifdef WIN32
99
unsigned short version = MAKEWORD(2, 2);
1010
::WSADATA wsaData = {0};
11-
return ::WSAStartup(version, &wsaData);
11+
return 0 == ::WSAStartup(version, &wsaData);
1212
#elif POSIX
13-
return 0;
13+
return true;
1414
#else
1515
#error "Undefine platform"
1616
#endif
1717
}
1818

19-
int Socket::Cleanup()
19+
bool Socket::Cleanup()
2020
{
2121
#ifdef WIN32
22-
return ::WSACleanup();
22+
return 0 == ::WSACleanup();
2323
#elif POSIX
24-
return 0;
24+
return true;
2525
#else
2626
#error "Undefine platform"
2727
#endif

httpserver/Socket.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ namespace HttpServer
3333
System::native_socket_type socket_handle;
3434

3535
public:
36-
int static Startup();
37-
int static Cleanup();
36+
bool static Startup();
37+
bool static Cleanup();
3838

3939
public:
4040
Socket();

httpserver/System.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,6 @@ namespace System
4242
#error "Undefine platform"
4343
#endif
4444

45-
inline size_t getProcessorsCount()
46-
{
47-
#ifdef WIN32
48-
::SYSTEM_INFO si = {0};
49-
::GetSystemInfo(&si);
50-
return si.dwNumberOfProcessors;
51-
#elif POSIX
52-
return ::get_nprocs();
53-
#else
54-
#error "Undefine platform"
55-
#endif
56-
}
57-
5845
inline native_processid_type getProcessId()
5946
{
6047
#ifdef WIN32

httpserver/Utils.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,22 @@ namespace Utils
191191
{"Jan", 0}, {"Feb", 1}, {"Mar", 2}, {"Apr", 3}, {"May", 4}, {"Jun", 5}, {"Jul", 6}, {"Aug", 7}, {"Sep", 8}, {"Oct", 9}, {"Nov", 10}, {"Dec", 11}
192192
};
193193

194-
const size_t str_mon_length = 32;
194+
if (strTime.length() > 64)
195+
{
196+
return (time_t) ~0;
197+
}
198+
199+
const size_t str_mon_length = 64;
195200
char *s_mon = new char[str_mon_length];
196201
::memset(s_mon, 0, str_mon_length);
197202

198203
struct ::tm tc = {0};
199204

200205
// Parse RFC 822
201206
#ifdef WIN32
202-
if (std::numeric_limits<int>::max() != ::sscanf_s(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon, str_mon_length, &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) )
207+
if (~0 != ::sscanf_s(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon, str_mon_length, &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) )
203208
#else
204-
if (std::numeric_limits<int>::max() != ::sscanf(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon, &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) )
209+
if (~0 != ::sscanf(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon, &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) )
205210
#endif
206211
{
207212
tc.tm_year -= 1900;

0 commit comments

Comments
 (0)