Skip to content

Commit 65737e4

Browse files
committed
Fixed #5: redesign parse query params
1 parent f1549c4 commit 65737e4

File tree

3 files changed

+89
-11
lines changed

3 files changed

+89
-11
lines changed

src/wsjcpp_light_web_http_request.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
#include <sstream>
44
#include <wsjcpp_core.h>
55

6+
// ----------------------------------------------------------------------
7+
8+
WSJCppLightWebHttpRequestQueryValue::WSJCppLightWebHttpRequestQueryValue(const std::string &sName, const std::string &sValue) {
9+
m_sName = sName;
10+
m_sValue = sValue;
11+
}
12+
13+
// ----------------------------------------------------------------------
14+
15+
std::string WSJCppLightWebHttpRequestQueryValue::getName() const {
16+
return m_sName;
17+
}
18+
19+
// ----------------------------------------------------------------------
20+
21+
std::string WSJCppLightWebHttpRequestQueryValue::getValue() const {
22+
return m_sValue;
23+
}
24+
625
// ----------------------------------------------------------------------
726
// WSJCppLightWebHttpRequest
827

@@ -54,9 +73,9 @@ std::string WSJCppLightWebHttpRequest::getRequestHttpVersion() const {
5473
}
5574

5675
// ----------------------------------------------------------------------
57-
// TODO redesign this to vector
58-
std::map<std::string,std::string> &WSJCppLightWebHttpRequest::getRequestQueryParams() {
59-
return m_sRequestQueryParams;
76+
77+
const std::vector<WSJCppLightWebHttpRequestQueryValue> &WSJCppLightWebHttpRequest::getRequestQueryParams() {
78+
return m_vRequestQueryParams;
6079
}
6180

6281
// ----------------------------------------------------------------------
@@ -164,7 +183,17 @@ void WSJCppLightWebHttpRequest::parseFirstLine(const std::string &sHeader) {
164183
std::size_t nFound2 = sParam.find("=");
165184
std::string sValue = sParam.substr(nFound2+1);
166185
std::string sName = sParam.substr(0, nFound2);
167-
m_sRequestQueryParams[sName] = sValue; // TODO wrong use map for params
186+
m_vRequestQueryParams.push_back(WSJCppLightWebHttpRequestQueryValue(
187+
this->decodeURIElement(sName),
188+
this->decodeURIElement(sValue)
189+
));
168190
}
169191
}
170192
}
193+
194+
// ----------------------------------------------------------------------
195+
196+
std::string WSJCppLightWebHttpRequest::decodeURIElement(const std::string &sElement) { // TODO move to WSJCppCore
197+
WSJCppLog::warn(TAG, "TODO Implement decodeURIElement");
198+
return sElement;
199+
}

src/wsjcpp_light_web_http_request.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
// ---------------------------------------------------------------------
99

10+
class WSJCppLightWebHttpRequestQueryValue {
11+
public:
12+
WSJCppLightWebHttpRequestQueryValue(const std::string &sName, const std::string &sValue);
13+
std::string getName() const;
14+
std::string getValue() const;
15+
private:
16+
std::string m_sName;
17+
std::string m_sValue;
18+
};
19+
20+
// ---------------------------------------------------------------------
21+
1022
class WSJCppLightWebHttpRequest {
1123
public:
1224
WSJCppLightWebHttpRequest(
@@ -25,12 +37,13 @@ class WSJCppLightWebHttpRequest {
2537
std::string getRequestPath() const;
2638
std::string getRequestBody() const;
2739
std::string getRequestHttpVersion() const;
28-
std::map<std::string,std::string> &getRequestQueryParams(); // TODO redesign to std::vector
40+
const std::vector<WSJCppLightWebHttpRequestQueryValue> &getRequestQueryParams();
2941

3042
private:
3143
std::string TAG;
3244

3345
void parseFirstLine(const std::string &sHeader);
46+
std::string decodeURIElement(const std::string &sElement); // TODO move to WSJCppCore
3447

3548
enum EnumParserState {
3649
START,
@@ -48,7 +61,7 @@ class WSJCppLightWebHttpRequest {
4861
std::string m_sRequestType;
4962
std::string m_sRequestPath;
5063
std::string m_sRequestBody;
51-
std::map<std::string,std::string> m_sRequestQueryParams; // wrong use map for params
64+
std::vector<WSJCppLightWebHttpRequestQueryValue> m_vRequestQueryParams;
5265
std::string m_sRequestHttpVersion;
5366

5467
std::string m_sResponseCacheControl;

unit-tests.wsjcpp/src/unit_test_parse_http_request.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ bool UnitTestParseHttpRequest::run() {
2828
bool enough;
2929
};
3030

31+
struct LPartsOfRequestQueryParam {
32+
LPartsOfRequestQueryParam(std::string sName, std::string sValue) {
33+
this->sName = sName;
34+
this->sValue = sValue;
35+
}
36+
std::string sName;
37+
std::string sValue;
38+
};
39+
3140
struct LTest {
3241
LTest(
3342
int sockFd,
@@ -36,7 +45,8 @@ bool UnitTestParseHttpRequest::run() {
3645
std::string expectedPath,
3746
std::string expectedType,
3847
std::string expectedBody,
39-
std::string expectedHttpVersion
48+
std::string expectedHttpVersion,
49+
std::vector<LPartsOfRequestQueryParam> expectedQueryParams
4050
) {
4151
this->sockFd = sockFd;
4252
this->address = address;
@@ -45,6 +55,7 @@ bool UnitTestParseHttpRequest::run() {
4555
this->expectedType = expectedType;
4656
this->expectedBody = expectedBody;
4757
this->expectedHttpVersion = expectedHttpVersion;
58+
this->expectedQueryParams = expectedQueryParams;
4859
}
4960
int sockFd;
5061
std::string address;
@@ -53,38 +64,50 @@ bool UnitTestParseHttpRequest::run() {
5364
std::string expectedType;
5465
std::string expectedBody;
5566
std::string expectedHttpVersion;
67+
std::vector<LPartsOfRequestQueryParam> expectedQueryParams;
5668
};
5769

5870
std::vector<LTest> tests;
5971
tests.push_back(LTest(0, "some-address", {
6072
LPartsOfRequest("GET /pub/WWW/TheProject.html HTTP/1.1\n", false),
6173
LPartsOfRequest("Host: www.w3.org\n", false),
6274
LPartsOfRequest("\n", true)
63-
}, "/pub/WWW/TheProject.html", "GET", "", "HTTP/1.1"));
75+
}, "/pub/WWW/TheProject.html", "GET", "", "HTTP/1.1", {}));
6476

6577
tests.push_back(LTest(1, "some-address2", {
6678
LPartsOfRequest("GET /pub/WWW/TheProject.html HTTP/1.1\n", false),
6779
LPartsOfRequest("Host: www.w3.org\n", false),
6880
LPartsOfRequest("Content-Length: 1\n", false),
6981
LPartsOfRequest("\n", false),
7082
LPartsOfRequest("1", true)
71-
}, "/pub/WWW/TheProject.html", "GET", "1", "HTTP/1.1"));
83+
}, "/pub/WWW/TheProject.html", "GET", "1", "HTTP/1.1", {}));
7284

7385
tests.push_back(LTest(2, "some-address3", {
7486
LPartsOfRequest("GET /1/../2 HTTP/1.1\n", false),
7587
LPartsOfRequest("Host: www.w3.org\n", false),
7688
LPartsOfRequest("Content-Length: 1\n", false),
7789
LPartsOfRequest("\n", false),
7890
LPartsOfRequest("1", true)
79-
}, "/2", "GET", "1", "HTTP/1.1"));
91+
}, "/2", "GET", "1", "HTTP/1.1", {}));
8092

