httpd_ssi.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2009 Denys Vlasenko <vda.linux@googlemail.com>
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  5. */
  6. /*
  7. * This program is a CGI application. It processes server-side includes:
  8. * <!--#include file="file.html" -->
  9. *
  10. * Usage: put these lines in httpd.conf:
  11. *
  12. * *.html:/bin/httpd_ssi
  13. * *.htm:/bin/httpd_ssi
  14. */
  15. /* Build a-la
  16. i486-linux-uclibc-gcc \
  17. -static -static-libgcc \
  18. -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 \
  19. -Wall -Wshadow -Wwrite-strings -Wundef -Wstrict-prototypes -Werror \
  20. -Wold-style-definition -Wdeclaration-after-statement -Wno-pointer-sign \
  21. -Wmissing-prototypes -Wmissing-declarations \
  22. -Os -fno-builtin-strlen -finline-limit=0 -fomit-frame-pointer \
  23. -ffunction-sections -fdata-sections -fno-guess-branch-probability \
  24. -funsigned-char \
  25. -falign-functions=1 -falign-jumps=1 -falign-labels=1 -falign-loops=1 \
  26. -march=i386 -mpreferred-stack-boundary=2 \
  27. -Wl,-Map -Wl,link.map -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
  28. httpd_ssi.c -o httpd_ssi
  29. */
  30. /* Size (i386, static uclibc, approximate):
  31. * text data bss dec hex filename
  32. * 9487 160 68552 78199 13177 httpd_ssi
  33. *
  34. * Note: it wouldn't be too hard to get rid of stdio and strdup,
  35. * (especially that fgets() mangles NULs...)
  36. */
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <errno.h>
  40. #include <fcntl.h>
  41. #include <stdint.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <stdio.h>
  46. #include <dirent.h>
  47. #include <time.h>
  48. static char* skip_whitespace(char *s)
  49. {
  50. while (*s == ' ' || *s == '\t') ++s;
  51. return s;
  52. }
  53. static char line[64 * 1024];
  54. static void process_includes(const char *filename)
  55. {
  56. int curdir_fd;
  57. char *end;
  58. FILE *fp = fopen(filename, "r");
  59. if (!fp)
  60. exit(1);
  61. /* Ensure that nested includes are relative:
  62. * if we include a/1.htm and it includes b/2.htm,
  63. * we need to include a/b/2.htm, not b/2.htm
  64. */
  65. curdir_fd = -1;
  66. end = strrchr(filename, '/');
  67. if (end) {
  68. curdir_fd = open(".", O_RDONLY);
  69. /* *end = '\0' would mishandle "/file.htm" */
  70. end[1] = '\0';
  71. chdir(filename);
  72. }
  73. #define INCLUDE "<!--#include"
  74. while (fgets(line, sizeof(line), fp)) {
  75. unsigned preceding_len;
  76. char *include_directive;
  77. include_directive = strstr(line, INCLUDE);
  78. if (!include_directive) {
  79. fputs(line, stdout);
  80. continue;
  81. }
  82. preceding_len = include_directive - line;
  83. if (memchr(line, '\"', preceding_len)
  84. || memchr(line, '\'', preceding_len)
  85. ) {
  86. /* INCLUDE string may be inside "str" or 'str',
  87. * ignore it */
  88. fputs(line, stdout);
  89. continue;
  90. }
  91. /* Small bug: we accept #includefile="file" too */
  92. include_directive = skip_whitespace(include_directive + sizeof(INCLUDE)-1);
  93. if (strncmp(include_directive, "file=\"", 6) != 0) {
  94. /* "<!--#include virtual=..."? - not supported */
  95. fputs(line, stdout);
  96. continue;
  97. }
  98. include_directive += 6; /* now it points to file name */
  99. end = strchr(include_directive, '\"');
  100. if (!end) {
  101. fputs(line, stdout);
  102. continue;
  103. }
  104. /* We checked that this is a valid include directive */
  105. /* Print everything before directive */
  106. if (preceding_len) {
  107. line[preceding_len] = '\0';
  108. fputs(line, stdout);
  109. }
  110. /* Save everything after directive */
  111. *end++ = '\0';
  112. end = strchr(end, '>');
  113. if (end)
  114. end = strdup(end + 1);
  115. /* FIXME:
  116. * (1) are relative paths with /../ etc ok?
  117. * (2) what to do with absolute paths?
  118. * are they relative to doc root or to real root?
  119. */
  120. process_includes(include_directive);
  121. /* Print everything after directive */
  122. if (end) {
  123. fputs(end, stdout);
  124. free(end);
  125. }
  126. }
  127. if (curdir_fd >= 0)
  128. fchdir(curdir_fd);
  129. fclose(fp);
  130. }
  131. int main(int argc, char *argv[])
  132. {
  133. if (!argv[1])
  134. return 1;
  135. /* Seen from busybox.net's Apache:
  136. * HTTP/1.1 200 OK
  137. * Date: Thu, 10 Sep 2009 18:23:28 GMT
  138. * Server: Apache
  139. * Accept-Ranges: bytes
  140. * Connection: close
  141. * Content-Type: text/html
  142. */
  143. fputs(
  144. /* "Date: Thu, 10 Sep 2009 18:23:28 GMT\r\n" */
  145. /* "Server: Apache\r\n" */
  146. /* "Accept-Ranges: bytes\r\n" - do we really accept bytes?! */
  147. "Connection: close\r\n"
  148. "Content-Type: text/html\r\n"
  149. "\r\n",
  150. stdout
  151. );
  152. process_includes(argv[1]);
  153. return 0;
  154. }