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

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