Index: wp-includes/default-filters.php
===================================================================
--- wp-includes/default-filters.php	(revision 15483)
+++ wp-includes/default-filters.php	(working copy)
@@ -229,6 +229,7 @@
 add_action( 'transition_post_status',     '_transition_post_status',  5, 3 );
 add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce'        );
 add_action( 'wp_scheduled_delete',        'wp_scheduled_delete'            );
+add_action( 'pre_enclose',                'dont_enclose_root_urls'         );
 
 // Navigation menu actions
 add_action( 'delete_post',                '_wp_delete_post_menu_item'      );
Index: wp-includes/functions.php
===================================================================
--- wp-includes/functions.php	(revision 15483)
+++ wp-includes/functions.php	(working copy)
@@ -1160,15 +1160,14 @@
  * @param string $content Post Content
  * @param int $post_ID Post ID
  */
-function do_enclose( $content, $post_ID ) {
+function do_enclose( $content, $post_id ) {
 	global $wpdb;
 	include_once( ABSPATH . WPINC . '/class-IXR.php' );
 
 	$log = debug_fopen( ABSPATH . 'enclosures.log', 'a' );
-	$post_links = array();
 	debug_fwrite( $log, 'BEGIN ' . date( 'YmdHis', time() ) . "\n" );
 
-	$pung = get_enclosed( $post_ID );
+	$pung = get_enclosed( $post_id );
 
 	$ltrs = '\w';
 	$gunk = '/#~:.?+=&%@!\-';
@@ -1177,64 +1176,101 @@
 
 	preg_match_all( "{\b http : [$any] +? (?= [$punc] * [^$any] | $)}x", $content, $post_links_temp );
 
+	// Don't ping the same link multiple times
+	$post_links = array_unique( $post_links_temp[0] );
+	// Only links we haven't pung
+	$post_links = array_diff( $post_links, $pung );
+	// Only links that are URLs
+	$post_links = array_filter( $post_links, 'parse_url' ); // This isn't a good check: only catches odd stuff (bool) parse_url( "url" ), for example, is true
+
 	debug_fwrite( $log, 'Post contents:' );
 	debug_fwrite( $log, $content . "\n" );
 
 	foreach ( $pung as $link_test ) {
-		if ( !in_array( $link_test, $post_links_temp[0] ) ) { // link no longer in post
-			$mid = $wpdb->get_col( $wpdb->prepare("SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $link_test . '%') );
+		if ( !in_array( $link_test, $post_links ) ) { // link no longer in post
+			$mid = $wpdb->get_col( $wpdb->prepare(
+				"SELECT meta_id FROM `$wpdb->postmeta` WHERE `post_id` = %d AND `meta_key` = 'enclosure' AND `meta_value` LIKE (%s)",
+				$post_id,
+				like_escape( $link_test ) . '%' // Delete everything for which this URL is a prefix?
+			) );
 			do_action( 'delete_postmeta', $mid );
-			$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN(%s)", implode( ',', $mid ) ) );
+			$_mid = join( ',', array_filter( array_map( 'absint', $mid ) ) );
+			$wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_id` IN($_mid)" );
 			do_action( 'deleted_postmeta', $mid );
 		}
 	}
 
-	foreach ( (array) $post_links_temp[0] as $link_test ) {
-		if ( !in_array( $link_test, $pung ) ) { // If we haven't pung it already
-			$test = @parse_url( $link_test );
-			if ( false === $test )
-				continue;
-			if ( isset( $test['query'] ) )
-				$post_links[] = $link_test;
-			elseif ( $test['path'] != '/' && $test['path'] != '' )
-				$post_links[] = $link_test;
-		}
-	}
+	do_action_ref_array( 'pre_enclose', array( &$post_links, &$pung ) );
 
+	$allowed_types = apply_filters( 'enclosure_mime_types', array( 'video', 'audio' ) );
+	$extension_map = get_allowed_mime_types();
+	$meta_key = 'enclosure';
+
 	foreach ( (array) $post_links as $url ) {
-		if ( $url != '' && !$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, $url . '%' ) ) ) {
+		if ( !$url || !$url_parts = parse_url( $url ) )
+			continue;
 
-			if ( $headers = wp_get_http_headers( $url) ) {
-				$len = (int) $headers['content-length'];
-				$type = $headers['content-type'];
-				$allowed_types = array( 'video', 'audio' );
+		// Done it or something like it
+		if ( $wpdb->get_var( $wpdb->prepare(
+			"SELECT `post_id` FROM `$wpdb->postmeta` WHERE `post_id` = %d AND `meta_key` = 'enclosure' AND `meta_value` LIKE (%s)",
+			$post_id,
+			like_escape( $url ) . '%'
+		) ) )
+			continue;
 
-				// Check to see if we can figure out the mime type from
-				// the extension
-				$url_parts = @parse_url( $url );
-				if ( false !== $url_parts ) {
-					$extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
-					if ( !empty( $extension ) ) {
-						foreach ( get_allowed_mime_types( ) as $exts => $mime ) {
-							if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
-								$type = $mime;
-								break;
-							}
-						}
+		if ( !$headers = wp_get_http_headers( $url ) )
+			continue;
+
+		$len = (int) $headers['content-length'];
+		$type = $headers['content-type'];
+
+		// Check to see if we can figure out the mime type from
+		// the extension
+		if ( false !== $url_parts ) {
+			$extension = pathinfo( $url_parts['path'], PATHINFO_EXTENSION );
+			if ( !empty( $extension ) ) {
+				foreach ( $extension_map as $exts => $mime ) {
+					if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
+						$type = $mime;
+						break;
 					}
 				}
-
-				if ( in_array( substr( $type, 0, strpos( $type, "/" ) ), $allowed_types ) ) {
-					$meta_value = "$url\n$len\n$type\n";
-					$wpdb->insert($wpdb->postmeta, array('post_id' => $post_ID, 'meta_key' => 'enclosure', 'meta_value' => $meta_value) );
-					do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, 'enclosure', $meta_value );
-				}
 			}
 		}
+
+		list( $type ) = explode( '/', $type, 2 );
+
+		if ( !in_array( $type, $allowed_types ) )
+			continue;
+
+		$meta_value = "$url\n$len\n$type\n";
+		$wpdb->insert( $wpdb->postmeta, compact( 'post_id', 'meta_key', 'meta_value' ) );
+		do_action( 'added_postmeta', $wpdb->insert_id, $post_ID, $meta_key, $meta_value );
 	}
 }
 
 /**
+ * Remove root URLs from the list of URLs to check for inclusion as enclosures.
+ *
+ * @package WordPress
+ * @since 3.1
+ *
+ * @param array $post_links Reference to array of URLs to check for inclusion as enclosures
+ */
+function dont_enclose_root_urls( &$post_links ) {
+	$r = array();
+	foreach ( (array) $post_links as $post_link ) {
+		// Only ping enclosures for URLs with a Query or Path - not Root URLs
+		$path = parse_url( $post_link, PHP_URL_PATH );
+		if ( $path && '/' != $path )
+			$r[] = $post_link;
+		else if ( parse_url( $post_link, PHP_URL_QUERY ) )
+			$r[] = $post_link;
+	}
+	$post_links = $r;
+}
+
+/**
  * Perform a HTTP HEAD or GET request.
  *
  * If $file_path is a writable filename, this will do a GET request and write
