Index: audio.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/audio/audio.module,v
retrieving revision 1.105.2.11
diff -u -p -r1.105.2.11 audio.module
--- audio.module	1 Aug 2007 18:52:49 -0000	1.105.2.11
+++ audio.module	5 Oct 2007 14:05:32 -0000
@@ -1546,15 +1546,64 @@ function audio_file_transfer($source, $h
 
   $source = file_create_path($source);
 
-  // Transfer file in 1024 byte chunks to save memory usage.
   if ($fd = fopen($source, 'rb')) {
     if (!ini_get('safe_mode')){
       set_time_limit(0);
     }
-    while (!feof($fd)) {
-      print fread($fd, 1024);
-      ob_flush();
-      flush();
+    // Tell client that we accept byte ranges.
+    drupal_set_header('Accept-Ranges: bytes');
+    // Get file size.
+    $filesize = filesize($source);
+    $end_file = $filesize - 1;
+    $valid = FALSE;
+    $satisfiable = FALSE;
+    // Is this a partial request?
+    if (isset($_SERVER['HTTP_RANGE'])) {
+      preg_match('/^bytes=(\S+)$/i', $_SERVER['HTTP_RANGE'], $ranges);
+      $ranges = explode(',', array_pop($ranges));
+      foreach ($ranges as $key => $range) {
+        if (substr_count($range, '-') == 1) {
+          $valid = TRUE;
+          if (substr($range, 0, 1) == '-') {
+            $satisfiable = TRUE;
+            $ranges[$key] = array('from' => max(0, $end_file + $range), 'to' => $end_file);
+          }
+          else {
+            $range = explode('-', $range);
+            if ($range[0] <= $end_file) {
+              if ($range[1] === '') {
+                $satisfiable = TRUE;
+                $ranges[$key] = array('from' => $range[0], 'to' => $end_file);
+              }
+              elseif ($range[0] <= $range[1]) {
+                $satisfiable = TRUE;
+                $ranges[$key] = array('from' => $range[0], 'to' => min($range[1], $end_file));
+              }
+            }
+          }
+        }
+        if (!$valid || !$satisfiable) {
+          unset($ranges[$key]);
+        }
+      }
+    }
+    if ($satisfiable) {
+      drupal_set_header('HTTP/1.1 206 Partial Content');
+      foreach ($ranges as $range) {
+        // Move file pointer.
+        fseek($fd, $range['from']);
+        drupal_set_header(sprintf('Content-Range: bytes %d-%d/%d', $range['from'], $range['to'], $filesize));
+        _audio_file_transfer_range($fd, $range['to'] + 1 - $range['from']);
+      }
+    }
+    else {
+      if ($valid && !isset($_SERVER['If-Range'])) {
+        drupal_set_header('HTTP/1.1 416 Requested Range Not Satisfiable');
+        drupal_set_header('Content-Range: *');
+      }
+      else {
+        _audio_file_transfer_range($fd, $filesize);
+      }
     }
     fclose($fd);
   }
@@ -1565,6 +1614,20 @@ function audio_file_transfer($source, $h
 }
 
 /**
+ * Transfer range in 1024 byte chunks to save memory usage.
+ */
+function _audio_file_transfer_range($fd, $length) {
+  drupal_set_header('Content-Length: '. $length);
+  while ($length > 0) {
+    $read = min(1024, $length);
+    print fread($fd, $read);
+    ob_flush();
+    flush();
+    $length -= $read;
+  }
+}
+
+/**
  * Create audio nodes from a file.
  *
  * Function for other modules to use to create a audio node from a file. Once
