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

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