slitaz-dev-tools view taztpd/taztpd @ rev 309

Remove ashism ==
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Feb 26 08:23:49 2019 +0100 (2019-02-26)
parents 116c7b18fea2
children
line source
1 #!/bin/sh
2 #
3 # TazTPD - The SliTaz micro HTTP Web Server all in SHell script
4 #
5 # (C) 2011 SliTaz GNU/Linux - GNU gpl v2
6 #
7 # AUTHOR: Christophe Lincoln
8 #
10 # Personal configuration overwrites system wide config.
11 [ -f "/etc/slitaz/taztpd.conf" ] && . /etc/slitaz/taztpd.conf
12 [ -f "taztpd.conf" ] && . taztpd.conf
14 # Web Server functions
16 # Output standardized header for valid requests
17 http_header() {
18 cat << EOT
19 HTTP/1.1 200 OK
20 EOT
21 }
23 html_header() {
24 cat << EOT
25 Content-Type: text/html
27 EOT
28 }
30 text_header() {
31 cat << EOT
32 Content-Type: text/plain
34 EOT
35 }
37 # List all files in a directory
38 directory_listing() {
39 cat << EOT
40 <!DOCTYPE html>
41 <html xmlns="http://www.w3.org/1999/xhtml">
42 <head>
43 <title>Index of $url</title>
44 <meta charset="utf-8" />
45 `css_style`
46 </head>
47 EOT
48 echo -e "<h1>$PAGE_HEADING $url</h1>"
49 echo "<ul>"
50 if [ "$url" != "/" ]; then
51 echo " <li><a href=\"../\">$PARENT_DIR/</a></li>"
52 fi
53 (cd ${SERVER_ROOT}$url && ls -p | while read line
54 do
55 echo " <li><a href=\"$line\">$line</a></li>"
56 done)
57 echo -e "</ul>"
58 taztpd_footer
59 echo -e "</body>\n</html>"
60 }
62 # Handled by an external CSS file
63 css_style() {
64 echo '<style type="text/css">'
65 cat $SERVER_CSS
66 echo '</style>'
67 }
69 taztpd_footer() {
70 echo "<div id=\"footer\">$SERVER_NAME</div>"
71 }
73 # Handle file type by extension
74 handle_filetype() {
75 case $file in
76 # Check for HTML first for fast anser (most requests)
77 *.html|*.htm) type="text/html";;
78 *.css) type="text/css" ;;
79 *.xml) type="text/xml" ;;
80 *.jpg|*.jpeg) type="image/jpeg" ;;
81 *.png) type="image/png" ;;
82 *.tar.gz) "application/x-tgz" ;;
83 *.tazpkg) "application/x-tazpkg" ;;
84 *)
85 # Default to plain text document
86 type=text/plain ;;
87 esac
88 cat << EOT
89 Content-Type: $type
91 EOT
92 }
94 # Server main function
95 read_request() {
96 # Record the HTTP request
97 read request
98 while /bin/true; do
99 read header
100 [ "$header" = $'\r' ] && break;
101 done
102 # Extract URL from the request string
103 url="${request#GET }"
104 url="${url% HTTP/*}"
105 query="${url#*\?}"
106 url="${url%%\?*}"
107 # Handle CGI scripts
108 if [ "$query" != "$url" -a -x "$file" ]; then
109 export QUERY_STRING="$query"
110 http_header
111 exec "$file"
112 echo -e "\r"
113 exit 0
114 fi
115 # Locate the wanted file
116 file="${SERVER_ROOT}$url"
117 # First try to display requested page
118 if [ -f "$file" ]; then
119 http_header
120 handle_filetype
121 cat "$file"
122 echo -e "\r" && exit 0
123 fi
124 # Requested URL may be a directory
125 if [ -d "$file" ]; then
126 http_header
127 if [ -f "$file/index.html" ]; then
128 file=$file/index.html
129 echo -e "Content-Type: text/html\r"
130 echo -e "\r"
131 cat "$file"
132 echo -e "\r" && exit 0
133 fi
134 html_header
135 directory_listing
136 echo -e "\r"
137 # 404 error
138 else
139 cat << EOT
140 HTTP/1.1 404 Not Found
141 Content-Type: text/html
143 <!DOCTYPE html>
144 <html xmlns="http://www.w3.org/1999/xhtml">
145 <head>
146 <title>404 Not Found</title>
147 <meta charset="utf-8" />
148 `css_style`
149 </head>
150 <body>
151 <h1>404 Not Found</h1>
152 <p>$NOT_FOUND</p>
153 `taztpd_footer`
154 </body>
155 </html>
156 EOT
157 echo -e "\r"
158 fi
159 }
161 # Web Server commands
163 case $1 in
164 status|-s)
165 echo ""
166 ps | grep taztpd
167 echo "" ;;
168 dev|-d)
169 # Devel mode by keeping the hand
170 echo "Starting Web Server on port: $SERVER_PORT (dev mode)"
171 while true
172 do
173 nc -l -p $SERVER_PORT -e /usr/bin/taztpd
174 done ;;
175 nc|-n)
176 # Use nc to listen on a port and execute TazTPD on a request
177 echo "Starting Web Server on port: $SERVER_PORT"
178 (while true
179 do
180 nc -l -p $SERVER_PORT -e /usr/bin/taztpd
181 done) & ;;
182 usage|*help|-u|*-h)
183 # Display a short usage
184 echo "Usage: `basename $0` [status|dev|nc]" ;;
185 *)
186 read_request ;;
187 esac
188 exit 0