Blame SOURCES/0226-Prepend-prefix-when-HTTP-path-is-relative.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Stephen Benjamin <stephen@redhat.com>
8631a2
Date: Thu, 16 Aug 2018 16:58:51 -0400
8631a2
Subject: [PATCH] Prepend prefix when HTTP path is relative
8631a2
8631a2
This sets a couple of variables.  With the url http://www.example.com/foo/bar :
8631a2
http_path: /foo/bar
8631a2
http_url: http://www.example.com/foo/bar
8631a2
8631a2
Signed-off-by: Peter Jones <pjones@redhat.com>
8631a2
---
8631a2
 grub-core/kern/main.c    | 10 +++++-
8631a2
 grub-core/net/efi/http.c | 82 ++++++++++++++++++++++++++++++++++++------------
8631a2
 2 files changed, 71 insertions(+), 21 deletions(-)
8631a2
8631a2
diff --git a/grub-core/kern/main.c b/grub-core/kern/main.c
8631a2
index da47b18b50e..dcf48726d54 100644
8631a2
--- a/grub-core/kern/main.c
8631a2
+++ b/grub-core/kern/main.c
8631a2
@@ -130,11 +130,19 @@ grub_set_prefix_and_root (void)
8631a2
   if (fwdevice && fwpath)
8631a2
     {
8631a2
       char *fw_path;
8631a2
+      char separator[3] = ")";
8631a2
 
8631a2
-      fw_path = grub_xasprintf ("(%s)/%s", fwdevice, fwpath);
8631a2
+      grub_dprintf ("fw_path", "\n");
8631a2
+      grub_dprintf ("fw_path", "fwdevice:\"%s\" fwpath:\"%s\"\n", fwdevice, fwpath);
8631a2
+
8631a2
+      if (!grub_strncmp(fwdevice, "http", 4) && fwpath[0] != '/')
8631a2
+	grub_strcpy(separator, ")/");
8631a2
+
8631a2
+      fw_path = grub_xasprintf ("(%s%s%s", fwdevice, separator, fwpath);
8631a2
       if (fw_path)
8631a2
 	{
8631a2
 	  grub_env_set ("fw_path", fw_path);
8631a2
+	  grub_dprintf ("fw_path", "fw_path:\"%s\"\n", fw_path);
8631a2
 	  grub_free (fw_path);
8631a2
 	}
8631a2
     }
8631a2
diff --git a/grub-core/net/efi/http.c b/grub-core/net/efi/http.c
8631a2
index 243acbaa35b..2a9624dacc4 100644
8631a2
--- a/grub-core/net/efi/http.c
8631a2
+++ b/grub-core/net/efi/http.c
8631a2
@@ -9,10 +9,52 @@
8631a2
 static void
8631a2
 http_configure (struct grub_efi_net_device *dev, int prefer_ip6)
