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

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