0

I need to create a Perl CGI script that will accept a single parameter as input. The parameter will be a fully qualified URL and the script will redirect the browser to the URL that has been passed as the parameter. The method is a GET and not a POST.

The browser address bar will accept the full script with the URL parameter like this: http://webserver/cgi-bin/myscript.pl?URL=http://www.google.com

I am new to Perl and I can figure out how to do it with a POST, but not a GET. Any help would be greatly appreciated.

I stole this code but it does not do a GET and I think I am using a bad example or one that does not apply to what I need to do:

UPDATE: This was my solution

#!/usr/local/bin/perl
use strict;
use warnings;


use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use DBI;
use URI::Escape;
use strictures;
use CGI;
use URI;

my $q = new CGI ;
my $url = "httpcandy";

# Process an HTTP request
#my @values  = $q->multi_param('form_field');
my $value   = $q->param('param_name');


print "Content-type: text/html\n\n";
#print "<pre>\n";
#
#foreach my $key (sort keys(%ENV)) {
#  print "$key = $ENV{$key}<br/>";
#}
#print "</pre>\n";



my $requested = URI->new( CGI::url() );
$requested->query( $ENV{QUERY_STRING} || $ENV{REDIRECT_QUERY_STRING} )
    if url_param();


#print header(),
#    start_html(),
#    h1("requested:"),
#    blockquote($requested),
#    h1("url:"),
#    blockquote($value),
#    h1("nothing else"),
#
#    end_html();
#    
if ($value =~ /http/)
{
   print "<META HTTP-EQUIV=refresh CONTENT=\"1;$value\">\n";
}
else
{
   print "<META HTTP-EQUIV=refresh CONTENT=\"1;URL=http://$value\">\n";
};

exit;
4
  • 1
    How you access POST or query string data depends on which library you are using to interact with the web server. You should provide some code to show us how you are accessing the data and tell us what you are using (Dancer? Catalyst? PSGI directly? CGI.pm?) Commented May 26, 2015 at 11:13
  • I guess the answer to your question is CGI. Commented May 26, 2015 at 11:31
  • 1
    You say you can “figure out how to do it with a POST yet your code uses a constant redirect URL, and not a parameter. Please can you show your working code? Commented May 26, 2015 at 11:48
  • I am sorry, you are correct. My code in the HTML says POST yet I am coding for a constant redirect. So my POST does not work either. I stopped trying to go down that POST path anyways since I need to do a GET. If anyone has some code snippet to share, I can learn by example. Coming into it brand new is difficult. Commented May 26, 2015 at 12:13

1 Answer 1

1

Your solution seems rather over-engineered. I think this does all that you need.

#!/bin/env perl

use strict;
use warnings;
use CGI ':cgi'; # Only the CGI functions

my $url = param('URL');

if ($url !~ /^http/) {
  $url = "http://$url";
}

print redirect($url);

In particular, your solution of using META HTTP-EQUIV in an HTML page seems really strange. HTTP-EQUIV is for situations where you can't alter the web server's headers. But as we're writing a CGI program, we can return whatever headers we warn. So using the redirect() function to return a 302 response seems to the be most obvious solution.

Note: If you get the input using the param() function, then it doesn't matter if it's a GET or POST request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.