On Mon, 22 Apr 2002, Vicki Brown wrote:

> One of my calls to the "remote CGI" returns a raw JPEG
>
>     my $ua = new LWP::UserAgent;
>     my $req;
>     if ($port == 80) {
>         $req = new HTTP::Request GET => "http://$host$doc";;
>     } else {
>         $req = new HTTP::Request GET => "http://$host:$port/$doc";;
>     }
>
>     my $response=$ua->request($req);   # hash ref; HTTP::Response object
>     $resp=$response->as_string;
>     $hdrs=$response->headers->as_string();
>     if ($response->is_success) {
>         $resp=$response->content();
>     }
>
> Headers ($hdrs):
>   Connection: close
>   Date: Tue, 23 Apr 2002 04:56:29 GMT
>   Server: Apache/1.3.6 (Unix) ApacheJServ/1.0
>   Content-Length: 11093
>   Content-Type: image/jpeg
>   Client-Date: Tue, 23 Apr 2002 05:02:38 GMT
>   Client-Peer: 10.0.0.4:80
>   Content-Disposition: inline; filename="UFkit7.jpg"
>   Content-Transfer-Encoding: binary
>   MIME-Version: 1.0
>
> Response ($resp):
>   a bunch of binary stuff
>
> When the result is HTML I just print it to the new page.  But this isn't
> HTML; I can't wrap it in <HTML...</HTML> tags. I can't simply print it (the
> CGI at _my_ end gives an error).
>
> Is there a convention for handling returns of binary image data?
>
> The person who "owns" the CGI that sends me the binary stream says:
>      We normally handle these by returning a page with the
>      correct "Content-length:.." and "Content-type: ..." which the browser
>      is supposed to interpret and display accordingly.
>
> I guess I'm "the browser" in this case. It may be that the flaw is in "my"
> CGI (the
> one I'm working with; I didn't write it) in its inability to handle a page
> consisting of a simple
>     print $resp;
>
> I get an error in the Apache log
>   malformed header from script. Bad header=����:
>
> I believe it.  JPEG binary code does not make a well-formed HTML header...

Your CGI script needs to print its own headers (most importantly,
Content-Type), then a blank line, then the JPEG data.

print "Content-Type: " . $response->header('Content-Type') . "\r\n";
print "\r\n"; # blank line ends the headers
print "$resp";

-- 
Liam Quinn

Reply via email to