3 static struct cgi_state *cgi;
5 /* this is a helper function for file upload. The scripts can call
6 @save_file(cgi_variablename, filename) to save the contents of
7 an uploaded file to disk
9 static void save_file(struct template_state *tmpl,
10 const char *name, const char *value,
11 int argc, char **argv)
13 char *var_name, *file_name;
19 printf("Invalid arguments to function %s (%d)\n", name, argc);
25 content = cgi->get_content(cgi, var_name, &size);
27 printf("No content for variable %s?\n", var_name);
30 fd = open(file_name, O_CREAT | O_TRUNC | O_WRONLY, 0644);
32 printf("Failed to open %s (%s)\n", file_name, strerror(errno));
35 ret = write(fd, content, size);
37 printf("out of space writing %s (wrote %u)\n", file_name, ret);
42 /* the main webserver process, called with stdin and stdout setup
44 static void run_webserver(void)
48 if (chdir("html") != 0) {
49 fprintf(stderr,"Can't find html directory?\n");
55 cgi->load_variables(cgi);
56 cgi->tmpl->put(cgi->tmpl, "save_file", "", save_file);
58 /* handle a direct file download */
59 if (!strstr(cgi->pathinfo, "..") && *cgi->pathinfo != '/' &&
60 stat(cgi->pathinfo, &st) == 0 && S_ISREG(st.st_mode)) {
61 cgi->download(cgi, cgi->pathinfo);
66 cgi->download(cgi, "index.html");
71 /* main program, just start listening and answering queries */
72 int main(int argc, char *argv[])
74 int port = TSERVER_PORT;
78 while ((opt=getopt(argc, argv, "p:")) != -1) {
86 tcp_listener(port, run_webserver);