@@ -24,8 +24,6 @@ void make_http_status_page(http_status status_code, std::string& page) {
24
24
}
25
25
26
26
void make_index_of_page (const char * dir, std::string& page, const char * url) {
27
- std::list<hdir_t > dirs;
28
- listdir (dir, dirs);
29
27
char c_str[1024 ] = {0 };
30
28
snprintf (c_str, sizeof (c_str), R"( <!DOCTYPE html>
31
29
<html>
@@ -35,26 +33,41 @@ void make_index_of_page(const char* dir, std::string& page, const char* url) {
35
33
<body>
36
34
<h1>Index of %s</h1>
37
35
<hr>
38
- <pre>
39
36
)" , url, url);
40
37
page += c_str;
38
+
39
+ page += " <table border=\" 0\" >\n " ;
40
+ page += R"( <tr>
41
+ <th align="left" width="30%">Name</th>
42
+ <th align="left" width="20%">Date</th>
43
+ <th align="left" width="20%">Size</th>
44
+ </tr>
45
+ )" ;
46
+
47
+ #define _ADD_TD_ (page, td ) \
48
+ page += " <td>" ; \
49
+ page += td; \
50
+ page += " </td>\n " ; \
51
+
52
+ std::list<hdir_t > dirs;
53
+ listdir (dir, dirs);
41
54
for (auto & item : dirs) {
42
55
if (item.name [0 ] == ' .' && item.name [1 ] == ' \0 ' ) continue ;
56
+ page += " <tr>\n " ;
43
57
int len = strlen (item.name ) + (item.type == ' d' );
44
58
// name
45
59
snprintf (c_str, sizeof (c_str), " <a href=\" %s%s\" >%s%s</a>" ,
46
60
item.name ,
47
61
item.type == ' d' ? " /" : " " ,
48
62
len < AUTOINDEX_FILENAME_MAXLEN ? item.name : std::string (item.name , item.name +AUTOINDEX_FILENAME_MAXLEN-4 ).append (" ..." ).c_str (),
49
63
item.type == ' d' ? " /" : " " );
50
- page += c_str;
64
+ _ADD_TD_ ( page, c_str)
51
65
if (strcmp (item.name , " .." ) != 0 ) {
52
66
// mtime
53
67
struct tm * tm = localtime (&item.mtime );
54
- snprintf (c_str, sizeof (c_str), " %04d-%02d-%02d %02d:%02d:%02d " ,
68
+ snprintf (c_str, sizeof (c_str), " %04d-%02d-%02d %02d:%02d:%02d" ,
55
69
tm ->tm_year +1900 , tm ->tm_mon +1 , tm ->tm_mday , tm ->tm_hour , tm ->tm_min , tm ->tm_sec );
56
- page += std::string (AUTOINDEX_FILENAME_MAXLEN - len, ' ' );
57
- page += c_str;
70
+ _ADD_TD_ (page, c_str)
58
71
// size
59
72
if (item.type == ' d' ) {
60
73
page += ' -' ;
@@ -74,13 +87,17 @@ void make_index_of_page(const char* dir, std::string& page, const char* url) {
74
87
hsize /= 1024 .0f ;
75
88
snprintf (c_str, sizeof (c_str), " %.1fG" , hsize);
76
89
}
77
- page += c_str;
90
+ _ADD_TD_ ( page, c_str)
78
91
}
79
92
}
80
- page += " \r \n" ;
93
+ page += " </tr> \n " ;
81
94
}
82
- page += R"( </pre>
95
+
96
+ #undef _ADD_TD_
97
+
98
+ page += R"( </table>
83
99
<hr>
84
100
</body>
85
- </html>)" ;
101
+ </html>
102
+ )" ;
86
103
}
0 commit comments