forked from xaxaxa/workspace
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdir_list.cppsm
More file actions
131 lines (125 loc) · 3.53 KB
/
dir_list.cppsm
File metadata and controls
131 lines (125 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<%#
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
* */
//cppsp module to enable directory listing
#include <sys/stat.h>
#define TO_C_STR(in,inLen,out) char out[inLen+1];\
memcpy(out,in,inLen);\
out[inLen]=0;
bool compareString(String s1, String s2) {
return s1.alphabeticalCompare(s2)<0;
}
struct DirListModule: public RGC::Object {
DelegateChain<AsyncValue<Handler>(String)>::item* it;
Server* server;
String filePath;
struct redirectHandler: public RGC::Object
{
Delegate<void()> cb;
void handleRequest(Request& req, Response& resp, Delegate<void()> cb) {
this->cb=cb;
resp.headers["Location"]=concat(*req.sp,req.path,"/");
resp.statusName="Moved permanently";
resp.statusCode=301;
resp.finalize({&redirectHandler::flushCB,this});
}
void flushCB(Response& resp) {
auto Cb=cb;
destruct();
Cb();
}
};
void handleRedirect(Request& req, Response& resp, Delegate<void()> cb) {
req.sp->New<redirectHandler>()->handleRequest(req,resp,cb);
}
AsyncValue<Handler> routeRequest(String path) {
struct stat st;
string s=server->mapPath(path.toSTDString());
if(::stat(s.c_str(),&st)>=0 && S_ISDIR(st.st_mode)) {
if(path.length()>0 && path.data()[path.length()-1]!='/') {
Handler h({&DirListModule::handleRedirect,this});
return h;
}
//redirect to self
return server->routeDynamicRequestFromFile(filePath);
}
return (*it->prev)(path);
}
DirListModule(ModuleParams& p) {
server=p.server;
filePath=p.filePath;
it=server->routeRequest.attach({&DirListModule::routeRequest,this});
}
~DirListModule() {
server->routeRequest.detach(it);
}
};
extern "C" void* initModule(ModuleParams& p) {
return new DirListModule(p);
}
extern "C" void deinitModule(void* mod) {
delete (DirListModule*)mod;
}
%><%
string path=server->mapPath(request->path.toSTDString());
%>
<!DOCTYPE HTML>
<html>
<head>
<title>Index of <% htmlEscape(request->path,output); %></title>
</head>
<body>
<h1 style="margin-top: 2px;">Index of <% htmlEscape(request->path,output); %></h1>
<table>
<tr>
<th>Name</th>
<th>Last modified</th>
<th>Size</th>
</tr>
<tr>
<th colspan="5"><hr /></th>
</tr>
<%
vector<String> list;
auto tmp=[&](const char* name) {
list.push_back(sp->addString(name));
};
listDirectory(path.c_str(), &tmp);
std::sort(list.begin(),list.end(),compareString);
for(int i=0;i<list.size();i++) {
String name=list[i];
struct stat st;
char p[path.length()+name.length()+1];
p[combinePathChroot(path.data(),path.length(),name.data(),name.length(),p)]=0;
if(stat(p,&st)<0) continue;
time_t rawtime=st.st_mtime;
struct tm * timeinfo;
char buffer[256];
timeinfo = localtime (&rawtime);
strftime(buffer,sizeof(buffer),"%F %r",timeinfo);
%>
<tr>
<td>
<a href="<% htmlAttributeEscape(name,output); %><%if(S_ISDIR(st.st_mode)){%>/<%}%>">
<% htmlEscape(name,output); %><%if(S_ISDIR(st.st_mode)){%>/<%}%>
</a>
</td>
<td align="right"><%=buffer%></td>
<td align="right"><%=(int64_t)st.st_size%></td>
</tr>
<%
}
%>
</table>
</body>
</html>