-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathplugin_dir.c
More file actions
148 lines (126 loc) · 3.79 KB
/
Copy pathplugin_dir.c
File metadata and controls
148 lines (126 loc) · 3.79 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
static int dir2html(char *path, char *uri, char *buf, int len)
{
int ret = 0;
struct dirent *dp = NULL;
char file[FILENAME_MAX], size[64], mod[64];
struct stat st;
const char *slash = "";
DIR *dirp = opendir(path);
if(NULL == dirp){
return 0;
}
ret += snprintf((!buf ? NULL : buf + ret), (!len ? 0 : len - ret),
"<html><head><title>Index of %s</title>"
"<style>th {text-align: left;}</style></head>"
"<body><h1>Index of %s</h1><pre><table cellpadding=\"0\">\n"
"<tr><th>Name</th><th>Modified</th><th>Size</th></tr>"
"<tr><td colspan=\"3\"><hr></td></tr>\n",
uri, uri);
while((dp = readdir(dirp))){
if(!strcmp(dp->d_name, ".")){continue;}
snprintf(file, sizeof(file), "%s/%s", path, dp->d_name);
stat(file, &st);
strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&st.st_mtime));
if (S_ISDIR(st.st_mode)) {
snprintf(size,sizeof(size),"%s","<DIR>");
} else {
if (st.st_size < 1024)
snprintf(size, sizeof(size), "%lu", (unsigned long) st.st_size);
else if (st.st_size < 1024 * 1024)
snprintf(size, sizeof(size), "%luk", (unsigned long) (st.st_size >> 10) + 1);
else
snprintf(size, sizeof(size), "%.1fM", (float) st.st_size / 1048576);
}
ret += snprintf((!buf ? NULL : buf + ret), (!len ? 0 : len - ret),
"<tr><td><a href=\"%s%s%s\">%s%s</a></td>"
"<td> %s</td><td> %s</td></tr>\n",
uri, slash, dp->d_name, dp->d_name,
S_ISDIR(st.st_mode) ? "/" : "", mod, size);
}
ret += snprintf((!buf ? NULL : buf + ret), (!len ? 0 : len - ret),
"</table></body></html>\n");
closedir(dirp);
return ret;
}
static int dir_list_local(HTTPContext *c)
{
struct stat st;
int ret = stat(c->url, &st);
char *path = NULL, uri[FILENAME_MAX] = "";
if(ret < 0 && !strcmp(c->url, "index.html")){
path = "."; snprintf(uri, sizeof(uri), "/");
}else if(ret == 0 && S_ISDIR(st.st_mode)){
path = c->url; snprintf(uri, sizeof(uri), "/%s/", c->url);
}
if(!path){
return 0;
}
char head[512], *charset = "utf-8";
#if defined(_WIN32)
charset = "gbk"; /*todo: detect from content.*/
#endif
int hlen;
int len = dir2html(path, uri, NULL, 0);
hlen = snprintf(head, sizeof(head),
"HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Content-Type: text/html; charset=%s\r\n"
"Content-Length: %d\r\n\r\n",
charset, len);
c->pb_buffer = av_malloc(hlen+len);
if(!c->pb_buffer){
c->buffer_ptr = c->buffer;
c->buffer_end = c->buffer;
return 0;
}
memcpy(c->pb_buffer, head, hlen);
dir2html(path, uri, c->pb_buffer+hlen, len);
c->buffer_ptr = c->pb_buffer;
c->buffer_end = c->pb_buffer + hlen + (c->only_header ? 0 : len);
c->http_error = 200;
c->local_fd = -1;
return 1;
}
static int dir_delete_file(HTTPContext *c)
{
struct stat st;
int code = 200;
int ret = stat(c->url, &st);
if(ret < 0){
code = 404;
}else{
int r2 = remove(c->url);
code = r2 ? 400 : 200;
}
return code;
}
static int dir_is_modifed(HTTPContext *c, struct stat *stp, char *dt, char *lm, char *etag, int tag_len)
{/*return 1 if modifed*/
int ret = 0;
const char *fmt = "%a, %d %b %Y %H:%M:%S GMT";
time_t tm0;
if(dt){
time_t tm = time(NULL);
strftime(dt, tag_len, fmt, localtime(&tm));
}
if(!lm || !etag || tag_len < 8+1+1+1){
printf("dir bad arg %d\n", tag_len);
return ret;
}
strftime(lm, tag_len, fmt, localtime(&stp->st_mtime));
snprintf(etag, tag_len, "\"%lx.%lx\"", (unsigned long)stp->st_mtime, (unsigned long)stp->st_size);
if(c->inm){
if(!strcmp(c->inm, "*")){//always match
ret = 0;
}else if(!strncmp(c->inm, "W/", 2)){//check sematics
ret = strcmp(c->inm+2, etag) ? 1 : 0;
}else{//check byte-to-byte
ret = strcmp(c->inm, etag) ? 1 : 0;
}
}else if(c->ims && strptime(c->ims, fmt, &tm0)){
ret = tm0 < stp->st_mtime < 0 ? 1 : 0;
}else{
ret = 1;
}
return ret;
}