Blame SOURCES/0074-Add-a-url-parser.patch

d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Peter Jones <pjones@redhat.com>
d9d99f
Date: Tue, 14 Jun 2016 16:18:44 -0400
d9d99f
Subject: [PATCH] Add a url parser.
d9d99f
d9d99f
This patch adds a url parser that can parse http, https, tftp, and tftps
d9d99f
urls, and is easily extensible to handle more types.
d9d99f
d9d99f
It's a little ugly in terms of the arguments it takes.
d9d99f
d9d99f
Signed-off-by: Peter Jones <pjones@redhat.com>
d9d99f
---
d9d99f
 grub-core/Makefile.core.def |   1 +
d9d99f
 grub-core/kern/misc.c       |  13 +
d9d99f
 grub-core/net/url.c         | 861 ++++++++++++++++++++++++++++++++++++++++++++
d9d99f
 include/grub/misc.h         |  45 +++
d9d99f
 include/grub/net/url.h      |  28 ++
d9d99f
 5 files changed, 948 insertions(+)
d9d99f
 create mode 100644 grub-core/net/url.c
d9d99f
 create mode 100644 include/grub/net/url.h
d9d99f
d9d99f
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
d9d99f
index cd0902b46b8..991891a6e09 100644
d9d99f
--- a/grub-core/Makefile.core.def
d9d99f
+++ b/grub-core/Makefile.core.def
d9d99f
@@ -2185,6 +2185,7 @@ module = {
d9d99f
   common = net/ethernet.c;
d9d99f
   common = net/arp.c;
d9d99f
   common = net/netbuff.c;
d9d99f
+  common = net/url.c;
d9d99f
 };
d9d99f
 
d9d99f
 module = {
d9d99f
diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
d9d99f
index 8344526be7f..f1fab700048 100644
d9d99f
--- a/grub-core/kern/misc.c
d9d99f
+++ b/grub-core/kern/misc.c
d9d99f
@@ -296,6 +296,19 @@ grub_strrchr (const char *s, int c)
d9d99f
   return p;
d9d99f
 }
d9d99f
 
d9d99f
+char *
d9d99f
+grub_strchrnul (const char *s, int c)
d9d99f
+{
d9d99f
+  do
d9d99f
+    {
d9d99f
+      if (*s == c)
d9d99f
+	break;
d9d99f
+    }
d9d99f
+  while (*s++);
d9d99f
+
d9d99f
+  return (char *) s;
d9d99f
+}
d9d99f
+
d9d99f
 int
d9d99f
 grub_strword (const char *haystack, const char *needle)
