#! @mgp_cv_path_perl@ # # Copyright (C) 1997 and 1998 WIDE Project. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. Neither the name of the project nor the names of its contributors # may be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # $Id: mgpnet.in,v 1.4 1998/08/26 05:30:13 itojun Exp $ # # configurations $tmpdir = '/tmp'; $httpdatestr = "date '+\%a, \%d \%b \%Y \%H:\%M:\%S \%Z'"; $seltimeout = 1; $refreshtimeout = 10; $debug = 0; $port = 9999; # it looks that "charset" parameter for Content-type makes many browsers # unhappy. it is a shame. $usecharset = 0; # do not edit beyond here $hostname = `hostname`; $hostname =~ s/\n$//; # portable? if (scalar(@ARGV) == 0) { open(IN, "ifconfig -a | grep 'inet '|") && do { do { $hostname = (split(/\s+/, ))[2]; } while ($hostname =~ /^127\./); close(IN); }; print "http://$hostname:$port/\n"; exit 0; } # greeting print STDERR "welcome to MagicPoint Netserver...\n"; # parameter parsing $tmpseed = 0; $checkfile = &tmpname; if (grep($ARGV[$_] eq '-T', 0 .. scalar(@ARGV) - 1)) { $checkfile = $ARGV[$_ + 1]; } else { @ARGV = ('-T', $checkfile, @ARGV); } $imagefile = &tmpname; $checkcontent = ''; # OS parameter &guessparam; if (!defined $AF_INET || !defined $PF_INET || !defined $SOCK_STREAM || !defined $sockaddr || !defined $WNOHANG) { print STDERR "could not guess system parameter. edit by hand.\n"; exit 1; } # HTTP/1.0 related $tmp = <; close(CHK); if ($checkcontent ne $t) { print STDERR "page updated.\n"; $checkcontent = $t; $pid = fork; return if (0 < $pid); # if fork success, parent returns. if ($pid == 0) { print STDERR "window grab: fork success.\n" if ($debug); } else { print STDERR "window grab: fork fail, ". "process without fork.\n" if ($debug); } $imgtmp = &tmpname; $errout = ($debug ? '' : '2>&-'); system "xwintoppm -silent -name MagicPoint | ". "ppmquant 256 $errout | ppmtogif $errout > $imgtmp"; if (-z $imgtmp) { unlink $imgtmp; $checkcontent = ''; } else { unlink $imagefile; link($imgtmp, $imagefile); unlink $imgtmp; $checkcontent = $t; } print STDERR 'window grab: done with ' . ($checkcontent eq '' ? 'failure' : 'success') . ".\n"; if ($pid == 0) { print STDERR "window grab: forked process dies.\n" if ($debug); # if fork success, child dies. exit 0; } } } sub chldhandler { local($sig) = @_; return if ($sig ne 'CHLD'); wait; } sub httpserver { local($httpreq, $httpmethod, $httppath, $httpver, $httphost); local($httpagent); local($imgplace, $imgwidth, $imgheight, $buf, $imglen); local($cthtml, $ctgif); $cthtml = ($usecharset ? 'text/html; charset=us-ascii' : 'text/html'); $ctgif = 'image/gif'; $httpreq = ; print STDERR 'HTTP in> ' . $httpreq if ($debug); $httpreq =~ s/[\r\n]+$//; $httpmethod = $httppath = $httpver = ''; ($httpmethod, $httppath, $httpver) = split(/\s+/, $httpreq); $httppath =~ s/http:\/\/[^:\/]+(:\d+)\//\//; if ($httpver eq '' || $httpver eq 'HTTP/1.0') { ; # ok } else { &httpheader(501, $cthtml) if ($httpver); print <Version not implemented

Version not implemented

HTTP protocol version $httpversion not supported.

