From 4e7c5375faae2d181d7f06cf279274a5b58679fa Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Thu, 2 Oct 2008 00:32:21 +0000 Subject: [PATCH] Error checking for HTTP requests. Props jacobsantos. see #7793 git-svn-id: https://develop.svn.wordpress.org/trunk@9051 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/update-links.php | 3 +++ wp-includes/comment.php | 5 ++++- wp-includes/functions.php | 14 +++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/wp-admin/update-links.php b/wp-admin/update-links.php index 976769d23f..a60aa6e442 100644 --- a/wp-admin/update-links.php +++ b/wp-admin/update-links.php @@ -38,6 +38,9 @@ $options['headers'] = array( $response = wp_remote_get('http://api.pingomatic.com/updated-batch/', $options); +if ( is_wp_error( $response ) ) + wp_die(__('Request Failed.')); + if ( $response['response']['code'] != 200 ) wp_die(__('Request Failed.')); diff --git a/wp-includes/comment.php b/wp-includes/comment.php index aa773857c4..a5e3446516 100644 --- a/wp-includes/comment.php +++ b/wp-includes/comment.php @@ -1316,7 +1316,10 @@ function trackback($trackback_url, $title, $excerpt, $ID) { 'excerpt' => urlencode($excerpt) ); - wp_remote_post($trackback_url, $options); + $response = wp_remote_post($trackback_url, $options); + + if ( is_wp_error( $response ) ) + return; $tb_url = addslashes( $trackback_url ); $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET pinged = CONCAT(pinged, '\n', '$tb_url') WHERE ID = %d", $ID) ); diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 3aaa535334..45fae39e0a 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -1039,6 +1039,9 @@ function wp_get_http( $url, $file_path = false, $deprecated = false ) { $response = wp_remote_request($url, $options); + if ( is_wp_error( $response ) ) + return false; + $headers = wp_remote_retrieve_headers( $response ); if ( false == $file_path ) return $headers; @@ -1072,6 +1075,10 @@ function wp_get_http( $url, $file_path = false, $deprecated = false ) { */ function wp_get_http_headers( $url, $deprecated = false ) { $response = wp_remote_head( $url ); + + if ( is_wp_error( $response ) ) + return false; + return wp_remote_retrieve_headers( $response ); } @@ -1240,11 +1247,9 @@ function add_magic_quotes( $array ) { * @uses wp_remote_get() * * @param string $uri URI/URL of web page to retrieve. - * @return string HTTP content. + * @return bool|string HTTP content. False on failure. */ function wp_remote_fopen( $uri ) { - // parse url() should not be used for validation of URLs. - // Keeping anyway, since the Filter extension is not available on all servers. $parsed_url = @parse_url( $uri ); if ( !$parsed_url || !is_array( $parsed_url ) ) @@ -1255,6 +1260,9 @@ function wp_remote_fopen( $uri ) { $response = wp_remote_get( $uri, $options ); + if ( is_wp_error( $response ) ) + return false; + return $response['body']; }