8631a2
 {
8631a2
+  grub_efi_ipv6_address_t address;
8631a2
   grub_efi_http_config_data_t http_config;
8631a2
   grub_efi_httpv4_access_point_t httpv4_node;
8631a2
   grub_efi_httpv6_access_point_t httpv6_node;
8631a2
   grub_efi_status_t status;
8631a2
+  int https;
8631a2
+  char *http_url;
8631a2
+  const char *rest, *http_server, *http_path = NULL;
8631a2
+
8631a2
+  http_server = grub_env_get ("root");
8631a2
+  https = grub_strncmp (http_server, "https", 5) ? 1 : 0;
8631a2
+
8631a2
+  /* extract http server + port */
8631a2
+  if (http_server)
8631a2
+    {
8631a2
+      http_server = grub_strchr (http_server, ',');
8631a2
+      if (http_server)
8631a2
+	http_server++;
8631a2
+    }
8631a2
+
8631a2
+  /* fw_path is like (http,192.168.1.1:8000)/httpboot, extract path part */
8631a2
+  http_path = grub_env_get ("fw_path");
8631a2
+  if (http_path)
8631a2
+    {
8631a2
+      http_path = grub_strchr (http_path, ')');
8631a2
+      if (http_path)
8631a2
+	{
8631a2
+	  http_path++;
8631a2
+	  grub_env_unset ("http_path");
8631a2
+	  grub_env_set ("http_path", http_path);
8631a2
+	}
8631a2
+    }
8631a2
+
8631a2
+  if (http_server && http_path)
8631a2
+    {
8631a2
+      if (grub_efi_string_to_ip6_address (http_server, &address, &rest) && *rest == 0)
8631a2
+	http_url = grub_xasprintf ("%s://[%s]%s", https ? "https" : "http", http_server, http_path);
8631a2
+      else
8631a2
+	http_url = grub_xasprintf ("%s://%s%s", https ? "https" : "http", http_server, http_path);
8631a2
+      if (http_url)
8631a2
+	{
8631a2
+	  grub_env_unset ("http_url");
8631a2
+	  grub_env_set ("http_url", http_url);
8631a2
+	  grub_free (http_url);
8631a2
+	}
8631a2
+    }
8631a2
 
8631a2
   grub_efi_http_t *http = dev->http;
8631a2
 
8631a2
@@ -352,32 +394,32 @@ grub_efihttp_open (struct grub_efi_net_device *dev,
8631a2
   grub_err_t err;
8631a2
   grub_off_t size;
8631a2
   char *buf;
8631a2
-  char *root_url;
8631a2
-  grub_efi_ipv6_address_t address;
8631a2
-  const char *rest;
8631a2
+  char *file_name;
8631a2
+  const char *http_path;
8631a2
 
8631a2
-  if (grub_efi_string_to_ip6_address (file->device->net->server, &address, &rest) && *rest == 0)
8631a2
-    root_url = grub_xasprintf ("%s://[%s]", type ? "https" : "http", file->device->net->server);
8631a2
+  /* If path is relative, prepend http_path */
8631a2
+  http_path = grub_env_get ("http_path");
8631a2
+  if (http_path && file->device->net->name[0] != '/')
8631a2
+    file_name = grub_xasprintf ("%s/%s", http_path, file->device->net->name);
8631a2
   else
8631a2
-    root_url = grub_xasprintf ("%s://%s", type ? "https" : "http", file->device->net->server);
8631a2
-  if (root_url)
8631a2
-    {
8631a2
-      grub_env_unset ("root_url");
8631a2
-      grub_env_set ("root_url", root_url);
8631a2
-      grub_free (root_url);
8631a2
-    }
8631a2
-  else
8631a2
-    {
8631a2
-      return grub_errno;
8631a2
-    }
8631a2
+    file_name = grub_strdup (file->device->net->name);
8631a2
 
8631a2
-  err = efihttp_request (dev->http, file->device->net->server, file->device->net->name, type, 1, 0);
8631a2
+  if (!file_name)
8631a2
+    return grub_errno;
8631a2
+
8631a2
+  err = efihttp_request (dev->http, file->device->net->server, file_name, type, 1, 0);
8631a2
   if (err != GRUB_ERR_NONE)
8631a2
-    return err;
8631a2
+    {
8631a2
+      grub_free (file_name);
8631a2
+      return err;
8631a2
+    }
8631a2
 
8631a2
-  err = efihttp_request (dev->http, file->device->net->server, file->device->net->name, type, 0, &size);
8631a2
+  err = efihttp_request (dev->http, file->device->net->server, file_name, type, 0, &size);
8631a2
+  grub_free (file_name);
8631a2
   if (err != GRUB_ERR_NONE)
8631a2
-    return err;
8631a2
+    {
8631a2
+      return err;
8631a2
+    }
8631a2
 
8631a2
   buf = grub_malloc (size);
8631a2
   efihttp_read (dev, buf, size);