Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.756.2.47
diff -u -F^f -r1.756.2.47 common.inc
--- includes/common.inc	25 Feb 2009 21:02:36 -0000	1.756.2.47
+++ includes/common.inc	17 May 2009 20:28:05 -0000
@@ -1830,15 +1830,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() . $file . $query_string .'" />'."\n";
+              $no_module_preprocess .= '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. file_create_url($file) . $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.
             else if (!$preprocess && $type == 'theme') {
-              $no_theme_preprocess .= '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
+              $no_theme_preprocess .= '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. file_create_url($file) . $query_string .'" />'."\n";
             }
             else {
-              $output .= '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. base_path() . $file . $query_string .'" />'."\n";
+              $output .= '<link type="text/css" rel="stylesheet" media="'. $media .'" href="'. file_create_url($file) . $query_string .'" />'."\n";
             }
           }
         }
@@ -1848,7 +1848,7 @@ function drupal_get_css($css = NULL) {
     if ($is_writable && $preprocess_css) {
       $filename = 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";
     }
   }
 
@@ -2183,7 +2183,7 @@ function drupal_get_js($scope = 'header'
         // Additionally, go through any remaining scripts if JS preprocessing is on and output the non-cached ones.
         foreach ($data as $path => $info) {
           if (!$info['preprocess'] || !$is_writable || !$preprocess_js) {
-            $no_preprocess[$type] .= '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .' src="'. base_path() . $path . ($info['cache'] ? $query_string : '?'. time()) ."\"></script>\n";
+            $no_preprocess[$type] .= '<script type="text/javascript"'. ($info['defer'] ? ' defer="defer"' : '') .' src="'. file_create_url($path) . ($info['cache'] ? $query_string : '?'. time()) ."\"></script>\n";
           }
           else {
             $files[$path] = $info;
@@ -2196,7 +2196,7 @@ function drupal_get_js($scope = 'header'
   if ($is_writable && $preprocess_js && count($files) > 0) {
     $filename = 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.121.2.5
diff -u -F^f -r1.121.2.5 file.inc
--- includes/file.inc	20 Oct 2008 09:42:31 -0000	1.121.2.5
+++ includes/file.inc	17 May 2009 20:28:06 -0000
@@ -35,19 +35,92 @@
 /**
  * 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.
+ *
  * @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));
+
+  // If pathologic is enabled, use its settings to perform similar processing on the path
+  if (module_exists('pathologic')) {
+
+    // Retrieve matching paths settings
+    $directive = trim(variable_get("filter_pathologic_abs_paths_1", '')); // using Filtered HTML format
+    if ($directive !== '') {
+      $paths = array_map('trim', explode("\n", $directive)); // get rid of white space on each line
+    } else {
+      $paths = array();
+    }
+    $paths[] = _pathologic_url('<front>');
+    $paths = array_unique($paths);
+
+    // Build regexp, match twice (with or without leading slash) and then trim slashes
+    $path_suffix =  '/?(index.php)?(\?q=)?'; // the optional suffix to match
+    $path_regexp = '(' . implode($path_suffix.'|',$paths) . $path_suffix . ')';
+    $original_path = $path;  
+    $path = preg_replace($path_regexp, '', $path);
+    $path = preg_replace($path_regexp, '', '/'.$path);
+    $path = trim($path, '\\/');
+    
+    // If the path was changed by pathologic rules, it is required to be relative
+    if ($path != $original_path) {
+
+      // 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;
+      }
+
+      // If we still have relative URL, make it absolute, just like pathologic would do
+      $path = $GLOBALS['base_url'] .'/'. str_replace('\\', '/', $path);
+    }
+  }
+  
+  // If we have absolute path with hostname, just keep it
+  $fragments = parse_url($path);
+  if (isset($fragments['host'])) return $path;
+
+  // Check if the file is in the files directory.
+  $is_in_files_directory = (strpos($path, file_directory_path() .'/') === 0);
+  
+  // 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.
+  if (!$is_in_files_directory) {
+    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.265.2.25
diff -u -r1.265.2.25 form.inc
--- includes/form.inc  26 May 2009 08:18:46 -0000  1.265.2.25
+++ includes/form.inc  4 Jun 2009 11:10:57 -0000
@@ -1980,7 +1980,7 @@
     (!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.415.2.19
diff -u -F^f -r1.415.2.19 theme.inc
--- includes/theme.inc	25 Feb 2009 21:02:16 -0000	1.415.2.19
+++ includes/theme.inc	17 May 2009 20:28:08 -0000
@@ -965,24 +965,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;
@@ -1203,7 +1203,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: themes/garland/template.php
===================================================================
RCS file: /cvs/drupal/drupal/themes/garland/template.php,v
retrieving revision 1.16.2.1
diff -u -F^f -r1.16.2.1 template.php
--- themes/garland/template.php	25 Feb 2009 11:47:37 -0000	1.16.2.1
+++ themes/garland/template.php	17 May 2009 20:28:19 -0000
@@ -93,9 +93,9 @@ function phptemplate_node_submitted($nod
 function phptemplate_get_ie_styles() {
   global $language;
 
-  $iecss = '<link type="text/css" rel="stylesheet" media="all" href="'. base_path() . path_to_theme() .'/fix-ie.css" />';
+  $iecss = '<link type="text/css" rel="stylesheet" media="all" href="'. file_create_url(path_to_theme() .'/fix-ie.css') . ' />';
   if ($language->direction == LANGUAGE_RTL) {
-    $iecss .= '<style type="text/css" media="all">@import "'. base_path() . path_to_theme() .'/fix-ie-rtl.css";</style>';
+    $iecss .= '<style type="text/css" media="all">@import "'. file_create_url(path_to_theme() .'/fix-ie-rtl.css') . '";</style>';
   }
 
   return $iecss;
