2 ############################################################################
3 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
4 ############################################################################
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License, version 2, as
7 # published by the Free Software Foundation.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 ############################################################################
32 # find program directory
35 my $l = readlink or die "readlink $_: $!\n";
36 if($l =~ m
|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
39 my $progdir = abs_path
($1);
40 my $wlib = "$progdir/soepkiptng_web.lib";
42 require "$progdir/soepkiptng.lib";
46 ############################################################################
54 $cookie = $cgiquery->cookie(
60 print $cgiquery->header(
61 -type
=>"text/html; charset=ISO-8859-15",
65 sub require_write_access
() {
67 if($conf{write_access_func
} &&
68 !eval $conf{write_access_func
}) {
71 printhdr
($conf{allstyle
});
72 print "<b>Access Denied.</b>\n";
82 my ($m, $c) = (stat $wlib)[9,10];
83 if($c > $m) { $m = $c };
84 return if $m <= $mtime;
96 warn "send_file: $f: $!\n";
99 $conn->print(sprintf(<<EOF, $m, (-s F)));
106 while(read F
, $buf, 4096) {
113 local $lifetime = $opt_l || $conf{httpd_lifetime
} || 100;
118 # busy, exit after finishing current request
121 warn "\n=====pid$$ SIGHUP caught, exiting\n";
126 my $dbh = connect_to_db
(\
%conf);
129 for($i = 0; $i < $lifetime; $i++) {
130 $conn = $daemon->accept or die "accept: $!\n";
134 $dbh = connect_to_db
(\
%conf);
137 my $r = $conn->get_request or next;
138 warn "\n=====pid$$->$i===== r->uri=[" . $r->uri . "]===\n";
140 if($r->method eq "GET") {
141 $r->uri =~ /\?(.*)/ and $content = $1;
142 if($r->uri =~ m
~^/(\w
+\
.(gif
|ico
))$~) {
143 send_file
($conn, "$progdir/web/$1", "image/gif");
147 } elsif($r->method eq "POST") {
148 $content = $r->content;
150 die "invalid request\n";
153 $ENV{HTTP_COOKIE
} = $r->header('cookie');
154 $cgiquery = new CGI
($content || "");
158 $req->{cgiquery
} = $cgiquery;
160 $req->{host
} = inet_ntoa
($conn->peeraddr);
161 my %cookies = $cgiquery->cookie('sv');
162 $req->{cookies
} = \
%cookies;
163 open STDOUT
, ">&=" . $conn->fileno;
164 handle_request
($req);
169 warn "$i connections served, exiting.\n";
173 ############################################################################
176 getopts
('dhp:i:l:c:F');
178 $opt_h and die <<EOF;
180 Usage: soepkiptng_httpd [-dFh] [-p port] [-i servers] [-l maxrequests]
183 -d : don't daemonize, log to stdout/stderr.
184 -F : stay in foreground
186 -p port : port to listen on
187 -i servers : number of child processes the parent process will spawn
188 -l maxrequests : the number of requests each child process is allowed to
189 process before it dies (to avoid problems like memory leaks
194 read_configfile
(\
%conf, $opt_c);
196 my $port = $opt_p || $conf{httpd_port
} || 80;
197 $daemon = new HTTP
::Daemon
(LocalPort
=> $port, ReuseAddr
=> 1)
198 or die "port $port: $!\n";
201 if(!$opt_F) { fork && exit; }
204 open STDIN
, "</dev/null";
205 if(defined($conf{httpd_errfile
})) {
206 rename $conf{httpd_errfile
}, "$conf{httpd_errfile}.old";
207 open STDOUT
, ">$conf{httpd_errfile}";
209 open STDOUT
, ">/dev/null";
211 open STDERR
, ">&STDOUT";
222 warn "master: killing -9 all children\n";
227 my $httpd_instances = $opt_i || $conf{httpd_instances
} || 2;
228 warn "master starting, pid=$$\n";
233 warn "master $$: exec self [$0]\n";
235 warn "master $$: exec self: $!\n";
239 warn "master: rereading configfile\n";
240 read_configfile
(\
%conf, $opt_c);
244 while(keys %pids < $httpd_instances) {
245 if(($pid = fork) == 0) {
250 next if !defined($pid);
251 warn "forked child, pid=$pid\n";
256 if($?
) { sleep 1; } # rate-limit failing children