#!/usr/athena/bin/perl # # Mini HTTP server; serves a predefined set of files # # $Header: /afs/athena.mit.edu/user/k/o/kolya/ops/mtpub/bin/RCS/http-server.pl,v 1.3 2001/01/23 01:49:24 kolya Exp $ use strict; my %documents = ( '/', 'dosub|text/html|/usr/local/share/mindterm/main.html', '/ssh.html', 'dosub|text/html|/usr/local/share/mindterm/ssh.html', '/ssh-mac.html', 'dosub|text/html|/usr/local/share/mindterm/ssh-mac.html', '/mindbright.jar', 'nosub|text/plain|/usr/local/share/mindterm/mindbright.jar', ); my $request = <>; $request =~ s/[\r\n]*$//; my $parsing = 1; while ($parsing) { my $input = <>; $input =~ s/[\r\n]*$//; # For now, we don't care about the input, so we just stop # parsing once we encounter a blank line. $parsing = 0 if $input =~ /^$/; } my ($req_method, $req_url, $req_proto) = split /\s/, $request; Error(405, "Unsupported method") unless $req_method =~ /^GET$/i; Error(500, "Invalid protocol") unless $req_proto =~ /^HTTP/; Error(404, "Document not found") unless $documents{$req_url}; my ($resp_sub, $resp_mime, $resp_file) = split /\|/, $documents{$req_url}; HttpResponse(200, "OK", $resp_mime); my $dialuptype; if ($resp_sub =~ /^dosub$/i) { $dialuptype = `/etc/athena/dialuptype`; $dialuptype =~ s/[\r\n]*$//; if ($dialuptype eq 'normal') { $dialuptype = 'athena'; } } open INDOC, $resp_file; while() { s/DIALUPTYPE/$dialuptype/g if $resp_sub =~ /^dosub$/i; print $_; } close INDOC; exit 0; sub Error { my ($code, $msg) = @_; HttpResponse($code, $msg, "text/html"); print "Error $code: $msg\n", "\n", "

Error $code: $msg

\n", "

\n", "Your request could not be completed as received.

\n", "Please check your URL and try again, or contact the\n", "owner of this machine for assistance.\n", "\n"; exit -1; } sub HttpResponse { my ($code, $msg, $mime) = @_; print "HTTP/1.0 $code $msg\n", "Connection: close\n", "Content-Type: $mime\n", "\n"; }