| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Web::Detect; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | our $DATE = '2015-01-03'; # DATE | 
| 4 |  |  |  |  |  |  | our $VERSION = '0.05'; # VERSION | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 26402 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 45 |  | 
| 7 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 91 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub import { | 
| 10 | 3 | 100 |  | 3 |  | 3311 | if ( @_ > 1 ) { | 
| 11 | 1 |  |  | 1 |  | 6 | no strict 'refs'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 671 |  | 
| 12 | 2 | 100 |  |  |  | 11 | if ( grep /^detect_web$/, @_ ) { | 
| 13 | 1 |  |  |  |  | 3 | *{ caller() . '::detect_web' } = \&detect_web; | 
|  | 1 |  |  |  |  | 5 |  | 
| 14 |  |  |  |  |  |  | } | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 2 | 100 |  |  |  | 14 | if ( grep /^detect_web_fast$/, @_ ) { | 
| 17 | 1 |  |  |  |  | 4 | *{ caller() . '::detect_web_fast' } = \&detect_web_fast; | 
|  | 1 |  |  |  |  | 6 |  | 
| 18 |  |  |  |  |  |  | } | 
| 19 |  |  |  |  |  |  | } | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub detect_web { | 
| 23 | 13 |  |  | 13 | 1 | 754 | my ($first) = @_; | 
| 24 | 13 |  |  |  |  | 12 | my %res; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 13 | 50 |  |  |  | 31 | if ( $ENV{MOD_PERL} ) {    # verified on mod_perl 1.3 and mod_perl 2.0 | 
| 27 | 0 |  |  |  |  | 0 | $res{mod_perl} = 1; | 
| 28 | 0 | 0 |  |  |  | 0 | return \%res if $first; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 13 | 50 |  |  |  | 21 | if ( $ENV{PLACK_ENV} ) { | 
| 32 | 0 |  |  |  |  | 0 | $res{plack} = 1; | 
| 33 | 0 |  |  |  |  | 0 | $res{psgi}  = 1; | 
| 34 | 0 | 0 |  |  |  | 0 | return \%res if $first; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 13 | 100 |  |  |  | 23 | if ( $ENV{'PANGEA'} ) { | 
| 38 | 7 |  |  |  |  | 11 | $res{'pangea'} = 1; | 
| 39 | 7 |  |  |  |  | 9 | $res{'psgi'}   = 1; | 
| 40 | 7 | 100 |  |  |  | 18 | return \%res if $first; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 11 | 50 | 33 |  |  | 44 | if ( $ENV{'CPANEL'} || $ENV{'WHM50'} ) { | 
| 44 | 0 |  |  |  |  | 0 | $res{'cpanel'} = 1; | 
| 45 | 0 | 0 |  |  |  | 0 | return \%res if $first; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 11 | 50 |  |  |  | 16 | if ( $ENV{'CATALYST_SCRIPT_GEN'} ) { | 
| 49 | 0 |  |  |  |  | 0 | $res{'catalyst'} = 1; | 
| 50 | 0 | 0 |  |  |  | 0 | return \%res if $first; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 11 | 50 |  |  |  | 29 | if ( $ENV{'DANCER_APPDIR'} ) { | 
| 54 | 0 |  |  |  |  | 0 | $res{'dancer'} = 1; | 
| 55 | 0 | 0 |  |  |  | 0 | return \%res if $first; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 11 | 50 |  |  |  | 17 | if ( $ENV{'MOJO_EXE'} ) { | 
| 59 | 0 |  |  |  |  | 0 | $res{'mojo'} = 1; | 
| 60 | 0 | 0 |  |  |  | 0 | return \%res if $first; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 11 | 50 |  |  |  | 14 | if ( $ENV{'FCGI_ROLE'} ) { | 
| 64 | 0 |  |  |  |  | 0 | $res{'FCGI.pm'} = 1; | 
| 65 | 0 | 0 |  |  |  | 0 | return \%res if $first; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 11 | 50 |  |  |  | 24 | if ( $ENV{'INSTANCE_ID'} ) { | 
| 69 | 0 |  |  |  |  | 0 | $res{'IIS'} = 1; | 
| 70 | 0 | 0 |  |  |  | 0 | return \%res if $first; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | # now, do more generic checks after specific server checks: | 
| 74 | 11 | 100 | 66 |  |  | 55 | if ( defined $ENV{GATEWAY_INTERFACE} && $ENV{GATEWAY_INTERFACE} =~ m/^CGI/ ) { | 
| 75 | 9 |  |  |  |  | 12 | $res{cgi} = 1; | 
| 76 | 9 | 100 |  |  |  | 21 | return \%res if $first; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # General server vars: | 
| 80 | 9 | 50 | 66 |  |  | 75 | if ( $ENV{'SCRIPT_NAME'} || $ENV{'SCRIPT_FILENAME'} || $ENV{'REMOTE_ADDR'} || $ENV{'HTTPS'} || $ENV{'QUERY_STRING'} || $ENV{'DOCUMENT_ROOT'} ) { | 
|  |  |  | 66 |  |  |  |  | 
|  |  |  | 33 |  |  |  |  | 
|  |  |  | 33 |  |  |  |  | 
|  |  |  | 33 |  |  |  |  | 
| 81 | 2 |  |  |  |  | 4 | $res{'general'} = 1; | 
| 82 | 2 | 50 |  |  |  | 50 | return \%res if $first; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | # still nothing? yikes ... | 
| 86 | 9 |  |  |  |  | 22 | for my $k ( keys %ENV ) { | 
| 87 | 16 | 100 |  |  |  | 81 | if ( $k =~ m/^(?:HTTP|SERVER|REQUEST)_/ ) { | 
| 88 | 2 |  |  |  |  | 4 | $res{'general'} += 2; | 
| 89 | 2 | 50 |  |  |  | 7 | return \%res if $first; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # the way scripts are run in some web servers they are still technically | 
| 94 |  |  |  |  |  |  | # "interactive", so interactivity is only reliable for this if it is false | 
| 95 |  |  |  |  |  |  | # (IO::Interactive::Tiny) | 
| 96 |  |  |  |  |  |  | # | 
| 97 |  |  |  |  |  |  | # ditto +/- for term detection (Term::Detect) | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 9 | 100 |  |  |  | 23 | return unless %res; | 
| 100 | 7 |  |  |  |  | 36 | return \%res; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub detect_web_fast { | 
| 104 | 5 |  |  | 5 | 1 | 11 | @_ = (1); | 
| 105 | 5 |  |  |  |  | 13 | goto &detect_web; | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | 1; | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | # ABSTRACT: Detect if program is running under some web environment | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | __END__ |