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

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