@@ -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
2775Example init, add handler and start server
2876```
29- LightHttpServer httpServer;
77+ WSJCppLightWebServer httpServer;
3078httpServer.setPort(1234);
3179httpServer.setMaxWorkers(1);
32- httpServer.handlers()->add(( WSJCppLightWebHttpHandlerBase *)new HttpHandlerWebFolderExample( "./web"));
80+ httpServer.addHandler(( WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerWebFolder("/app2/", "./web"));
3381httpServer.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
3888Define 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+ ```
0 commit comments