You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Chris Drake <ch...@pobox.com> on 2004/01/24 05:07:22 UTC

Re[2]: GIF contents not "coming out"

Hi Stas,

Thanks for the pointers.  I've spent another hour on the problem, and
found that this fixes it:-

print "Content-type: image/gif\r\n\r\n"; local $| = 1; print "";

(interestingly, omitting the empty print causes the problem to come
back);

This only happens with GIF data incidentally - sending text worked
fine before now.

Thanks also for the efficiency tip.  Reading up on the meaning of "$/"
I find that it's on page 666 of the perl bible - is this an omen?

Sorry I didn't follow the bug procedure - the mod_perl site has
altogether way too much information, and as I'm in a production
environment with (as usual) unreasonable timeframes to get stuff
working laden upon me, I missed that bit :-)

I'm guessing that most people only want to send HTML or graphics out
using mod_perl, so maybe a small extra manual section "EXAMPLES" with
suggested skeleton code to accomplish this would be a great idea
(especially for busy people like me)?

Grr. My ADSL went down, so please excuse the missing "mybugreport"
stuff.  Incidentally - ModPerl 2.0 is part of RedHat ES 3.0 - but
there is no "mybugreport" file anyplace on my system...

Kind Regards,
Chris Drake


Saturday, January 24, 2004, 1:25:43 PM, you wrote:

SB> Chris Drake wrote:
>> Hi All,
>> 
>> My mod_perl 2 script is not sending back the contents of the GIF I'm
>> trying to print.  Running it locally, it does, or if I go:-
>> print "foo";
>> ... I get "foo" - but when I print the GIF - I just get back the
>> headers without any content!!!

SB> Please read on how we like problems to be reported: http://perl.apache.org/bugs/

SB> Make sure to include the relevant config section (is it 'perl-script' or 
SB> 'modperl' SetHandler?)

SB> Also any difference if you remove 'use bytes'?

SB> Also this is much more efficient way to read/print a multiline file:

SB>      local $/;
SB>      $::r->print(<GIF>);

SB> __________________________________________________________________
SB> Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
SB> http://stason.org/     mod_perl Guide ---> http://perl.apache.org
SB> mailto:stas@stason.org http://use.perl.org http://apacheweek.com
SB> http://modperlbook.org http://apache.org   http://ticketmaster.com


-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: GIF contents not "coming out"

Posted by Stas Bekman <st...@stason.org>.
Hi Chris,

Finally I've got a chance to analyze your problem.

> The "GIF" isn't actually a GIF at all - it was the "favicon.ico" icon
> file - so my guess is that mod_perl is sneaking a peek at what comes
> next (expecting GIF89a or whatever) and doing something wrong when
> that 'aint it.  I realize I shouldn't be saying "image/gif" in this
> case, but then - mod_perl shouldn't be screwing up my headers just
> 'coz of that mistake?

mod_perl wasn't doing anything like that. The problem was in the contents of 
favicon.ico. .ico files seem to start with the sequence \000\000 and mod_perl 
2.0 had a bug dealing with parsing printed headers immediatelly followed by 
response body starting with '\0'. I've reduced your test to a one liner script:

print "Content-type: image/x-icon\n\n\000\000\001\000";

It's now fixed in the cvs.

> On a personal note - I think the whole mod_perl header system should
> have been omitted.  If a script wants to send the headers, it should
> be allowed to, without interference.  Making people learn an
> indeterminate number of new custom non-standard callbacks like
> "content_type()" and whatever the ones are for caching, p3p, cookies,
> accept, encoding, charsets, and so forth is silly - since everyone who
> knows they're needed already knows how to send them normally - hence
> the extra bunch of callback stuff just wastes everyone's time.  Just
> my $0.02...

Nobody prevents you from printing your own headers. It's just much slower. I 
just wrote a new docs section explaining the differences and nuances of 
headers generating:
http://perl.apache.org/docs/2.0/user/coding/coding.html#Generating_HTTP_Response_Headers

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: GIF contents not "coming out"

Posted by Stas Bekman <st...@stason.org>.
Chris Drake wrote:
> Hi Stas,
> 
> Thanks for the pointers.  I've spent another hour on the problem, and
> found that this fixes it:-
> 
> print "Content-type: image/gif\r\n\r\n"; local $| = 1; print "";
> 
> (interestingly, omitting the empty print causes the problem to come
> back);
> 
> This only happens with GIF data incidentally - sending text worked
> fine before now.

