aboutsummaryrefslogtreecommitdiff
path: root/networking/httpd_index_cgi_example
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-21 00:12:09 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-21 00:12:09 +0000
commit0656dfd4b41c308c45c874ff85100874e7c9dfa5 (patch)
treefae60c2d7ca0d917f73f6a87554657c09a0ddb7b /networking/httpd_index_cgi_example
parent121105952f7db668ea774ce7889c9485723019f1 (diff)
downloadbusybox-w32-0656dfd4b41c308c45c874ff85100874e7c9dfa5.tar.gz
busybox-w32-0656dfd4b41c308c45c874ff85100874e7c9dfa5.tar.bz2
busybox-w32-0656dfd4b41c308c45c874ff85100874e7c9dfa5.zip
httpd: fix cgi-bin/index.cgi support, add example of it,
stat: fix end-of-line if format is specified (wasn't printing it), fix %z (time) format to match coreutils 6.3 git-svn-id: svn://busybox.net/trunk/busybox@16597 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to '')
-rw-r--r--networking/httpd_index_cgi_example55
1 files changed, 55 insertions, 0 deletions
diff --git a/networking/httpd_index_cgi_example b/networking/httpd_index_cgi_example
new file mode 100644
index 000000000..31e768c70
--- /dev/null
+++ b/networking/httpd_index_cgi_example
@@ -0,0 +1,55 @@
1#!/bin/sh
2# This CGI creates directory index.
3# Put it into cgi-bin/index.cgi and chmod 0755.
4#
5# Problems:
6# * Unsafe wrt weird filenames with <>"'& etc...
7# * Not efficient: calls stat (program, not syscall) for each file
8# * Probably requires bash
9#
10# If you want speed and safety, you need to code it in C
11
12# Must start with '/'
13test "${QUERY_STRING:0:1}" = "/" || exit 1
14# /../ is not allowed
15test "${QUERY_STRING%/../*}" = "$QUERY_STRING" || exit 1
16test "${QUERY_STRING%/..}" = "$QUERY_STRING" || exit 1
17
18# Outta cgi-bin...
19cd .. 2>/dev/null || exit 1
20# Strip leading '/', go to target dir
21cd "${QUERY_STRING:1}" 2>/dev/null || exit 1
22
23f=`dirname "$QUERY_STRING"`
24test "$f" = "/" && f=""
25
26# Pipe thru dd (need to write header as single write(),
27# or else httpd doesn't see "Content-type: text/html"
28# in first read() and decides that it is not html)
29{
30printf "%s" \
31$'HTTP/1.0 200 OK\r\n'\
32$'Content-type: text/html\r\n\r\n'\
33"<html><head><title>Index of $QUERY_STRING</title></head>"$'\r\n'\
34"<body><h1>Index of $QUERY_STRING</h1><pre>"$'\r\n'\
35$'<table width=100%>\r\n'\
36$'<col><col><col width=0*>\r\n'\
37$'<tr><th>Name<th align=right>Last modified<th align=right>Size\r\n'\
38\
39"<tr><td><a href='$f/'>..</a><td><td>"$'\r\n'
40
41IFS='#'
42for f in *; do
43 # Guard against empty dirs...
44 test -e "$f" && \
45 stat -c "%F#%s#%z" "$f" | {
46 read type size cdt junk
47 dir=''
48 test "$type" = "directory" && dir='/'
49 cdt="${cdt//.*}" # no fractional seconds
50 cdt="${cdt// /&nbsp;}" # prevent wrapping around space
51 printf "%s" "<tr><td><a href='$f$dir'>$f</a><td align=right>$cdt<td align=right>$size"$'\r\n'
52 }
53done
54printf "</table></pre><hr></body></html>"$'\r\n'
55} | dd bs=4k