From 90ead8f9fa5bb6d54be996cd1a3f5bae27d948bc Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Tue, 9 Apr 2013 21:27:45 +0100
Subject: [PATCH] Issue #1912594 by krishworks, Berdir: Convert
 drupal_http_request() usage in system.module to Guzzle.

---
 core/modules/system/system.module | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/core/modules/system/system.module b/core/modules/system/system.module
index 5a90e3eba35e..24883eaaadf6 100644
--- a/core/modules/system/system.module
+++ b/core/modules/system/system.module
@@ -13,6 +13,8 @@
 use Drupal\system\Plugin\block\block\SystemMenuBlock;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Response;
+use Guzzle\Http\Exception\BadResponseException;
+use Guzzle\Http\Exception\RequestException;
 
 /**
  * Maximum age of temporary files in seconds.
@@ -3667,7 +3669,7 @@ function theme_system_compact_link() {
 }
 
 /**
- * Attempts to get a file using drupal_http_request and to store it locally.
+ * Attempts to get a file using Guzzle HTTP client and to store it locally.
  *
  * @param $url
  *   The URL of the file to grab.
@@ -3708,12 +3710,22 @@ function system_retrieve_file($url, $destination = NULL, $managed = FALSE, $repl
       $path = $destination;
     }
   }
-  $result = drupal_http_request($url);
-  if ($result->code != 200) {
-    drupal_set_message(t('HTTP error @errorcode occurred when trying to fetch @remote.', array('@errorcode' => $result->code, '@remote' => $url)), 'error');
+  try {
+    $data = Drupal::httpClient()
+      ->get($url)
+      ->send()
+      ->getBody(TRUE);
+    $local = $managed ? file_save_data($data, $path, $replace) : file_unmanaged_save_data($data, $path, $replace);
+  }
+  catch (BadResponseException $exception) {
+    $response = $exception->getResponse();
+    drupal_set_message(t('Failed to fetch file due to HTTP error "%error"', array('%error' => $response->getStatusCode() . ' ' . $response->getReasonPhrase())), 'error');
+    return FALSE;
+  }
+  catch (RequestException $exception) {
+    drupal_set_message(t('Failed to fetch file due to error "%error"', array('%error' => $exception->getMessage())), 'error');
     return FALSE;
   }
-  $local = $managed ? file_save_data($result->data, $path, $replace) : file_unmanaged_save_data($result->data, $path, $replace);
   if (!$local) {
     drupal_set_message(t('@remote could not be saved to @path.', array('@remote' => $url, '@path' => $path)), 'error');
   }
-- 
GitLab