From: Christian Hansen Date: Sun, 16 Oct 2005 13:17:34 +0000 (+0000) Subject: added two examples X-Git-Tag: v1.0~56 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FHTTP-Request-AsCGI.git;a=commitdiff_plain;h=3cdea3c70da494847f25f7b1ae3376e9a294dd38 added two examples --- diff --git a/examples/daemon.pl b/examples/daemon.pl new file mode 100644 index 0000000..083afd1 --- /dev/null +++ b/examples/daemon.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use CGI; +use HTTP::Daemon; +use HTTP::Request; +use HTTP::Request::AsCGI; +use HTTP::Response; + +my $server = HTTP::Daemon->new || die; + +print "Please contact me at: url, ">\n"; + +while ( my $client = $server->accept ) { + + while ( my $request = $client->get_request ) { + + my $c = HTTP::Request::AsCGI->new($request)->setup; + my $q = CGI->new; + + print $q->header, + $q->start_html('Hello World'), + $q->h1('Hello World'), + $q->end_html; + + $c->restore; + + my $message = "HTTP/1.1 200 OK\x0d\x0a"; + + while ( my $line = $c->stdout->getline ) { + $message .= $line; + last if $line =~ /^\x0d?\x0a$/; + } + + my $response = HTTP::Response->parse($message); + $response->content( sub { + if ( $c->stdout->read( my $buffer, 4096 ) ) { + return $buffer; + } + return undef; + }); + + $client->send_response($response); + } + + $client->close; +} diff --git a/examples/synopsis.pl b/examples/synopsis.pl new file mode 100644 index 0000000..2106a1e --- /dev/null +++ b/examples/synopsis.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use CGI; +use HTTP::Request; +use HTTP::Request::AsCGI; + +my $request = HTTP::Request->new( GET => 'http://www.host.com/' ); +my $stdout; + +{ + my $c = HTTP::Request::AsCGI->new($request)->setup; + my $q = CGI->new; + + print $q->header, + $q->start_html('Hello World'), + $q->h1('Hello World'), + $q->end_html; + + $stdout = $c->stdout; + + # enviroment and descriptors will automatically be restored when $c is destructed. +} + +while ( my $line = $stdout->getline ) { + print $line; +} diff --git a/lib/HTTP/Request/AsCGI.pm b/lib/HTTP/Request/AsCGI.pm index f7b3b12..ccae2b4 100644 --- a/lib/HTTP/Request/AsCGI.pm +++ b/lib/HTTP/Request/AsCGI.pm @@ -83,7 +83,10 @@ sub setup { or croak("Can't seek stdin: $!"); } - %ENV = %{ $self->enviroment }; + { + no warnings 'uninitialized'; + %ENV = %{ $self->enviroment }; + } open( STDIN, '<&=', $self->stdin->fileno ) or croak("Can't open stdin: $!");