networking/httpd_ssi.c: expand comments
[oweals/busybox.git] / networking / httpd_ssi.c
1 /*
2  * Copyright (c) 2009 Denys Vlasenko <vda.linux@googlemail.com>
3  *
4  * Licensed under GPLv2, see file LICENSE in this tarball for details.
5  */
6
7 /*
8  * This program is a CGI application. It processes server-side includes:
9  * <!--#include file="file.html" -->
10  *
11  * Usage: put these lines in httpd.conf:
12  *
13  * *.html:/bin/httpd_ssi
14  * *.htm:/bin/httpd_ssi
15  */
16
17 /* Build a-la
18 i486-linux-uclibc-gcc \
19 -static -static-libgcc \
20 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 \
21 -Wall -Wshadow -Wwrite-strings -Wundef -Wstrict-prototypes -Werror \
22 -Wold-style-definition -Wdeclaration-after-statement -Wno-pointer-sign \
23 -Wmissing-prototypes -Wmissing-declarations \
24 -Os -fno-builtin-strlen -finline-limit=0 -fomit-frame-pointer \
25 -ffunction-sections -fdata-sections -fno-guess-branch-probability \
26 -funsigned-char \
27 -falign-functions=1 -falign-jumps=1 -falign-labels=1 -falign-loops=1 \
28 -march=i386 -mpreferred-stack-boundary=2 \
29 -Wl,-Map -Wl,link.map -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
30 httpd_ssi.c -o httpd_ssi
31 */
32
33 /* Size (i386, static uclibc, approximate):
34    text    data     bss     dec     hex filename
35    8931     164   68552   77647   12f4f httpd_ssi
36 */
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <errno.h>
41 #include <stdint.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <stdio.h>
46 #include <dirent.h>
47 #include <time.h>
48
49 static char line[64 * 1024];
50
51 /*
52  * Currently only handles directives which are alone on the line
53  */
54 static void process_includes(const char *filename)
55 {
56         FILE *fp = fopen(filename, "r");
57         if (!fp)
58                 exit(1);
59
60 #define INCLUDE "<!--#include file=\""
61         while (fgets(line, sizeof(line), fp)) {
62                 char *closing_dq;
63
64                 /* FIXME: output text leading to INCLUDE first */
65                 if (strncmp(line, INCLUDE, sizeof(INCLUDE)-1) != 0
66                  || (closing_dq = strchr(line + sizeof(INCLUDE)-1, '"')) == NULL
67                 /* or strstr(line + sizeof(INCLUDE)-1, "\" -->")? */
68                 ) {
69                         fputs(line, stdout);
70                         continue;
71                 }
72                 *closing_dq = '\0';
73                 /* FIXME:
74                  * (1) are relative paths with /../ etc ok?
75                  * (2) if we include a/1.htm and it includes b/2.htm,
76                  * do we need to include a/b/2.htm or b/2.htm?
77                  * IOW, do we need to "cd $dirname"?
78                  */
79                 process_includes(line + sizeof(INCLUDE)-1);
80                 /* FIXME: this should be the tail of line after --> */
81                 putchar('\n');
82         }
83 }
84
85 int main(int argc, char *argv[])
86 {
87         if (!argv[1])
88                 return 1;
89
90         /* Seen from busybox.net's Apache:
91          * HTTP/1.1 200 OK
92          * Date: Thu, 10 Sep 2009 18:23:28 GMT
93          * Server: Apache
94          * Accept-Ranges: bytes
95          * Connection: close
96          * Content-Type: text/html
97          */
98         printf(
99                 /* "Date: Thu, 10 Sep 2009 18:23:28 GMT\r\n" */
100                 /* "Server: Apache\r\n" */
101                 /* "Accept-Ranges: bytes\r\n" - do we really accept bytes?! */
102                 "Connection: close\r\n"
103                 "Content-Type: text/html\r\n"
104                 "\r\n"
105         );
106         process_includes(argv[1]);
107         return 0;
108 }