0

How to detect inside Perl script if it is called under FCGI or CGI. I want to detect if running under FCGI then load the FCGI modules, something like this at the top of the script:

if ($ENV{FCGI}) {
    use FCGI;
    use MyFCGIHandler;
}

I know I can do something like this:

use FCGI;
my $request = FCGI::Request();

#Returns whether or not the program was run as a FastCGI.
$isfcgi = $req->IsFastCGI();

But this means I have to load the FCGI module and call its Request and isFastCGI methods to check which is not good if app is not running under FCGI.

daliaessam
  • 1,636
  • 2
  • 21
  • 43
  • 1
    Can you explain why you want to do this? It's backwards from the usual method, which is to make your FCGI/CGI interface setup a bridge to the app code instead. I would recommend against trying to do your own handling of something this mundane. It's been done many times before. I'd most recommend checking out [Plack](https://metacpan.org/pod/Plack) and [PSGI](https://metacpan.org/pod/distribution/PSGI/PSGI.pod), which will do this for you and give you a low-level, consistent interface to both. – zostay Jul 31 '14 at 23:38
  • 2
    Your design is wrong. Once you have determined the environment and loaded the appropriate modules you still have to cope with multiple different APIs, so your code will be littered with tests to see which API to use. Don't do that. Use [`Plack`](https://metacpan.org/module/Plack) instead, which unifies all the popular servers and presents the same API to your code regardless of the flavour of web server – Borodin Aug 01 '14 at 00:25
  • 1
    From [this earlier question](http://stackoverflow.com/q/24982040) it looks like you're not understanding the difference between Plack/PSGI and FastCGI. They are very different beasts, and you should do some reading to clarify their purposes before you go much further – Borodin Aug 01 '14 at 00:30
  • @Borodin it is an old large application that already have index.cgi and index.fcgi that I want to combine in one file based on the test I am looking for. I can not refactor it for Plack. – daliaessam Aug 01 '14 at 00:36

1 Answers1

1

Found this at PerlMonks:

#!/usr/bin/perl

use strict;
use warnings;

use CGI::Fast qw(:standard);

sub mode
{
        my $h=$CGI::Fast::Ext_Request;
        if (defined($h) && ref($h) && $h->IsFastCGI()) {
                return 'FastCGI';
        } else {
                return 'CGI';
        }
}

while (CGI::Fast->new()) {
        print
                header(),
                start_html('CGI or FastCGI?'),
                h1('CGI or FastCGI?'),
                p('This application runs in ',mode(),' mode.'),
                end_html();
}
jimtut
  • 2,366
  • 2
  • 16
  • 37
  • This does not solve the problem. This solution still loads the FCGI module and get a handle on the request as I posted in my question. What I am trying to do is detect if the script is called under FastCGI then based on that I load the FCGI and create the requests and accept the requests. – daliaessam Aug 01 '14 at 00:05
  • Try checking `$ENV{GATEWAY_INTERFACE}`. I don't know if FastCGI sets this differently than CGI, but PerlEX does. – jimtut Aug 01 '14 at 01:03
  • It sets it to `'GATEWAY_INTERFACE' => 'CGI/1.1'` inside the accept request loop but nothing is set before the accept loop. – daliaessam Aug 01 '14 at 01:18
  • You might be able to use that then. Plain CGI will always set the env var, so if you have it outside the request loop, it must be plain CGI. – jimtut Aug 01 '14 at 02:32
  • Yes your idea may be correct, i.e., if not set it means FCGI after I check also I think not running from the command line which also does not set the %ENV. The question here for me to confirm, will this `$ENV{GATEWAY_INTERFACE}` set by all types of web servers, or may be some servers will not set it. – daliaessam Aug 01 '14 at 02:38