For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2004 > Called as CGI or from command line?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Called as CGI or from command line?
bill

2004-05-19, 10:37 am





Is there a way for a Perl script to know whether it was called via
a CGI request or through the command line?

Thanks,

-bill
Tad McClellan

2004-05-19, 10:37 am

bill <please_post@nomail.edu> wrote:
>
>
> Is there a way for a Perl script to know whether it was called via
> a CGI request or through the command line?



I dunno off-hand.

But I _do_ know how to tell if it was run from a tty (command line)
or not:

perldoc -f -t


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Sherif Zaroubi

2004-05-19, 11:31 am


On Wed, 19 May 2004 08:52:47 -0500, Tad McClellan <tadmc@augustmail.com>
wrote:

> bill <please_post@nomail.edu> wrote:
>
>
> I dunno off-hand.
>
> But I _do_ know how to tell if it was run from a tty (command line)
> or not:
>
> perldoc -f -t
>
>


Check the environement variables ( %ENV, for example: $ENV{REMOTE_ADDR} )
Alan J. Flavell

2004-05-19, 11:31 am

On Wed, 19 May 2004, bill wrote:

> Is there a way for a Perl script to know whether it was called via
> a CGI request or through the command line?


But what's your _real_ question?

The CGI defines a common gateway interface. That interface includes
some environment variables, standard input, and standard output.

Those define the "common gateway". Anything else would be incidental,
and probably dependent on the web server etc.

So, if you wanted to test a CGI script from the command line, you
could set those variables beforehand. And then the script would think
it was being called from a CGI request. Now tell us whether you'd
define that to be "the command line" or "a CGI request". In a sense
it would be both, since you would have established the defined
requirements for the Common Gateway Interface.
A. Sinan Unur

2004-05-19, 11:31 am

Sherif Zaroubi <blah@blah.com> wrote in
news:opr783ymtfcx74rp@news.videotron.ca:

> On Wed, 19 May 2004 08:52:47 -0500, Tad McClellan
> <tadmc@augustmail.com> wrote:
>
>
> Check the environement variables ( %ENV, for example: $ENV{REMOTE_ADDR} )


IMHO, checking $ENV{GATEWAY_INTERFACE} interface communicates the purpose
of the code more clearly.

--
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)
Gregory Toomey

2004-05-19, 8:31 pm

bill wrote:

>
>
>
>
> Is there a way for a Perl script to know whether it was called via
> a CGI request or through the command line?
>
> Thanks,
>
> -bill


YES!

http://www.gregorytoomey.com/index....id=14&Itemid=28

(Apologies for the site to far - I just installed www.mamboserver.com on the wend)

gtoomey
Matt Garrish

2004-05-19, 9:31 pm


"Gregory Toomey" <nospam@bigpond.com> wrote in message
news:1137170.ErMfVmLqCn@GMT-hosting-and-pickle-farming...
>
> (Apologies for the site to far - I just installed www.mamboserver.com on

the wend)
>


Henceforth you shall be known as Orange Julius! : )

Matt


Anno Siegel

2004-05-21, 7:31 am

A. Sinan Unur <1usa@llenroc.ude> wrote in comp.lang.perl.misc:
> Sherif Zaroubi <blah@blah.com> wrote in
> news:opr783ymtfcx74rp@news.videotron.ca:
>
>
> IMHO, checking $ENV{GATEWAY_INTERFACE} interface communicates the purpose
> of the code more clearly.


Environment-checking probably is the solution OP was looking for, but it
should be clear that nothing stops a command-line user from setting those
variables. Their existence doesn't prove the program was called through
the CGI.

Anno
Alan J. Flavell

2004-05-21, 8:31 am

On Fri, 21 May 2004, Anno Siegel wrote:

> Environment-checking probably is the solution OP was looking for, but it
> should be clear that nothing stops a command-line user from setting those
> variables.


Right

> Their existence doesn't prove the program was called through
> the CGI.


