Skip to content

Commit 2587050

Browse files
committed
Fixed subfolders
1 parent 2649ff9 commit 2587050

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ int main(int argc, const char* argv[]) {
2929
httpServer.setPort(1234);
3030
httpServer.setMaxWorkers(1);
3131
if (sType == "folder") {
32+
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerWebFolder("/app/", sDir));
3233
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerWebFolder("/", sDir));
3334
} else if (sType == "rewrite") {
35+
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerRewriteFolder("/app/", sDir));
3436
httpServer.addHandler((WSJCppLightWebHttpHandlerBase *)new WSJCppLightWebHttpHandlerRewriteFolder("/", sDir));
3537
}
3638
httpServer.startSync();

src/wsjcpp_light_web_http_handler_rewrite_folder.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ WSJCppLightWebHttpHandlerRewriteFolder::WSJCppLightWebHttpHandlerRewriteFolder(c
77
: WSJCppLightWebHttpHandlerBase("rewrite-folder") {
88

99
TAG = "WSJCppLightWebHttpHandlerRewriteFolder";
10-
m_sPrefixPath = sPrefixPath;
11-
m_sWebFolder = sWebFolder;
10+
m_sPrefixPath = WSJCppCore::doNormalizePath(sPrefixPath + "/");
11+
m_sWebFolder = WSJCppCore::doNormalizePath(sWebFolder + "/");
1212
}
1313

1414
// ----------------------------------------------------------------------
@@ -39,9 +39,9 @@ bool WSJCppLightWebHttpHandlerRewriteFolder::handle(const std::string &sWorkerId
3939
std::string _tag = TAG + "-" + sWorkerId;
4040
std::string sRequestPath = pRequest->getRequestPath();
4141
// WSJCppLog::warn(_tag, pRequest->requestPath());
42+
43+
// cat subfolder
4244
std::string sRequestPath2 = sRequestPath.substr(m_sPrefixPath.length(), sRequestPath.length() - m_sPrefixPath.length());
43-
std::string sFilePath = m_sWebFolder + sRequestPath2;
44-
4545
std::string sFilePath = m_sWebFolder + sRequestPath2;
4646
if (WSJCppCore::fileExists(sFilePath)) {
4747
WSJCppLightWebHttpResponse resp(pRequest->getSockFd());

src/wsjcpp_light_web_http_handler_web_folder.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ WSJCppLightWebHttpHandlerWebFolder::WSJCppLightWebHttpHandlerWebFolder(const std
77
: WSJCppLightWebHttpHandlerBase("web-folder") {
88

99
TAG = "WSJCppLightWebHttpHandlerWebFolder";
10-
m_sPrefixPath = sPrefixPath;
11-
m_sWebFolder = sWebFolder;
10+
m_sPrefixPath = WSJCppCore::doNormalizePath(sPrefixPath + "/");
11+
m_sWebFolder = WSJCppCore::doNormalizePath(sWebFolder + "/");
1212
}
1313

1414
// ----------------------------------------------------------------------
@@ -38,11 +38,15 @@ bool WSJCppLightWebHttpHandlerWebFolder::canHandle(const std::string &sWorkerId,
3838
bool WSJCppLightWebHttpHandlerWebFolder::handle(const std::string &sWorkerId, WSJCppLightWebHttpRequest *pRequest) {
3939
std::string _tag = TAG + "-" + sWorkerId;
4040
std::string sRequestPath = pRequest->getRequestPath();
41-
// WSJCppLog::warn(_tag, pRequest->requestPath());
42-
if (sRequestPath == "/") {
43-
sRequestPath = "/index.html";
41+
// WSJCppLog::warn(_tag, sRequestPath);
42+
std::string sRequestPath2 = sRequestPath.substr(m_sPrefixPath.length(), sRequestPath.length() - m_sPrefixPath.length());
43+
// WSJCppLog::warn(_tag, sRequestPath2);
44+
if (sRequestPath2 == "") {
45+
sRequestPath2 = "index.html";
4446
}
45-
std::string sFilePath = m_sWebFolder + sRequestPath;
47+
std::string sFilePath = m_sWebFolder + sRequestPath2;
48+
// WSJCppLog::warn(_tag, sFilePath);
49+
4650
if (WSJCppCore::fileExists(sFilePath)) {
4751
WSJCppLightWebHttpResponse resp(pRequest->getSockFd());
4852
resp.cacheSec(60).ok().sendFile(sFilePath);

0 commit comments

Comments
 (0)