Greetings fellow monks,
A while back, I posted about trying to wrap an existing CGI::Application web app inside a Net::HTTPServer script without making any modifications to the CGI::Application app's code. After some hacking, I think I've come up with something that mostly works. I've tested it for GET, POST, cookies, and redirects. It seems to do OK. There's one known bug: It doesn't save multiple cookies in a single request due to (what I think is) a bug in how Net::HTTPServer handles headers. So you can only save one cookie at a time for now.
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use Net::HTTPServer;
my $httpd = Net::HTTPServer->new(
'port' => 8081,
'docroot' => 'htdocs',
'index' => ['index.html', 'index.pl', 'index.cgi'],
'log' => 'STDOUT',
'type' => 'forking',
'type' => 'single',
'numproc' => 5
);
use CGI qw(-compile -oldstyle_urls);
use YourCGIApplicationModule;
sub yourapp {
my $req = shift;
my $res = $req->Response;
$ENV{$_} = $req->Env($_) || '' foreach (keys(%{$req->Env()}));
$ENV{HTTP_COOKIE} = join ('; ', map { $_ . '=' . $req->Cookie($_)
+}
keys %{$req->Cookie()});
my $your_cgi_app = YourCGIApplicationModule->new({
QUERY => CGI->new(
($req->Method eq 'GET') ?
$req->Query : ($req->Request =~ /\n\W*\n([^\n]+)/) ? $
+1 : ''
)
});
$res->CaptureSTDOUT;
$your_cgi_app->run;
$res->ProcessSTDOUT({ strip_header => 0 });
my ($headers, @content) = split(/\n\n/, $res->Body);
my $code = 200;
foreach (split(/\n/, $headers)) {
if (/^(.*?): (.*)$/) {
$res->Header($1, $2);
$code = $1 if (($1 eq 'Status') and ($2 =~ /^(\d+)\s/));
}
}
$res->Code($code);
$res->Body( join("\n\n", @content) );
return $res;
}
$httpd->RegisterURL({
'/yourapp' => \&yourapp
});
$httpd->Start;
$httpd->Process;
$httpd->Stop;
exit 0;
As always, I'm interested in any rewrites or suggestions you folks have. Thanks.
gryphon
Whitepages.com
code('Perl') || die;
Janitored by holli - added readmore tags
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|