-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtserver.c
89 lines (75 loc) · 1.9 KB
/
tserver.c
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
#include "includes.h"
static struct cgi_state *cgi;
/* this is a helper function for file upload. The scripts can call
@save_file(cgi_variablename, filename) to save the contents of
an uploaded file to disk
*/
static void save_file(struct template_state *tmpl,
const char *name, const char *value,
int argc, char **argv)
{
char *var_name, *file_name;
int fd;
const char *content;
unsigned size, ret;
if (argc != 2) {
printf("Invalid arguments to function %s (%d)\n", name, argc);
return;
}
var_name = argv[0];
file_name = argv[1];
content = cgi->get_content(cgi, var_name, &size);
if (!content) {
printf("No content for variable %s?\n", var_name);
return;
}
fd = open(file_name, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (fd == -1) {
printf("Failed to open %s (%s)\n", file_name, strerror(errno));
return;
}
ret = write(fd, content, size);
if (ret != size) {
printf("out of space writing %s (wrote %u)\n", file_name, ret);
}
close(fd);
}
/* the main webserver process, called with stdin and stdout setup
*/
static void run_webserver(void)
{
struct stat st;
if (chdir("html") != 0) {
fprintf(stderr,"Can't find html directory?\n");
exit(1);
}
cgi = cgi_init();
cgi->setup(cgi);
cgi->load_variables(cgi);
cgi->tmpl->put(cgi->tmpl, "save_file", "", save_file);
/* handle a direct file download */
if (!strstr(cgi->pathinfo, "..") && *cgi->pathinfo != '/' &&
stat(cgi->pathinfo, &st) == 0 && S_ISREG(st.st_mode)) {
cgi->download(cgi, cgi->pathinfo);
cgi->destroy(cgi);
return;
}
cgi->download(cgi, "index.html");
cgi->destroy(cgi);
}
/* main program, just start listening and answering queries */
int main(int argc, char *argv[])
{
int port = TSERVER_PORT;
extern char *optarg;
int opt;
while ((opt=getopt(argc, argv, "p:")) != -1) {
switch (opt) {
case 'p':
port = atoi(optarg);
break;
}
}
tcp_listener(port, run_webserver);
return 0;
}