8193
tests.push_back(LTest(3, "some-address4", {
8294
LPartsOfRequest("POST /1/../../2/ HTTP/1.1\n", false),
8395
LPartsOfRequest("Host: www.w3.org\n", false),
8496
LPartsOfRequest("Content-Length: 10\n", false),
8597
LPartsOfRequest("\n", false),
8698
LPartsOfRequest("{\"some\":1} ", true)
87-
}, "/2/", "POST", "{\"some\":1}", "HTTP/1.1"));
99+
}, "/2/", "POST", "{\"some\":1}", "HTTP/1.1", {}));
100+
101+
tests.push_back(LTest(4, "some-address4", {
102+
LPartsOfRequest("GET /query?somebook=Hello%20sss&somebook=Hello%20ddd&somebook2=dmsf&p4=11_-%dk123 HTTP/1.1\n", false),
103+
LPartsOfRequest("Host: www.w3.org\n", false),
104+
LPartsOfRequest("\n", true),
105+
}, "/query", "GET", "", "HTTP/1.1", {
106+
LPartsOfRequestQueryParam("somebook", "Hello%20sss"),
107+
LPartsOfRequestQueryParam("somebook", "Hello%20ddd"),
108+
LPartsOfRequestQueryParam("somebook2", "dmsf"),
109+
LPartsOfRequestQueryParam("p4", "11_-%dk123")
110+
}));
88111

89112
for (int i = 0; i < tests.size(); i++) {
90113
LTest test = tests[i];
@@ -103,6 +126,19 @@ bool UnitTestParseHttpRequest::run() {
103126
compareS(bTestSuccess, sNTest + " request expected type", request.getRequestType(), test.expectedType);
104127
compareS(bTestSuccess, sNTest + " request expected body", request.getRequestBody(), test.expectedBody);
105128
compareS(bTestSuccess, sNTest + " request expected body", request.getRequestHttpVersion(), test.expectedHttpVersion);
129+
130+
std::vector<WSJCppLightWebHttpRequestQueryValue> params = request.getRequestQueryParams();
131+
for (int i = 0; i < params.size(); i++) {
132+
WSJCppLightWebHttpRequestQueryValue qv = params[i];
133+
std::string sExpectedName = "";
134+
std::string sExpectedValue = "";
135+
if (i < test.expectedQueryParams.size()) {
136+
sExpectedName = test.expectedQueryParams[i].sName;
137+
sExpectedValue = test.expectedQueryParams[i].sValue;
138+
}
139+
compareS(bTestSuccess, sNTest + " request query param" + std::to_string(i) + " name", qv.getName(), sExpectedName);
140+
compareS(bTestSuccess, sNTest + " request query param" + std::to_string(i) + " value", qv.getValue(), sExpectedValue);
141+
}
106142

107143
}
108144
return bTestSuccess;

0 commit comments

Comments
 (0)