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