#!/usr/bin/perl -w #===================================================================== # # FILE: pm-css-test.pl # # USAGE: ./pm-css-test.pl # # SYNOPSIS: Stand-alone script for testing CSS for PerlMonks. # # DESCRIPTION: Acts as stand-alone server for testing CSS markup # for the PerlMonks website # OPTIONS: [-addr address] [-port n] [-listen n] [-timeout n] # [-help] # REQUIREMENTS: Data::Dumper, Getopt::Long, HTTP::Daemon, # HTTP::Status, and URI::Escape # BUGS: Lacks good test page content, others sure to be found # NOTES: Based on conversation with Corion, # and code from perl.com article by Robert Spier # ( "Embedding Web Servers" # - http://www.perl.com/pub/a/2002/09/17/ewispp.html ) # AUTHOR: atcroft # VERSION: 0.0a # CREATED: 09/23/2004 06:54:14 AM EDT # REVISION: --- #===================================================================== use strict; use vars qw(%service_ops $sample_page); use Data::Dumper; use Getopt::Long; use HTTP::Daemon; use HTTP::Status; use URI::Escape; $| = 1; %service_ops = ( 'LocalAddr' => 'localhost', 'LocalPort' => 65080, 'Proto' => 'tcp', 'Listen' => 5, 'Timeout' => 600, 'ToHandle' => 100 ); if ( scalar( grep( /^-/, @ARGV ) ) ) { my ($listen); my ($localaddr); my ($localport); my ($proto); my ($timeout); my ($tohandle); GetOptions( 'addr:s' => \$localaddr, 'port:i' => \$localport, 'listen:i' => \$listen, 'timeout:i' => \$timeout, 'tohandle:i' => \$tohandle, 'help' => \&help ); $service_ops{'Listen'} = $listen if ( defined($listen) ); $service_ops{'LocalAddr'} = $localaddr if ( defined($localaddr) ); $service_ops{'LocalPort'} = $localport if ( defined($localport) ); $service_ops{'Timeout'} = $timeout if ( defined($timeout) ); $service_ops{'ToHandle'} = $tohandle if ( defined($tohandle) ); } { my @lines = ; $sample_page = join( "\n", @lines ); } my $d = HTTP::Daemon->new(%service_ops) or die("Could not create HTTP::Daemon object: $!\n"); printf <accept ) { if (fork) { print "forking...\n"; } else { my (@remote_addr); my ($remote_port); my $to_handle = $service_ops{'ToHandle'}; ( $remote_port, @remote_addr ) = unpack( "x2nC4", $client ); while ( ( my $r = $c->get_request ) and ($to_handle) ) { $to_handle--; print join( ' ', scalar(localtime), join( ':', join( '.', @remote_addr ), $remote_port ), $r->method, $r->url->path, $r->content_length ), "\n"; my $test = $r->url; my $content = $r->content; # print Data::Dumper->Dump( [ \$test ], [qw(*test)] ), "\n"; # print Data::Dumper->Dump( [ \$r, \$content ], [qw(*r *content)] ), "\n"; if ( $r->method eq 'POST' and $r->url->path eq "/xyzzy" ) { my $buf = $r->content; while ( my $a = $c->read_buffer ) { $buf .= $a if ( defined($a) ); } $content = generate_page_content( $buf, 1 ); $c->send_response($content); } elsif ( $r->method eq 'GET' and $r->url->path eq '/sample.html' ) { $content = generate_sample_content(); $c->send_response($content); } elsif ( $r->method eq 'GET' and $r->url->path eq '/sample.css' ) { $c->send_file_response( $0 . '.css' ); } elsif ( $r->method eq 'GET' and $r->url->path eq '/' ) { $content = generate_page_content( '', 0 ); $c->send_response($content); } else { $c->send_error(RC_FORBIDDEN); } } $c->close; undef($c); } } sub generate_sample_content { my $h = HTTP::Headers->new( 'Content_Type' => 'text/html' ); my $t = HTTP::Response->new( 200, 'OK', $h ); $t->content($sample_page); return ($t); } sub generate_page_content { my ( $s, $write_flag ) = @_; my (%params); if ( -e $0 . '.css' ) { open( DF, $0 . '.css' ) or die("Could not open $0.css for input: $!\n"); @{ $params{'content'} } = ; chomp( @{ $params{'content'} } ); close(DF); } foreach ( split( /\&/, $s ) ) { my @parts = split( /=/, $_, 2 ); push( @{ $params{ $parts[0] } }, $parts[1] ); } @{ $params{'content'} } = ('') unless ( defined( $params{'content'} ) ); # local ( $\ = "\r\n" ); my $h = HTTP::Headers->new( 'Content_Type' => 'text/html' ); my $t = HTTP::Response->new( 200, 'OK', $h ); my $escaped_content = join( "\n", @{ $params{'content'} } ); my $submitted_content = uri_unescape( $escaped_content, "\x00-\xff" ); $submitted_content =~ s/\+/ /g; my $html_content = sprintf < test

Test CSS

Edit Current (also in %s.css, if present)
%s
CONTENT $t->content($html_content); if ($write_flag) { open( DF, '>' . $0 . '.css' ) or die("Could not open $0.css for output: $!\n"); print DF $submitted_content; close(DF); } return ($t); } sub help { print < test

Test CSS

Edit Current (also in $0.css, if present)
%s