diff options
Diffstat (limited to 'doc/examples/cgi.c')
-rw-r--r-- | doc/examples/cgi.c | 100 |
1 files changed, 0 insertions, 100 deletions
diff --git a/doc/examples/cgi.c b/doc/examples/cgi.c deleted file mode 100644 index 5e1b76b9..00000000 --- a/doc/examples/cgi.c +++ /dev/null | |||
@@ -1,100 +0,0 @@ | |||
1 | /* by Luis Figueiredo (stdio@netc.pt) | ||
2 | * | ||
3 | * This is only a example if you intend to use this, please | ||
4 | * make this secure (checking file exec permissions etc.etc.) | ||
5 | */ | ||
6 | #include <stdio.h> | ||
7 | #include <fcntl.h> | ||
8 | #include <stdlib.h> | ||
9 | #include <string.h> | ||
10 | #include <dirent.h> | ||
11 | #include "web_server.h" | ||
12 | #include "debug.h" | ||
13 | |||
14 | |||
15 | |||
16 | extern struct web_client *current_web_client; | ||
17 | int PORT=80; | ||
18 | |||
19 | extern char **environ; | ||
20 | void cgi() { | ||
21 | char *reqfile=ClientInfo->request+1; // Skip '/' | ||
22 | char *tmp; | ||
23 | FILE *instream; | ||
24 | int stdo; | ||
25 | int ret; | ||
26 | int pid; | ||
27 | int outp; | ||
28 | if(!(pid=fork())) { | ||
29 | instream=tmpfile(); | ||
30 | // Setup envvars | ||
31 | setenv("SCRIPT_NAME",ClientInfo->request,1); | ||
32 | setenv("REQUEST_METHOD",ClientInfo->method,1); | ||
33 | if(strlen(tmp=ClientInfo->Query(NULL))) { | ||
34 | setenv("QUERY_STRING",tmp,1); | ||
35 | }; | ||
36 | if(strlen(tmp=ClientInfo->Post(NULL))) { | ||
37 | fwrite(tmp,strlen(tmp),1,instream); | ||
38 | } | ||
39 | setenv("CONTENT_TYPE",ClientInfo->Header("Content-type"),1); | ||
40 | if(strlen(tmp=ClientInfo->Header("Cookie"))) { | ||
41 | setenv("HTTP_COOKIE",tmp,1); | ||
42 | }; | ||
43 | dup2(ClientInfo->outfd,1); | ||
44 | dup2(fileno(instream),0); | ||
45 | fseek(instream,0,SEEK_SET); | ||
46 | execve(reqfile,NULL,environ); | ||
47 | }; | ||
48 | while(!(ret=waitpid(pid,&ret,0)))fprintf(stderr,"ret-%d\n",ret);; | ||
49 | |||
50 | }; | ||
51 | void index_() { | ||
52 | DIR *dir; | ||
53 | struct dirent *dire; | ||
54 | dir=opendir("cgi-bin"); | ||
55 | printf("Content-type: text/html\r\n\r\n"); | ||
56 | printf("<HTML>\n"); | ||
57 | printf("<BODY bgcolor='EFEFEF'>\n"); | ||
58 | printf("Browse /cgi-bin/*<BR>\n"); | ||
59 | while(dire=readdir(dir)) { | ||
60 | if(dire->d_name[0]!='.') { | ||
61 | if((int)(strstr(dire->d_name,".cgi")+4)==(int)(dire->d_name+strlen(dire->d_name))) { | ||
62 | printf("<a href='/cgi-bin/%s'>%s</a> -- ",dire->d_name,dire->d_name); | ||
63 | printf("<a href='/source?src=/cgi-bin/%s'>(see source)</a><BR>",dire->d_name,dire->d_name); | ||
64 | }; | ||
65 | }; | ||
66 | }; | ||
67 | closedir(dir); | ||
68 | }; | ||
69 | void source() { | ||
70 | char *tmp=ClientInfo->Query("src"); | ||
71 | char *tmp1; | ||
72 | /* security */ | ||
73 | while((tmp1=strstr(tmp,"../"))) { | ||
74 | tmp=tmp1+3; | ||
75 | }; | ||
76 | while((tmp1=strstr(tmp,"//"))) { | ||
77 | tmp=tmp1+2; | ||
78 | }; | ||
79 | tmp1=strstr(tmp,"/"); | ||
80 | if(tmp1==tmp) { | ||
81 | tmp=tmp1+1; | ||
82 | }; | ||
83 | /* security */ | ||
84 | printf("Content-type: text/plain\r\n\r\n"); | ||
85 | web_client_addfile(tmp); | ||
86 | }; | ||
87 | main() { | ||
88 | struct web_server server; | ||
89 | |||
90 | while(!web_server_init(&server,PORT,"cgi.log",0)) { | ||
91 | PORT++; | ||
92 | }; | ||
93 | printf("http://localhost:%d\n",PORT); | ||
94 | web_server_addhandler(&server,"* /source",source,0); | ||
95 | web_server_addhandler(&server,"* /*.cgi",cgi,0); | ||
96 | web_server_addhandler(&server,"* /",index_,0); | ||
97 | while(1) { | ||
98 | web_server_run(&server); | ||
99 | }; | ||
100 | }; | ||