]> bicyclesonthemoon.info Git - yplom/proxy/commitdiff
Support for 206 Partial content. main git-svn
authorb <b@05ce6ebb-7522-4a6e-a768-0026ae12be9f>
Mon, 29 Aug 2016 20:11:07 +0000 (20:11 +0000)
committerb <b@05ce6ebb-7522-4a6e-a768-0026ae12be9f>
Mon, 29 Aug 2016 20:11:07 +0000 (20:11 +0000)
git-svn-id: svn://botcastle1b/yplom/proxy@19 05ce6ebb-7522-4a6e-a768-0026ae12be9f

proxy.1.pl

index 1c07bcb249ae4a5194a45ce69c379d5c9c63b953..6f46bff2c0cd1e566a56ed3a6979671bcb018032 100755 (executable)
@@ -1,7 +1,7 @@
 ###PERL;
 
 # proxy.pl is generated from proxy.1.pl
-# 02.01.2016
+# 29.08.2016
 #
 # This is the proxy software. It's launched as a CGI program by the http server.
 #
@@ -132,6 +132,8 @@ sub proxy {
        my $lastskipped=1;   # if the previous header field was not forwarded
        my $forceexit=0;     # if the proxy should exit with error
        my $errormsg;
+       my $rangestart;      # first byte of partial content
+       my $rangelength;     # total length when partial content
        
        my $contentlength;
        
@@ -458,6 +460,14 @@ sub proxy {
                                        $contentlength = int($headval);
                                }
                                
+                               # Only part of the content is transmitted in this request
+                               if(($status =~ /^206/)&&($headname eq 'content-range')) {
+                                       if (lc($headval) =~ /^[\r\n \t]*bytes ([0-9]+)\-[0-9]\/([0-9]*)/) {
+                                               $rangestart = int($1);
+                                               $rangelength = int($2);
+                                       }
+                               }
+                               
                                # Some header fields should not be forwarded.
                                if($headname =~ RESPONSE_HEADER_BLOCK) {
                                        $lastskipped =1;
@@ -469,7 +479,7 @@ sub proxy {
                        
                        # Only add the Content-length header if it's safe to do so.
                        if ($definedlength and ($safelength or ($contentlength==0))) {
-                               push @resphead, 'Content-length: '.$contentlength;
+                               push @resphead, 'Content-length: '.(($rangelength ne '')?$rangelength:$contentlength);
                        }
                        
                        # After forwarding the response header it's too late to send any error
@@ -478,7 +488,12 @@ sub proxy {
                        
                        # Forward the header and safe it to file if possible and allowed.
                        if ($archive) {
-                               $respheadopen = open($respheadfile, ">", $respheadpath);
+                               if($rangestart ne '') {
+                                       $respheadopen = open($respheadfile, ">>", $respheadpath);
+                               }
+                               else{
+                                       $respheadopen = open($respheadfile, ">", $respheadpath);
+                               }
                        }
                        foreach $line (@resphead) {
                                print $line."\n";
@@ -509,13 +524,28 @@ sub proxy {
                                        }
                                        if ($firstline) {
                                                if ($archive) {
-                                                       $respcontopen = open($respcontfile, ">", $respcontpath);
+                                                       if($rangestart ne '') {
+                                                               if(-f $respcontpath) {
+                                                                       $respcontopen = open($respcontfile, "+<", $respcontpath);
+                                                               }
+                                                               else {
+                                                                       $respcontopen = open($respcontfile, ">", $respcontpath);
+                                                               }
+                                                       }
+                                                       else {
+                                                               $respcontopen = open($respcontfile, ">", $respcontpath);
+                                                       }
                                                }
                                                if ($respcontopen){
                                                        unless (binmode($respcontfile)) {
                                                                close($respcontfile);
                                                                $respcontopen=0;
                                                        }
+                                                       else {
+                                                               if($rangestart ne '') {
+                                                                       seek($respcontfile,0,$rangestart);
+                                                               }
+                                                       }
                                                }
                                        }
                                        $firstline=0;
@@ -524,6 +554,9 @@ sub proxy {
                                        }
                                }
                                if($respcontopen) {
+                                       if($rangelength ne '') {
+                                               truncate($respcontfile,$rangelength);
+                                       }
                                        close ($respcontfile);
                                }
                        }