The CGI scripts can be compiled separately without a dependency to BusyBox and 
used with any other web server.
But for usability we can also compile them as applets and thus users may see 
and select them in menuconfig.
Also, we can install the cgi scripts into a known location and users may latter 
easily use them.

The httpd_indexcgi will be installed into /usr/bin/cgi-dirlist. The "dirlist" 
is better to understand than index which may be confusing with index.html files.
The httpd_ssi is in fact not a CGI script but an Interpreter. It will be 
installed to /usr/bin/ssi. The ssi name is not used by any other linux tool.

We also can reuse some functions from libbb. E.g. httpd_ssi.c may reuse 
skip_whitespace().
It works similarly except that it also skips newlines \n\r.
This difference is not important because the include_directive anyway is 
already a line, so it doesn't contain any newline symbol.

Signed-off-by: Sergey Ponomarev <stok...@gmail.com>
---
 networking/httpd_indexcgi.c | 36 +++++++++++++++++++++++++++--
 networking/httpd_ssi.c      | 46 +++++++++++++++++++++++++++++++------
 2 files changed, 73 insertions(+), 9 deletions(-)

diff --git a/networking/httpd_indexcgi.c b/networking/httpd_indexcgi.c
index 47b1159f4..53d316d05 100644
--- a/networking/httpd_indexcgi.c
+++ b/networking/httpd_indexcgi.c
@@ -6,10 +6,31 @@
 
 /*
  * This program is a CGI application. It outputs directory index page.
- * Put it into cgi-bin/index.cgi and chmod 0755.
+ * For BusyBox httpd put the program into cgi-bin/index.cgi and chmod 0755.
+ * 
+ * The BusyBox httpd must be compiled with FEATURE_HTTPD_CGI=y
+ * 
+ * It can be built as a standalone program or as part of busybox itself
  */
 
-/* Build a-la
+//config:config CGI_DIRLIST
+//config:      bool "CGI dirlist"
+//config:      default n
+//config:      help
+//config:       GGI script to generate directory list index i.e. table of 
files.
+//config:       To use it link the cgi-dirlist binary to a target folder's 
cgi-bin e.g.:
+//config:         ln /usr/bin/cgi-dirlist  /srv/www/cgi-bin/index.cgi
+//config:       When open in browser you'll see list of files in the /srv/www/ 
directory
+
+//kbuild:lib-$(CONFIG_CGI_DIRLIST) += httpd_indexcgi.o
+//applet:IF_CGI_DIRLIST(APPLET_ODDNAME(cgi-dirlist, cgi_dirlist, 
BB_DIR_USR_BIN, BB_SUID_DROP, cgi_dirlist))
+
+//usage:#define cgi_dirlist_trivial_usage
+//usage:       "REQUEST_URI=/ cgi_dirlist"
+//usage:#define cgi_dirlist_full_usage "\n\n"
+//usage:       "Generate an HTML with files from requested directory\n"
+
+/* Build it as a standalone program a-la
 i486-linux-uclibc-gcc \
 -static -static-libgcc \
 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 \
@@ -36,6 +57,10 @@ httpd_indexcgi.c -o index.cgi
  */
 
 #define _GNU_SOURCE 1  /* for strchrnul */
+/* If building as applet */
+#if ENABLE_CGI_DIRLIST
+ #include "libbb.h"
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
@@ -211,7 +236,14 @@ static void fmt_04u(/*char *dst,*/ unsigned n)
        fmt_02u(n % 100);
 }
 
+/* If building as applet */
+#if ENABLE_CGI_DIRLIST
+int cgi_dirlist_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int cgi_dirlist_main(int argc UNUSED_PARAM, char **argv)
+#else
+/* standalone */
 int main(int argc, char *argv[])
+#endif
 {
        dir_list_t *dir_list;
        dir_list_t *cdir;
diff --git a/networking/httpd_ssi.c b/networking/httpd_ssi.c
index 4bd9a6d97..a14a09bbb 100644
--- a/networking/httpd_ssi.c
+++ b/networking/httpd_ssi.c
@@ -5,16 +5,36 @@
  */
 
 /*
- * This program is a CGI application. It processes server-side includes:
+ * This program is a HTTPD interpreter. It processes server-side includes:
  * <!--#include file="file.html" -->
  *
- * Usage: put these lines in httpd.conf:
+ * Usage: For BusyBox httpd put these lines in httpd.conf:
  *
- * *.html:/bin/httpd_ssi
- * *.htm:/bin/httpd_ssi
+ * *.shtml:/usr/bin/ssi
+ * 
+ * The BusyBox httpd must be compiled with 
FEATURE_HTTPD_CONFIG_WITH_SCRIPT_INTERPR=y
+ * 
+ * It can be built as a standalone program or as part of busybox itself
  */
 
-/* Build a-la
+//config:config SSI
+//config:      bool "Server-Side Includes (SSI) interpreter"
+//config:      default n
+//config:      help
+//config:       Tool to generate a single HTML from several files.
+//config:       E.g. you have a template index.shtml and it's body starting 
with
+//config:        <!--#include file="header.html" -->
+//config:       SSI interpreter will replace the directive with raw text from 
header.html
+
+//kbuild:lib-$(CONFIG_SSI) += httpd_ssi.o
+//applet:IF_SSI(APPLET(ssi, BB_DIR_USR_BIN, BB_SUID_DROP))
+
+//usage:#define ssi_trivial_usage
+//usage:       "ssi /index.shtml"
+//usage:#define ssi_full_usage "\n\n"
+//usage:       "Generate an HTML from template with SSI\n"
+
+/* Build it as a standalone program a-la
 i486-linux-uclibc-gcc \
 -static -static-libgcc \
 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 \
@@ -27,7 +47,7 @@ i486-linux-uclibc-gcc \
 -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
+httpd_ssi.c -o ssi
 */
 
 /* Size (i386, static uclibc, approximate):
@@ -37,7 +57,10 @@ httpd_ssi.c -o httpd_ssi
  * Note: it wouldn't be too hard to get rid of stdio and strdup,
  * (especially that fgets() mangles NULs...)
  */
-
+/* If building as applet */
+#if ENABLE_SSI
+ #include "libbb.h"
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
@@ -50,12 +73,14 @@ httpd_ssi.c -o httpd_ssi
 #include <dirent.h>
 #include <time.h>
 
+#if !ENABLE_SSI
 static char* skip_whitespace(char *s)
 {
        while (*s == ' ' || *s == '\t') ++s;
 
        return s;
 }
+#endif
 
 static char line[64 * 1024];
 
@@ -143,7 +168,14 @@ static void process_includes(const char *filename)
        fclose(fp);
 }
 
+/* If building as applet */
+#if ENABLE_SSI
+int ssi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int ssi_main(int argc UNUSED_PARAM, char **argv)
+#else
+/* standalone */
 int main(int argc, char *argv[])
+#endif
 {
        if (!argv[1])
                return 1;
-- 
2.30.2

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to