2009-09-11 00:54:45 +05:30
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Denys Vlasenko <vda.linux@googlemail.com>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2009-09-11 00:54:45 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program is a CGI application. It processes server-side includes:
|
|
|
|
* <!--#include file="file.html" -->
|
2009-09-11 00:57:39 +05:30
|
|
|
*
|
|
|
|
* Usage: put these lines in httpd.conf:
|
|
|
|
*
|
|
|
|
* *.html:/bin/httpd_ssi
|
|
|
|
* *.htm:/bin/httpd_ssi
|
2009-09-11 00:54:45 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
/* Build a-la
|
|
|
|
i486-linux-uclibc-gcc \
|
|
|
|
-static -static-libgcc \
|
|
|
|
-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 \
|
|
|
|
-Wall -Wshadow -Wwrite-strings -Wundef -Wstrict-prototypes -Werror \
|
|
|
|
-Wold-style-definition -Wdeclaration-after-statement -Wno-pointer-sign \
|
|
|
|
-Wmissing-prototypes -Wmissing-declarations \
|
|
|
|
-Os -fno-builtin-strlen -finline-limit=0 -fomit-frame-pointer \
|
|
|
|
-ffunction-sections -fdata-sections -fno-guess-branch-probability \
|
|
|
|
-funsigned-char \
|
|
|
|
-falign-functions=1 -falign-jumps=1 -falign-labels=1 -falign-loops=1 \
|
|
|
|
-march=i386 -mpreferred-stack-boundary=2 \
|
|
|
|
-Wl,-Map -Wl,link.map -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
|
|
|
|
httpd_ssi.c -o httpd_ssi
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Size (i386, static uclibc, approximate):
|
2009-09-13 21:08:15 +05:30
|
|
|
* text data bss dec hex filename
|
|
|
|
* 9487 160 68552 78199 13177 httpd_ssi
|
|
|
|
*
|
|
|
|
* Note: it wouldn't be too hard to get rid of stdio and strdup,
|
|
|
|
* (especially that fgets() mangles NULs...)
|
|
|
|
*/
|
2009-09-11 00:54:45 +05:30
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
2009-09-13 21:08:15 +05:30
|
|
|
#include <fcntl.h>
|
2009-09-11 00:54:45 +05:30
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2009-09-13 21:08:15 +05:30
|
|
|
static char* skip_whitespace(char *s)
|
|
|
|
{
|
2011-02-03 04:35:48 +05:30
|
|
|
while (*s == ' ' || *s == '\t') ++s;
|
2009-09-13 21:08:15 +05:30
|
|
|
|
2011-02-03 04:35:48 +05:30
|
|
|
return s;
|
2009-09-13 21:08:15 +05:30
|
|
|
}
|
|
|
|
|
2009-09-11 00:54:45 +05:30
|
|
|
static char line[64 * 1024];
|
|
|
|
|
|
|
|
static void process_includes(const char *filename)
|
|
|
|
{
|
2009-09-13 21:08:15 +05:30
|
|
|
int curdir_fd;
|
|
|
|
char *end;
|
2009-09-11 00:54:45 +05:30
|
|
|
FILE *fp = fopen(filename, "r");
|
|
|
|
if (!fp)
|
|
|
|
exit(1);
|
|
|
|
|
2009-09-13 21:08:15 +05:30
|
|
|
/* Ensure that nested includes are relative:
|
|
|
|
* if we include a/1.htm and it includes b/2.htm,
|
|
|
|
* we need to include a/b/2.htm, not b/2.htm
|
|
|
|
*/
|
|
|
|
curdir_fd = -1;
|
|
|
|
end = strrchr(filename, '/');
|
|
|
|
if (end) {
|
|
|
|
curdir_fd = open(".", O_RDONLY);
|
|
|
|
/* *end = '\0' would mishandle "/file.htm" */
|
|
|
|
end[1] = '\0';
|
|
|
|
chdir(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INCLUDE "<!--#include"
|
2009-09-11 00:54:45 +05:30
|
|
|
while (fgets(line, sizeof(line), fp)) {
|
2009-09-13 21:08:15 +05:30
|
|
|
unsigned preceding_len;
|
|
|
|
char *include_directive;
|
2009-09-11 00:54:45 +05:30
|
|
|
|
2009-09-13 21:08:15 +05:30
|
|
|
include_directive = strstr(line, INCLUDE);
|
|
|
|
if (!include_directive) {
|
|
|
|
fputs(line, stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
preceding_len = include_directive - line;
|
|
|
|
if (memchr(line, '\"', preceding_len)
|
|
|
|
|| memchr(line, '\'', preceding_len)
|
2009-09-11 00:54:45 +05:30
|
|
|
) {
|
2009-09-13 21:08:15 +05:30
|
|
|
/* INCLUDE string may be inside "str" or 'str',
|
|
|
|
* ignore it */
|
|
|
|
fputs(line, stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Small bug: we accept #includefile="file" too */
|
|
|
|
include_directive = skip_whitespace(include_directive + sizeof(INCLUDE)-1);
|
|
|
|
if (strncmp(include_directive, "file=\"", 6) != 0) {
|
|
|
|
/* "<!--#include virtual=..."? - not supported */
|
|
|
|
fputs(line, stdout);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
include_directive += 6; /* now it points to file name */
|
|
|
|
end = strchr(include_directive, '\"');
|
|
|
|
if (!end) {
|
2009-09-11 00:54:45 +05:30
|
|
|
fputs(line, stdout);
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-13 21:08:15 +05:30
|
|
|
/* We checked that this is a valid include directive */
|
|
|
|
|
|
|
|
/* Print everything before directive */
|
|
|
|
if (preceding_len) {
|
|
|
|
line[preceding_len] = '\0';
|
|
|
|
fputs(line, stdout);
|
|
|
|
}
|
|
|
|
/* Save everything after directive */
|
|
|
|
*end++ = '\0';
|
|
|
|
end = strchr(end, '>');
|
|
|
|
if (end)
|
|
|
|
end = strdup(end + 1);
|
|
|
|
|
2009-09-11 00:57:39 +05:30
|
|
|
/* FIXME:
|
|
|
|
* (1) are relative paths with /../ etc ok?
|
2009-09-13 21:08:15 +05:30
|
|
|
* (2) what to do with absolute paths?
|
|
|
|
* are they relative to doc root or to real root?
|
2009-09-11 00:54:45 +05:30
|
|
|
*/
|
2009-09-13 21:08:15 +05:30
|
|
|
process_includes(include_directive);
|
|
|
|
|
|
|
|
/* Print everything after directive */
|
2013-01-14 06:04:48 +05:30
|
|
|
if (end) {
|
2009-09-13 21:08:15 +05:30
|
|
|
fputs(end, stdout);
|
|
|
|
free(end);
|
|
|
|
}
|
2009-09-11 00:54:45 +05:30
|
|
|
}
|
2009-09-13 21:08:15 +05:30
|
|
|
if (curdir_fd >= 0)
|
|
|
|
fchdir(curdir_fd);
|
|
|
|
fclose(fp);
|
2009-09-11 00:54:45 +05:30
|
|
|
}
|
|
|
|
|
2022-05-01 20:36:00 +05:30
|
|
|
int main(int argc, char **argv)
|
2009-09-11 00:54:45 +05:30
|
|
|
{
|
|
|
|
if (!argv[1])
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Seen from busybox.net's Apache:
|
|
|
|
* HTTP/1.1 200 OK
|
|
|
|
* Date: Thu, 10 Sep 2009 18:23:28 GMT
|
|
|
|
* Server: Apache
|
|
|
|
* Accept-Ranges: bytes
|
|
|
|
* Connection: close
|
|
|
|
* Content-Type: text/html
|
|
|
|
*/
|
2009-09-13 21:08:15 +05:30
|
|
|
fputs(
|
2009-09-11 00:54:45 +05:30
|
|
|
/* "Date: Thu, 10 Sep 2009 18:23:28 GMT\r\n" */
|
|
|
|
/* "Server: Apache\r\n" */
|
|
|
|
/* "Accept-Ranges: bytes\r\n" - do we really accept bytes?! */
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Content-Type: text/html\r\n"
|
2009-09-13 21:08:15 +05:30
|
|
|
"\r\n",
|
|
|
|
stdout
|
2009-09-11 00:54:45 +05:30
|
|
|
);
|
|
|
|
process_includes(argv[1]);
|
|
|
|
return 0;
|
|
|
|
}
|