acting as a proxy server

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

  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:-

   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 =
  " ?
      $req- referer(" ");
    my $res = $ua- request($req);
    my $cookie_jar = HTTP::Cookies-> new(
                     File     =  "$ENV/cookies",
                     AutoSave =  1,
      $cookie_jar-> extract_cookies($res);

      my $id = $res-> -> - [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 =
  ' ');
  # returns gif
    my $secreq = HTTP::Request- new(GET =
  ' ?

     $secreq-> referer( );
     $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-> ;
      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


Re: acting as a proxy server

>   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-> ;

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->;
    else {
    print $query->header(-type => 'text/plain', -expires => 'now');
    print "Failed: ", $secres->status_line, "\n";


Site Timeline