Merge remote-tracking (subtree) branch 'PracticingPerl/master'

This commit is contained in:
Dan Buch
2012-03-03 20:19:47 -05:00
41 changed files with 5186 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
ROOT ?= $(PWD)
HTTP_PORT ?= 49228
NGINX_CONF_IN := nginx.conf.in
ALL_NGINX_CONF_INPUTS := \
$(NGINX_CONF_IN) \
$(wildcard perl_requirements.d/*) \
$(wildcard perl_locations.d/*)
serve-lighttpd: lighttpd.conf
lighttpd -D -f $(ROOT)/$<
serve-nginx: nginx.conf
nginx -p $(ROOT)/ -c $(ROOT)/$<
nginx.conf: $(ALL_NGINX_CONF_INPUTS)
cat $< | \
sed -e 's@__ROOT__@$(ROOT)@g' \
-e 's@__HTTP_PORT__@$(HTTP_PORT)@g' > $@
lighttpd.conf: lighttpd.conf.in
cat $< | \
sed -e 's@__ROOT__@$(ROOT)@g' \
-e 's@__HTTP_PORT__@$(HTTP_PORT)@g' > $@
serve-cgiwrap: cgiwrap-fcgi.pl
perl $<
cgiwrap-fcgi.pl: cgiwrap-fcgi.pl.in
cat $< | sed -e 's@__ROOT__@$(ROOT)@g' > $@
.PHONY: serve-nginx serve-lighttpd serve-cgiwrap

View File

@@ -0,0 +1,155 @@
#!/usr/bin/env perl
use FCGI;
use Socket;
use FCGI::ProcManager;
sub shutdown { FCGI::CloseSocket($socket); exit; }
sub restart { FCGI::CloseSocket($socket); &main; }
use sigtrap 'handler', \&shutdown, 'normal-signals';
use sigtrap 'handler', \&restart, 'HUP';
require 'syscall.ph';
use POSIX qw(setsid);
END() { }
BEGIN() { }
{
no warnings;
*CORE::GLOBAL::exit = sub { die "fakeexit\nrc=" . shift() . "\n"; };
};
eval q{exit};
if ($@) {
exit unless $@ =~ /^fakeexit/;
}
&main;
sub daemonize() {
chdir '/' or die "Can't chdir to /: $!";
defined( my $pid = fork ) or die "Can't fork: $!";
exit if $pid;
setsid() or die "Can't start a new session: $!";
umask 0;
}
sub main {
$proc_manager = FCGI::ProcManager->new( {n_processes => 5} );
$socket = FCGI::OpenSocket("__ROOT__/nginx-cgiwrap-dispatch.sock", 10 )
; #use UNIX sockets - user running this script must have w access to the 'nginx' folder!!
$request =
FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%req_params, $socket,
&FCGI::FAIL_ACCEPT_ON_INTR );
$proc_manager->pm_manage();
if ($request) { request_loop() }
FCGI::CloseSocket($socket);
}
sub request_loop {
while ( $request->Accept() >= 0 ) {
$proc_manager->pm_pre_dispatch();
#processing any STDIN input from WebServer (for CGI-POST actions)
$stdin_passthrough = '';
{ no warnings; $req_len = 0 + $req_params{'CONTENT_LENGTH'}; };
if ( ( $req_params{'REQUEST_METHOD'} eq 'POST' ) && ( $req_len != 0 ) ) {
my $bytes_read = 0;
while ( $bytes_read < $req_len ) {
my $data = '';
my $bytes = read( STDIN, $data, ( $req_len - $bytes_read ) );
last if ( $bytes == 0 || !defined($bytes) );
$stdin_passthrough .= $data;
$bytes_read += $bytes;
}
}
#running the cgi app
if (
( -x $req_params{SCRIPT_FILENAME} ) && #can I execute this?
( -s $req_params{SCRIPT_FILENAME} ) && #Is this file empty?
( -r $req_params{SCRIPT_FILENAME} ) #can I read this file?
) {
pipe( CHILD_RD, PARENT_WR );
pipe( PARENT_ERR, CHILD_ERR );
my $pid = open( CHILD_O, "-|" );
unless ( defined($pid) ) {
print("Content-type: text/plain\r\n\r\n");
print "Error: CGI app returned no output - Executing $req_params{SCRIPT_FILENAME} failed !\n";
next;
}
$oldfh = select(PARENT_ERR);
$| = 1;
select(CHILD_O);
$| = 1;
select($oldfh);
if ( $pid > 0 ) {
close(CHILD_RD);
close(CHILD_ERR);
print PARENT_WR $stdin_passthrough;
close(PARENT_WR);
$rin = $rout = $ein = $eout = '';
vec( $rin, fileno(CHILD_O), 1 ) = 1;
vec( $rin, fileno(PARENT_ERR), 1 ) = 1;
$ein = $rin;
$nfound = 0;
while ( $nfound = select( $rout = $rin, undef, $ein = $eout, 10 ) ) {
die "$!" unless $nfound != -1;
$r1 = vec( $rout, fileno(PARENT_ERR), 1 ) == 1;
$r2 = vec( $rout, fileno(CHILD_O), 1 ) == 1;
$e1 = vec( $eout, fileno(PARENT_ERR), 1 ) == 1;
$e2 = vec( $eout, fileno(CHILD_O), 1 ) == 1;
if ($r1) {
while ( $bytes = read( PARENT_ERR, $errbytes, 4096 ) ) {
print STDERR $errbytes;
}
if ($!) {
$err = $!;
die $!;
vec( $rin, fileno(PARENT_ERR), 1 ) = 0
unless ( $err == EINTR or $err == EAGAIN );
}
}
if ($r2) {
while ( $bytes = read( CHILD_O, $s, 4096 ) ) {
print $s;
}
if ( !defined($bytes) ) {
$err = $!;
die $!;
vec( $rin, fileno(CHILD_O), 1 ) = 0
unless ( $err == EINTR or $err == EAGAIN );
}
}
last if ( $e1 || $e2 );
}
close CHILD_RD;
close PARENT_ERR;
waitpid( $pid, 0 );
} else {
foreach $key ( keys %req_params ) {
$ENV{$key} = $req_params{$key};
}
# cd to the script's local directory
if ( $req_params{SCRIPT_FILENAME} =~ /^(.*)\/[^\/] +$/ ) {
chdir $1;
}
close(PARENT_WR);
#close(PARENT_ERR);
close(STDIN);
close(STDERR);
#fcntl(CHILD_RD, F_DUPFD, 0);
syscall( &SYS_dup2, fileno(CHILD_RD), 0 );
syscall( &SYS_dup2, fileno(CHILD_ERR), 2 );
#open(STDIN, "<&CHILD_RD");
exec( $req_params{SCRIPT_FILENAME} );
die("exec failed");
}
} else {
print("Content-type: text/plain\r\n\r\n");
print "Error: No such CGI app - $req_params{SCRIPT_FILENAME} may not exist or is not executable by this process.\n";
}
}
}

View File

@@ -0,0 +1,19 @@
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# vim:filetype=nginx

View File

@@ -0,0 +1,35 @@
#!/usr/bin/env perl
use strict;
use warnings;
use CGI;
use CGI::Carp qw(carpout fatalsToBrowser);
BEGIN {
use CGI::Carp qw(carpout);
open(LOG, ">>/home/me/tmp/mycgi-log") or
die("Unable to open mycgi-log: $!\n");
carpout(*LOG);
}
sub main {
my $query = CGI->new();
print $query->header(-content_type => 'text/plain');
my $search = $query->param('q');
if (!$search eq undef) {
printf("You searched for: %s\n", $query->escapeHTML($search));
} else {
printf("Invalid search? You must provide a 'q' argument.\n");
}
}
main();
1;
__END__

View File

@@ -0,0 +1,7 @@
#!/usr/bin/env perl
printf("Content-Type: text/plain\n\n");
printf("Hello Test\n");
1;
__END__

View File

@@ -0,0 +1,38 @@
package base;
use strict;
use warnings;
use nginx;
sub handler {
my $r = shift();
$r->send_http_header("text/plain");
return OK if $r->header_only();
$r->print("Hello There!\n");
$r->rflush();
if (-f $r->filename or -d _) {
$r->print($r->uri, " exists!\n");
} else {
$r->print($r->uri, " does not exist!\n");
}
my $directory = '/home/me/tmp';
# '/home/me/src/LearningPerl/src/web';
my $didopen = opendir(DIR, $directory);
if ($didopen) {
while (my $file = readdir(DIR)) {
$r->print("$file\n");
}
closedir(DIR);
}
}
1;
__END__

View File

@@ -0,0 +1,119 @@
## modules to load
server.modules = (
"mod_alias",
"mod_compress",
"mod_cgi",
# "mod_rewrite",
# "mod_redirect",
# "mod_usertrack",
# "mod_expire",
# "mod_flv_streaming",
# "mod_evasive"
)
## a static document-root, for virtual-hosting take look at the
## server.virtual-* options
server.document-root = "__ROOT__/html"
## where to upload files to, purged daily.
server.upload-dirs = ("__ROOT__/lighttpd-uploads")
## where to send error-messages to
server.errorlog = "__ROOT__/lighttpd-error.log"
## files to check for if .../ is requested
index-file.names = (
"index.php",
"index.cgi",
"index.py",
"index.pl",
"index.html",
"index.htm",
"default.htm",
"index.lighttpd.html",
)
## Use the "Content-Type" extended attribute to obtain mime type if possible
# mimetype.use-xattr = "enable"
##
# which extensions should not be handle via static-file transfer
#
# .php, .pl, .fcgi are most often handled by mod_fastcgi or mod_cgi
static-file.exclude-extensions = (".php", ".pl", ".fcgi")
cgi.assign = (
".pl" => "/usr/bin/perl",
".php" => "/usr/bin/php-cgi",
".py" => "/usr/bin/python",
)
$HTTP["url"] =~ "^/cgi-bin/" {
cgi.assign += ("" => "")
}
######### Options that are good to be but not neccesary to be changed #######
## Use ipv6 only if available. (disabled for while, check #560837)
#include_shell "/usr/share/lighttpd/use-ipv6.pl"
## bind to port (default: 80)
server.port = __HTTP_PORT__
## bind to localhost only (default: all interfaces)
server.bind = "localhost"
## error-handler for status 404
#server.error-handler-404 = "/error-handler.html"
#server.error-handler-404 = "/error-handler.php"
## to help the rc.scripts
server.pid-file = "__ROOT__/lighttpd.pid"
##
## Format: <errorfile-prefix><status>.html
## -> ..../status-404.html for 'File not found'
#server.errorfile-prefix = "/var/www/"
## virtual directory listings
dir-listing.encoding = "utf-8"
server.dir-listing = "enable"
### only root can use these options
#
# chroot() to directory (default: no chroot() )
#server.chroot = "/"
## change uid to <uid> (default: don't change)
# server.username = "www-data"
## change gid to <gid> (default: don't change)
# server.groupname = "www-data"
#### compress module
compress.cache-dir = "__ROOT__/lighttpd-compress/"
compress.filetype = ("text/plain", "text/html", "application/x-javascript", "text/css")
#### url handling modules (rewrite, redirect, access)
# url.rewrite = ( "^/$" => "/server-status" )
# url.redirect = ( "^/wishlist/(.+)" => "http://www.123.org/$1" )
#### expire module
# expire.url = ( "/buggy/" => "access 2 hours", "/asdhas/" => "access plus 1 seconds 2 minutes")
#### external configuration files
## mimetype mapping
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
## load enabled configuration files,
## read /etc/lighttpd/conf-available/README first
# include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
# vim:filetype=lighttpd

View File

@@ -0,0 +1,71 @@
types {
text/html html htm shtml;
text/css css;
text/xml xml rss;
image/gif gif;
image/jpeg jpeg jpg;
application/x-javascript js;
application/atom+xml atom;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
image/svg+xml svg svgz;
application/java-archive jar war ear;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.ms-excel xls;
application/vnd.ms-powerpoint ppt;
application/vnd.wap.wmlc wmlc;
application/vnd.wap.xhtml+xml xhtml;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream eot;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mpeg mpeg mpg;
video/quicktime mov;
video/x-flv flv;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}

View File

@@ -0,0 +1,63 @@
worker_processes 1;
error_log __ROOT__/error.log;
pid __ROOT__/server.pid;
daemon off;
events {
worker_connections 4096;
}
http {
include __ROOT__/mime.types;
perl_modules __ROOT__/lib/perl;
include __ROOT__/perl_requirements.d/*;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
tcp_nopush on;
autoindex on;
server {
root __ROOT__/html;
index index.html;
listen __HTTP_PORT__;
server_name localhost;
access_log __ROOT__/access.log main;
include __ROOT__/perl_locations.d/*;
location ~ ^/cgi-bin/.*\.cgi$ {
gzip off;
fastcgi_pass unix:__ROOT__/nginx-cgiwrap-dispatch.sock;
fastcgi_index index.cgi;
fastcgi_param SCRIPT_FILENAME __ROOT__/html$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
}
}
}
# vim:filetype=nginx

View File

@@ -0,0 +1,5 @@
location /embedded {
perl base::handler;
}
# vim:filetype=nginx

View File

@@ -0,0 +1 @@
perl_require base.pm;