#!/usr/local/bin/gawk -f # # ahttpd -- awk httpd. # $Id: ahttpd,v 1.2 2000-04-08 03:48:44+09 daichi Exp $ # ## ## server configuration. ## BEGIN { server_root = "/web"; passwd = "/etc/passwd"; awkpath = "/usr/local/bin"; server_version = getversion(); directory_index = "index.html"; user_dir = "web"; } BEGIN { # mime types types["\.gif"] = "image/gif"; types["\.jpg"] = "image/jpeg"; types["\.png"] = "image/png"; types["\..?html"] = "text/html"; types["default"] = "text/plain"; } # # HTTP server.. # BEGIN { # # get input. # getline; request = $1; html = $2; version = $3; # # url parse. # if (match(html, /\.\./)) { # for security httperror(); exit 1; } # # ~user matching. # if (match(html, "~[a-zA-Z0-9]+\/")) { logname = substr(html, RSTART+1, RLENGTH-2); pwhome = getpwhome(logname); if (pwhome == "") { print "Error 404: no such user."; exit 1; } sub(/~[a-zA-Z0-9]+\//, pwhome "/" user_dir "/" , html); } else { html = server_root "/" html; } if (match(html, /\/$/)) { html = html directory_index; } gsub(/\/\//, "/", html); # # mime types. # type = mimetype(html); httpreply(type); cat(html); } # # utilities. # func getversion () { if (ARGV[0] ~ /gawk/) { sprintf("%s/%s --version 2>&1", awkpath, ARGV[0]) | getline; i = match($0, /[0-9.]+/); version = substr($0, i, RLENGTH); } else { version = "1.0"; } return version; } func mimetype (html) { for (regex in types) { if (html ~ regex "$") { return types[regex]; } } return types["default"]; } func getpwhome (name) { while (getline 0) { split($0, pwent, ":"); pwname = pwent[1]; pwhome = pwent[6]; if (name == pwname) { return pwhome; } } } func getypwhome (name) # yp version { while ("ypcat passwd" |getline > 0) { split($0, pwent, ":"); pwname = pwent[1]; pwhome = pwent[6]; if (name == pwname) { return pwhome; } } } func cat (file) { while (getline 0) { print; } } func httpreply (mimetype) { print "HTTP/1.0 200 OK"; httphdr(mimetype); } func httperror () { print "HTTP/1.0 403 Forbidden"; httphdr(); print ""; print "

Error 403

\nascending directory is prohibited."; print "


" \ "" \ "AWK httpd", server_version, "
"; } func httphdr (mimetype) { print "MIME-Version: 1.0"; print "Server: awk/" server_version; print "Content-Type:", mimetype; print ""; }