Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Simplest way (or module) to tweak query in CGI server-side validation

Reply
Thread Tools

Simplest way (or module) to tweak query in CGI server-side validation

 
 
Jerry Krinock
Guest
Posts: n/a
 
      06-13-2008
I've been working on my first perl cgi script for server-side form
validation today. I was amazed to find out that, after validating,
all I have to do is

print "Location: $redirect\n\n";

and my form gets redirected to another script. One line? No
modules? Great!

However, it looks like this simply passes through the received query
string, and now I realize I need to change a value or two in the query
after validation, for example, multiplying price by a coupon discount.

Is there any similar simple way to edit the query before sending the
redirect? Or do I need a module? Looks like

use CGI ;

has the functions I need. But I see there are other modules
available. If I need a module, what is a good one that my web host is
likely to have already installed?

Thank you,

Jerry Krinock
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-13-2008
Jerry Krinock wrote:
> I've been working on my first perl cgi script for server-side form
> validation today. I was amazed to find out that, after validating,
> all I have to do is
>
> print "Location: $redirect\n\n";
>
> and my form gets redirected to another script. One line? No
> modules? Great!
>
> However, it looks like this simply passes through the received query
> string, and now I realize I need to change a value or two in the query
> after validation, for example, multiplying price by a coupon discount.


Sounds like you don't want to just redirect, after all, but rather
submit a new query. The libwww-perl family of modules can do that; see
for instance LWP::UserAgent.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
jerrykrinock@gmail.com
Guest
Posts: n/a
 
      06-26-2008
On Jun 12, 7:37*pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:

> Sounds like you don't want to just redirect, after all, but rather
> submit a new query. The libwww-perl family of modules can do that; see
> for instance LWP::UserAgent.


Thank you, Gunnar. After playing with this for a couple weeks (not
full-time), I decided that I ^really^do^ want a redirect, not to
submit a new query. The reason is that I need the user to get a
response from the site that I redirected to, not my redirecting/
tweaking script. I could not find any function in LWP::UserAgent to
do a redirect, so I used CGI.

I have uploaded two "bonehead" scripts:
"Redirector.pl" redirects to "QueryHandler.pl", using CGI's
"redirect" function.
"QueryHandler.pl echoes the query back to the user, using CGI's
"Dump()" function.

Result of Experiments:

If you "go direct", entering into your web browser something like:
http://sheepsystems.com/cgi-test/sal...rry&color=blue
you get your query echoed back to you as expected.

But if you try and do that through my redirect, entering
http://sheepsystems.com/cgi-test/sal...rry&color=blue
it gets redirected OK but you don't get your query pairs echoed back.
This is because QueryHandler.pl does not get the query pairs. (I have
also confirmed this by writing to a log file.)

Is there any way to pass a query through a redirecting script, or am I
expecting something that is fundamentally impossible? (Recall that in
my actual application, the redirecting script will tweak some of the
values in the query.)

Thanks again,

Jerry

******** Redirector.pl *****************

#!/usr/bin/perl

use strict ;
use warnings ;
use CGI ;

my $httpQuery = new CGI;
my $redirectURL = "http://sheepsystems.com/cgi-test/sales/
QueryHandler.pl" ;
print $httpQuery->redirect($redirectURL) ;

exit() ;


******** QueryHandler.pl *****************

#!/usr/bin/perl

use strict ;
use warnings ;
use CGI ;

my $httpQuery = new CGI ;

my $returnBody .= "<p>Module CGI got query list:</p>" ;
my $cgiDump = $httpQuery->Dump() ;
if (defined($cgiDump)) {
$returnBody .= $cgiDump ;
}

# Output to client application
print "Content-type: text/html\n";
print "Status: ", 200, " \n\n";
print "<html>$returnBody</html>" ;

exit() ;


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-27-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Jun 12, 7:37 pm, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>> Sounds like you don't want to just redirect, after all, but rather
>> submit a new query. The libwww-perl family of modules can do that; see
>> for instance LWP::UserAgent.

