Skip to content

Commit cacdac4

Browse files
committed
Fixed #7
1 parent 4d5fb53 commit cacdac4

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed

src/http_handlers/http_handler_web_folder_example.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ bool HttpHandlerWebFolderExample::handle(const std::string &sWorkerId, WSJCppLig
4545

4646
std::string sFilePath = m_sWebFolder + sRequestPath; // TODO check /../ in path
4747
if (WSJCppCore::fileExists(sFilePath)) {
48-
WSJCppLightWebHttpResponse resp(pRequest->sockFd());
48+
WSJCppLightWebHttpResponse resp(pRequest->getSockFd());
4949
resp.cacheSec(60).ok().sendFile(sFilePath);
5050
} else {
5151
std::string sFilePath = m_sWebFolder + "/index.html";
52-
WSJCppLightWebHttpResponse resp(pRequest->sockFd());
52+
WSJCppLightWebHttpResponse resp(pRequest->getSockFd());
5353
resp.cacheSec(60).ok().sendFile(sFilePath);
5454
}
5555
return true;

src/wsjcpp_light_web_http_request.cpp

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

1919
WSJCppLightWebHttpRequest::WSJCppLightWebHttpRequest(int nSockFd, const std::string &sAddress) {
2020
TAG = "WSJCppLightWebHttpRequest";
21+
m_sUniqueId = WSJCppCore::createUuid();
2122
m_nSockFd = nSockFd;
2223
m_sAddress = sAddress;
2324
m_bClosed = false;
@@ -30,12 +31,18 @@ WSJCppLightWebHttpRequest::WSJCppLightWebHttpRequest(int nSockFd, const std::str
3031

3132
// ----------------------------------------------------------------------
3233

33-
int WSJCppLightWebHttpRequest::sockFd() {
34+
int WSJCppLightWebHttpRequest::getSockFd() {
3435
return m_nSockFd;
3536
}
3637

3738
// ----------------------------------------------------------------------
3839

40+
std::string WSJCppLightWebHttpRequest::getUniqueId() {
41+
return m_sUniqueId;
42+
}
43+
44+
// ----------------------------------------------------------------------
45+
3946
std::string WSJCppLightWebHttpRequest::requestType() {
4047
return m_sRequestType;
4148
}
@@ -60,7 +67,7 @@ std::map<std::string,std::string> &WSJCppLightWebHttpRequest::requestQueryParams
6067

6168
// ----------------------------------------------------------------------
6269

63-
std::string WSJCppLightWebHttpRequest::address() {
70+
std::string WSJCppLightWebHttpRequest::getAddress() {
6471
return m_sAddress;
6572
}
6673

src/wsjcpp_light_web_http_request.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ class WSJCppLightWebHttpRequest {
3030
);
3131
~WSJCppLightWebHttpRequest() {};
3232

33-
int sockFd();
33+
int getSockFd();
34+
std::string getUniqueId();
3435
void appendRecieveRequest(const std::string &sRequestPart);
3536
bool isEnoughAppendReceived();
3637

37-
std::string address();
38+
std::string getAddress();
3839
std::string requestType();
3940
std::string requestPath();
4041
std::string requestBody();
@@ -56,6 +57,7 @@ class WSJCppLightWebHttpRequest {
5657
EnumParserState m_nParserState;
5758
std::vector<std::string> m_vHeaders;
5859
int m_nContentLength;
60+
std::string m_sUniqueId;
5961
std::string m_sRequest;
6062
std::string m_sAddress;
6163
std::string m_sRequestType;

src/wsjcpp_light_web_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void WSJCppLightWebHttpThreadWorker::run() {
116116
bool bExists = pInfo != nullptr;
117117
// TODO refactor
118118
if (bExists) {
119-
int nSockFd = pInfo->sockFd();
119+
int nSockFd = pInfo->getSockFd();
120120

121121
// set timeout options
122122
struct timeval timeout;

unit-tests.wsjcpp/src/unit_test_parse_http_request.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,60 @@ void UnitTestParseHttpRequest::init() {
1919

2020
bool UnitTestParseHttpRequest::run() {
2121
bool bTestSuccess = true;
22-
// compareB(bTestSuccess, "Not implemented", true, false);
22+
struct LPartsOfRequest {
23+
LPartsOfRequest(std::string sPart, bool enough) {
24+
this->sPart = sPart;
25+
this->enough = enough;
26+
}
27+
std::string sPart;
28+
bool enough;
29+
};
30+
31+
struct LTest {
32+
LTest(
33+
int sockFd,
34+
std::string address,
35+
std::vector<LPartsOfRequest> parts
36+
) {
37+
this->sockFd = sockFd;
38+
this->address = address;
39+
this->parts = parts;
40+
}
41+
int sockFd;
42+
std::string address;
43+
std::vector<LPartsOfRequest> parts;
44+
};
45+
46+
std::vector<LTest> tests;
47+
tests.push_back(LTest(0, "some-address", {
48+
LPartsOfRequest("GET /pub/WWW/TheProject.html HTTP/1.1\n", false),
49+
LPartsOfRequest("Host: www.w3.org\n", false),
50+
LPartsOfRequest("Content-Length: 0\n", false),
51+
LPartsOfRequest("\n", true)
52+
}));
53+
54+
tests.push_back(LTest(1, "some-address2", {
55+
LPartsOfRequest("GET /pub/WWW/TheProject.html HTTP/1.1\n", false),
56+
LPartsOfRequest("Host: www.w3.org\n", false),
57+
LPartsOfRequest("Content-Length: 1\n", false),
58+
LPartsOfRequest("\n", false),
59+
LPartsOfRequest("1", true)
60+
}));
61+
62+
for (int i = 0; i < tests.size(); i++) {
63+
LTest test = tests[i];
64+
WSJCppLightWebHttpRequest request(test.sockFd, test.address);
65+
std::string sNTest = "test" + std::to_string(i) + "#";
66+
67+
compareS(bTestSuccess, sNTest + " request address", request.getAddress(), test.address);
68+
compareN(bTestSuccess, sNTest + " request sockfd", request.getSockFd(), test.sockFd);
69+
70+
for (int n = 0; n < test.parts.size(); n++) {
71+
std::string sNTest2 = sNTest + " append part" + std::to_string(n) + "#";
72+
request.appendRecieveRequest(test.parts[n].sPart);
73+
compareB(bTestSuccess, sNTest2, request.isEnoughAppendReceived(), test.parts[n].enough);
74+
}
75+
}
2376
return bTestSuccess;
2477
}
2578

0 commit comments

Comments
 (0)