Skip to content

Commit 68c79d3

Browse files
committed
Fixed: parsing requests without data
1 parent ed0682a commit 68c79d3

File tree

9 files changed

+117
-75
lines changed

9 files changed

+117
-75
lines changed

apps.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
server {
22
listen 2280;
3-
server_name servertest www.servertest 192.168.1.35;
3+
server_name servertest www.servertest;
44
server_module /media/projects/appgameserver/build/Release/libappgameserver.so;
55
server_module_update /media/projects/httpserverapp/httpserverapp/bin/Release/libhttpserverapp.so;
66
root_dir /media/projects/sites/servertest/www/;

httpserver.pro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ DEFINES += POSIX
77

88
CONFIG(debug, debug|release):DEFINES += DEBUG
99

10-
QMAKE_CXXFLAGS += -std=c++11
10+
QMAKE_CXXFLAGS += -std=c++1y
1111

1212
CONFIG(release, debug|release)
1313
{
1414
QMAKE_CFLAGS_RELEASE -= -O
1515
QMAKE_CFLAGS_RELEASE -= -O1
16-
QMAKE_CFLAGS_RELEASE -= -O2
17-
QMAKE_CFLAGS_RELEASE *= -O3
16+
QMAKE_CFLAGS_RELEASE *= -O2
17+
# QMAKE_CFLAGS_RELEASE *= -O3
1818
}
1919

2020
LIBS += -ldl -pthread

httpserver.pro.user

Lines changed: 2 additions & 2 deletions
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.2.1, 2015-06-19T14:11:06. -->
3+
<!-- Written by QtCreator 3.2.1, 2015-08-28T20:24:23. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
@@ -61,7 +61,7 @@
6161
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
6262
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
6363
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{c77ee4a2-1c2a-4bac-9185-8378ec4ebf5d}</value>
64-
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
64+
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
6565
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
6666
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
6767
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">

httpserver/DataVariantFormUrlencoded.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,44 @@ namespace HttpServer
2121
std::unordered_multimap<std::string, FileIncoming> &files
2222
)
2323
{
24-
if (str.length() )
24+
if (str.empty() )
2525
{
26-
for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1)
27-
{
28-
// Поиск следующего параметра
29-
var_end = str.find('&', var_pos);
30-
31-
// Поиск значения параметра
32-
size_t delimiter = str.find('=', var_pos);
33-
34-
if (delimiter >= var_end)
35-
{
36-
// Получить имя параметра
37-
std::string var_name = Utils::urlDecode(str.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos) );
26+
return true;
27+
}
3828

39-
// Сохранить параметр с пустым значением
40-
data.emplace(std::move(var_name), "");
41-
}
42-
else
43-
{
44-
// Получить имя параметра
45-
std::string var_name = Utils::urlDecode(str.substr(var_pos, delimiter - var_pos) );
29+
for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1)
30+
{
31+
// Поиск следующего параметра
32+
var_end = str.find('&', var_pos);
4633

47-
++delimiter;
34+
// Поиск значения параметра
35+
size_t delimiter = str.find('=', var_pos);
4836

49-
// Получить значение параметра
50-
std::string var_value = Utils::urlDecode(str.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos) );
37+
if (delimiter >= var_end)
38+
{
39+
// Получить имя параметра
40+
std::string var_name = Utils::urlDecode(str.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos) );
5141

52-
// Сохранить параметр и значение
53-
data.emplace(std::move(var_name), std::move(var_value) );
54-
}
42+
// Сохранить параметр с пустым значением
43+
data.emplace(std::move(var_name), "");
5544
}
45+
else
46+
{
47+
// Получить имя параметра
48+
std::string var_name = Utils::urlDecode(str.substr(var_pos, delimiter - var_pos) );
5649

57-
str.clear();
50+
++delimiter;
5851

59-
return true;
52+
// Получить значение параметра
53+
std::string var_value = Utils::urlDecode(str.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos) );
54+
55+
// Сохранить параметр и значение
56+
data.emplace(std::move(var_name), std::move(var_value) );
57+
}
6058
}
6159

