satanklawz has asked for the wisdom of the Perl Monks concerning the following question:

What I'm working on is a cgi script that is on a computer that has access to two subnets. The desired result is when the script is accessed from one side, it requests an image off of a remote device on one subnet and then displays it to the user who is on the other subnet. Right now, I'm having an issue with getting the output of the image to the users page. Here is a sample snippet if the output:
... kVRFMNI"*#VZHF$2TPWPRj{#8HRW Xj  L !.x + 2)C‹4Ts c"ىYv9+V 0VOi8>Z@)ki›nH& + ...
Below is the code.
#!/usr/bin/perl use CGI; use LWP; use LWP::Simple; use LWP::UserAgent; $ua = new LWP::UserAgent; $q = new CGI; $url = "http://10.50.1.1/pics/topbanner1.gif"; $req= HTTP::Request->new(GET => $url); $page = $ua->request($req); if ($page->is_success) { $pic=$page->content; print $q->header, $q->start_html('hello world'), $q->h1("<img src='$pic'>"), $q->end_html; } else { print $q->header, $q->start_html($page->error_as_HTML), $q->h1($error), $q->end_html; }
The question is does anyone know how to get the image to be relayed without having to be stored locally? I'm intentionally not using any scrict at the moment so that debugging is easier.

Replies are listed 'Best First'.
Re: Printing inline images
by fglock (Vicar) on Jul 03, 2003 at 01:00 UTC
     $q->h1("<img src='$pic'>"),

    img src expects an URL. You are printing the picture binary data instead.

    What you need there is to print a link to another script, that will then "print" the picture with a "Content-type: image/gif" header.

      Maybe I'm going about this the wrong way... I have a camera server (soon to be more) on a 10.xxx.xxx.xxx IP, and this box with a routable IP and also a 10.xxx.xxx.xxx IP. What I would like to do, after user credentials are passed, have something like http://server/?cameraip=10.50.1.1 and then the streaming images, provided by a java script on the camera server, to appear on the outside users machine without them having to edit their internet client to use a proxy server. I know this can be done, I've seen it with public proxy servers. Am I going about solving this the right way?
Re: Printing inline images
by cleverett (Friar) on Jul 03, 2003 at 05:12 UTC
    You're getting garbage output because $src gets set to the binary data of the image, and then you put that directly into a HTML page.

    Something along the lines of this might work better:

    #!/usr/bin/perl use CGI; use LWP; use LWP::Simple; use LWP::UserAgent; $ua = new LWP::UserAgent; $q = new CGI; $url = "http://10.50.1.1/pics/topbanner1.gif"; $req= HTTP::Request->new(GET => $url); $page = $ua->request($req); if ($page->is_success) { print q->header(-type => image/gif), $page->content; } else { print $q->header(-status => 404), $q->h1('Not Found'), $q->p($page->error_as_HTML), $q->end_html; }
      Ah HA! More brains are better than one :)

      #!/usr/bin/perl use CGI; use LWP; use LWP::Simple; use LWP::UserAgent; $ua = new LWP::UserAgent; $q = new CGI; $url = "http://10.50.1.1/pics/topbanner1.gif"; $req= HTTP::Request->new(GET => $url); $req->header('Accept' => "$mime_type"); $page = $ua->request($req); if ($page->is_success) { print "Content-type: $mime_type\n\n"; print $page->content; } else { print $q->header, $q->start_html($page->error_as_HTML), $q->h1($error), $q->end_html; }

      IT WORKS! Thanks to all who helped :) I'm still looking into that proxy feature though as an option.
Re: Printing inline images
by fglock (Vicar) on Jul 03, 2003 at 01:36 UTC

    to appear on the outside users machine without them having to edit their internet client to use a proxy server

    If you are using Apache you could configure it as a proxy server using mod_proxy and your images would show up using plain http - no programming necessary, and no client-side configuration.

      So, if mod_proxy is installed on the apache server, and I have the image tag <img src="http://10.50.1.1/someimage">, the client who have no routing possibilities to 10.255.255.255 will have the image displayed?

      Learn something new every day I guess :)

        Not quite. Your image tag will still need to point to the 'visible' server, but the server will be able to decode the URL and proxy the request to the proper server.

        One way to do this is to use the ProxyPass directive provided by mod_proxy. Put something like the following in the conf file for the visible server:

        ProxyPass /10_50_1_1/ http://10.50.1.1/

        Now an URL pointing to http://visibleserver/10_50_1_1/foo/bar would get proxied by the server to http://10.50.1.1/foo/bar. Of course you could use another string in place of 10_50_1_1 to make your URLs look nicer.

        If you need more control over how the URL gets rewritten, have a look at the mod_rewrite module, which will work in concert with the mod_proxy module.

        ps. if you do setup mod_proxy, make sure you do not set it up as an open proxy... Make sure you have 'ProxyRequests off' somewhere in your config file. The ProxyPass directive will still work regardless of this setting.