From 772c2db9716f2aa255a5316b3435899b00483dde Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sun, 13 Sep 2009 17:38:15 +0200 Subject: httpd_ssi.c: make it a bit less broken Signed-off-by: Denys Vlasenko --- networking/httpd_ssi.c | 104 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 21 deletions(-) (limited to 'networking/httpd_ssi.c') diff --git a/networking/httpd_ssi.c b/networking/httpd_ssi.c index 86f341fbd..03f263397 100644 --- a/networking/httpd_ssi.c +++ b/networking/httpd_ssi.c @@ -31,13 +31,17 @@ httpd_ssi.c -o httpd_ssi */ /* Size (i386, static uclibc, approximate): - text data bss dec hex filename - 8931 164 68552 77647 12f4f httpd_ssi -*/ + * 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...) + */ #include #include #include +#include #include #include #include @@ -46,40 +50,97 @@ httpd_ssi.c -o httpd_ssi #include #include +static char* skip_whitespace(char *s) +{ + while (*s == ' ' || *s == '\t') ++s; + + return s; +} + static char line[64 * 1024]; -/* - * Currently only handles directives which are alone on the line - */ static void process_includes(const char *filename) { + int curdir_fd; + char *end; FILE *fp = fopen(filename, "r"); if (!fp) exit(1); -#define INCLUDE "")? */ + 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) ) { + /* 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) { + /* " */ - putchar('\n'); + process_includes(include_directive); + + /* Print everything after directive */ + if (end) { + fputs(end, stdout); + free(end); + } } + if (curdir_fd >= 0) + fchdir(curdir_fd); + fclose(fp); } int main(int argc, char *argv[]) @@ -95,13 +156,14 @@ int main(int argc, char *argv[]) * Connection: close * Content-Type: text/html */ - printf( + fputs( /* "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" - "\r\n" + "\r\n", + stdout ); process_includes(argv[1]); return 0; -- cgit v1.2.3