2 * Copyright (c) 2009 Denys Vlasenko <vda.linux@googlemail.com>
4 * Licensed under GPLv2, see file LICENSE in this source tree.
8 * This program is a CGI application. It processes server-side includes:
9 * <!--#include file="file.html" -->
11 * Usage: put these lines in httpd.conf:
13 * *.html:/bin/httpd_ssi
14 * *.htm:/bin/httpd_ssi
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 \
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
33 /* Size (i386, static uclibc, approximate):
34 * text data bss dec hex filename
35 * 9487 160 68552 78199 13177 httpd_ssi
37 * Note: it wouldn't be too hard to get rid of stdio and strdup,
38 * (especially that fgets() mangles NULs...)
41 #include <sys/types.h>
53 static char* skip_whitespace(char *s
)
55 while (*s
== ' ' || *s
== '\t') ++s
;
60 static char line
[64 * 1024];
62 static void process_includes(const char *filename
)
66 FILE *fp
= fopen(filename
, "r");
70 /* Ensure that nested includes are relative:
71 * if we include a/1.htm and it includes b/2.htm,
72 * we need to include a/b/2.htm, not b/2.htm
75 end
= strrchr(filename
, '/');
77 curdir_fd
= open(".", O_RDONLY
);
78 /* *end = '\0' would mishandle "/file.htm" */
83 #define INCLUDE "<!--#include"
84 while (fgets(line
, sizeof(line
), fp
)) {
85 unsigned preceding_len
;
86 char *include_directive
;
88 include_directive
= strstr(line
, INCLUDE
);
89 if (!include_directive
) {
93 preceding_len
= include_directive
- line
;
94 if (memchr(line
, '\"', preceding_len
)
95 || memchr(line
, '\'', preceding_len
)
97 /* INCLUDE string may be inside "str" or 'str',
102 /* Small bug: we accept #includefile="file" too */
103 include_directive
= skip_whitespace(include_directive
+ sizeof(INCLUDE
)-1);
104 if (strncmp(include_directive
, "file=\"", 6) != 0) {
105 /* "<!--#include virtual=..."? - not supported */
109 include_directive
+= 6; /* now it points to file name */
110 end
= strchr(include_directive
, '\"');
115 /* We checked that this is a valid include directive */
117 /* Print everything before directive */
119 line
[preceding_len
] = '\0';
122 /* Save everything after directive */
124 end
= strchr(end
, '>');
126 end
= strdup(end
+ 1);
129 * (1) are relative paths with /../ etc ok?
130 * (2) what to do with absolute paths?
131 * are they relative to doc root or to real root?
133 process_includes(include_directive
);
135 /* Print everything after directive */
146 int main(int argc
, char *argv
[])
151 /* Seen from busybox.net's Apache:
153 * Date: Thu, 10 Sep 2009 18:23:28 GMT
155 * Accept-Ranges: bytes
157 * Content-Type: text/html
160 /* "Date: Thu, 10 Sep 2009 18:23:28 GMT\r\n" */
161 /* "Server: Apache\r\n" */
162 /* "Accept-Ranges: bytes\r\n" - do we really accept bytes?! */
163 "Connection: close\r\n"
164 "Content-Type: text/html\r\n"
168 process_includes(argv
[1]);