can you please send me (off-list) an image that you have the problem with? Or 
is it with any image at all?

> Sorry I didn't follow the bug procedure - the mod_perl site has
> altogether way too much information, and as I'm in a production
> environment with (as usual) unreasonable timeframes to get stuff
> working laden upon me, I missed that bit :-)
> 
> I'm guessing that most people only want to send HTML or graphics out
> using mod_perl, so maybe a small extra manual section "EXAMPLES" with
> suggested skeleton code to accomplish this would be a great idea
> (especially for busy people like me)?
> 
> Grr. My ADSL went down, so please excuse the missing "mybugreport"
> stuff.  Incidentally - ModPerl 2.0 is part of RedHat ES 3.0 - but
> there is no "mybugreport" file anyplace on my system...

I suppose that your ADSL is still down, since you've never sent in the details 
I've asked for, Chris. I was trying to reproduce your problem but without 
seeing your configuration and the rest of things I don't know about, I can't 
reproduce it and therefore fix it.

You load 'use Chris', which you didn't show what it does. I suspect that some 
code that you use is changing the value of $| to 1, w/o localizing it, which 
affects your script's header sending section:

  print "Content-type: image/gif\n"; print "\n";

which if my guess is correct, becomes:

  local $| = 1;
  print "Content-type: image/gif\n"; print "\n";

the first print sends out an incomplete header, and the thing obviously breaks.

Also I'm not sure why do you mix, print and $r->print.

I wrote the test which looks like this:

use strict;
use warnings FATAL => 'all';

use Apache::RequestIO ();
use Apache::RequestRec ();

my $r = shift;
$r->content_type('image/gif');

my $image_path = "whatever.gif";
open my $fh, $image_path or die "Can't open $image_path: $!";
local $/;
$r->print(<$fh>);
close $fh;

it works just fine for me and it'll work correctly no matter what's the value 
of $|, since it sets the header via the content_type API and not printing it. 
The configuration it's running under is:

Alias /registry/ 
/home/stas/apache.org/modperl-2.0/ModPerl-Registry/t/cgi-bin/
PerlModule ModPerl::Registry
<Location /registry>
     SetHandler perl-script
     Options +ExecCGI
     PerlResponseHandler ModPerl::Registry
     PerlOptions +ParseHeaders
</Location>


__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: Re[2]: GIF contents not "coming out"

Posted by Ged Haywood <ge...@www2.jubileegroup.co.uk>.
Hi there,

On Sat, 24 Jan 2004, Chris Drake wrote:

> Reading up on the meaning of "$/" I find that it's on page 666 of
> the perl bible - is this an omen?

Wrong beast.  My copy of the Camel Book only goes up to page 645.

> maybe a small extra manual section "EXAMPLES" ...
> (especially for busy people like me)?

You can't expect the contributors to the mod_perl List to make up for
your lack of effort in the study department.  If you're to do the best
job that you can then you must take the time to research the subject.

There's a LARGE set of examples called the "Perl Cookbook" and I can
assure you that the people who wrote it were kept very busy indeed
by their effort.

73,
Ged.




-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re[4]: GIF contents not "coming out"

Posted by Chris Drake <ch...@pobox.com>.
s> If you want to eliminate the need of the 'print ""' line, you should reverse
s> the order of the commands:
s> local $| = 1; print "Content-type: image/gif\r\n\r\n"; 

No, that doesn't work - I forget the exact error - something about
"script did not send headers" I think.

Kind Regards,
Chris Drake


-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: GIF contents not "coming out"

Posted by Stas Bekman <st...@stason.org>.
Xavier Noria wrote:
> On Jan 25, 2004, at 0:05, Stas Bekman wrote:
> 
>> Ged Haywood wrote:
>>
>>> Hello there,
>>> On Sat, 24 Jan 2004, semuel wrote:
>>>
>>>> you don't need to "\r\n\r\n". "\n\n" will do the job.
>>>
>>> According to the standard you should send both \r and \n.
>>> It is bad practice to play fast and loose with standards.
>>> http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
>>
>>
>> Not if you use Apache. It does the right thing with "\n\n" for you, 
>> because it parses your headers and strips "\n\n" anyway, adding other 
>> HTTP headers and terminating each with "\r\n".

