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