acting as a proxy server

A

Angela

Hi,

I am trying to develop a cgi routine that requests an image on
behalf of a user and returns it to their mobile phone without caching the
image on my server. (Caching would break the conditions of the remote
image site's use).

I am able to return HTML content to a user but gif images have so far
defeated me.


My current code iteration looks like this:-

#!/www/perl/bin/perl.exe
use LWP::UserAgent;
use CGI;
use HTTP::Cookies;
use CGI::Carp qw/fatalsToBrowser/;

my $query = CGI-> new();
my $username=$query-> param('username');
my $password=$query-> param('password');

my $ua = LWP::UserAgent-> new;
my $req = HTTP::Request-> new(GET =
"http://secure.metoffice.com/logon.do?
username=$username&password=$password&submitted=&submit=logon");
$req- referer("http://www.activitae.com");
my $res = $ua- request($req);
my $cookie_jar = HTTP::Cookies-> new(
File = "$ENV{HOME}/cookies",
AutoSave = 1,
);
$cookie_jar-> extract_cookies($res);

my $id = $res-> {'_headers'}-> {'set-cookie'}- [10];
if ($res-> is_success) {
# for testing only
#print $res-> as_string;
#print $id;
} else {
print "Failed: ", $res-> status_line, "\n";
}
# alternate lines for testing
# returns html
#my $secreq = HTTP::Request-> new(GET =
'http://secure.metoffice.com/aviation/reports.do?type=METAR&list=04');
# returns gif
my $secreq = HTTP::Request- new(GET =
'http://secure.metoffice.com/aviation/productcomponentserver.do?
productId=157&componentId=0&imageFile=F215_1824.gif');

$secreq-> referer(http://www.activitae.com);
$cookie_jar-> add_cookie_header($secreq);

my $secres=$ua->request($secreq);
if ($secres- is_success) {
print "HTTP/1.0 \r\n";
print "Content-Type: image/gif\r\n";
print "Connection: close\r\n\r\n";

binmode print $secres-> {'_content'};
exit 0;
} else {
print "Failed: ", $secres- status_line, "\n";
}

Firstly the code does a remote logon for a user and gets a session ID,
then a second request for the image of weather data is made. When run
via the Windows development environment of Komodo code headed by GIF87 is
returned but this appears not to be a good representation of a gif image.
When the alternate line for $secreq is used good html is returned.

However, when both alternatives are run as a server cgi routine, a server
error 'premature end
of script errors' is reported.

So my code has at least two errors!!

Any pointers to the correct way of doing things will be greatly
appreciated!

Angela
 
C

chris-usenet

Angela said:
My current code iteration looks like this:-
#!/www/perl/bin/perl.exe
use LWP::UserAgent;
use CGI;
use HTTP::Cookies;
use CGI::Carp qw/fatalsToBrowser/;

use strict?
use warnings?
my $secres=$ua->request($secreq);
if ($secres- is_success) {
print "HTTP/1.0 \r\n";
print "Content-Type: image/gif\r\n";
print "Connection: close\r\n\r\n";

That's not valid CGI. (It does look like plausible HTTP, though.)
binmode print $secres-> {'_content'};

My version of perl suggests that this is not how you should use
binmode. See "perldoc -f binmode" for details.
} else {
print "Failed: ", $secres- status_line, "\n";

This definitely isn't CGI.

Try this (untested) snippet instead:

if ($secres->is_success) {
print $query->header(-type => 'image/gif', -expires => 'now');
binmode STDOUT, ":raw";
print $secres->{'_content'};
}
else {
print $query->header(-type => 'text/plain', -expires => 'now');
print "Failed: ", $secres->status_line, "\n";
}

Chris
 
T

Tad McClellan

I am able to return HTML content to a user


With the code you show below?

I doubt it.

#!/www/perl/bin/perl.exe


You should ask for all the help you can get!

use warnings;
use strict;

my $req = HTTP::Request-> new(GET =

my $req = HTTP::Request-> new(GET =>
^^
File = "$ENV{HOME}/cookies",
AutoSave = 1,


File => "$ENV{HOME}/cookies",
AutoSave => 1,

my $id = $res-> {'_headers'}-> {'set-cookie'}- [10];

my $id = $res-> {'_headers'}-> {'set-cookie'}->[10];
^
^
my $secreq = HTTP::Request- new(GET =

my $secreq = HTTP::Request- new(GET =>

if ($secres- is_success) {
print "HTTP/1.0 \r\n";
print "Content-Type: image/gif\r\n";
print "Connection: close\r\n\r\n";


Why are you putting all of those "\r"s in there?

binmode print $secres-> {'_content'};


perldoc -f binmode

binmode FILEHANDLE, LAYER
binmode FILEHANDLE

binmode() takes a filehandle as its first argument.

You are not passing it a filehandle as its first argument.

print "Failed: ", $secres- status_line, "\n";


print "Failed: ", $secres->status_line, "\n";
^
^

Firstly the code does a remote logon


No it doesn't. It can't "do" anything if it cannot first compile.


However, when both alternatives are run as a server cgi routine, a server
error 'premature end
of script errors' is reported.


That can happen when your program will not compile.

See if it will compile from the command line before putting it
on the server:

perl -cw myprog.cgi

So my code has at least two errors!!


Your code has dozens of errors.

Too many to fix for you.

Start over, working on one small part of your problem at a time.

Get working code that does that one part first, then move on
the next part.

Lather, rinse, repeat.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top