I must correct myself and say that I didn't check whether it actually sends 
"\r\n" or "\n" or "\r". What I was trying to say is that since it re-parses 
your headers (in a non-nph mode) it'll do the right thing...

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: GIF contents not "coming out"

Posted by Xavier Noria <fx...@hashref.com>.
On Jan 25, 2004, at 0:05, Stas Bekman wrote:

> Ged Haywood wrote:
>> Hello there,
>> On Sat, 24 Jan 2004, semuel wrote:
>>> you don't need to "\r\n\r\n". "\n\n" will do the job.
>> According to the standard you should send both \r and \n.
>> It is bad practice to play fast and loose with standards.
>> http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
>
> Not if you use Apache. It does the right thing with "\n\n" for you, 
> because it parses your headers and strips "\n\n" anyway, adding other 
> HTTP headers and terminating each with "\r\n".

Looks like handling "\n" is required in CGI 1.1[*] if the script is not 
using NPH:

* On the one hand in section 7.2.1.1 we have

     Content-Type = "Content-Type" ":" media-type NL

and NL is defined as CR | LF. In my understanding that makes "\r\n" 
illegal indeed!

* On the other hand section 7.2.2 says "Servers MUST resolve conflicts 
beteen CGI header and HTTP header formats or names (see section 8)." 
Which might have as a side effect the fact that "\r\n" ends up working 
in practice.

-- fxn

[*] http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html


-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: GIF contents not "coming out"

Posted by Stas Bekman <st...@stason.org>.
Ged Haywood wrote:
> Hello there,
> 
> On Sat, 24 Jan 2004, semuel wrote:
> 
> 
>>you don't need to "\r\n\r\n". "\n\n" will do the job.
> 
> 
> According to the standard you should send both \r and \n.
> It is bad practice to play fast and loose with standards.
> 
> http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html

Not if you use Apache. It does the right thing with "\n\n" for you, because it 
parses your headers and strips "\n\n" anyway, adding other HTTP headers and 
terminating each with "\r\n".

Of course if you use non-parsed-headers scripts/handlers, then you are on your 
own to do the right things.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


RE: Re[2]: GIF contents not "coming out"

Posted by Ged Haywood <ge...@www2.jubileegroup.co.uk>.
Hello there,

On Sat, 24 Jan 2004, semuel wrote:

> you don't need to "\r\n\r\n". "\n\n" will do the job.

According to the standard you should send both \r and \n.
It is bad practice to play fast and loose with standards.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html

73,
Ged.



-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


RE: Re[2]: GIF contents not "coming out"

Posted by semuel <se...@semuel.co.il>.
Hello There.

If you want to eliminate the need of the 'print ""' line, you should reverse
the order of the commands:
local $| = 1; print "Content-type: image/gif\r\n\r\n"; 
(That's because the $| is a flashing command. NM)
And you don't need to "\r\n\r\n". "\n\n" will do the job.

Semuel.

-----Original Message-----
From: Chris Drake [mailto:christopher@pobox.com] 
Sent: Saturday, January 24, 2004 6:07 AM
To: modperl@perl.apache.org
Subject: Re[2]: GIF contents not "coming out"

Hi Stas,

Thanks for the pointers.  I've spent another hour on the problem, and
found that this fixes it:-

print "Content-type: image/gif\r\n\r\n"; local $| = 1; print "";

(interestingly, omitting the empty print causes the problem to come
back);

This only happens with GIF data incidentally - sending text worked
fine before now.


Kind Regards,
Chris Drake


Saturday, January 24, 2004, 1:25:43 PM, you wrote:

SB> Chris Drake wrote:
>> Hi All,
>> 
>> My mod_perl 2 script is not sending back the contents of the GIF I'm
>> trying to print.  Running it locally, it does, or if I go:-
>> print "foo";
>> ... I get "foo" - but when I print the GIF - I just get back the
>> headers without any content!!!

SB> Please read on how we like problems to be reported:
http://perl.apache.org/bugs/

SB> Make sure to include the relevant config section (is it 'perl-script' or

SB> 'modperl' SetHandler?)

SB> Also any difference if you remove 'use bytes'?

SB> Also this is much more efficient way to read/print a multiline file:

SB>      local $/;
SB>      $::r->print(<GIF>);





-- 
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html