"Streaming" FLV through MogileFS

Eric Lambrecht eml at guba.com
Fri Apr 13 23:04:54 UTC 2007


Brad Fitzpatrick wrote:
> 
> Could you send a diff -u?  Hurts my brain to read that other format.  :)
> 

attached.

Eric...

-------------- next part --------------
--- BackendHTTP.pm	2007-04-13 14:34:05.000000000 -0700
+++ BackendHTTP.pm.giao	2007-04-13 14:42:14.000000000 -0700
@@ -47,6 +47,8 @@
 # of using its slab cache.
 use constant BACKEND_READ_SIZE => 61449;  # 60k, to fit in a 64k slab
 
+use constant FLV_HEADER => 'FLV' . pack('CCNN', 1, 1, 9, 9);
+
 # keys set here when an endpoint is found to not support persistent
 # connections and/or the OPTIONS method
 our %NoVerify; # { "ip:port" => next-verify-time }
@@ -415,6 +417,7 @@
     }
 
     # regular path:
+    my $write_flv_header = 0;
     my $res_source = $client->{primary_res_hdrs} || $hd;
     my $thd = $client->{res_headers} = $res_source->clone;
 
@@ -426,19 +429,34 @@
     # we send the real content-length (from the reproxied URL)
     # and not the one the first server gave us
     if ($client->{primary_res_hdrs}) {
-        $thd->header('Content-Length', $hd->header('Content-Length'));
+        #If this is a partial flv request we must clean up the ranges
+        #and pretend it was a non range request
+        if($client->{primary_res_hdrs}->header('X-REPROXY-FLV-START')) {
+            $write_flv_header = 1;
+            #13 bytes for the flv header
+            $thd->header('Content-Length', $hd->header('Content-Length') + 13);
+        } else {
+            $thd->header('Content-Length', $hd->header('Content-Length'));
+        } 
         $thd->header('X-REPROXY-FILE', undef);
         $thd->header('X-REPROXY-URL', undef);
         $thd->header('X-REPROXY-EXPECTED-SIZE', undef);
+        $thd->header('X-REPROXY-FLV-START', undef);
         $thd->header('X-REPROXY-CACHE-FOR', undef);
 
         # also update the response code, in case of 206 partial content
         my $rescode = $hd->response_code;
-        $thd->code($rescode) if $rescode == 206 || $rescode == 416;
+        if (($rescode == 416) || ($rescode == 206) and !$write_flv_header) {
+            $thd->code($rescode);
+            $thd->header('Accept-Ranges', $hd->header('Accept-Ranges')) if $hd->header('Accept-Ranges');
+            $thd->header('Content-Range', $hd->header('Content-Range')) if $hd->header('Content-Range');
+        } 
     }
 
     print "  writing response headers to client\n" if Perlbal::DEBUG >= 3;
-    $client->write($thd->to_string_ref);
+    my $response_header = $thd->to_string_ref;
+    $client->write($response_header);
+    $client->write(FLV_HEADER) if $write_flv_header;
 
     print("  content_length=", (defined $self->{content_length} ? $self->{content_length} : "(undef)"),
           "  remain=",         (defined $self->{content_length_remain} ? $self->{content_length_remain} : "(undef)"), "\n")


More information about the mogilefs mailing list