>
> Thank you, Gunnar. After playing with this for a couple weeks (not
> full-time), I decided that I ^really^do^ want a redirect, not to
> submit a new query. The reason is that I need the user to get a
> response from the site that I redirected to, not my redirecting/
> tweaking script. I could not find any function in LWP::UserAgent to
> do a redirect, so I used CGI.
>
> I have uploaded two "bonehead" scripts:
> "Redirector.pl" redirects to "QueryHandler.pl", using CGI's
> "redirect" function.
> "QueryHandler.pl echoes the query back to the user, using CGI's
> "Dump()" function.
>
> Result of Experiments:
>
> If you "go direct", entering into your web browser something like:
> http://sheepsystems.com/cgi-test/sal...rry&color=blue
> you get your query echoed back to you as expected.
>
> But if you try and do that through my redirect, entering
> http://sheepsystems.com/cgi-test/sal...rry&color=blue
> it gets redirected OK but you don't get your query pairs echoed back.
> This is because QueryHandler.pl does not get the query pairs. (I have
> also confirmed this by writing to a log file.)
>
> Is there any way to pass a query through a redirecting script, or am I
> expecting something that is fundamentally impossible?


<snip>

Try this variant of Redirector.pl:

#!/usr/bin/perl

use strict ;
use warnings ;
use CGI ;
use URI::Escape;

my $httpQuery = new CGI;
my $redirectURL =
"http://sheepsystems.com/cgi-test/sales/QueryHandler.pl" ;
addquery( $redirectURL );
print $httpQuery->redirect($redirectURL) ;

sub addquery {
my %params = $httpQuery->Vars;

# change the params to your liking

my %esc_params;
while ( my ($k, $v) = each %params ) {
$esc_params{ uri_escape($k) } = uri_escape($v);
}
my $query = join ';', map "$_=$esc_params{$_}", keys %esc_params;
$_[0] .= "?$query" if $query;
}

__END__

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
jerrykrinock@gmail.com
Guest
Posts: n/a
 
      06-27-2008
On Jun 27, 3:17 am, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:

> Try this variant of Redirector.pl:


Big thanks to Gunnar. Yes, it works. So, the query is ^not^
automatically carried through by CGI. I have to read in the query,
change as desired, then append it to the redirect URL, starting with
"?". Very simple!

One more thing, though. I believe that this GET request will serve my
purposes, but what if I needed to POST the parameters to the
redirect? I've read some indications that this may not be possible.

The only way I've ever seen a POST transmitted is to use an HTML
<form>. Is there a "bare metal" way to transmit a POST? In order to
^receive^ a POST from CGI using "bare metal", I know you can simply
read STDIN. By analogy, I tried to ^transmit^ a POST by simply
print()-ing out the "key1=value1;key2=value2" string before or after
print($cgi->redirect()), but that does not seem to work. I have
tested and verified that my QueryHandler.pl will handle POST as well
as GET requests from curl, but it does not echo any parameters when
handling a redirect from Redirect.pl, so modified to print() a POST.

Jerry
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      06-27-2008
(E-Mail Removed) wrote:
> On Jun 27, 3:17 am, Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>
> > Try this variant of Redirector.pl:

>
> Big thanks to Gunnar. Yes, it works. So, the query is ^not^
> automatically carried through by CGI. I have to read in the query,
> change as desired, then append it to the redirect URL, starting with
> "?". Very simple!
>
> One more thing, though. I believe that this GET request will serve my
> purposes, but what if I needed to POST the parameters to the
> redirect? I've read some indications that this may not be possible.


You can direct the user's web-browser to submit to another CGI via GET by
writing it into the URL you are redirecting to. This mostly works because
it is just another URL, and the web browser probably doesn't know or care
it is for CGI. Obviously you would require more active support from the
browser in order to do this with a POST rather than than a GET. And the
browser is unlikely to provide this support, both because it is extra work
and for security reasons.