62-
return false;
60+
str.clear();
61+
62+
return true;
6363
}
6464
};

httpserver/DataVariantMultipartFormData.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,14 @@ namespace HttpServer
7575
// Определить признак начала блока данных
7676
std::string block_delimiter("--" + it->second);
7777
// Определить признак окончания всех данных
78-
const std::string data_end("\r\n--" + it->second + "--\r\n");
78+
std::string data_end("--" + it->second + "--\r\n");
79+
80+
if (0 == str.find(data_end) )
81+
{
82+
return true;
83+
}
84+
85+
data_end = "\r\n" + data_end;
7986

8087
// Установить размер буфера данных
8188
const size_t buf_len = (leftBytes >= 512 * 1024) ? 512 * 1024 : leftBytes;

httpserver/DataVariantTextPlain.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,44 @@ namespace HttpServer
1919
std::unordered_multimap<std::string, FileIncoming> &files
2020
)
2121
{
22-
if (str.length() )
22+
if (str.empty() )
2323
{
24-
for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1)
25-
{
26-
// Поиск следующего параметра
27-
var_end = str.find('&', var_pos);
28-
29-
// Поиск значения параметра
30-
size_t delimiter = str.find('=', var_pos);
31-
32-
if (delimiter >= var_end)
33-
{
34-
// Получить имя параметра
35-
std::string var_name = str.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos);
24+
return true;
25+
}
3626

37-
// Сохранить параметр с пустым значением
38-
data.emplace(std::move(var_name), "");
39-
}
40-
else
41-
{
42-
// Получить имя параметра
43-
std::string var_name = str.substr(var_pos, delimiter - var_pos);
27+
for (size_t var_pos = 0, var_end = 0; std::string::npos != var_end; var_pos = var_end + 1)
28+
{
29+
// Поиск следующего параметра
30+
var_end = str.find('&', var_pos);
4431

45-
++delimiter;
32+
// Поиск значения параметра
33+
size_t delimiter = str.find('=', var_pos);
4634

47-
// Получить значение параметра
48-
std::string var_value = str.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos);
35+
if (delimiter >= var_end)
36+
{
37+
// Получить имя параметра
38+
std::string var_name = str.substr(var_pos, std::string::npos != var_end ? var_end - var_pos : std::string::npos);
4939

50-
// Сохранить параметр и значение
51-
data.emplace(std::move(var_name), std::move(var_value) );
52-
}
40+
// Сохранить параметр с пустым значением
41+
data.emplace(std::move(var_name), "");
5342
}
43+
else
44+
{
45+
// Получить имя параметра
46+
std::string var_name = str.substr(var_pos, delimiter - var_pos);
5447

55-
str.clear();
48+
++delimiter;
5649

57-
return true;
50+
// Получить значение параметра
51+
std::string var_value = str.substr(delimiter, std::string::npos != var_end ? var_end - delimiter : std::string::npos);
52+
53+
// Сохранить параметр и значение
54+
data.emplace(std::move(var_name), std::move(var_value) );
55+
}
5856
}
5957

60-
return false;
58+
str.clear();
59+
60+
return true;
6161
}
6262
};

