Created
September 23, 2010 05:10
-
-
Save maop/593147 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ws.sh Web Server in Shell Scripting | |
# @author: maop www.maop.mx | |
# INSTALL: Just add to your /etc/inetd.conf: | |
# http-alt stream tcp nowait nobody /home/maop/Proyectos/ws-sh/ws.sh | |
# restart inetd and launch your browser to 127.0.0.1:8080 | |
# --[ Config ]-- | |
htdocs="/home/maop/Proyectos/ws-sh" | |
dirindex="index.html" | |
# --[ globals ]-- | |
version="ws.sh v0.01a" | |
# --[ Functions ]-- | |
send_file(){ | |
echo -e "HTTP/1.1 200 OK\r" | |
echo -e "Content-Type: $(/usr/bin/file -bi "$1")\r" | |
echo -e "\r" | |
cat "$1" | |
echo -e "\r" | |
} | |
send_404(){ | |
echo -e "HTTP/1.1 404 Not Found\r" | |
echo -e "Content-Type: text/html\r" | |
echo -e "\r" | |
echo -e "<html><head><title> No 'ta </title></head>\r" | |
echo -e "<body><h1>404 No'ta</h1>\r" | |
echo -e "<p>La madre que quieres no 'ta aqui</p>\r" | |
echo -e "<hr><small>Server: $version | Request: $request | URL: $url | Query: $query</small> </body></html>\r" | |
echo -e "\r" | |
} | |
send_403(){ | |
echo -e "HTTP/1.1 403 Forbidden\r" | |
echo -e "Content-Type: text/html\r" | |
echo -e "\r" | |
echo -e "<html><head><title> Ni madres</title></head>\r" | |
echo -e "<body><h1>403 Ni madres</h1>\r" | |
echo -e "<p>No puedes ver la madre que quieres, a volar!</p>\r" | |
echo -e "<hr><small>Server: $version | URL: $url </small> </body></html>\r" | |
echo -e "\r" | |
} | |
proc_req(){ | |
#process request: | |
# Se trata de un directorio? añadir directory index, si no existe mandar 403 | |
# pues aun no soportamos Directory Listing | |
if (echo $url|grep -oE "/$") ;then | |
if [ -f "${htdocs}${url}${dirindex}" ];then | |
send_file "${htdocs}${url}${dirindex}" | |
else | |
send_403 | |
fi | |
return | |
fi | |
# TODO: procesar QUERY_STRING e idenfiticar los php y mandarlos a php-cgi -q | |
# no cayó ningún if, tons nostá | |
send_404 | |
} | |
# --[ "Core" ]-- | |
# leemos request y luego el resto de headers, pues solo nos interesa el request | |
read request | |
while true | |
do | |
read header | |
[ "$header" == $'\r' ] && break; | |
done | |
url="${request#GET }" | |
url="${url% HTTP/*}" | |
query="${url#*\?}" | |
url="${url%%\?*}" | |
# Es el index? | |
if [ "$url" == '/' ];then url="$url$dirindex";fi | |
file="$htdocs$url" | |
if [ -f "$file" ] | |
then | |
send_file $file | |
else | |
proc_req | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment