Git Product home page Git Product logo

p5-cgi-emulate-psgi's Introduction

NAME

CGI::Emulate::PSGI - PSGI adapter for CGI

SYNOPSIS

my $app = CGI::Emulate::PSGI->handler(sub {
    # Existing CGI code
});

DESCRIPTION

This module allows an application designed for the CGI environment to run in a PSGI environment, and thus on any of the backends that PSGI supports.

It works by translating the environment provided by the PSGI specification to one expected by the CGI specification. Likewise, it captures output as it would be prepared for the CGI standard, and translates it to the format expected for the PSGI standard using CGI::Parse::PSGI module.

CGI.pm

If your application uses CGI, be sure to cleanup the global variables in the handler loop yourself, so:

my $app = CGI::Emulate::PSGI->handler(sub {
    use CGI;
    CGI::initialize_globals();
    my $q = CGI->new;
    # ...
});

Otherwise previous request variables will be reused in the new requests.

Alternatively, you can install and use CGI::Compile from CPAN and compiles your existing CGI scripts into a sub that is perfectly ready to be converted to PSGI application using this module.

my $sub = CGI::Compile->compile("/path/to/script.cgi");
my $app = CGI::Emulate::PSGI->handler($sub);

This will take care of assigning a unique namespace for each script etc. See CGI::Compile for details.

You can also consider using CGI::PSGI but that would require you to slightly change your code from:

my $q = CGI->new;
# ...
print $q->header, $output;

into:

use CGI::PSGI;

my $app = sub {
    my $env = shift;
    my $q = CGI::PSGI->new($env);
    # ...
    return [ $q->psgi_header, [ $output ] ];
};

See CGI::PSGI for details.

METHODS

  • handler

      my $app = CGI::Emulate::PSGI->handler($code);
    

    Creates a PSGI application code reference out of CGI code reference.

  • emulate_environment

      my %env = CGI::Emulate::PSGI->emulate_environment($env);
    

    Creates an environment hash out of PSGI environment hash. If your code or framework just needs an environment variable emulation, use this method like:

      local %ENV = (%ENV, CGI::Emulate::PSGI->emulate_environment($env));
      # run your application
    

    If you use handler method to create a PSGI environment hash, this is automatically called in the created application.

AUTHOR

Tokuhiro Matsuno [email protected]

Tatsuhiko Miyagawa

COPYRIGHT AND LICENSE

Copyright (c) 2009-2010 by tokuhirom.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the LICENSE file included with this module.

SEE ALSO

PSGI CGI::Compile CGI::PSGI Plack CGI::Parse::PSGI

p5-cgi-emulate-psgi's People

Contributors

ctfliblime avatar dakkar avatar haarg avatar kazeburo avatar kazuho avatar markstos avatar miyagawa avatar tehmoth avatar tokuhirom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

p5-cgi-emulate-psgi's Issues

Perl 5.20: Deep recursion on subroutine "IO::Handle::print" at lib/CGI/Emulate/PSGI.pm line 86.

It's fine on 5.18, but on 5.20 I'm getting this error:

$ perlbrew exec perl -Ilib t/06_streams.t

18.2@std
==========
ok 1
ok 2
1..2

20.0
==========
Deep recursion on subroutine "IO::Handle::print" at lib/CGI/Emulate/PSGI.pm line 86.
Command terminated with non-zero status.

Here's a failing test report:
http://cpantesters.org/cpan/report/f8524b0a-1da7-11e4-a1f7-352ae0bfc7aa

Haven't yet tracked down info about what changed or what should be changed.

Tied STDERR in CGI::Emulate::PSGI creates an infinite loop

When CGI::Emulate::PSGI ties STDERR to psgi.errors, it doesn't check if psgi.errors is already set to STDERR; this creates an infinite loop when the CGI script does something like print STDERR "whatever", which calls CGI::Emulate::PSGI::ErrorsHandle::PRINT which calls IO::Handle::print which, which then calls CGI::Emulate::PSGI::ErrorsHandle and on until exhaustion. Something like the following:

$ cat app.psgi
use Plack::App::WrapCGI;
use IO::File;
my $app = Plack::App::WrapCGI->new(script => "./env.cgi")->to_app;
$ cat env.cgi 
#!/usr/bin/perl
use CGI;
print "Content-type: text/plain\n\n";

print qq{"$_" : '$ENV{$_}'\n} for (keys(%ENV)); 
print STDERR "complete\n";
$ plackup -s Standalone --port 9090 app.psgi           
HTTP::Server::PSGI: Accepting connections at http://0:9090/
Segmentation fault (core dumped) 
$ perl -d -S plackup -s Standalone --port 9090 app.psgi

Loading DB routines from perl5db.pl version 1.37
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(/perl5/bin/plackup:3):
3:      eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
4:          if 0; # not running under some shell
  DB<1> c
HTTP::Server::PSGI: Accepting connections at http://0:9090/
IO::Handle::print(perl5/IO/Handle.pm:415):
415:        @_ or croak 'usage: $io->print(ARGS)';
100 levels deep in subroutine calls!
  DB<1> T
$ = IO::Handle::print(ref(GLOB), 'complete^J') called from file 'perl5/CGI/Emulate/PSGI.pm' line 86
$ = CGI::Emulate::PSGI::ErrorsHandle::PRINT(ref(CGI::Emulate::PSGI::ErrorsHandle), 'complete^J') called from file 'perl5/IO/Handle.pm' line 417
$ = IO::Handle::print(ref(GLOB), 'complete^J') called from file 'perl5/CGI/Emulate/PSGI.pm' line 86
$ = CGI::Emulate::PSGI::ErrorsHandle::PRINT(ref(CGI::Emulate::PSGI::ErrorsHandle), 'complete^J') called from file 'perl5/IO/Handle.pm' line 417
...

perl 5.21.x does not include CGI anymore

This means that running the CGI-Emulate-PSGI without installing CGI first causes test failures:

PERL_DL_NONLAZY=1 /usr/perl5.21.2/bin/perl5.21.2 "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Harness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/00_compile.t .......... ok
Can't locate CGI.pm in @INC (you may need to install the CGI module) (@INC contains: /home/cpansand/.cpan/build/CGI-Emulate-PSGI-0.18-hr0jyz/blib/lib /home/cpansand/.cpan/build/CGI-Emulate-PSGI-0.18-hr0jyz/blib/arch /usr/perl5.21.2/lib/site_perl/5.21.2/amd64-freebsd /usr/perl5.21.2/lib/site_perl/5.21.2 /usr/perl5.21.2/lib/5.21.2/amd64-freebsd /usr/perl5.21.2/lib/5.21.2 .) at t/01_simple.t line 3.
BEGIN failed--compilation aborted at t/01_simple.t line 3.
t/01_simple.t ........... 
...

Adding CGI as a PREREQ_PM probably does not hurt here, also not for older perl versions.

CGI::Parse::PSGI::parse_cgi_output optimization idea

Hi Tokuhiro,
Thank you for your work on PSGI.

I had a problem with memory consumption on CGI::Parse::PSGI::parse_cgi_output and looked inside. I suggest to split support for filehandles and scalar refs into 2 separate subs and to stop using HTTP::Response. The code provided is about 13 times faster on small response (8K) and 15% times faster on large response (about 10Mb). Also the memory consumption is reduced on 15% on large response (and in becomes really great when we inline this code to avoid copying $stdout_buffer).
What do you think about this code/idea?
parse_cgi_output.txt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.