Skip to content

Commit 1093129

Browse files
committed
Redesign classes, added default rewrite/simple folder, fixed #6
1 parent cacdac4 commit 1093129

26 files changed

+536
-310
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tmp/*
22
wsjcpp-light-web-server
33
.logs/*
4-
__pycache__
4+
__pycache__
5+
.vscode/*

CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_http_request.h")
1515
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_http_request.cpp")
1616
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_http_response.cpp")
1717
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_http_response.h")
18+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_deque_http_requests.h")
19+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_deque_http_requests.cpp")
20+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_http_handler_rewrite_folder.h")
21+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_http_handler_rewrite_folder.cpp")
22+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_http_handler_web_folder.h")
23+
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_http_handler_web_folder.cpp")
1824
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_server.h")
1925
list (APPEND WSJCPP_SOURCES "src/wsjcpp_light_web_server.cpp")
2026

21-
## Http Handlers
22-
list (APPEND WSJCPP_SOURCES "src/http_handlers/http_handler_web_folder_example.h")
23-
list (APPEND WSJCPP_SOURCES "src/http_handlers/http_handler_web_folder_example.cpp")
24-
2527
include_directories(${WSJCPP_INCLUDE_DIRS})
2628

2729
add_executable (wsjcpp-light-web-server ${WSJCPP_SOURCES})

README.md

Lines changed: 100 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,101 +12,159 @@ Just include this files:
1212
- src/wsjcpp_light_web_http_request.cpp
1313
- src/wsjcpp_light_web_http_response.h
1414
- src/wsjcpp_light_web_http_response.cpp
15+
- src/wsjcpp_light_web_http_handler_rewrite_folder.h
16+
- src/wsjcpp_light_web_http_handler_rewrite_folder.cpp
17+
- src/wsjcpp_light_web_http_handler_web_folder.h
18+
- src/wsjcpp_light_web_http_handler_web_folder.cpp
1519
- src/wsjcpp_light_web_server.h
1620
- src/wsjcpp_light_web_server.cpp
1721

18-
1922
## Integrate via wsjcpp
2023

2124
```
2225
$ wsjcpp install https://github.com/wsjcpp/wsjcpp-light-web-server:master
2326
```
2427

25-
# Http Handlers examples
28+
## Examples
29+
30+
### Example for simular rewrute_rules
31+
32+
Simpular apache:
33+
```
34+
RewriteCond %{REQUEST_FILENAME} !-f
35+
RewriteCond %{REQUEST_FILENAME} !-d
36+
RewriteRule . index.html
37+
```
38+
39+
Contains base handler:
40+
```
41+
WSJCppLightWebHttpHandlerRewriteFolder(sPrefixPath, sDirPath)
42+
```
43+
Where
44+
* sPrefixPath - like "/app1/" -> "http://localhost:1234/app1/"
45+
* sDirPath - relative or absulte path to folder with web files
46+
47+
Specific: if file will be not found in web folder so will be returned file index.html
48+
49+
Will be good for single-web-app (like angular)
50+
51+
Example init, add handler and start server
52+
```
53+
WSJCppLightWebServer httpServer;
54+
httpServer.setPort(1234);
55+
httpServer.setMaxWorkers(1);
56+
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerRewriteFolder("/app1/", "./web"));
57+
httpServer.startSync(); // this method will be hold current thread, if you with you can call just start/stop command
58+
```
59+
60+
After compile and start will be available on `http://localhost:1234/app1/`
61+
62+
### Example simple web folder
63+
64+
Contains base handler:
65+
```
66+
WSJCppLightWebHttpHandlerWebFolder(sPrefixPath, sDirPath)
67+
```
68+
69+
Where
70+
* sPrefixPath - like "/app2/" -> "http://localhost:1234/app2/"
71+
* sDirPath - relative or absulte path to folder with web files
72+
73+
Specific: if file does not exists wil be returned 404 not found
2674

2775
Example init, add handler and start server
2876
```
29-
LightHttpServer httpServer;
77+
WSJCppLightWebServer httpServer;
3078
httpServer.setPort(1234);
3179
httpServer.setMaxWorkers(1);
32-
httpServer.handlers()->add((WSJCppLightWebHttpHandlerBase *)new HttpHandlerWebFolderExample("./web"));
80+
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerWebFolder("/app2/", "./web"));
3381
httpServer.startSync(); // this method will be hold current thread, if you with you can call just start/stop command
3482
```
3583

36-
## Web Folder example
84+
After compile and start will be available on `http://localhost:1234/app2/`
85+
86+
### Example custom handler
3787

3888
Define class:
3989

90+
header-file:
4091
```
92+
#ifndef HTTP_HANDLER_CUSTOM_H
93+
#define HTTP_HANDLER_CUSTOM_H
94+
4195
#include <wsjcpp_light_web_server.h>
4296
43-
class HttpHandlerWebFolderExample : WSJCppLightWebHttpHandlerBase {
97+
class HttpHandlerCustom : WSJCppLightWebHttpHandlerBase {
4498
public:
45-
HttpHandlerWebFolderExample(const std::string &sWebFolder);
99+
HttpHandlerCustom();
46100
virtual bool canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest);
47101
virtual bool handle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest);
48102
49103
private:
50104
std::string TAG;
51-
std::string m_sWebFolder;
52105
};
106+
#endif // HTTP_HANDLER_CUSTOM_H
53107
```
54108

55-
Class implementation:
109+
source-file:
56110
```
57111
#include <wsjcpp_core.h>
58112
59113
// ----------------------------------------------------------------------
60114
61-
HttpHandlerWebFolderExample::HttpHandlerWebFolderExample(const std::string &sWebFolder)
62-
: WSJCppLightWebHttpHandlerBase("web-folder") {
115+
HttpHandlerCustom::HttpHandlerCustom()
116+
: WSJCppLightWebHttpHandlerBase("custom") {
63117
64-
TAG = "HttpHandlerWebFolderExample";
65-
m_sWebFolder = sWebFolder;
118+
TAG = "HttpHandlerCustom";
66119
}
67120
68121
// ----------------------------------------------------------------------
69122
70-
bool HttpHandlerWebFolderExample::canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest) {
123+
bool HttpHandlerCustom::canHandle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest) {
71124
std::string _tag = TAG + "-" + sWorkerId;
72-
// WSJCppLog::warn(_tag, "canHandle: " + pRequest->requestPath());
73-
std::string sRequestPath = pRequest->requestPath();
74-
75-
if (sRequestPath == "") {
76-
sRequestPath = "/";
77-
WSJCppLog::warn(_tag, "Request path is empty");
78-
}
125+
std::string sRequestPath = pRequest->getRequestPath();
79126
80-
if (sRequestPath == "/") {
81-
sRequestPath = "/index.html";
127+
if (sRequestPath == "/custom/" || sRequestPath == "/custom") {
128+
return true;
82129
}
83-
84-
if (!WSJCppCore::dirExists(m_sWebFolder)) {
85-
WSJCppLog::warn(_tag, "Directory " + m_sWebFolder + " does not exists");
130+
if (sRequestPath == "/custom1/" || sRequestPath == "/custom1") {
131+
return true;
86132
}
87-
return true;
133+
return false;
88134
}
89135
90136
// ----------------------------------------------------------------------
91137
92-
bool HttpHandlerWebFolderExample::handle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest) {
138+
bool HttpHandlerCustom::handle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest) {
93139
std::string _tag = TAG + "-" + sWorkerId;
94-
std::string sRequestPath = pRequest->requestPath();
95-
// WSJCppLog::warn(_tag, pRequest->requestPath());
140+
std::string sRequestPath = pRequest->getRequestPath();
141+
// WSJCppLog::warn(_tag, sRequestPath);
96142
97-
if (sRequestPath == "") {
98-
sRequestPath = "/";
99-
}
100-
101-
std::string sFilePath = m_sWebFolder + sRequestPath; // TODO check /../ in path
102-
if (WSJCppCore::fileExists(sFilePath)) {
103-
WSJCppLightWebHttpResponse resp(pRequest->sockFd());
104-
resp.cacheSec(60).ok().sendFile(sFilePath);
143+
WSJCppLightWebHttpResponse resp(pRequest->sockFd());
144+
if (sRequestPath == "/custom" || sRequestPath == "/custom/") {
145+
resp.cacheSec(60).ok().sendText(
146+
"<h1>This is custom</h1>"
147+
);
148+
} else if (sRequestPath == "/custom1" || sRequestPath == "/custom1/") {
149+
resp.cacheSec(60).ok().sendText(
150+
"<h1>But this is custom1</h1>"
151+
);
105152
} else {
106-
std::string sFilePath = m_sWebFolder + "/index.html";
107-
WSJCppLightWebHttpResponse resp(pRequest->sockFd());
108-
resp.cacheSec(60).ok().sendFile(sFilePath);
153+
resp.noCache().ok().sendText(
154+
"<h1>Unknown</h1>"
155+
);
109156
}
110157
return true;
111158
}
112-
```
159+
```
160+
161+
Example init, add handler and start server
162+
__order is important! Server will call canHandle & handle in same order as addHandler called__
163+
```
164+
WSJCppLightWebServer httpServer;
165+
httpServer.setPort(1234);
166+
httpServer.setMaxWorkers(1);
167+
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new HttpHandlerCustom());
168+
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerRewriteFolder("/", "./web"));
169+
httpServer.startSync(); // this method will be hold current thread, if you with you can call just start/stop command
170+
```

server-tests/run_tests.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
try:
1111
print(" > > > TESTS: begin ")
12-
liblightwebservertest.start_server("../", ["./wsjcpp-light-web-server", "start"], 1234)
13-
12+
liblightwebservertest.start_server("../", ["./wsjcpp-light-web-server", "folder", "./web"], 1234)
1413
print("index.html - testing...")
1514
index_html = open("../web/index.html", 'r').read()
1615
r = requests.get(TEST_URL)
@@ -44,7 +43,11 @@
4443
else:
4544
print("index.css - ok")
4645

47-
46+
print("not-found - testing...")
47+
r = requests.get(TEST_URL + "/not-found 0101")
48+
if r.status_code != 404:
49+
raise Exception("not-found - Wrong status code expected 404, but got " + str(r.status_code))
50+
print("index.css - ok")
4851

4952
finally:
5053
liblightwebservertest.stop_server()

src/http_handlers/http_handler_web_folder_example.h

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/main.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
#include <string.h>
2-
#include <iostream>
3-
#include <algorithm>
41
#include <wsjcpp_core.h>
52
#include <wsjcpp_light_web_server.h>
6-
#include "http_handlers/http_handler_web_folder_example.h"
3+
#include "wsjcpp_light_web_http_handler_rewrite_folder.h"
4+
#include "wsjcpp_light_web_http_handler_web_folder.h"
75

86
int main(int argc, const char* argv[]) {
97
std::string TAG = "MAIN";
@@ -15,11 +13,26 @@ int main(int argc, const char* argv[]) {
1513
WSJCppLog::setPrefixLogFile("wsjcpp");
1614
WSJCppLog::setLogDirectory(".logs");
1715

16+
if (argc != 3) {
17+
std::cout << "Usage: " << argv[0] << " [folder|rewrite] <dir>" << std::endl;
18+
return -1;
19+
}
20+
std::string sType = std::string(argv[1]);
21+
if (sType != "folder" && sType != "rewrite") {
22+
std::cout << "Usage: " << argv[0] << " [folder|rewrite] <dir>" << std::endl;
23+
return -1;
24+
}
25+
26+
std::string sDir = std::string(argv[2]);
27+
1828
WSJCppLightWebServer httpServer;
1929
httpServer.setPort(1234);
2030
httpServer.setMaxWorkers(1);
21-
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new HttpHandlerWebFolderExample("./web"));
22-
31+
if (sType == "folder") {
32+
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerWebFolder("/", sDir));
33+
} else if (sType == "rewrite") {
34+
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerRewriteFolder("/", sDir));
35+
}
2336
httpServer.startSync();
2437
return 0;
2538
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "wsjcpp_light_web_deque_http_requests.h"
2+
3+
#include <wsjcpp_core.h>
4+
5+
// ----------------------------------------------------------------------
6+
// WSJCppLightWebDequeHttpRequests
7+
8+
WSJCppLightWebDequeHttpRequests::WSJCppLightWebDequeHttpRequests() {
9+
TAG = "WSJCppLightWebDequeHttpRequests";
10+
}
11+
12+
// ----------------------------------------------------------------------
13+
14+
WSJCppLightWebHttpRequest *WSJCppLightWebDequeHttpRequests::popRequest() {
15+
if (m_dequeRequests.size() == 0) {
16+
m_mtxWaiterRequests.lock();
17+
}
18+
std::lock_guard<std::mutex> guard(this->m_mtxDequeRequests);
19+
WSJCppLightWebHttpRequest *pRequest = nullptr;
20+
int nSize = m_dequeRequests.size();
21+
if (nSize > 0) {
22+
pRequest = m_dequeRequests.back();
23+
m_dequeRequests.pop_back();
24+
}
25+
return pRequest;
26+
}
27+
28+
// ----------------------------------------------------------------------
29+
30+
void WSJCppLightWebDequeHttpRequests::pushRequest(WSJCppLightWebHttpRequest *pRequest) {
31+
{
32+
std::lock_guard<std::mutex> guard(this->m_mtxDequeRequests);
33+
if (m_dequeRequests.size() > 20) {
34+
WSJCppLog::warn(TAG, " deque more than " + std::to_string(m_dequeRequests.size()));
35+
}
36+
m_dequeRequests.push_front(pRequest);
37+
}
38+
39+
if (m_dequeRequests.size() == 1) {
40+
m_mtxWaiterRequests.unlock();
41+
}
42+
}
43+
44+
// ----------------------------------------------------------------------
45+
46+
void WSJCppLightWebDequeHttpRequests::cleanup() {
47+
std::lock_guard<std::mutex> guard(this->m_mtxDequeRequests);
48+
while (m_dequeRequests.size() > 0) {
49+
delete m_dequeRequests.back();
50+
m_dequeRequests.pop_back();
51+
}
52+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef WSJCPP_LIGHT_WEB_DEQUE_HTTP_REQUESTS_H
2+
#define WSJCPP_LIGHT_WEB_DEQUE_HTTP_REQUESTS_H
3+
4+
#include <string>
5+
#include <deque>
6+
#include <mutex>
7+
8+
#include "wsjcpp_light_web_http_request.h"
9+
10+
// ---------------------------------------------------------------------
11+
12+
class WSJCppLightWebDequeHttpRequests {
13+
public:
14+
WSJCppLightWebDequeHttpRequests();
15+
WSJCppLightWebHttpRequest *popRequest();
16+
void pushRequest(WSJCppLightWebHttpRequest *pRequest);
17+
void cleanup();
18+
19+
private:
20+
std::string TAG;
21+
22+
std::mutex m_mtxDequeRequests;
23+
std::mutex m_mtxWaiterRequests;
24+
std::deque<WSJCppLightWebHttpRequest *> m_dequeRequests;
25+
};
26+
27+
#endif // WSJCPP_LIGHT_WEB_DEQUE_HTTP_REQUESTS_H
28+
29+

0 commit comments

Comments
 (0)