d9d99f
 {
d9d99f
diff --git a/grub-core/net/url.c b/grub-core/net/url.c
d9d99f
new file mode 100644
d9d99f
index 00000000000..146858284cd
d9d99f
--- /dev/null
d9d99f
+++ b/grub-core/net/url.c
d9d99f
@@ -0,0 +1,861 @@
d9d99f
+/*
d9d99f
+ *  GRUB  --  GRand Unified Bootloader
d9d99f
+ *  Copyright (C) 2016  Free Software Foundation, Inc.
d9d99f
+ *
d9d99f
+ *  GRUB is free software: you can redistribute it and/or modify
d9d99f
+ *  it under the terms of the GNU General Public License as published by
d9d99f
+ *  the Free Software Foundation, either version 3 of the License, or
d9d99f
+ *  (at your option) any later version.
d9d99f
+ *
d9d99f
+ *  GRUB is distributed in the hope that it will be useful,
d9d99f
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
d9d99f
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d9d99f
+ *  GNU General Public License for more details.
d9d99f
+ *
d9d99f
+ *  You should have received a copy of the GNU General Public License
d9d99f
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
d9d99f
+ */
d9d99f
+
d9d99f
+#ifdef URL_TEST
d9d99f
+
d9d99f
+#define _GNU_SOURCE 1
d9d99f
+
d9d99f
+#include <errno.h>
d9d99f
+#include <limits.h>
d9d99f
+#include <stdio.h>
d9d99f
+#include <stdlib.h>
d9d99f
+#include <string.h>
d9d99f
+#include <sys/types.h>
d9d99f
+#include <unistd.h>
d9d99f
+
d9d99f
+#define N_(x) x
d9d99f
+
d9d99f
+#define grub_malloc(x) malloc(x)
d9d99f
+#define grub_free(x) ({if (x) free(x);})
d9d99f
+#define grub_error(a, fmt, args...) printf(fmt "\n", ## args)
d9d99f
+#define grub_dprintf(a, fmt, args...) printf(a ": " fmt, ## args)
d9d99f
+#define grub_strlen(x) strlen(x)
d9d99f
+#define grub_strdup(x) strdup(x)
d9d99f
+#define grub_strstr(x,y) strstr(x,y)
d9d99f
+#define grub_memcpy(x,y,z) memcpy(x,y,z)
d9d99f
+#define grub_strcmp(x,y) strcmp(x,y)
d9d99f
+#define grub_strncmp(x,y,z) strncmp(x,y,z)
d9d99f
+#define grub_strcasecmp(x,y) strcasecmp(x,y)
d9d99f
+#define grub_strchrnul(x,y) strchrnul(x,y)
d9d99f
+#define grub_strchr(x,y) strchr(x,y)
d9d99f
+#define grub_strndup(x,y) strndup(x,y)
d9d99f
+#define grub_strtoul(x,y,z) strtoul(x,y,z)
d9d99f
+#define grub_memmove(x,y,z) memmove(x,y,z)
d9d99f
+#define grub_size_t size_t
d9d99f
+#define grub_errno errno
d9d99f
+
d9d99f
+#else
d9d99f
+#include <grub/types.h>
d9d99f
+#include <grub/mm.h>
d9d99f
+#include <grub/misc.h>
d9d99f
+#include <grub/net/url.h>
d9d99f
+#endif
d9d99f
+
d9d99f
+static char *
d9d99f
+translate_slashes(char *str)
d9d99f
+{
d9d99f
+  int i, j;
d9d99f
+  if (str == NULL)
d9d99f
+    return str;
d9d99f
+
d9d99f
+  for (i = 0, j = 0; str[i] != '\0'; i++, j++)
d9d99f
+    {
d9d99f
+      if (str[i] == '\\')
d9d99f
+	{
d9d99f
+	  str[j] = '/';
d9d99f
+	  if (str[i+1] == '\\')
d9d99f
+	    i++;
d9d99f
+	}
d9d99f
+    }
d9d99f
+
d9d99f
+  return str;
d9d99f
+}
d9d99f
+
d9d99f
+static inline int
d9d99f
+hex2int (char c)
d9d99f
+{
d9d99f
+  if (c >= '0' && c <= '9')
d9d99f
+    return c - '0';
d9d99f
+  c |= 0x20;
d9d99f
+  if (c >= 'a' && c <= 'f')
d9d99f
+    return c - 'a' + 10;
d9d99f
+  return -1;
d9d99f
+}
d9d99f
+
d9d99f
+static int
d9d99f
+url_unescape (char *buf, grub_size_t len)
d9d99f
+{
d9d99f
+  int c, rc;
d9d99f
+  unsigned int i;
d9d99f
+
d9d99f
+
d9d99f
+  if (len < 3)
d9d99f
+    {
d9d99f
+      for (i = 0; i < len; i++)
d9d99f
+	if (buf[i] == '%')
d9d99f
+	  return -1;
d9d99f
+      return 0;
d9d99f
+    }
d9d99f
+
d9d99f
+  for (i = 0; len > 2 && i < len - 2; i++)
d9d99f
+    {
d9d99f
+      if (buf[i] == '%')
d9d99f
+	{
d9d99f
+	  unsigned int j;
d9d99f
+	  for (j = i+1; j < i+3; j++)
d9d99f
+	    {
d9d99f
+	      if (!(buf[j] >= '0' && buf[j] <= '9') &&
d9d99f
+		  !(buf[j] >= 'a' && buf[j] <= 'f') &&
d9d99f
+		  !(buf[j] >= 'A' && buf[j] <= 'F'))
d9d99f
+		return -1;
d9d99f
+	    }
d9d99f
+	  i += 2;
d9d99f
+	}
d9d99f
+    }
d9d99f
+  if (i == len - 2)
d9d99f
+    {
d9d99f
+      if (buf[i+1] == '%' || buf[i+2] == '%')
d9d99f
+	return -1;
d9d99f
+    }
d9d99f
+  for (i = 0; i < len - 2; i++)
d9d99f
+    {
d9d99f
+      if (buf[i] == '%')
d9d99f
+	{
d9d99f
+	  rc = hex2int (buf[i+1]);
d9d99f
+	  if (rc < 0)
d9d99f
+	    return -1;
d9d99f
+	  c = (rc & 0xf) << 4;
d9d99f
+	  rc = hex2int (buf[i+2]);
d9d99f
+	  if (rc < 0)
d9d99f
+	    return -1;
d9d99f
+	  c |= (rc & 0xf);
d9d99f
+
d9d99f
+	  buf[i] = c;
d9d99f
+	  grub_memmove (buf+i+1, buf+i+3, len-(i+2));
d9d99f
+	  len -= 2;
d9d99f
+	}
d9d99f
+    }
d9d99f
+  return 0;
d9d99f
+}
d9d99f
+
d9d99f
+static int
d9d99f
+extract_http_url_info (char *url, int ssl,
d9d99f
+		       char **userinfo, char **host, int *port,
d9d99f
+		       char **file)
d9d99f
+{
d9d99f
+  char *colon, *slash, *query, *at = NULL, *separator, *auth_end;
d9d99f
+
d9d99f
+  char *userinfo_off = NULL;
d9d99f
+  char *userinfo_end;
d9d99f
+  char *host_off = NULL;
d9d99f
+  char *host_end;
d9d99f
+  char *port_off = NULL;
d9d99f
+  char *port_end;
d9d99f
+  char *file_off = NULL;
d9d99f
+
d9d99f
+  grub_size_t l;
d9d99f
+  int c;
d9d99f
+
d9d99f
+  if (!url || !userinfo || !host || !port || !file)
d9d99f
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument");
d9d99f
+
d9d99f
+  *userinfo = *host = *file = NULL;
d9d99f
+  *port = -1;
d9d99f
+
d9d99f
+  userinfo_off = url;
d9d99f
+
d9d99f
+  slash = grub_strchrnul (userinfo_off, '/');
d9d99f
+  query = grub_strchrnul (userinfo_off, '?');
d9d99f
+  auth_end = slash < query ? slash : query;
d9d99f
+  /* auth_end here is one /past/ the last character in the auth section, i.e.
d9d99f
+   * it's the : or / or NUL */
d9d99f
+
d9d99f
+  separator = grub_strchrnul (userinfo_off, '@');
d9d99f
+  if (separator > auth_end)
d9d99f
+    {
d9d99f
+      host_off = userinfo_off;
d9d99f
+      userinfo_off = NULL;
d9d99f
+      userinfo_end = NULL;
d9d99f
+    }
d9d99f
+  else
d9d99f
+    {
d9d99f
+      at = separator;
d9d99f
+      *separator = '\0';
d9d99f
+      userinfo_end = separator;
d9d99f
+      host_off = separator + 1;
d9d99f
+    }
d9d99f
+
d9d99f
+  if (*host_off == '[')
d9d99f
+    {
d9d99f
+      separator = grub_strchrnul (host_off, ']');
d9d99f
+      if (separator >= auth_end)
d9d99f
+	goto fail;
d9d99f
+
d9d99f
+      separator += 1;
d9d99f
+      host_end = separator;
d9d99f
+    }
d9d99f
+  else
d9d99f
+    {
d9d99f
+      host_end = separator = colon = grub_strchrnul (host_off, ':');
d9d99f
+
d9d99f
+      if (colon > auth_end)
d9d99f
+	{
d9d99f
+	  separator = NULL;
d9d99f
+	  host_end = auth_end;
d9d99f
+	}
d9d99f
+    }
d9d99f
+
d9d99f
+  if (separator && separator < auth_end)
d9d99f
+    {
d9d99f
+      if (*separator == ':')
d9d99f
+	{
d9d99f
+	  port_off = separator + 1;
d9d99f
+	  port_end = auth_end;
d9d99f
+
d9d99f
+	  if (auth_end - port_end > 0)
d9d99f
+	    goto fail;
d9d99f
+	  if (port_end - port_off < 1)
d9d99f
+	    goto fail;
d9d99f
+	}
d9d99f
+      else
d9d99f
+	goto fail;
d9d99f
+    }
d9d99f
+
d9d99f
+  file_off = auth_end;
d9d99f
+  if (port_off)
d9d99f
+    {
d9d99f
+      unsigned long portul;
d9d99f
+
d9d99f
+      separator = NULL;
d9d99f
+      c = *port_end;
d9d99f
+      *port_end = '\0';
d9d99f
+
d9d99f
+      portul = grub_strtoul (port_off, &separator, 10);
d9d99f
+      *port_end = c;
d9d99f
+#ifdef URL_TEST
d9d99f
+      if (portul == ULONG_MAX && errno == ERANGE)
d9d99f
+	goto fail;
d9d99f
+#else
d9d99f
+      if (grub_errno == GRUB_ERR_OUT_OF_RANGE)
d9d99f
+	goto fail;
d9d99f
+#endif
d9d99f
+      if (portul & ~0xfffful)
d9d99f
+	goto fail;
d9d99f
+      if (separator != port_end)
d9d99f
+	goto fail;
d9d99f
+
d9d99f
+      *port = portul & 0xfffful;
d9d99f
+    }
d9d99f
+  else if (ssl)
d9d99f
+    *port = 443;
d9d99f
+  else
d9d99f
+    *port = 80;
d9d99f
+
d9d99f
+  if (userinfo_off && *userinfo_off)
d9d99f
+    {
d9d99f
+      l = userinfo_end - userinfo_off + 1;
d9d99f
+
d9d99f
+      *userinfo = grub_strndup (userinfo_off, l);
d9d99f
+      if (!*userinfo)
d9d99f
+	goto fail;
d9d99f
+      (*userinfo)[l-1]= '\0';
d9d99f
+    }
d9d99f
+
d9d99f
+  l = host_end - host_off;
d9d99f
+
d9d99f
+  if (host_end == NULL)
d9d99f
+    goto fail;
d9d99f
+  else
d9d99f
+    c = *host_end;
d9d99f
+
d9d99f
+  *host_end = '\0';
d9d99f
+  *host = grub_strndup (host_off, l);
d9d99f
+  *host_end = c;
d9d99f
+  if (!*host)
d9d99f
+    goto fail;
d9d99f
+  (*host)[l] = '\0';
d9d99f
+
d9d99f
+  *file = grub_strdup (file_off);
d9d99f
+  if (!*file)
d9d99f
+    goto fail;
d9d99f
+
d9d99f
+  if (at)
d9d99f
+    *at = '@';
d9d99f
+  return 0;
d9d99f
+fail:
d9d99f
+  if (at)
d9d99f
+    *at = '@';
d9d99f
+  grub_free (*userinfo);
d9d99f
+  grub_free (*host);
d9d99f
+  grub_free (*file);
d9d99f
+
d9d99f
+  return -1;
d9d99f
+}
d9d99f
+
d9d99f
+static int
d9d99f
+extract_tftp_url_info (char *url, int ssl, char **host, char **file, int *port)
d9d99f
+{
d9d99f
+  char *slash, *semi;
d9d99f
+
d9d99f
+  char *host_off = url;
d9d99f
+  char *host_end;
d9d99f
+  char *file_off;
d9d99f
+
d9d99f
+  int c;
d9d99f
+
d9d99f
+  if (!url || !host || !file || !port)
d9d99f
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument");
d9d99f
+
d9d99f
+  if (ssl)
d9d99f
+    *port = 3713;
d9d99f
+  else
d9d99f
+    *port = 69;
d9d99f
+
d9d99f
+  slash = grub_strchr (url, '/');
d9d99f
+  if (!slash)
d9d99f
+    return -1;
d9d99f
+
d9d99f
+  host_end = file_off = slash;
d9d99f
+
d9d99f
+  semi = grub_strchrnul (slash, ';');
d9d99f
+  if (!grub_strncmp (semi, ";mode=", 6) && grub_strcmp (semi+6, "octet"))
d9d99f
+    {
d9d99f
+      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
d9d99f
+		  N_("TFTP mode `%s' is not implemented."), semi+6);
d9d99f
+      return -1;
d9d99f
+    }
d9d99f
+
d9d99f
+  /*
d9d99f
+   * Maybe somebody added a new method, I dunno.  Anyway, semi is a reserved
d9d99f
+   * character, so if it's there, it's the start of the mode block or it's
d9d99f
+   * invalid.  So set it to \0 unconditionally, not just for ;mode=octet
d9d99f
+   */
d9d99f
+  *semi = '\0';
d9d99f
+
d9d99f
+  c = *host_end;
d9d99f
+  *host_end = '\0';
d9d99f
+  *host = grub_strdup (host_off);
d9d99f
+  *host_end = c;
d9d99f
+
d9d99f
+  *file = grub_strdup (file_off);
d9d99f
+
d9d99f
+  if (!*file || !*host)
d9d99f
+    {
d9d99f
+      grub_free (*file);
d9d99f
+      grub_free (*host);
d9d99f
+      return -1;
d9d99f
+    }
d9d99f
+
d9d99f
+  return 0;
d9d99f
+}
d9d99f
+
d9d99f
+int
d9d99f
+extract_url_info (const char *urlbuf, grub_size_t buflen,
d9d99f
+		  char **scheme, char **userinfo,
d9d99f
+		  char **host, int *port, char **file)
d9d99f
+{
d9d99f
+  char *url;
d9d99f
+  char *colon;
d9d99f
+
d9d99f
+  char *scheme_off;
d9d99f
+  char *specific_off;
d9d99f
+
d9d99f
+  int rc;
d9d99f
+  int c;
d9d99f
+
d9d99f
+  int https;
d9d99f
+
d9d99f
+  if (!urlbuf || !buflen || !scheme || !userinfo || !host || !port || !file)
d9d99f
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "Invalid argument");
d9d99f
+
d9d99f
+  *scheme = *userinfo = *host = *file = NULL;
d9d99f
+  *port = -1;
d9d99f
+
d9d99f
+  /* make sure we have our own coherent grub_string. */
d9d99f
+  url = grub_malloc (buflen + 1);
d9d99f
+  if (!url)
d9d99f
+    return -1;
d9d99f
+
d9d99f
+  grub_memcpy (url, urlbuf, buflen);
d9d99f
+  url[buflen] = '\0';
d9d99f
+
d9d99f
+  grub_dprintf ("net", "dhcpv6 boot-file-url: `%s'\n", url);
d9d99f
+
d9d99f
+  /* get rid of any backslashes */
d9d99f
+  url = translate_slashes (url);
d9d99f
+
d9d99f
+  /* find the constituent parts */
d9d99f
+  colon = grub_strstr (url, "://");
d9d99f
+  if (!colon)
d9d99f
+    goto fail;
d9d99f
+
d9d99f
+  scheme_off = url;
d9d99f
+  c = *colon;
d9d99f
+  *colon = '\0';
d9d99f
+  specific_off = colon + 3;
d9d99f
+
d9d99f
+  https = !grub_strcasecmp (scheme_off, "https");
d9d99f
+
d9d99f
+  rc = 0;
d9d99f
+  if (!grub_strcasecmp (scheme_off, "tftp"))
d9d99f
+    {
d9d99f
+      rc = extract_tftp_url_info (specific_off, 0, host, file, port);
d9d99f
+    }
d9d99f
+#ifdef URL_TEST
d9d99f
+  else if (!grub_strcasecmp (scheme_off, "http") || https)
d9d99f
+#else
d9d99f
+  else if (!grub_strcasecmp (scheme_off, "http"))
d9d99f
+#endif
d9d99f
+    {
d9d99f
+      rc = extract_http_url_info (specific_off,
d9d99f
+				  https, userinfo, host, port, file);
d9d99f
+    }
d9d99f
+#ifdef URL_TEST
d9d99f
+  else if (!grub_strcasecmp (scheme_off, "iscsi"))
d9d99f
+    {
d9d99f
+      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
d9d99f
+		  N_("Unimplemented URL scheme `%s'"), scheme_off);
d9d99f
+      *colon = c;
d9d99f
+      goto fail;
d9d99f
+    }
d9d99f
+  else if (!grub_strcasecmp (scheme_off, "tftps"))
d9d99f
+    {
d9d99f
+      rc = extract_tftp_url_info (specific_off, 1, host, file, port);
d9d99f
+    }
d9d99f
+#endif
d9d99f
+  else
d9d99f
+    {
d9d99f
+      grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
d9d99f
+		  N_("Unimplemented URL scheme `%s'"), scheme_off);
d9d99f
+      *colon = c;
d9d99f
+      goto fail;
d9d99f
+    }
d9d99f
+
d9d99f
+  if (rc < 0)
d9d99f
+    {
d9d99f
+      *colon = c;
d9d99f
+      goto fail;
d9d99f
+    }
d9d99f
+
d9d99f
+  *scheme = grub_strdup (scheme_off);
d9d99f
+  *colon = c;
d9d99f
+  if (!*scheme)
d9d99f
+    goto fail;
d9d99f
+
d9d99f
+  if (*userinfo)
d9d99f
+    {
d9d99f
+      rc = url_unescape (*userinfo, grub_strlen (*userinfo));
d9d99f
+      if (rc < 0)
d9d99f
+	goto fail;
d9d99f
+    }
d9d99f
+
d9d99f
+  if (*host)
d9d99f
+    {
d9d99f
+      rc = url_unescape (*host, grub_strlen (*host));
d9d99f
+      if (rc < 0)
d9d99f
+	goto fail;
d9d99f
+    }
d9d99f
+
d9d99f
+  if (*file)
d9d99f
+    {
d9d99f
+      rc = url_unescape (*file, grub_strlen (*file));
d9d99f
+      if (rc < 0)
d9d99f
+	goto fail;
d9d99f
+    }
d9d99f
+
d9d99f
+  grub_free (url);
d9d99f
+  return 0;
d9d99f
+fail:
d9d99f
+  grub_free (*scheme);
d9d99f
+  grub_free (*userinfo);
d9d99f
+  grub_free (*host);
d9d99f
+  grub_free (*file);
d9d99f
+
d9d99f
+  if (!grub_errno)
d9d99f
+    grub_error (GRUB_ERR_NET_BAD_ADDRESS, N_("Invalid boot-file-url `%s'"),
d9d99f
+		url);
d9d99f
+  grub_free (url);
d9d99f
+  return -1;
d9d99f
+}
d9d99f
+
d9d99f
+#ifdef URL_TEST
d9d99f
+
d9d99f
+struct test {
d9d99f
+    char *url;
d9d99f
+    int rc;
d9d99f
+    char *scheme;
d9d99f
+    char *userinfo;
d9d99f
+    char *host;
d9d99f
+    int port;
d9d99f
+    char *file;
d9d99f
+} tests[] = {
d9d99f
+  {.url = "http://foo.example.com/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 80,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "http://foo.example.com/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 80,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "http://[foo.example.com/",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "http://[foo.example.com/?foobar",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "http://foo.example.com:/",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "http://foo.example.com:81/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "http://foo.example.com:81/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "http://[1234::1]/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 80,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "http://[1234::1]/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 80,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "http://[1234::1]:81/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "http://[1234::1]:81/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "http://foo@foo.example.com/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 80,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "http://foo@foo.example.com/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 80,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "http://foo@[foo.example.com/",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "http://foo@[foo.example.com/?foobar",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "http://foo@foo.example.com:81/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "http://foo@foo.example.com:81/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "http://foo@[1234::1]/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 80,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "http://foo@[1234::1]/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 80,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "http://foo@[1234::1]:81/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "http://foo@[1234::1]:81/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "http",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "https://foo.example.com/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 443,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "https://foo.example.com/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 443,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "https://[foo.example.com/",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "https://[foo.example.com/?foobar",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "https://foo.example.com:81/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "https://foo.example.com:81/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "https://[1234::1]/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 443,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "https://[1234::1]/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 443,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "https://[1234::1]:81/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "https://[1234::1]:81/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "https://foo@foo.example.com/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 443,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "https://foo@foo.example.com/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 443,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "https://foo@[foo.example.com/",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "https://f%6fo@[foo.example.com/?fooba%72",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "https://foo@foo.example.com:81/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "https://foo@foo.example.com:81/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "https://foo@[1234::1]/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 443,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "https://foo@[1234::1]/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 443,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "https://f%6fo@[12%334::1]:81/",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/",
d9d99f
+  },
d9d99f
+  {.url = "https://foo@[1234::1]:81/?foobar",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "https",
d9d99f
+   .userinfo = "foo",
d9d99f
+   .host = "[1234::1]",
d9d99f
+   .port = 81,
d9d99f
+   .file = "/?foobar",
d9d99f
+  },
d9d99f
+  {.url = "tftp://foo.e%78ample.com/foo/bar/b%61%7a",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "tftp",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 69,
d9d99f
+   .file = "/foo/bar/baz",
d9d99f
+  },
d9d99f
+  {.url = "tftp://foo.example.com/foo/bar/baz",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "tftp",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 69,
d9d99f
+   .file = "/foo/bar/baz",
d9d99f
+  },
d9d99f
+  {.url = "tftps://foo.example.com/foo/bar/baz",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "tftps",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 3713,
d9d99f
+   .file = "/foo/bar/baz",
d9d99f
+  },
d9d99f
+  {.url = "tftps://foo.example.com/foo/bar/baz;mode=netascii",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "tftps://foo.example.com/foo/bar/baz;mode=octet",
d9d99f
+   .rc = 0,
d9d99f
+   .scheme = "tftps",
d9d99f
+   .host = "foo.example.com",
d9d99f
+   .port = 3713,
d9d99f
+   .file = "/foo/bar/baz",
d9d99f
+  },
d9d99f
+  {.url = "tftps://foo.example.com/foo/bar/baz;mode=invalid",
d9d99f
+   .rc = -1,
d9d99f
+  },
d9d99f
+  {.url = "",
d9d99f
+  },
d9d99f
+};
d9d99f
+
d9d99f
+static int
d9d99f
+string_test (char *name, char *a, char *b)
d9d99f
+{
d9d99f
+  if ((a && !b) || (!a && b))
d9d99f
+    {
d9d99f
+      printf("expected %s \"%s\", got \"%s\"\n", name, a, b);
d9d99f
+      return -1;
d9d99f
+    }
d9d99f
+  if (a && b && strcmp(a, b))
d9d99f
+    {
d9d99f
+      printf("expected %s \"%s\", got \"%s\"\n", name, a, b);
d9d99f
+      return -1;
d9d99f
+    }
d9d99f
+  return 0;
d9d99f
+}
d9d99f
+
d9d99f
+int
d9d99f
+main(void)
d9d99f
+{
d9d99f
+	unsigned int i;
d9d99f
+	int rc;
d9d99f
+
d9d99f
+	for (i = 0; tests[i].url[0] != '\0'; i++)
d9d99f
+	{
d9d99f
+		char *scheme, *userinfo, *host, *file;
d9d99f
+		int port;
d9d99f
+
d9d99f
+		printf("======= url: \"%s\"\n", tests[i].url);
d9d99f
+		rc = extract_url_info(tests[i].url, strlen(tests[i].url) + 1,
d9d99f
+				      &scheme, &userinfo, &host, &port, &file;;
d9d99f
+		if (tests[i].rc != rc)
d9d99f
+		  {
d9d99f
+		    printf("  extract_url_info(...) = %d\n", rc);
d9d99f
+		    exit(1);
d9d99f
+		  }
d9d99f
+		else if (rc >= 0)
d9d99f
+		  {
d9d99f
+		    if (string_test("scheme", tests[i].scheme, scheme) < 0)
d9d99f
+		      exit(1);
d9d99f
+		    if (string_test("userinfo", tests[i].userinfo, userinfo) < 0)
d9d99f
+		      exit(1);
d9d99f
+		    if (string_test("host", tests[i].host, host) < 0)
d9d99f
+		      exit(1);
d9d99f
+		    if (port != tests[i].port)
d9d99f
+		      {
d9d99f
+			printf("  bad port \"%d\" should have been \"%d\"\n",
d9d99f
+			       port, tests[i].port);
d9d99f
+			exit(1);
d9d99f
+		      }
d9d99f
+		    if (string_test("file", tests[i].file, file) < 0)
d9d99f
+		      exit(1);
d9d99f
+		  }
d9d99f
+		free(scheme);
d9d99f
+		free(userinfo);
d9d99f
+		free(host);
d9d99f
+		free(file);
d9d99f
+	}
d9d99f
+	printf("everything worked?!?\n");
d9d99f
+}
d9d99f
+#endif
d9d99f
diff --git a/include/grub/misc.h b/include/grub/misc.h
d9d99f
index 83fd69f4ada..fcaf1201e39 100644
d9d99f
--- a/include/grub/misc.h
d9d99f
+++ b/include/grub/misc.h
d9d99f
@@ -85,6 +85,7 @@ int EXPORT_FUNC(grub_strncmp) (const char *s1, const char *s2, grub_size_t n);
d9d99f
 
d9d99f
 char *EXPORT_FUNC(grub_strchr) (const char *s, int c);
d9d99f
 char *EXPORT_FUNC(grub_strrchr) (const char *s, int c);
d9d99f
+char *EXPORT_FUNC(grub_strchrnul) (const char *s, int c);
d9d99f
 int EXPORT_FUNC(grub_strword) (const char *s, const char *w);
d9d99f
 
d9d99f
 /* Copied from gnulib.
d9d99f
@@ -207,6 +208,50 @@ grub_toupper (int c)
d9d99f
   return c;
d9d99f
 }
d9d99f
 
d9d99f
+static inline char *
d9d99f
+grub_strcasestr (const char *haystack, const char *needle)
d9d99f
+{
d9d99f
+  /* Be careful not to look at the entire extent of haystack or needle
d9d99f
+     until needed.  This is useful because of these two cases:
d9d99f
+       - haystack may be very long, and a match of needle found early,
d9d99f
+       - needle may be very long, and not even a short initial segment of
d9d99f
+       needle may be found in haystack.  */
d9d99f
+  if (*needle != '\0')
d9d99f
+    {
d9d99f
+      /* Speed up the following searches of needle by caching its first
d9d99f
+	 character.  */
d9d99f
+      char b = *needle++;
d9d99f
+
d9d99f
+      for (;; haystack++)
d9d99f
+	{
d9d99f
+	  if (*haystack == '\0')
d9d99f
+	    /* No match.  */
d9d99f
+	    return 0;
d9d99f
+	  if (grub_tolower(*haystack) == grub_tolower(b))
d9d99f
+	    /* The first character matches.  */
d9d99f
+	    {
d9d99f
+	      const char *rhaystack = haystack + 1;
d9d99f
+	      const char *rneedle = needle;
d9d99f
+
d9d99f
+	      for (;; rhaystack++, rneedle++)
d9d99f
+		{
d9d99f
+		  if (*rneedle == '\0')
d9d99f
+		    /* Found a match.  */
d9d99f
+		    return (char *) haystack;
d9d99f
+		  if (*rhaystack == '\0')
d9d99f
+		    /* No match.  */
d9d99f
+		    return 0;
d9d99f
+		  if (grub_tolower(*rhaystack) != grub_tolower(*rneedle))
d9d99f
+		    /* Nothing in this round.  */
d9d99f
+		    break;
d9d99f
+		}
d9d99f
+	    }
d9d99f
+	}
d9d99f
+    }
d9d99f
+  else
d9d99f
+    return (char *) haystack;
d9d99f
+}
d9d99f
+
d9d99f
 static inline int
