Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.925
diff -u -F^f -r1.925 common.inc
--- includes/common.inc	18 Jun 2009 21:19:01 -0000	1.925
+++ includes/common.inc	23 Jun 2009 09:28:35 -0000
@@ -2456,15 +2456,15 @@ function drupal_get_css($css = NULL) {
             // If a CSS file is not to be preprocessed and it's a module CSS file, it needs to *always* appear at the *top*,
             // regardless of whether preprocessing is on or off.
             if (!$preprocess && $type == 'module') {
-              $no_module_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $data . $query_string . '" />' . "\n";
+              $no_module_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . file_create_url($data) . $query_string . '" />' . "\n";
             }
             // If a CSS file is not to be preprocessed and it's a theme CSS file, it needs to *always* appear at the *bottom*,
             // regardless of whether preprocessing is on or off.
             elseif (!$preprocess && $type == 'theme') {
-              $no_theme_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $data . $query_string . '" />' . "\n";
+              $no_theme_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . file_create_url($data) . $query_string . '" />' . "\n";
             }
             else {
-              $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $data . $query_string . '" />' . "\n";
+              $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . file_create_url($data) . $query_string . '" />' . "\n";
             }
           }
         }
@@ -2476,7 +2476,7 @@ function drupal_get_css($css = NULL) {
       // starting with "ad*".
       $filename = 'css_' . md5(serialize($types) . $query_string) . '.css';
       $preprocess_file = drupal_build_css_cache($types, $filename);
-      $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $preprocess_file . '" />' . "\n";
+      $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . file_create_url($preprocess_file) . '" />' . "\n";
     }
   }
   if (!empty($no_inline_preprocess)) {
@@ -2925,7 +2925,7 @@ function drupal_get_js($scope = 'header'
 
       case 'file':
         if (!$item['preprocess'] || !$is_writable || !$preprocess_js) {
-          $no_preprocess .= '<script type="text/javascript"' . ($item['defer'] ? ' defer="defer"' : '') . ' src="' . base_path() . $item['data'] . ($item['cache'] ? $query_string : '?' . REQUEST_TIME) . "\"></script>\n";
+          $no_preprocess .= '<script type="text/javascript"' . ($item['defer'] ? ' defer="defer"' : '') . ' src="' . file_create_url($item['data']) . ($item['cache'] ? $query_string : '?' . REQUEST_TIME) . "\"></script>\n";
         }
         else {
           $files[$item['data']] = $item;
@@ -2945,7 +2945,7 @@ function drupal_get_js($scope = 'header'
     // starting with "ad*".
     $filename = 'js_' . md5(serialize($files) . $query_string) . '.js';
     $preprocess_file = drupal_build_js_cache($files, $filename);
-    $preprocessed .= '<script type="text/javascript" src="' . base_path() . $preprocess_file . '"></script>' . "\n";
+    $preprocessed .= '<script type="text/javascript" src="' . file_create_url($preprocess_file) . '"></script>' . "\n";
   }
 
   // Keep the order of JS files consistent as some are preprocessed and others are not.
Index: includes/file.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/file.inc,v
retrieving revision 1.171
diff -u -F^f -r1.171 file.inc
--- includes/file.inc	9 Jun 2009 21:33:12 -0000	1.171
+++ includes/file.inc	23 Jun 2009 09:28:46 -0000
@@ -79,20 +79,53 @@
 /**
  * Create the download path to a file.
  *
+ * There are two kinds of files:
+ * - those in the files directory (which is stored in the file_directory_path
+ *   variable and can be retrieved using file_directory_path()). These are
+ *   files that have either been uploaded by users or were generated
+ *   automatically (for example through CSS aggregation).
+ * - those outside of the files directory, which ship as part of Drupal core
+ *   or contributed modules or themes.
+ * When the custom_file_url_rewrite() function is defined and returns a
+ * rewritten path (and not FALSE), then that rewritten path is used instead of
+ * creating a URL for the file at the given path.
+ *
  * @param $path A string containing the path of the file to generate URL for.
  * @return A string containing a URL that can be used to download the file.
  */
 function file_create_url($path) {
-  // Strip file_directory_path from $path. We only include relative paths in
-  // URLs.
-  if (strpos($path, file_directory_path() . '/') === 0) {
-    $path = trim(substr($path, strlen(file_directory_path())), '\\/');
-  }
-  switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) {
-    case FILE_DOWNLOADS_PUBLIC:
-      return $GLOBALS['base_url'] . '/' . file_directory_path() . '/' . str_replace('\\', '/', $path);
-    case FILE_DOWNLOADS_PRIVATE:
-      return url('system/files/' . $path, array('absolute' => TRUE));
+  // Allow rewriting of file URLs, so one can use static file servers and
+  // CDNs.
+  if (function_exists('custom_file_url_rewrite')) {
+    $rewritten_path = custom_file_url_rewrite($path);
+    if ($rewritten_path != FALSE) {
+      return $rewritten_path;
+    }
+  }
+
+  // Otherwise serve the file from Drupal's web server. This point will only
+  // be reached when either no custom_file_url_rewrite() function has been
+  // defined, or when that function returned FALSE, thereby indicating that it
+  // cannot (or doesn't wish to) rewrite the URL. This is typically because
+  // the file doesn't match some conditions to be served from a CDN or static
+  // file server, or because the file has not yet been synced to the CDN or
+  // static file server.
+  $path_parts = explode('/', $path);
+  if (in_array($path_parts[0], array('misc', 'modules', 'profiles', 'sites', 'themes'))) {
+    return base_path() . $path;
+  }
+  else {
+    // Strip file_directory_path from $path. We only include relative paths in urls.
+    if (strpos($path, file_directory_path() . '/') === 0) {
+      $path = trim(substr($path, strlen(file_directory_path())), '\\/');
+    }
+
+    switch (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC)) {
+      case FILE_DOWNLOADS_PUBLIC:
+        return $GLOBALS['base_url'] . '/' . file_directory_path() . '/' . str_replace('\\', '/', $path);
+      case FILE_DOWNLOADS_PRIVATE:
+        return url('system/files/' . $path, array('absolute' => TRUE));
+    }
   }
 }
 
Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.343
diff -u -F^f -r1.343 form.inc
--- includes/form.inc	20 Jun 2009 15:17:38 -0000	1.343
+++ includes/form.inc	23 Jun 2009 09:28:48 -0000
@@ -2413,7 +2413,7 @@ function theme_image_button($element) {
     (!empty($element['#value']) ? ('value="' . check_plain($element['#value']) . '" ') : '') .
     'id="' . $element['#id'] . '" ' .
     drupal_attributes($element['#attributes']) .
-    ' src="' . base_path() . $element['#src'] . '" ' .
+    ' src="' . file_create_url($element['#src']) . '" ' .
     (!empty($element['#title']) ? 'alt="' . check_plain($element['#title']) . '" title="' . check_plain($element['#title']) . '" ' : '' ) .
     "/>\n";
 }
Index: includes/theme.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.inc,v
retrieving revision 1.496
diff -u -F^f -r1.496 theme.inc
--- includes/theme.inc	18 Jun 2009 21:19:02 -0000	1.496
+++ includes/theme.inc	23 Jun 2009 09:28:50 -0000
@@ -1086,24 +1086,24 @@ function theme_get_setting($setting_name
 
     if ($settings['toggle_logo']) {
       if ($settings['default_logo']) {
-        $settings['logo'] = base_path() . dirname($theme_object->filename) . '/logo.png';
+        $settings['logo'] = file_create_url(dirname($theme_object->filename) . '/logo.png');
       }
       elseif ($settings['logo_path']) {
-        $settings['logo'] = base_path() . $settings['logo_path'];
+        $settings['logo'] = file_create_url($settings['logo_path']);
       }
     }
 
     if ($settings['toggle_favicon']) {
       if ($settings['default_favicon']) {
         if (file_exists($favicon = dirname($theme_object->filename) . '/favicon.ico')) {
-          $settings['favicon'] = base_path() . $favicon;
+          $settings['favicon'] = file_create_url($favicon);
         }
         else {
-          $settings['favicon'] = base_path() . 'misc/favicon.ico';
+          $settings['favicon'] = file_create_url('misc/favicon.ico');
         }
       }
       elseif ($settings['favicon_path']) {
-        $settings['favicon'] = base_path() . $settings['favicon_path'];
+        $settings['favicon'] = file_create_url($settings['favicon_path']);
       }
       else {
         $settings['toggle_favicon'] = FALSE;
@@ -1327,7 +1327,7 @@ function theme_links($links, $attributes
 function theme_image($path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
   if (!$getsize || (is_file($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path)))) {
     $attributes = drupal_attributes($attributes);
-    $url = (url($path) == $path) ? $path : (base_path() . $path);
+    $url = file_create_url($path);
     return '<img src="' . check_url($url) . '" alt="' . check_plain($alt) . '" title="' . check_plain($title) . '" ' . (isset($image_attributes) ? $image_attributes : '') . $attributes . ' />';
   }
 }
Index: modules/system/system.api.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.api.php,v
retrieving revision 1.43
diff -u -F^f -r1.43 system.api.php
--- modules/system/system.api.php	22 Jun 2009 09:10:06 -0000	1.43
+++ modules/system/system.api.php	23 Jun 2009 09:28:52 -0000
@@ -1078,6 +1078,38 @@ function custom_url_rewrite_inbound(&$re
 }
 
 /**
+ * custom_file_url_rewrite is not a hook, it's a function you can add to
+ * settings.php to alter all file URLs generated by Drupal. This function is
+ * called from file_create_url().
+ * This function is called fairly frequently (10+ times per page), depending
+ * on how many files there are in a given page and. If CSS and JS aggregation
+ * are disabled, this can become very frequently (50+ times per page) so
+ * performance is critical.
+ *
+ * This function should return the rewritten path, if it wants to rewrite the
+ * file URL. If it doesn't want to rewrite a given path, then it should
+ * return FALSE.
+ *
+ * @param $path A string containing the path of the file to generate URL for.
+ * @return
+ *   Rewritten $path variable or FALSE.
+ */
+function custom_file_url_rewrite($path) {
+  $static1 = 'http://static.example.com';
+  $static2 = 'http://cdn.example.com';
+
+  $pathinfo = pathinfo($path);
+  if (in_array($pathinfo['extension'], array('css', 'js', 'gif', 'jpg', 'jpeg', 'png')) {
+    return $static1 .'/'. str_replace('\\', '/', $path);
+  }
+  else {
+    return $static2 .'/'. str_replace('\\', '/', $path);
+  }
+
+  return FALSE;
+}
+
+/**
  * Load additional information into file objects.
  *
  * file_load_multiple() calls this hook to allow modules to load
Index: themes/garland/template.php
===================================================================
RCS file: /cvs/drupal/drupal/themes/garland/template.php,v
retrieving revision 1.22
diff -u -F^f -r1.22 template.php
--- themes/garland/template.php	2 Jun 2009 03:57:22 -0000	1.22
+++ themes/garland/template.php	23 Jun 2009 09:28:52 -0000
@@ -91,9 +91,9 @@ function garland_node_submitted($node) {
 function garland_get_ie_styles() {
   global $language;
 
-  $ie_styles = '<link type="text/css" rel="stylesheet" media="all" href="' . base_path() . path_to_theme() . '/fix-ie.css" />' . "\n";
+  $ie_styles = '<link type="text/css" rel="stylesheet" media="all" href="' . file_create_url(path_to_theme() . '/fix-ie.css') . '" />' . "\n";
   if ($language->direction == LANGUAGE_RTL) {
-    $ie_styles .= '      <style type="text/css" media="all">@import "' . base_path() . path_to_theme() . '/fix-ie-rtl.css";</style>' . "\n";
+    $ie_styles .= '      <style type="text/css" media="all">@import "' . file_create_url(path_to_theme() . '/fix-ie-rtl.css') . '";</style>' . "\n";
   }
 
   return $ie_styles;
