Created
December 4, 2024 19:06
-
-
Save paigeadelethompson/5f0bbbc5e732188f9ae51927cd2a9717 to your computer and use it in GitHub Desktop.
HTTP::Simple::Server::CGI::PreFork example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use Env; | |
| use HTTP::Server::Simple::CGI::PreFork; | |
| { | |
| package WebServer; | |
| use base 'HTTP::Server::Simple::CGI::PreFork'; | |
| use File::Slurp; | |
| sub handle_request { | |
| $ENV{SCRIPT_FILENAME} = $0; | |
| my ($self, $cgi) = @_; | |
| my $pid = fork(); | |
| if($pid == 0) { | |
| my $path = $cgi->path_info(); | |
| if($path eq "/admin" || $path eq "/admin.cgi") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| require "./cgi-bin/admin.cgi"; | |
| } elsif($path eq "/bbs" || $path eq "/bbs.cgi" || $path eq "/") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| require "./cgi-bin/bbs.cgi"; | |
| } elsif($path eq "/read" || $path eq "/read.cgi") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| require "./cgi-bin/read.cgi"; | |
| } elsif($path eq "/search" || $path eq "/search.cgi") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| require "./cgi-bin/search.cgi"; | |
| } elsif($path eq "/remake" || $path eq "/remake.cgi") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| require "./cgi-bin/remake.cgi"; | |
| } elsif($path eq "/madakana" || $path eq "/madakana.cgi") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| require "./cgi-bin/madakana.cgi"; | |
| } elsif($path eq "/datas/admin.css") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| print "Content-Type: text/css;\r\n"; | |
| print "\r\n"; | |
| my $content = read_file('./cgi-bin/datas/admin.css'); | |
| print $content; | |
| } elsif($path eq "/datas/search.css") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| print "Content-Type: text/css;\r\n"; | |
| print "\r\n"; | |
| my $content = read_file('./cgi-bin/datas/search.css'); | |
| print $content; | |
| } elsif($path eq "/datas/design.css") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| print "Content-Type: text/css;\r\n"; | |
| print "\r\n"; | |
| my $content = read_file('./cgi-bin/datas/design.css'); | |
| print $content; | |
| } elsif($path eq "/datas/script.js") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| print "Content-Type: application/javascript;\r\n"; | |
| print "\r\n"; | |
| my $content = read_file('./cgi-bin/datas/script.js'); | |
| print $content; | |
| } elsif($path eq "/datas/default_bac.gif") { | |
| print "HTTP/1.1 200 OK\r\n"; | |
| print "Content-Type: image/gif;\r\n"; | |
| print "\r\n"; | |
| my $content = read_file('./cgi-bin/datas/default_bac.gif'); | |
| print $content; | |
| } else { | |
| print "HTTP/1.1 204 NO CONTENT\r\n"; | |
| } | |
| } | |
| } | |
| } | |
| my $ca = $ENV{CA_CERT}; | |
| my $cert = $ENV{CERT}; | |
| my $key = $ENV{KEY}; | |
| my $port = $ENV{PORT}; | |
| my $prefork = $ENV{WORKERS}; | |
| if($port eq "") { | |
| if(!$ca eq "" && !$cert eq "" && !key eq "") { | |
| $port = 8443; | |
| } else { | |
| $port = 8080; | |
| } | |
| } | |
| if($prefork eq "") { | |
| $prefork = 32; | |
| } | |
| my $server = WebServer->new($port); | |
| if(!$ca eq "" && !$cert eq "" && !key eq "") { | |
| $server->run(prefork => $prefork, usessl => 1, proto => 'ssleay', "--SSL_key_file"=> $key, "--SSL_cert_file"=> $cert); | |
| } else { | |
| $server->run(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment