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

Hello there. If anybody could be kind enough to help me here I would appreciate it. It's very simple. I have a html page
<html> <head> <title></title> </head> <body bgcolor="#FFFFFF"> <FORM ACTION="/cgi-bin/formtest.pl"> <P>Enter a URL to retrieve: <INPUT NAME="name"> <P><INPUT TYPE="SUBMIT" VALUE="GO!"> </FORM> </body> </html>
and a perl handler
#!/usr/bin/perl -T use LWP::UserAgent; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); $url = param('name'); print header; print start_html('My Remote Webpage Request Service'); my $ua = LWP::UserAgent->new; my $res = $ua->get($url); print $res->content; print end_html;
So, you enter a URL in the html page, and the perl retrieves it (well that's the idea).

If I enter a URL to a jpeg, the jpeg is displayed as garbage. An example line from what is returned to my browser is:

gifGIF89anY1e!YMIEJ}c +Q<sE<)I; YJaRm9qޔ)QcyƵsƾ +
etc. What am I doing wrong? Thank you very much. Jason

Replies are listed 'Best First'.
Re: Why is my image obtained by LWP displayed as garbage (i.e. plain/text)?
by Joost (Canon) on Oct 19, 2005 at 19:58 UTC
    You're printing the image's data as html content. Ofcourse that won't work.

    If you're only interested in images, you can do:

    #!/usr/bin/perl -T use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); $url = param('name'); print header; print start_html('My Remote Webpage Request Service'); print qq(<img src="$url">); print end_html;

    If you want different content types, you should take care of the url's content-type header and respond accordingly.

    update: I'm ignoring potential security issues here, since that would make the whole program a lot more complex.

      Thank you for responding so quickly. I just tried your suggestion, but unfortunately it didn't work. I *cough* am trying to defeat the corporate firewall (why not, it's motivational for learning perl) and what happens when I try your suggestion is that I see the dratted little red cross which (correct me if I'm wrong) is indicative that the image is being blocked. Or am I wrong? Thanks.

        Disclaimer: I am not advocating that you try to break your company's security policy. They probably have their policy for a reason - and breaking this policy is likely to get you into all kinds of trouble. Think carefully about this before doing it.

        However, in the interests of furthering discussion, if your browser sees the string <img src="/blah"> in a html document, it will attempt to make a separate http request to fetch the image - just as if you tried to type the address into your browser directly. This is therefore being blocked by the relevant firewall.

        To get around this, you need to look at the content_type header that you get back from the remote request (see the earlier post by Joost), and send that content-type back to the browser.

        Actually, to do this *properly* there are other headers that you would need to thing about (authentication and cookies being two good examples that come to mind). What you really want is a proxy server (eg squid, or apache with mod_proxy). However - if you have never set up such a thing before, I'd recommend either getting help, or at least reading the docs quite carefully, as open proxies on the internet are a bad thing (tm)

        defeating the corporate firewall may sound like a fun thing to do but can get you in to loads of trouble. Loss of a job and/or criminal/civil prosecution. It's just not worth the hassle. IMHO

        Jason L. Froebe

        Team Sybase member

        No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

        If the machine that's running the script is behind the firewall, you probably won't be able to "defeat the firewall" anyway (except see last paragraph).

        IIRC internet explorer will show a red cross i.e. a "red ex" for either unreachable urls AND incorrectly formatted images.

        My suggestion to subvert a firewall would be to use a http proxy. Searching for "anonymous proxy" on a www search engine should give you plenty of results.

Re: Why is my image obtained by LWP displayed as garbage (i.e. plain/text)?
by EvanCarroll (Chaplain) on Oct 19, 2005 at 20:45 UTC
    Please remove that line of extended ascii from your node, or segment it into lines of 80characters a peace it breaks the front page causing it to horizontally span the width of that line.


    Evan Carroll
    www.EvanCarroll.com
      it should be placed in a <code> block IMHO.

      Update: the output is now in code blocks. Thanks Crontab for updating it :)

      Jason L. Froebe

      Team Sybase member

      No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

        It is already in a <code> block.