blob: 620b96332f7e67edf7ae938299be4e1ff41c5204 [file] [log] [blame]
Denys Vlasenkodb4a6762009-09-10 21:24:45 +02001/*
2 * Copyright (c) 2009 Denys Vlasenko <vda.linux@googlemail.com>
3 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2, see file LICENSE in this source tree.
Denys Vlasenkodb4a6762009-09-10 21:24:45 +02005 */
6
7/*
8 * This program is a CGI application. It processes server-side includes:
9 * <!--#include file="file.html" -->
Denys Vlasenko56573cb2009-09-10 21:27:39 +020010 *
11 * Usage: put these lines in httpd.conf:
12 *
13 * *.html:/bin/httpd_ssi
14 * *.htm:/bin/httpd_ssi
Denys Vlasenkodb4a6762009-09-10 21:24:45 +020015 */
16
17/* Build a-la
18i486-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 \
30httpd_ssi.c -o httpd_ssi
31*/
32
33/* Size (i386, static uclibc, approximate):
Denys Vlasenko772c2db2009-09-13 17:38:15 +020034 * text data bss dec hex filename
35 * 9487 160 68552 78199 13177 httpd_ssi
36 *
37 * Note: it wouldn't be too hard to get rid of stdio and strdup,
38 * (especially that fgets() mangles NULs...)
39 */
Denys Vlasenkodb4a6762009-09-10 21:24:45 +020040
41#include <sys/types.h>
42#include <sys/stat.h>
43#include <errno.h>
Denys Vlasenko772c2db2009-09-13 17:38:15 +020044#include <fcntl.h>
Denys Vlasenkodb4a6762009-09-10 21:24:45 +020045#include <stdint.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49#include <stdio.h>
50#include <dirent.h>
51#include <time.h>
52
Denys Vlasenko772c2db2009-09-13 17:38:15 +020053static char* skip_whitespace(char *s)
54{
Denys Vlasenkob7c9fb22011-02-03 00:05:48 +010055 while (*s == ' ' || *s == '\t') ++s;
Denys Vlasenko772c2db2009-09-13 17:38:15 +020056
Denys Vlasenkob7c9fb22011-02-03 00:05:48 +010057 return s;
Denys Vlasenko772c2db2009-09-13 17:38:15 +020058}
59
Denys Vlasenkodb4a6762009-09-10 21:24:45 +020060static char line[64 * 1024];
61
Denys Vlasenkodb4a6762009-09-10 21:24:45 +020062static void process_includes(const char *filename)
63{
Denys Vlasenko772c2db2009-09-13 17:38:15 +020064 int curdir_fd;
65 char *end;
Denys Vlasenkodb4a6762009-09-10 21:24:45 +020066 FILE *fp = fopen(filename, "r");
67 if (!fp)
68 exit(1);
69
Denys Vlasenko772c2db2009-09-13 17:38:15 +020070 /* 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
73 */
74 curdir_fd = -1;
75 end = strrchr(filename, '/');
76 if (end) {
77 curdir_fd = open(".", O_RDONLY);
78 /* *end = '\0' would mishandle "/file.htm" */
79 end[1] = '\0';
80 chdir(filename);
81 }
Denys Vlasenkodb4a6762009-09-10 21:24:45 +020082
Denys Vlasenko772c2db2009-09-13 17:38:15 +020083#define INCLUDE "<!--#include"
84 while (fgets(line, sizeof(line), fp)) {
85 unsigned preceding_len;
86 char *include_directive;
87
88 include_directive = strstr(line, INCLUDE);
89 if (!include_directive) {
Denys Vlasenkodb4a6762009-09-10 21:24:45 +020090 fputs(line, stdout);
91 continue;
92 }
Denys Vlasenko772c2db2009-09-13 17:38:15 +020093 preceding_len = include_directive - line;
94 if (memchr(line, '\"', preceding_len)
95 || memchr(line, '\'', preceding_len)
96 ) {
97 /* INCLUDE string may be inside "str" or 'str',
98 * ignore it */
99 fputs(line, stdout);
100 continue;
101 }
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 */
106 fputs(line, stdout);
107 continue;
108 }
109 include_directive += 6; /* now it points to file name */
110 end = strchr(include_directive, '\"');
111 if (!end) {
112 fputs(line, stdout);
113 continue;
114 }
115 /* We checked that this is a valid include directive */
116
117 /* Print everything before directive */
118 if (preceding_len) {
119 line[preceding_len] = '\0';
120 fputs(line, stdout);
121 }
122 /* Save everything after directive */
123 *end++ = '\0';
124 end = strchr(end, '>');
125 if (end)
126 end = strdup(end + 1);
127
Denys Vlasenko56573cb2009-09-10 21:27:39 +0200128 /* FIXME:
129 * (1) are relative paths with /../ etc ok?
Denys Vlasenko772c2db2009-09-13 17:38:15 +0200130 * (2) what to do with absolute paths?
131 * are they relative to doc root or to real root?
Denys Vlasenkodb4a6762009-09-10 21:24:45 +0200132 */
Denys Vlasenko772c2db2009-09-13 17:38:15 +0200133 process_includes(include_directive);
134
135 /* Print everything after directive */
Denys Vlasenko69675782013-01-14 01:34:48 +0100136 if (end) {
Denys Vlasenko772c2db2009-09-13 17:38:15 +0200137 fputs(end, stdout);
138 free(end);
139 }
Denys Vlasenkodb4a6762009-09-10 21:24:45 +0200140 }
Denys Vlasenko772c2db2009-09-13 17:38:15 +0200141 if (curdir_fd >= 0)
142 fchdir(curdir_fd);
143 fclose(fp);
Denys Vlasenkodb4a6762009-09-10 21:24:45 +0200144}
145
Denys Vlasenko9b6f44e2022-05-01 17:06:00 +0200146int main(int argc, char **argv)
Denys Vlasenkodb4a6762009-09-10 21:24:45 +0200147{
148 if (!argv[1])
149 return 1;
150
151 /* Seen from busybox.net's Apache:
152 * HTTP/1.1 200 OK
153 * Date: Thu, 10 Sep 2009 18:23:28 GMT
154 * Server: Apache
155 * Accept-Ranges: bytes
156 * Connection: close
157 * Content-Type: text/html
158 */
Denys Vlasenko772c2db2009-09-13 17:38:15 +0200159 fputs(
Denys Vlasenkodb4a6762009-09-10 21:24:45 +0200160 /* "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"
Denys Vlasenko772c2db2009-09-13 17:38:15 +0200165 "\r\n",
166 stdout
Denys Vlasenkodb4a6762009-09-10 21:24:45 +0200167 );
168 process_includes(argv[1]);
169 return 0;
170}