forked from Hattaki-Hajar/webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoindex.cpp
45 lines (44 loc) · 989 Bytes
/
autoindex.cpp
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
#include "Webserv.hpp"
void find_files(const std::string &_uri)
{
DIR *dir;
struct dirent *d;
struct stat s;
char buff[18];
std::string file, name, path;
file = "<!DOCTYPE html>\n<html>\n<head>\n<title>Index of ";
file += _uri;
file += "</title>\n</head>\n<body>\n<h1>Index of ";
file += _uri + "</h1>\n<hr>\n<pre>\n";
dir = opendir(_uri.c_str());
if (dir)
{
d = readdir(dir);
while (d)
{
name = d->d_name;
if (name == ".") {
d = readdir(dir);
continue ;
}
path = _uri + "/" + name;
if (stat(path.c_str(), &s) != 0) {
std::cout << "fail" << std::endl;
closedir(dir);
return ;
}
file += "<a href=" + name;
if (d->d_type == DT_DIR)
file += "/>" + name + "/</a>";
else
file += ">" + name + "</a>";
strftime(buff, sizeof(buff), "%d-%b-%Y %H:%M", gmtime(&s.st_mtim.tv_sec));
file += " ";
file += buff;
file += "\n";
d = readdir(dir);
}
file += "</pre>\n<hr>\n</body>\n</html>";
}
closedir(dir);
}