httpserver/Server.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ namespace HttpServer
244244
const std::chrono::milliseconds &timeout,
245245
const std::string &fileName,
246246
const std::unordered_map<std::string, std::string> &inHeaders,
247-
const std::map<std::string, std::string> &outHeaders,
247+
const std::unordered_map<std::string, std::string> &outHeaders,
248248
const std::string &connectionHeader,
249249
const bool headersOnly
250250
) const
@@ -340,10 +340,6 @@ namespace HttpServer
340340
if (false == headersOnly && file_size)
341341
{
342342
std::vector<std::string::value_type> buf(file_size < 512 * 1024 ? file_size : 512 * 1024);
343-
// buf.assign(headers.cbegin(), headers.cend() );
344-
345-
// file.read(reinterpret_cast<char *>(buf.data() + headers.length() ), buf.size() - headers.length() );
346-
// size_t send_size = clientSocket.nonblock_send(buf, file.gcount(), timeout);
347343

348344
size_t send_size;
349345

@@ -449,7 +445,7 @@ namespace HttpServer
449445
std::unordered_multimap<std::string, std::string> incoming_data;
450446
std::unordered_multimap<std::string, FileIncoming> incoming_files;
451447

452-
std::map<std::string, std::string> outgoing_headers;
448+
std::unordered_map<std::string, std::string> outgoing_headers;
453449

454450
std::string method;
455451
std::string version;
@@ -771,7 +767,7 @@ namespace HttpServer
771767

772768
if (EXIT_SUCCESS == app_exit_code)
773769
{
774-
Utils::rawPairsToStlMap(outgoing_headers, response.headers, response.headers_count);
770+
Utils::rawPairsToStl(outgoing_headers, response.headers, response.headers_count);
775771
}
776772

777773
try
@@ -783,7 +779,7 @@ namespace HttpServer
783779
Utils::destroyRawPairs(raw_pair_params, incoming_params.size() );
784780
Utils::destroyRawPairs(raw_pair_headers, incoming_headers.size() );
785781
Utils::destroyRawPairs(raw_pair_data, incoming_data.size() );
786-
Utils::destroyRawFilesInfo(raw_fileinfo_files, incoming_files.size() );
782+
Utils::destroyRawFilesInfo(raw_fileinfo_files, incoming_files.size() );
787783
}
788784
else
789785
{
@@ -867,7 +863,7 @@ namespace HttpServer
867863

868864
if (false == connection_upgrade)
869865
{
870-
// TODO: wait for send all data to client
866+
// Wait for send all data to client
871867
clientSocket.nonblock_send_sync();
872868

873869
clientSocket.shutdown();

httpserver/Server.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <cstddef>
44

55
#include <memory>
6+
67
#include <vector>
78
#include <queue>
89
#include <string>
@@ -52,7 +53,17 @@ namespace HttpServer
5253
int threadRequestProc(Socket) const;
5354
void threadRequestCycle(std::queue<Socket> &) const;
5455
int transferFilePart(const Socket &, const std::chrono::milliseconds &, const std::string &, const time_t, const size_t, const std::string &, const std::string &, const std::string &, const bool) const;
55-
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;
56+
57+
int transferFile(
58+
const Socket &,
59+
const std::chrono::milliseconds &,
60+
const std::string &,
61+
const std::unordered_map<std::string, std::string> &,
62+
const std::unordered_map<std::string, std::string> &,
63+
const std::string &,
64+
const bool
65+
) const;
66+
5667
bool parseIncomingVars(std::unordered_multimap<std::string, std::string> &, const std::string &) const;
5768

5869
bool init();

httpserver/Utils.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,34 @@ namespace Utils
3434

3535
char *stlStringToPChar(const std::string &);
3636

37+
template<typename T>
38+
void stlToRawPairs(Utils::raw_pair *raw[], const T &stl)
39+
{
40+
if (raw && stl.size() )
41+
{
42+
raw_pair *arr = new raw_pair[stl.size()];
43+
44+
*raw = arr;
45+
46+
size_t i = 0;
47+
48+
for (auto it = stl.cbegin(); stl.cend() != it; ++it, ++i)
49+
{
50+
arr[i].key = stlStringToPChar(it->first);
51+
arr[i].value = stlStringToPChar(it->second);
52+
}
53+
}
54+
}
55+
56+
template<typename T>
57+
void rawPairsToStl(T &stl, const Utils::raw_pair raw[], const size_t count)
58+
{
59+
for (size_t i = 0; i < count; ++i)
60+
{
61+
stl.emplace(raw[i].key ? raw[i].key : "", raw[i].value ? raw[i].value : "");
62+
}
63+
}
64+
3765
void stlMapToRawPairs(Utils::raw_pair *[], const std::map<std::string, std::string> &);
3866
void stlMultimapToRawPairs(Utils::raw_pair *[], const std::multimap<std::string, std::string> &);
3967
void stlUnorderedMapToRawPairs(Utils::raw_pair *[], const std::unordered_map<std::string, std::string> &);

0 commit comments

Comments
 (0)