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