EOF return; } $httphost = "$hostname:$port"; if ($httpver) { while () { $_ =~ s/[\r\n]+$//; $httphost = $1 if ($_ =~ /^Host:\s*(\S+)$/i); $httpagent = $1 if ($_ =~ /^User-Agent:\s*(\S+)$/i); last if ($_ eq ''); print STDERR 'HTTP in> ' . $_ . "\n" if ($debug); } } if ($httpmethod !~ /^(GET|HEAD)$/) { &httpheader(501, $cthtml) if ($httpver); print <Method not implemented

Method not implemented

$httpmethod to $httppath not supported.

EOF return; } $imgwidth = $imgheight = 0; if ($httppath =~ /^\/(\d+)x(\d+)\.html$/) { $imgwidth = $1; $imgheight = $2; $httppath = '/index.html'; } if ($httppath eq '/' || $httppath eq '/index.html') { if ($imgwidth && $imgheight) { $imgplace = "WIDTH=$imgwidth HEIGHT=$imgheight "; } else { $imgplace = ''; } &httpheader(200, $cthtml) if ($httpver); return if ($httpmethod ne 'GET'); if ($refreshtimeout) { print < EOF } print <MagicPoint Netserver \"presentation
normal
800x600
640x480
400x300
100x75


MagicPoint Netserver Presented by itojun.org
Supported by MagicPoint Project, WIDE Internet
EOF } elsif ($httppath eq '/presentation.gif') { open(IMG, "< $imagefile") || do { $checkcontent = ''; # invalidate &httpheader(404, $cthtml) if ($httpver); return if ($httpmethod ne 'GET'); print <File Not found

File Not found

The requested URL $httppath was not found on this server.

Looks like a mitake in configuration. Contact the administrator.

EOF return; }; &httpheader(200, $ctgif) if ($httpver); return if ($httpmethod ne 'GET'); while (0 < ($imglen = sysread(IMG, $buf, 4096))) { syswrite(NS, $buf, $imglen); } close(IMG); } else { &httpheader(404, $cthtml) if ($httpver); return if ($httpmethod ne 'GET'); print <File Not found

File Not found

The requested URL $httppath was not found on this server.

EOF } } sub httpheader { local($code, $ct) = @_; local($tmp); local($date); $date = `$httpdatestr`; $date =~ s/[\r\n]+//; $tmp = < ", split(/\n/, $tmp)); $tmp = 'HTTP out> ' . $tmp . "\n"; print STDERR $tmp; } } #------------------------------------------------------------ sub guessparam { local($tmpnam, $tmp, @tmp1, @tmp2); local(%varnames); %varnames = ( 'XXX1', 'AF_INET', 'XXX2', 'PF_INET', 'XXX3', 'SOL_SOCKET', 'XXX4', 'SO_REUSEPORT', 'XXX5', 'SOCK_STREAM', 'XXX6', 'WNOHANG', ); $tmpnam = &tmpname; open(CPP, "| @CPP@ >$tmpnam") || return; print CPP "#include \n"; print CPP "#include \n"; foreach $tmp (keys %varnames) { print CPP "$tmp $varnames{$tmp}\n"; } close(CPP) || return; $tmp = ''; open(CPP, "< $tmpnam") || return; while () { $tmp .= $_; } close(CPP); unlink $tmpnam; @tmp1 = split(/\n/, $tmp); if (grep($_ =~ /sin_len/, @tmp1)) { $havesinlen = 1; $sockaddr = 'C C n a4 x8'; } else { $havesinlen = 0; $sockaddr = 'S n a4 x8'; } foreach $i (keys %varnames) { if (@tmp2 = grep($_ =~ /^$i/, @tmp1)) { $tmp = (split(/\s+/, @tmp2[0]))[1]; $tmp = oct($tmp) if ($tmp =~ /^0/); next if ($tmp !~ /^[0-9]+$/); eval "\$$varnames{$i} = \$tmp;"; } } } sub tmpname { local($fname); do { $fname = $tmpdir . '/' . time . '.' . $$ . '.' . $tmpseed++; } while (-e $fname); return $fname; }