If the place you are redirecting too will accept a GET instead of a POST,
you could rewrite the POST to your site into the form of a GET for
redirecting. If the place you are redirecting to will not accept GET, or
the query string is too long to be in the form of a GET, that won't work.
What I've done in the past is use LWP to have my CGI post the POST to the
other site, and have the CGI receive the response and proxy it back to the
response back to the user (adding a BASE tag to it so links resolve
properly).

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      06-27-2008
(E-Mail Removed) <(E-Mail Removed)> wrote:

> The only way I've ever seen a POST transmitted is to use an HTML
><form>. Is there a "bare metal" way to transmit a POST?

^^^^
^^^^

perldoc -q form

How do I automate an HTML form submission?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
jerrykrinock@gmail.com
Guest
Posts: n/a
 
      06-28-2008
On Jun 27, 2:19*pm, (E-Mail Removed) wrote:

> You can direct the user's web-browser to submit to another CGI via GET by
> writing it into the URL you are redirecting to. *This mostly works because
> it is just another URL, and the web browser probably doesn't know or care
> it is for CGI. *Obviously you would require more active support from the
> browser in order to do this with a POST rather than than a GET. *And the
> browser is unlikely to provide this support, both because it is extra work
> and for security reasons.


Hmmm. I see quite a few issues there

> If the place you are redirecting too will accept a GET instead of a POST,


then I will will use the solution given by Gunnar!

> If [not] ...
> What I've done in the past is use LWP to have my CGI post the POST to the
> other site, and have the CGI receive the response and proxy it back to the
> response back to the user (adding a BASE tag to it so links resolve
> properly).


If I understand you correctly, believe that the address bar in the
user's web browser would then show my Redirector.pl instead of the
Redirectee.pl. That's not acceptable.

Xho, thank you very much for these ideas. After considering them, I
believe the answer to my question "What if the redirectee site won't
take GET and wants POST?" is "Well, then it's time to step back and
redefine the problem so that easier solutions will come into play!"

Jerry
 
Reply With Quote
 
jerrykrinock@gmail.com
Guest
Posts: n/a
 
      06-28-2008
On Jun 27, 4:49*pm, Tad J McClellan <(E-Mail Removed)> wrote:

> (E-Mail Removed) <(E-Mail Removed)> wrote:
> > *Is there a "bare metal" way to transmit a POST?

> [Reference in perldoc faq which suggesting using LWP::UserAgent]


Well, a module isn't my idea of "bare metal", but the module has
source code, and that source code invokes HTTP::Request::Common, which
has source code too...

http://search.cpan.org/src/GAAS/libw...uest/Common.pm

and the "sub POST" function beginning at line 24 looks like what I
asked for, the "bare metal" of doing a POST! Hmmm...not a simple
solution, but at least now I know where to look in case I ever really
need to modify and redirect a POST.

Thank you, Tad.
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      06-28-2008

Quoth (E-Mail Removed):
>
> Well, a module isn't my idea of "bare metal", but the module has
> source code, and that source code invokes HTTP::Request::Common, which
> has source code too...
>
> http://search.cpan.org/src/GAAS/libw...uest/Common.pm
>
> and the "sub POST" function beginning at line 24 looks like what I
> asked for, the "bare metal" of doing a POST! Hmmm...not a simple
> solution, but at least now I know where to look in case I ever really
> need to modify and redirect a POST.


If you really need to know that level of detail, you'd be better off
reading RFC 2616 than the source to LWP...

Ben

--
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
(E-Mail Removed) Groucho Marx
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
+Simplest+ way to attributes to all of a particular Web Form control type? ASP .Net 2 09-16-2005 03:38 AM
What's the simplest way to update an XML file? Alan Silver ASP .Net 4 08-10-2005 05:23 PM
What's the simplest way to do URL rewriting? Alan Silver ASP .Net 9 06-26-2005 02:16 PM
simplest way to password protect website with SQL Server Brent Burkart ASP .Net 5 10-16-2003 05:34 AM
what is the simplest way that I can achieve this? Newbie question Fendi Baba XML 0 10-15-2003 04:13 PM



Advertisments