4 require 'conf/paths.pl';
7 $HELPME="http://reality.sgi.com/employees/mblakele_engr/WebStone/";
8 $| = 1; # set pipes to hot
16 # These strings are used in, among others, PERL-to-HTML scripts.
21 print "$html_port\n" if $debug;
23 $HTML_STARTPAGE = "http://localhost:$html_port$wd/doc/WebStone.html";
26 # Fork off the HTML client, and fork off a server process that
27 # handles requests from that client. The parent process waits
28 # until the client exits and terminates the server.
30 print "Starting $MOSAIC...\n" if $debug;
32 if (($client = fork()) == 0) {
34 delete $ENV{$_} if (/proxy/i && !/no_proxy/i);
36 exec($MOSAIC, "$HTML_STARTPAGE")
37 || die "cannot exec $MOSAIC: $!";
40 if (($server = fork()) == 0) {
41 if (($helper = fork()) == 0) {
46 $SIG{'PIPE'} = 'IGNORE';
48 accept(CLIENT
, SOCK
) || die "accept: $!";
49 select((select(CLIENT
), $| = 1)[0]);
50 &process_html_request
();
56 # Wait until the client terminates, then terminate the server.
60 kill('TERM', $server);
65 # Set up a listener on an arbitrary port. There is no good reason to
66 # listen on a well-known port number.
68 sub start_html_server
{
69 local($sockaddr, $proto, $junk);
75 $sockaddr = 'S n a4 x8';
76 $this = pack($sockaddr, $AF_INET, $PORT, "\0\0\0\0");
77 ($junk, $junk, $proto) = getprotobyname('tcp');
78 socket(SOCK
, $AF_INET, $SOCK_STREAM, $proto) || die "socket: $!";
79 setsockopt(SOCK
, 0xffff, 0x0004, 1) || die "setsockopt: $!";
80 bind(SOCK
, $this) || die "bind: $!";
81 listen(SOCK
, 1) || die "listen: $!";
82 ($junk, $html_port) = unpack($sockaddr, getsockname(SOCK
));
87 # Process one client request. We expect the client to send stuff that
90 # command /password/perl_script junk
92 # Where perl_script is the name of a perl file that is executed via
95 # In case of a POST command the values in the client's attribute-value
96 # list are assigned to the corresponding global PERL variables.
98 sub process_html_request
{
99 local($request, $command, $script, $magic, $url, $peer);
103 # Parse the command and URL. Update the default file prefix.
106 print $request if $debug;
107 ($command, $url) = split(/\s+/, $request);
108 if ($command eq "" || $command eq "QUIT") {
112 #($junk, $script) = split(/\//, $url, 2);
113 #($script, $html_script_args) = split(',', $script, 2);
114 #($HTML_CWD = "file:$script") =~ s/\/[^\/]*$//;
121 if ($command eq "GET") {
125 elsif ($script =~ /\.pl\b/) {
126 perl_html_script
($script);
131 } elsif ($command eq "POST") {
133 print $request if $debug;
136 # Process the attribute-value list.
139 print "Hi $_" if $debug;
144 $html_post_attributes = '';
146 for (split(/(%[0-9][0-9A-Z])/, $_)) {
147 $html_post_attributes .= (/%([0-9][0-9A-Z])/) ?
148 pack('c',hex($1)) : $_;
150 %args = ('_junk_', split(/\n([^=]+)=/, $html_post_attributes));
151 delete $args{'_junk_'};
153 print "\$$_ = $args{$_}\n" if $debug;
156 perl_html_script
($script);
158 &bad_html_form
($script);
161 &bad_html_command
($request);
167 # Unexpected HTML command.
169 sub bad_html_command
{
170 local($request) = @_;
175 <TITLE>Unknown command</TITLE>
176 <LINK REV="made" HREF=$HELPME>
179 <H1>Unknown command</H1>
180 The command <TT>$request<TT> was not recognized.
188 # Execute PERL script
190 sub perl_html_script
{
197 <TITLE>File not found</TITLE>
198 <LINK REV="made" HREF=$HELPME>
201 <H1>File not found</H1>
202 The file <TT>$script</TT> does not exist or is not accessible.
209 if ($@
&& ($@
ne "\n")) {
213 <TITLE>Command failed</TITLE>
214 <LINK REV="made" HREF=$HELPME>
217 <H1>Command failed</H1>
227 # Missing attribute list
235 <TITLE>No attribute list</TITLE>
236 <LINK REV="made" HREF=$HELPME>
239 <H1>No attribute list</H1>
241 No attribute list was found.
249 # Give them something to read while the server is initializing.
253 accept(CLIENT
, SOCK
) || die "accept: $!";
258 <TITLE>Initializing...</TITLE>
259 <LINK REV="made" HREF=$HELPME>
262 <H1>Initializing...</H1>
263 WebStone is initializing...
283 unless ($file =~ /(html|htm|gif|jpeg|jpg)\b/);
295 unless ($file =~ /(html|htm|gif|jpeg|jpg)\b/);
300 opendir(DIRECTORY
, $dir);
301 @listing = readdir(DIRECTORY
);
314 print CLIENT
"<P><A HREF=$dir/$_>$_</A>";
316 print CLIENT
"</HTML>";