That's a matter of terminology. If it walks like a duck, and quacks
like a duck... If all the relevant requirements have been set up for
the common gateway interface, then it -is- the common gateway
interface. What it really -doesn't- prove is that the program was
invoked from a web server (HTTPD), which is presumably the real
criterion that someone asking this question would have had in mind.
But I know of no portable (i.e platform-independent) way of proving
that conclusively.

ctcgag@hotmail.com

2004-05-21, 11:35 am

"Alan J. Flavell" <flavell@ph.gla.ac.uk> wrote:
> On Fri, 21 May 2004, Anno Siegel wrote:
>
>
> Right
>
>
> That's a matter of terminology. If it walks like a duck, and quacks
> like a duck... If all the relevant requirements have been set up for
> the common gateway interface, then it -is- the common gateway
> interface. What it really -doesn't- prove is that the program was
> invoked from a web server (HTTPD), which is presumably the real
> criterion that someone asking this question would have had in mind.
> But I know of no portable (i.e platform-independent) way of proving
> that conclusively.


Presumably, someone running Perl code from a CLI has access to the
code itself. That being the case, they can do whatever they want
with the code, such as removing or reversing the CGI detection logic.
So if you are assuming a hostile environment, at this point you are
already screwed. I think what the OP really wants to know is whether
the options should be gotten from CGI or from CL arguments, and whether
the output should be HTML or not. If an authorized CLI user wishes to
go out of his way to trick the script into making life more difficult for
himself, I really couldn't care less, unless I'm paying for his time.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Alan J. Flavell

2004-05-21, 12:34 pm

On Fri, 21 May 2004 ctcgag@hotmail.com wrote:

> Presumably, someone running Perl code from a CLI has access to the
> code itself. That being the case, they can do whatever they want
> with the code, such as removing or reversing the CGI detection logic.
> So if you are assuming a hostile environment, at this point you are
> already screwed.


Well, I was thinking more in terms of someone who is trying to design
a portable bi-modal script, which will behave in one way when invoked
from an HTTPD via the CGI (common gateway interface), and in a
different way when invoked from a user call.

I wasn't imputing "hostility" on the part of its user, but rather,
asking what they would want to happen if they, as a user, deliberately
synthesised a "common gateway interface" environment for the script
(for example, in order to debug the way in which it would behave when
run from an HTTPD via the CGI).

If you decided to base your test on the presence of a TTY, for
example, then you'd defeat the ability of the user to do that. Which
is why I was asking to think about the real purpose of this test.

> I think what the OP really wants to know is whether
> the options should be gotten from CGI or from CL arguments,


agreed

> and whether the output should be HTML or not.


Not necessarily! Some CGI scripts are designed to return JPEG, or
even plain text. Maybe you meant to say "whether the output needs to
begin with CGI response header(s) or not" ?

> If an authorized CLI user wishes to go out of his way to trick the
> script into making life more difficult for himself, I really
> couldn't care less,


Sure. But I don't think that was really the point that I was trying
to make. Sorry for any confusion caused.
Purl Gurl

2004-05-21, 1:31 pm

bill wrote:

> Is there a way for a Perl script to know whether it was called via
> a CGI request or through the command line?



I played around with this.

During play I discovered using a command line environment
variable works well. I set my "cmdline" variable before
running a test, and discovered this variable is reset
to reflect the current command line and arguments, when
my test script is invoked.

For a cgi call, my command line variable is null.

This applies to Win32 platforms.

C:\APACHE\USERS\TEST>set cmdline=purl

C:\APACHE\USERS\TEST>perl test.pl
Command Line Invoked


When browser accessed,

CGI Invoked

A more generic version would be to simply test
for existence of a command line variable, with
no test for actual content.

I did not test this for a Unix/Linux environment.


Purl Gurl
--
#!perl

if ($ENV{CMDLINE} eq "perl test.pl")
{ print "Command Line Invoked" }
else
{ print "Content-type: text/plain\n\nCGI Invoked"; }
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com