plex/0000755000175000017500000000000011222146226010500 5ustar scioscioplex/Plex.pm0000644000000000000000000000016311222146063011767 0ustar rootrootpackage Plex; my $VERSION = "2.7"; our ($WEBSERVER, $USER_DIR, $ROOT, %SERVER); $SERVER{VERSION} = $VERSION; 1; plex/Plex/0000755000000000000000000000000011222146124011427 5ustar rootrootplex/Plex/Compiler.pm0000644000000000000000000000166611222146124013550 0ustar rootrootpackage Plex::Compiler; use Plex::Compiler::Lexer; my $debug = 1; @ISA = ( Plex::Compiler::Lexer ); my @comp; sub new { my ($class, @tokens) = @_; my $self = {}; @{$self->{tokens}} = @tokens; bless ($self, $class); return $self; } sub compile { my $self = shift; foreach my $token_ref (@{$self->{tokens}}) { my $to = $token_ref->[1]; $to =~ s/\?/$token_ref->[0]/; push(@comp, qq[$to\n]); } my $comp = join('', @comp); $self->{comp} = $comp; #$cache->cache_code( $comp ); @comp = (); $comp = undef; return $self; } sub execute { my ($self, %ARGS) = @_; if (defined %ARGS) { local %ENV; foreach my $arg (keys %ARGS) { $ENV{$arg} = $ARGS{$arg}; } } my $code = $self->{comp}; eval($code) or die "error: $!"; my @buffer = getbuf(); my $buffer = join('', @buffer); $self->{buffer} = $buffer; return $self; } 1; plex/Plex/HTTP.pm0000644000000000000000000000361711222146124012553 0ustar rootrootpackage Plex::HTTP; use IO::Socket; use HTTP::Status; use Plex::HTTP::Call; use HTTP::Request; use HTTP::Date; use URI::Escape; use Plex::Text; local $/ = undef; @ISA = ( Plex::HTTP::Cache, Plex::HTTP::Call ); my $CRLF = "\015\012"; my %cfg = ( 'version' => '1.1', 'server' => 'plex/2.7', ); sub new ($%) { my ($class, %args) = @_; $args{Listen} ||= 10; $args{Proto} ||= 'tcp'; $args{Reuse} ||= 1; $args{LocalPort} ||= 80; my $s = IO::Socket::INET->new(%args) or die "Couldn't set up socket: $!"; my $self = {}; $self->{server} = $s; bless($self, $class); return $self; } sub accept ($) { my $self = shift; my $c = $self->{server}->accept(); $self->{client} = $c; return $self; } sub get_request ($) { my $self = shift; my %data; my $c = $self->{client}; my $req; while (<$c>) { $req = $req.$_; if ($_ eq $CRLF) { last; } } my $http = HTTP::Request->parse($req); $self->{req} = $http; return $self; } sub make ($$$$) { my ($self, $status, $type, $content) = @_; my $c = $self->{client}; print $c "HTTP/$cfg{version} $status ".status_message($status).$CRLF; print $c "Date: ".time2str(localtime()).$CRLF; print $c "Server: $cfg{server}".$CRLF; print $c "Content-Type: $type".$CRLF; print $c "Content-Length: ".length($content).$CRLF.$CRLF; print $c $content; } sub make_error { my ($self, $status, $message) = @_; my $c = $self->{client}; my $content = "$status ".status_message($status)."
$message
".$cfg{server}.$CRLF; print $c "HTTP/$cfg{version} $status ".status_message($status).$CRLF; print $c "Date: ".time2str(localtime()).$CRLF; print $c "Server: $cfg{server}".$CRLF; print $c "Content-Type: text/html".$CRLF; print $c "Content-Length: ".length($content).$CRLF.$CRLF; print $c $content; } 1; plex/Plex/Language.pm0000644000000000000000000000045111222146124013510 0ustar rootrootpackage Plex::Language; use Filter::Util::Call; sub import { my ($type) = @_; my ($status); if (($status = filter_read() > 0)) { s/\/\*(.*?)\*\//MultiLineComment($1)/seg; } $status; } sub MultiLineComment { my $comment = shift; while (<$comment>) { chomp; } return $comment; } 1;plex/Plex/Template.pm0000644000000000000000000000030411222146124013535 0ustar rootrootpackage Plex::Template; local $/ = undef; sub display { my $display = shift; } sub compile { my $file = shift; open(FILE, "<$file"); my $text = ; close(FILE); } 1; plex/Plex/Text.pm0000644000000000000000000000031411222146124012707 0ustar rootrootpackage Plex::Text; sub html_escape ($) { my $string = shift; $string =~ s//>/g; $string =~ s/&/&/g; $string =~ s/"/"/g; return $string; } 1; plex/Plex/View.pm0000644000000000000000000000313011222146124012674 0ustar rootrootpackage Plex::View; use Plex::Compiler; our %cache; our @need; our $need_as_string; my @buffer; sub exec { my $file = shift; eval($cache{$file}) or die $!; my $buffer = join('', @buffer); @buffer = (); return $buffer; } sub out { my $string = shift; push(@buffer, $string); return 1; } sub show_cached { foreach (keys %cache) { print "$_ => [$cache{$_}]\n"; } } sub compile_all { my $root = $Plex::ROOT; traversedir($root); if ($Plex::USER_DIR) { traversedir("/home/"); } foreach my $need (@need) { my $lexer = Plex::Compiler::Lexer->new(); $lexer->read_file( $need ); $lexer->lex; my $compiler = Plex::Compiler->new( @{$lexer->{tokens}} ); $compiler->compile; print "after compiling it is ".$compiler->{comp}; my $code = $compiler->{comp}; $cache{$need} = $code; $code = undef; $compiler->{comp} = undef; $lexer = undef; $compiler = undef; print "after deleting it is ".$compiler->{comp}; } $need_as_string = join('', @need); } sub traversedir { my $path = shift; opendir(my $dhl,"$path") || die "Can't open directory: $!"; my @files = grep {!/^\.{1,2}$/ } readdir($dhl); foreach my $file (@files) { if(-d "$path$file") { traversedir("$path$file"); } else { my $absolute = "$path/$file"; if ($absolute =~ /\./) { if ($absolute =~ /\.view/i) { push(@need, $absolute); } } else { push(@need, $absolute); } } } closedir($dhl); } 1; plex/Plex/YAML.pm0000644000000000000000000000003011222146124012520 0ustar rootrootpackage Plex::YAML; 1; plex/Plex/Compiler/0000755000000000000000000000000011222146124013201 5ustar rootrootplex/Plex/Compiler/Cache.pm0000644000000000000000000000005711222146124014544 0ustar rootrootpackage Plex::Compiler::Cache; my %Cache; 1; plex/Plex/Compiler/Lexer.pm0000644000000000000000000000344511222146124014624 0ustar rootrootpackage Plex::Compiler::Lexer; my %default = ( STANDARD => { START => '<%', END => '%>', TO => '?', }, ); my %rules = ( COMMENT => { START => '<\#', END => '\#>', TO => '# ?\n', }, ); my %interpolations = ( PRINT => { TAG => '=', TO => 'out qq[?];', }, ); my $text; my @tokens; sub new { my $class = shift; my $self = {}; bless ($self, $class); return $self; } sub read { my ($self, $text_r) = @_; $text = $text_r; return 1; } sub read_file { my ($self, $file) = @_; local $/ = undef; open(FILE, "<$file"); $text = ; close(FILE); return 1; } sub lex { my $self = shift; while (1) { last if $text =~ m/\G\z/; if ($text =~ m/ \G $default{STANDARD}{START}(.+?)$default{STANDARD}{END} /sgcx) { push @tokens, [$1, $default{STANDARD}{TO}]; } foreach my $type (keys %rules) { if ($text =~ m/ \G $rules{$type}{START}(.+?)$rules{$type}{END} /sgcx) { push @tokens, [$1, $rules{$type}{TO}]; } } foreach my $interpolation (keys %interpolations) { if ($text =~ m/ \G $default{STANDARD}{START}$interpolations{$interpolation}{START}(.+?)$default{STANDARD}{END} /sgcx) { push @tokens, [$1, $interpolations{$interpolation}{TO}]; } } match_text() or die "Syntax error!"; } @{$self->{tokens}} = @tokens; @tokens = undef; return $self; } sub match_text { if ($text =~ m/ \G (.+?) (?= <\/?[%#] | \z) /sgcx) { push @tokens, [$1, 'out qq[?];']; return 1; } return 0; } 1; plex/Plex/Compiler/Simple.pm0000644000000000000000000000165711222146124015001 0ustar rootrootpackage Plex::Compiler::Simple; use Exporter; use Plex::Compiler::Cache; @ISA = ( Exporter ); use Plex::Compiler; my $lexer = Plex::Compiler::Lexer->new(); @EXPORT = (compile_file); @EXPORT_OK = (compile_file); sub compile_file { my (undef, $file) = @_; $lexer->read_file( $file ); $lexer->lex; my $compiler = Plex::Compiler->new( @{$lexer->{tokens}} ); $compiler->compile; $compiler->execute; return $compiler->{buffer}; } sub cache_compile { my (undef, $file) = @_; $lexer->read_file( $file ); $lexer->lex; my $compilr = Plex::Compiler->new( @{$lexer->{tokens}} ); $compiler->compile; $compiler->execute; $Plex::Compiler::Cache{$file} = sub { $compiler->{buffer} }; } sub compile { my (undef, $str) = @_; $lexer->read( $str ); $lexer->lex; my $compiler = Plex::Compiler->new( @{$lexer->{tokens}} ); $compiler->compile; $compiler->execute; return $compiler->{buffer}; } 1; plex/Plex/HTTP/0000755000000000000000000000000011222146124012206 5ustar rootrootplex/Plex/HTTP/Cache.pm0000644000000000000000000000045211222146124013550 0ustar rootrootpackage Plex::HTTP::Cache; my %cache; sub new { my $class = shift; return $class; } sub cache_code { my ($self, $file, $code) = @_; $cache{$file}{Code} = $code; return 1; } sub cache_out { my ($self, $file, $out) = @_; $cache{$file}{Out} = $out; return 1; } 1; plex/Plex/HTTP/Call.pm0000644000000000000000000000533211222146124013422 0ustar rootrootpackage Plex::HTTP::Call; use File::MMagic; use Plex::Compiler::Simple; my $mm = new File::MMagic; my %obj; sub new { my ($class, $root) = @_; my $self = {}; $self->{root} = $root; bless($self, $class); return $self; } sub handle { my ($self, $host, $file, $http) = @_; my $absolute = $Plex::ROOT."/".$host."/".$file; if ($file =~ /\/~(.*?)\/(.*)/) { $absolute = "/home/$1/$2"; } $absolute =~ s/\/\//\//g; if (-d $absolute) { if(is_index_in_dir($absolute)) { my $path = $absolute."index.view"; print "Called index file of $absolute\n"; $http->make(200, "text/html", &Plex::View::exec($path)); } else { $http->make_error(404, "No index file. LOL. You thought i'll give YOU directory listing!? :D"); } } elsif (-e $absolute) { my $type = $mm->checktype_filename( $absolute ); print "Type: $type :: $file\n"; $type =~ /(.*?)\/(.*)/; if ($file =~ /\./) { if ($file =~ /\/(.*?)\.view$/i) { print "Call $absolute\n"; $http->make(200, "text/html", &Plex::View::exec($absolute) ); } elsif ($file =~ /\/(.*?)\.css/i) { local $/ = undef; open(CSS, "<$absolute"); my $css = ; close(CSS); $http->make(200, "text/css", $css); } elsif ($file =~ /\/(.*?)\.gif/) { local $/ = undef; open(GIF, "<$absolute"); my $gif = ; close(GIF); $http->make(200, "image/gif", $gif); } elsif ($file =~ /\.jpg/) { local $/ = undef; open(JPG, "<$absolute"); my $jpg = ; close(JPG); $http->make(200, "image/jpg", $jpg); } } elsif ($file =~ /\/(.*?)$/) { print "Call $absolute\n"; $http->make(200, "text/html", &Plex::View::exec($absolute) ); } else { local $/ = undef; print "Delivering $file as $type\n"; open(FILE, "<$absolute"); my $file = ; close(FILE); $http->make(200, $type, $file); } } else { if (-e $absolute.".view") { print "Call $absolute".".view"; $http->make(200, "text/html", &Plex::View::exec($absolute.".view")); } else { $http->make_error(404, "No such file or directory."); } } } sub is_index_in_dir { my $dir = shift; opendir(DIR, $dir); my @files = readdir(DIR); closedir(DIR); foreach my $file (@files) { if ($file eq "index.view") { return 1; } } return 0; } 1; plex/Plex/loesung.pl0000644000000000000000000000052211222146124013437 0ustar rootrootsub traversedir { my $path = shift; opendir(my $dhl,"$path") || die "Can't open directory: $!"; my @files = grep {!/^\.{1,2}$/ } readdir($dhl); foreach my $file (@files) { if(-d "$path$file") { traversedir("$path$file"); } else { print "$path/$file\n"; } } closedir($dhl); } traversedir("/var/www/"); plex/Plex/Plex.pm0000644000000000000000000000005411222146124012674 0ustar rootrootpackage Plex; our ($WEBSERVER, $ROOT); 1; plex/plex0000755000000000000000000000500211222146133011412 0ustar rootroot#!/usr/bin/perl use Plex; use Plex::View; use Plex::HTTP; use Getopt::Long; use Proc::Daemon; use Symbol; use POSIX; my $PREFORK = 8; my $MAX_CLIENTS_PER_CHILD = 8; my %children = (); my $children = 0; my ($port, $config, $root, $dbeug, $dev, $vhosts, $user_dir) = undef; GetOptions( 'port=s' => \$port, 'root=s' => \$root, 'vhosts' => \$vhosts, 'debug' => \$debug, 'dev' => \$dev, 'user_dir' => \$user_dir, 'config' => \$config); if ($config) { use YAML::Syck; } $Plex::ROOT = $root; $Plex::USER_DIR = 1 if $user_dir; Plex::View::compile_all(); print "Plex/3.0 Application Server with PSL\n-----------------------\n"; print "Port => $port\n"; print "Root => $root\n"; print "Vhosts => $vhosts\n"; print "Debug => $dev\n"; print "Devel => $dev\n"; print "Compiled => $Plex::View::need_as_string\n"; print "-----------------------\n"; Proc::Daemon::Init if !$dev; my $http = Plex::HTTP->new( LocalPort => $port ); my $call = Plex::HTTP::Call->new( $root ); my $server = $http->{server}; $Plex::WEBSERVER = $http; for (1 .. $PREFORK) { spawn(); } $SIG{CHLD} = \&REAPER; $SIG{INT} = \&HUNTSMAN; while (1) { sleep; for ($i = $children; $i < $PREFORK; $i++) { $Plex::SERVER{spawned} = $children; $Plex::SERVER{to_spawn} = $PREFORK; spawn(); } } sub spawn { my $pid; my $sigset; $sigset = POSIX::SigSet->new(SIGINT); sigprocmask(SIG_BLOCK, $sigset) or die "Can't block SIGINT for fork: $!\n"; die "fork: $!" unless defined ($pid = fork); if ($pid) { sigprocmask(SIG_UNBLOCK, $sigset) or die "Can't unblock SIGINT for fork: $!\n"; $children{$pid} = 1; $children++; return; } else { $SIG{INT} = 'DEFAULT'; sigprocmask(SIG_UNBLOCK, $sigset) or die "Can't unblock SIGINT for fork: $!\n"; for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) { $http->accept() or last; my $client = $http->{client}; $http->get_request(); my $host = $http->{req}->header('Host'); my $file = $http->{req}->url->path; $call->handle( $host, $file, $http ); $client->close(); exit; } exit; } } sub REAPER { $SIG{CHLD} = \&REAPER; my $pid = wait; $children --; delete $children{$pid}; } sub HUNTSMAN { local($SIG{CHLD}) = 'IGNORE'; kill 'INT' => keys %children; exit; } plex/INSTALL0000644000175000017500000000020111222146226011522 0ustar scioscioINSTALL --- No install script yet. Just run plex to daemonize. Run: plex --port=80 --root=/var/www/ --user_dir --vhosts [--dev]