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


In reply to CGI::Application + Net::HTTPServer by gryphon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.