d9d99f
 grub_strcasecmp (const char *s1, const char *s2)
d9d99f
 {
d9d99f
diff --git a/include/grub/net/url.h b/include/grub/net/url.h
d9d99f
new file mode 100644
d9d99f
index 00000000000..a215fa27d0a
d9d99f
--- /dev/null
d9d99f
+++ b/include/grub/net/url.h
d9d99f
@@ -0,0 +1,28 @@
d9d99f
+/* url.h - prototypes for url parsing functions */
d9d99f
+/*
d9d99f
+ *  GRUB  --  GRand Unified Bootloader
d9d99f
+ *  Copyright (C) 2016  Free Software Foundation, Inc.
d9d99f
+ *
d9d99f
+ *  GRUB is free software: you can redistribute it and/or modify
d9d99f
+ *  it under the terms of the GNU General Public License as published by
d9d99f
+ *  the Free Software Foundation, either version 3 of the License, or
d9d99f
+ *  (at your option) any later version.
d9d99f
+ *
d9d99f
+ *  GRUB is distributed in the hope that it will be useful,
d9d99f
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
d9d99f
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d9d99f
+ *  GNU General Public License for more details.
d9d99f
+ *
d9d99f
+ *  You should have received a copy of the GNU General Public License
d9d99f
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
d9d99f
+ */
d9d99f
+
d9d99f
+#ifndef GRUB_URL_HEADER
d9d99f
+#define GRUB_URL_HEADER	1
d9d99f
+
d9d99f
+int
d9d99f
+EXPORT_FUNC(extract_url_info) (const char *urlbuf, grub_size_t buflen,
d9d99f
+			       char **scheme, char **userinfo,
d9d99f
+			       char **host, int *port, char **file);
d9d99f
+
d9d99f
+#endif /* GRUB_URL_HEADER */