Hi there, This is the first time I am posting a message. I need to compress (gzip) and make chunked output for a multiple content blocks (chunks). First, I need to compress (gzip) and output the 1st content block. Then do some other stuff and compress (gzip) and output the remaining 1 or 2 content blocks. The problem is if I only process and serve the entire content in 1 combined block, it works. But, I need to process the first block, do something else and process the remaining 1 (or 2 content blocks). Below is the code. Obviously I am doing something wrong. Asking for help and advice. Thanks in advance!
#!/usr/bin/perl # ------------------------------------- use strict; use Compress::Zlib; # ------------------------------------- # ------------------------------------- my $content1 = "<p>This is content part1</p>"; my $content2 = "<p>This is content part2</p>"; my $content3 = "<p>This is content part3</p>"; #my $apply_encoding_type = 'deflate'; my $apply_encoding_type = 'gzip'; my $this_line_break = "\r\n"; my $status; # ------------------------------------- # compress and prepare '$content1' # ------------------------------------- my $compressed_content1 = ''; $status = &compress_content($apply_encoding_type,\$content1,\$compress +ed_content1); unless($status eq 'ok'){ print "Content-type: text/html\n\n"; print "<p>Problem-1: $status </p>"; exit; } my $hexadecimal_length_compressed_content1 = sprintf("%X", length($com +pressed_content1)); # ------------------------------------- # output compressed '$content1' chunk ! # ------------------------------------- # First HTTP Response Headers print "Cache-Control: private, max-age=0, must-revalidate",$this_l +ine_break; print "Vary: Accept-Encoding",$this_line_break; print "Content-Encoding: $apply_encoding_type",$this_line_break; print "Transfer-Encoding: chunked",$this_line_break; print "Content-type: text/html; charset=UTF-8",$this_line_break,$t +his_line_break; # Now, output compressed '$content_part1' print $hexadecimal_length_compressed_content1,$this_line_break; print $compressed_content1,$this_line_break; # ... do something else ... # ... do something else ... # ... do something else ... # ... do something else ... # ... do something else ... # ... do something else ... # ------------------------------------- # compress and prepare '$content2' # ------------------------------------- my $compressed_content2 = ''; $status = &compress_content($apply_encoding_type,\$content2,\$compress +ed_content2); unless($status eq 'ok'){ print "Content-type: text/html\n\n"; print "<p>Problem-2: $status </p>"; exit; } my $hexadecimal_length_compressed_content2 = sprintf("%X", length($com +pressed_content2)); # ------------------------------------- # output compressed '$content2' chunk ! # ------------------------------------- print $hexadecimal_length_compressed_content2,$this_line_break; print $compressed_content2,$this_line_break; # . # . # . # Now, do the same for the final '$content3' # ------------------------------------- # compress and prepare '$content3' # ------------------------------------- my $compressed_content3 = ''; $status = &compress_content($apply_encoding_type,\$content3,\$compress +ed_content3); unless($status eq 'ok'){ print "Content-type: text/html\n\n"; print "<p>Problem-3: $status </p>"; exit; } my $hexadecimal_length_compressed_content3 = sprintf("%X", length($com +pressed_content3)); # ------------------------------------- # output compressed '$content3' chunk ! # ------------------------------------- print $hexadecimal_length_compressed_content3,$this_line_break; print $compressed_content3,$this_line_break; # ------------------------------------------------- # Now, finish with the final (zero length) header/s # ------------------------------------------------- print "0", $this_line_break, $this_line_break; exit; ########################################### # END OF MAIN SCRIPT ! ########################################### sub compress_content{ #my($apply_encoding_type,$content_ref,$compressed_content_ref,$end_of_ +stream) = @_; my($apply_encoding_type,$content_ref,$compressed_content_ref) = @_; $apply_encoding_type = $apply_encoding_type || 'gzip'; my($d, $out1, $out2, $status); # ------------------------------------- if($apply_encoding_type eq 'gzip'){ ($d,$status) = deflateInit( -WindowBits => 16 + &MAX_WBITS() ); } else{ # ------------ # 'deflate' ? # ------------ ($d,$status) = deflateInit(); } # ------------------------------------- # ------------------------------------- unless(defined($d)){ my $err_msg = $status; print "Content-type: text/html\n\n"; print "<P>DEFLATE-INIT ERROR: $err_msg (\$apply_encoding_type: $ap +ply_encoding_type)<P>"; exit; #return "fail-1 ($err_msg); } # ------------------------------------- # ------------------------------------- ($out1, $status) = $d->deflate($content_ref); unless($status == Z_OK){ my $err_msg = $d->msg(); #my $err_msg = $status; print "Content-type: text/html\n\n"; print "<P>COMPRESS-ERROR: $err_msg (\$apply_encoding_type: $apply_ +encoding_type)<P>"; exit; #return "fail-2 ($err_msg); } # ------------------------------------- # ------------------------------------- if($apply_encoding_type eq 'gzip'){ #if($end_of_stream){ # ($out2, $status) = $d->flush(Z_FINISH); #} #else{ # ($out2, $status) = $d->flush(Z_SYNC_FLUSH); #} ($out2, $status) = $d->flush(Z_SYNC_FLUSH); } else{ # ------------ # 'deflate' ? # ------------ ($out2, $status) = $d->flush(Z_SYNC_FLUSH); } # ------------------------------------- # ------------------------------------- unless($status == Z_OK){ my $err_msg = $d->msg(); #my $err_msg = $status; print "Content-type: text/html\n\n"; print "<P>DEFLATE-FINISH ERROR: $err_msg (\$apply_encoding_type: $ +apply_encoding_type)<P>"; exit; #return "fail-3 (error: $err_msg, status: $status)"; } # ------------------------------------- # Finally, add 'flush' output data to the initially deflated data '$ou +t1') !!! if(defined($out2)){ $out1 .= $out2; } # ------------------------------------- $$compressed_content_ref = $out1; return 'ok'; } ############################################ # END OF EVERYTHING !!! ############################################

In reply to zlib compression question by anaras

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.