You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Christopher Stanton <cs...@mail.utexas.edu> on 2005/09/21 01:52:11 UTC

Strange $r-print($buffer)/Content-Disposition problem

Fedora Core 4
httpd-2.0.54-10
mod_perl-2.0.0-0.rc5.3

I have an nph mod_perl script which will send a JPEG to a client either
inline or as an attachment (if a param is passed). If it is an
attachment, I set the Content-Disposition to:
"Content-Disposition: attachment; filename=\"$snapshot_filename\"\r\n"
otherwise, I do not include this tag in the header.

I am creating the buffer as I go (appending header tags). I append the
separator and the JPEG:
$write_buffer .= "\r\n" . $jpeg . "\r\n\r\n";

And then send the buffer to the client:
$r->print($write_buffer);

If the Content-Disposition line is not present, $r->print returns the
number of bytes sent to the client which equals length($write_buffer).
7628/7628 (bytes_sent/buffer_length)

If the Content-Disposition line is present, $r->print returns the number
of bytes sent to the client which is larger than length($write_buffer).
10882/7647 (bytes_sent/buffer_length)

(the buffer length is different because the image sent was different)

In addition to this, the JPEG is valid in the first case but is
"corrupt" in the second. Saving the attachment through Firefox or using
curl results in a file which is not a valid JPEG.

If I break the attachment send into 3 sends:
$r->print($write_buffer);
$r->print($jpeg);
$r->print("\r\n\r\n");

Each returns bytes sent equal to the length of the buffer and everything
is valid on the client's end.

"print $write_buffer" works in both the inline and append cases without
needing to separate the transfer into parts.

Is this a bug in mod_perl or am I missing something??

thanks,
Christopher

Re: Strange $r-print($buffer)/Content-Disposition problem

Posted by Christopher Stanton <cs...@mail.utexas.edu>.
In the second example I have already appended "\r\n" to the write buffer.

So it really is:
$write_buffer .= "\r\n";
$r->print($write_buffer);
$r->print($jpeg);
$r->print("\r\n\r\n");

I try to write the same data to the client. In one case I write it as 
one big buffer, in the other case I write the header, then the JPEG, 
then the footer.

I can use print and $r->print to write the unified buffer if the 
Content-Disposition tag is not added or if it does not have the "; 
filename=.*" substring as part of the value

Works:
""
"Content-Disposition: attachment"

$r->print failes on the unified buffer if it contains the full 
Content-Disposition value:
"Content-Disposition: attachment; filename=\"$snapshot_filename\"\r\n"

The standalone print works and I can get $r->print to work if I split 
the printing into the three chunks I mentioned.

$r->print on the unified buffer returns that it sent more bytes than 
were even in the buffer (Example: bytes sent=10882 and 
length($write_buffer)=7647).

So, the two big questions are:
1) why is $r->print returning more bytes sent than are in the buffer
2) why does print work but $r->print does not on this case

sorry about the confusion,
Christopher

Larry Leszczynski wrote:
> Hi Christopher -
> 
> I don't know if it's a typo but this:
> 
>> $write_buffer .= "\r\n" . $jpeg . "\r\n\r\n";
>> $r->print($write_buffer);
> 
> 
> is not the same as this:
> 
>> $r->print($write_buffer);
>> $r->print($jpeg);
>> $r->print("\r\n\r\n");
> 
> 
> since the latter does not insert "\r\n" between $write_buffer and $jpeg...
> 
> 
> Larry

Re: Strange $r-print($buffer)/Content-Disposition problem

Posted by Larry Leszczynski <la...@emailplus.org>.
Hi Christopher -

I don't know if it's a typo but this:

> $write_buffer .= "\r\n" . $jpeg . "\r\n\r\n";
> $r->print($write_buffer);

is not the same as this:

> $r->print($write_buffer);
> $r->print($jpeg);
> $r->print("\r\n\r\n");

since the latter does not insert "\r\n" between $write_buffer and $jpeg...


Larry