#!/usr/bin/perl # ------------------------------------- use strict; use Compress::Zlib; # ------------------------------------- # ------------------------------------- my $content1 = "
This is content part1
"; my $content2 = "This is content part2
"; my $content3 = "This is content part3
"; #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,\$compressed_content1); unless($status eq 'ok'){ print "Content-type: text/html\n\n"; print "Problem-1: $status
"; exit; } my $hexadecimal_length_compressed_content1 = sprintf("%X", length($compressed_content1)); # ------------------------------------- # output compressed '$content1' chunk ! # ------------------------------------- # First HTTP Response Headers print "Cache-Control: private, max-age=0, must-revalidate",$this_line_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,$this_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,\$compressed_content2); unless($status eq 'ok'){ print "Content-type: text/html\n\n"; print "Problem-2: $status
"; exit; } my $hexadecimal_length_compressed_content2 = sprintf("%X", length($compressed_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,\$compressed_content3); unless($status eq 'ok'){ print "Content-type: text/html\n\n"; print "Problem-3: $status
"; exit; } my $hexadecimal_length_compressed_content3 = sprintf("%X", length($compressed_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 "DEFLATE-INIT ERROR: $err_msg (\$apply_encoding_type: $apply_encoding_type)
"; 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 "
COMPRESS-ERROR: $err_msg (\$apply_encoding_type: $apply_encoding_type)
"; 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 "
DEFLATE-FINISH ERROR: $err_msg (\$apply_encoding_type: $apply_encoding_type)
"; exit; #return "fail-3 (error: $err_msg, status: $status)"; } # ------------------------------------- # Finally, add 'flush' output data to the initially deflated data '$out1') !!! if(defined($out2)){ $out1 .= $out2; } # ------------------------------------- $$compressed_content_ref = $out1; return 'ok'; } ############################################ # END OF EVERYTHING !!! ############################################