]> git.ozlabs.org Git - petitboot/commitdiff
lib/url: Move URL-handling code to lib
authorJeremy Kerr <jk@ozlabs.org>
Tue, 5 Mar 2013 07:28:14 +0000 (15:28 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 15 Apr 2013 07:42:26 +0000 (15:42 +0800)
We'll need to use the URL handling code in the server, so move it to the
lib/ directory.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
lib/Makefile.am
lib/url/url.c [new file with mode: 0644]
lib/url/url.h [new file with mode: 0644]
test/urls/Makefile.am
test/urls/parse-url.c
ui/common/Makefile.am
ui/common/loader.c
ui/common/url.c [deleted file]
ui/common/url.h [deleted file]

index 7847d0968a48576da199a9ce8369faafcc8d83af..cce05fe6b3b218ab505840c3afe48a4f21936b13 100644 (file)
@@ -32,6 +32,8 @@ libpbcore_la_SOURCES = \
        talloc/talloc.c \
        talloc/talloc.h \
        system/system.c \
        talloc/talloc.c \
        talloc/talloc.h \
        system/system.c \
-       system/system.h
+       system/system.h \
+       url/url.c \
+       url/url.h
 
 MAINTAINERCLEANFILES = Makefile.in
 
 MAINTAINERCLEANFILES = Makefile.in
diff --git a/lib/url/url.c b/lib/url/url.c
new file mode 100644 (file)
index 0000000..4e4b961
--- /dev/null
@@ -0,0 +1,215 @@
+/*
+ *  Copyright (C) 2009 Sony Computer Entertainment Inc.
+ *  Copyright 2009 Sony Corp.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; version 2 of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#define _GNU_SOURCE
+#include <assert.h>
+#include <string.h>
+
+#include "log/log.h"
+#include "talloc/talloc.h"
+#include "url.h"
+
+/**
+ * pb_scheme_info - Helper for parsing URLs.
+ */
+
+struct pb_scheme_info {
+       enum pb_url_scheme scheme;
+       const char *str;
+       unsigned int str_len;
+};
+
+static const struct pb_scheme_info schemes[] = {
+       {
+               .scheme = pb_url_file,
+               .str = "file",
+               .str_len = sizeof("file") - 1,
+       },
+       {
+               .scheme = pb_url_ftp,
+               .str = "ftp",
+               .str_len = sizeof("ftp") - 1,
+       },
+       {
+               .scheme = pb_url_http,
+               .str = "http",
+               .str_len = sizeof("http") - 1,
+       },
+       {
+               .scheme = pb_url_https,
+               .str = "https",
+               .str_len = sizeof("https") - 1,
+       },
+       {
+               .scheme = pb_url_nfs,
+               .str = "nfs",
+               .str_len = sizeof("nfs") - 1,
+       },
+       {
+               .scheme = pb_url_sftp,
+               .str = "sftp",
+               .str_len = sizeof("sftp") - 1,
+       },
+       {
+               .scheme = pb_url_tftp,
+               .str = "tftp",
+               .str_len = sizeof("tftp") - 1,
+       },
+};
+
+static const struct pb_scheme_info *file_scheme = &schemes[0];
+
+/**
+ * pb_url_find_scheme - Find the pb_scheme_info for a URL string.
+ */
+
+static const struct pb_scheme_info *pb_url_find_scheme(const char *url)
+{
+       static const int sep_len = sizeof("://") - 1;
+       static const char *sep = "://";
+       unsigned int i, url_len;
+
+       url_len = strlen(url);
+
+       for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++) {
+               const struct pb_scheme_info *scheme = &schemes[i];
+
+               if (url_len < scheme->str_len + sep_len)
+                       continue;
+
+               if (strncmp(url + scheme->str_len, sep, sep_len))
+                       continue;
+
+               if (strncasecmp(url, scheme->str, scheme->str_len))
+                       continue;
+
+               return scheme;
+       }
+
+       /* Assume this is a non-url local file. */
+
+       return file_scheme;
+}
+
+/**
+ * pb_url_parse - Parse a remote file URL.
+ * @ctx: The talloc context to associate with the returned string.
+ *
+ * Returns a talloc'ed struct pb_url instance on success, or NULL on error.
+ */
+
+struct pb_url *pb_url_parse(void *ctx, const char *url_str)
+{
+       const struct pb_scheme_info *si;
+       struct pb_url *url;
+       const char *p;
+
+       pb_log("%s: '%s'\n", __func__, url_str);
+
+       if (!url_str || !*url_str) {
+               assert(0 && "bad url");
+               return NULL;
+       }
+
+       url = talloc_zero(ctx, struct pb_url);
+
+       if (!url)
+               return NULL;
+
+       si = pb_url_find_scheme(url_str);
+
+       url->scheme = si->scheme;
+       p = url_str + si->str_len + strlen("://");
+
+       url->full = talloc_strdup(url, url_str);
+
+       if (url->scheme == pb_url_file) {
+               url->port = NULL;
+               url->host = NULL;
+               url->path = talloc_strdup(url, p);
+       } else {
+               int len;
+               const char *col;
+               const char *path;
+
+               path = strchr(p, '/');
+
+               if (!path) {
+                       pb_log("%s: parse path failed '%s'\n", __func__ , p);
+                       goto fail;
+               }
+
+               col = strchr(p, ':');
+
+               if (col) {
+                       len = path - col - 1;
+                       url->port = len ? talloc_strndup(url, col + 1, len)
+                               : NULL;
+                       len = col - p;
+                       url->host = len ? talloc_strndup(url, p, len) : NULL;
+               } else {
+                       url->port = NULL;
+                       url->host = talloc_strndup(url, p, path - p);
+               }
+
+               /* remove multiple leading slashes */
+               for (; *path && *(path+1) == '/'; path++)
+                       ;
+
+               url->path = talloc_strdup(url, path);
+       }
+
+       p = strrchr(url->path, '/');
+
+       if (p) {
+               p++;
+               url->dir = talloc_strndup(url, url->path, p - url->path);
+               url->file = talloc_strdup(url, p);
+       } else {
+               url->dir = NULL;
+               url->file = talloc_strdup(url, url->path);
+       }
+
+       pb_log(" scheme %d\n", url->scheme);
+       pb_log(" host '%s'\n", url->host);
+       pb_log(" port '%s'\n", url->port);
+       pb_log(" path '%s'\n", url->path);
+       pb_log(" dir '%s'\n", url->dir);
+       pb_log(" file '%s'\n", url->file);
+
+       return url;
+
+fail:
+       talloc_free(url);
+       return NULL;
+}
+
+const char *pb_url_scheme_name(enum pb_url_scheme scheme)
+{
+       unsigned int i;
+
+       for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++)
+               if (schemes[i].scheme == scheme)
+                       return schemes[i].str;
+
+       return NULL;
+}
diff --git a/lib/url/url.h b/lib/url/url.h
new file mode 100644 (file)
index 0000000..3cb7cd8
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ *  Copyright (C) 2009 Sony Computer Entertainment Inc.
+ *  Copyright 2009 Sony Corp.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; version 2 of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#if !defined(_PB_URL_PARSER_H)
+#define _PB_URL_PARSER_H
+
+/**
+ * enum pb_url_scheme - Enumeration of the URL schemes we can handle.
+ */
+
+enum pb_url_scheme {
+       pb_url_unknown = 0,
+       pb_url_file,
+       pb_url_ftp,
+       pb_url_http,
+       pb_url_https,
+       pb_url_nfs,
+       pb_url_sftp,
+       pb_url_tftp,
+};
+
+/**
+ * struct pb_url - Parsed URL info.
+ * @scheme: The enum pb_url_scheme for this URL.
+ * @full: The full URL = scheme://host:port/path.
+ * @host: The host part of URL.
+ * @port: The port part of URL.
+ * @path: The path part of URL.
+ * @dir: The dir part of path.
+ * @file: The file part of path.
+ *
+ * For info on URL syntax see:
+ *  http://www.ietf.org/rfc/rfc3986.txt
+ */
+
+struct pb_url {
+       enum pb_url_scheme scheme;
+       char *full;
+       char *host;
+       char *port;
+       char *path;
+       char *dir;
+       char *file;
+};
+
+struct pb_url *pb_url_parse(void *ctx, const char *url_str);
+
+const char *pb_url_scheme_name(enum pb_url_scheme scheme);
+
+#endif
index 850c58f1abebef887890cda9fbf5f26128969023..d16ffdc21547a51b0db669a159626784d09da4ae 100644 (file)
@@ -22,10 +22,7 @@ AM_CPPFLAGS = \
 AM_CFLAGS = \
        $(DEFAULT_CFLAGS)
 
 AM_CFLAGS = \
        $(DEFAULT_CFLAGS)
 
-parse_url_SOURCES = \
-       parse-url.c \
-       ../../ui/common/url.c \
-       ../../ui/common/url.h
+parse_url_SOURCES = parse-url.c
 
 parse_url_LDADD = ../../lib/libpbcore.la
 
 
 parse_url_LDADD = ../../lib/libpbcore.la
 
index 3e2f10f0871c2be2aead6de56751942f239b0dfc..d748cdb2277d7d5b4fe834c76c40443a06fc1b6c 100644 (file)
@@ -2,7 +2,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 
 #include <stdlib.h>
 #include <stdio.h>
 
-#include <ui/common/url.h>
+#include <url/url.h>
 #include <log/log.h>
 
 int main(int argc, char **argv)
 #include <log/log.h>
 
 int main(int argc, char **argv)
index 284bf36ab9d551ff4e48303743ab9bad76f1b299..9e8d3eabc5397074a7bf5e31bf30f6ebf49b09ce 100644 (file)
@@ -33,9 +33,7 @@ libpbui_la_SOURCES = \
        timer.c \
        timer.h \
        ui-system.c \
        timer.c \
        timer.h \
        ui-system.c \
-       ui-system.h \
-       url.c \
-       url.h
+       ui-system.h
 
 if ENABLE_PS3
 libpbui_la_SOURCES += \
 
 if ENABLE_PS3
 libpbui_la_SOURCES += \
index 74b3c76ed63ddb44a84e70662b414df7ad81a8b0..b3ae7659b327a88813d229c48e7bd5e2fb7698f5 100644 (file)
@@ -26,9 +26,9 @@
 
 #include "log/log.h"
 #include <system/system.h>
 
 #include "log/log.h"
 #include <system/system.h>
+#include <url/url.h>
 #include "talloc/talloc.h"
 #include "loader.h"
 #include "talloc/talloc.h"
 #include "loader.h"
-#include "url.h"
 
 
 /**
 
 
 /**
diff --git a/ui/common/url.c b/ui/common/url.c
deleted file mode 100644 (file)
index 4e4b961..0000000
+++ /dev/null
@@ -1,215 +0,0 @@
-/*
- *  Copyright (C) 2009 Sony Computer Entertainment Inc.
- *  Copyright 2009 Sony Corp.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; version 2 of the License.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#if defined(HAVE_CONFIG_H)
-#include "config.h"
-#endif
-
-#define _GNU_SOURCE
-#include <assert.h>
-#include <string.h>
-
-#include "log/log.h"
-#include "talloc/talloc.h"
-#include "url.h"
-
-/**
- * pb_scheme_info - Helper for parsing URLs.
- */
-
-struct pb_scheme_info {
-       enum pb_url_scheme scheme;
-       const char *str;
-       unsigned int str_len;
-};
-
-static const struct pb_scheme_info schemes[] = {
-       {
-               .scheme = pb_url_file,
-               .str = "file",
-               .str_len = sizeof("file") - 1,
-       },
-       {
-               .scheme = pb_url_ftp,
-               .str = "ftp",
-               .str_len = sizeof("ftp") - 1,
-       },
-       {
-               .scheme = pb_url_http,
-               .str = "http",
-               .str_len = sizeof("http") - 1,
-       },
-       {
-               .scheme = pb_url_https,
-               .str = "https",
-               .str_len = sizeof("https") - 1,
-       },
-       {
-               .scheme = pb_url_nfs,
-               .str = "nfs",
-               .str_len = sizeof("nfs") - 1,
-       },
-       {
-               .scheme = pb_url_sftp,
-               .str = "sftp",
-               .str_len = sizeof("sftp") - 1,
-       },
-       {
-               .scheme = pb_url_tftp,
-               .str = "tftp",
-               .str_len = sizeof("tftp") - 1,
-       },
-};
-
-static const struct pb_scheme_info *file_scheme = &schemes[0];
-
-/**
- * pb_url_find_scheme - Find the pb_scheme_info for a URL string.
- */
-
-static const struct pb_scheme_info *pb_url_find_scheme(const char *url)
-{
-       static const int sep_len = sizeof("://") - 1;
-       static const char *sep = "://";
-       unsigned int i, url_len;
-
-       url_len = strlen(url);
-
-       for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++) {
-               const struct pb_scheme_info *scheme = &schemes[i];
-
-               if (url_len < scheme->str_len + sep_len)
-                       continue;
-
-               if (strncmp(url + scheme->str_len, sep, sep_len))
-                       continue;
-
-               if (strncasecmp(url, scheme->str, scheme->str_len))
-                       continue;
-
-               return scheme;
-       }
-
-       /* Assume this is a non-url local file. */
-
-       return file_scheme;
-}
-
-/**
- * pb_url_parse - Parse a remote file URL.
- * @ctx: The talloc context to associate with the returned string.
- *
- * Returns a talloc'ed struct pb_url instance on success, or NULL on error.
- */
-
-struct pb_url *pb_url_parse(void *ctx, const char *url_str)
-{
-       const struct pb_scheme_info *si;
-       struct pb_url *url;
-       const char *p;
-
-       pb_log("%s: '%s'\n", __func__, url_str);
-
-       if (!url_str || !*url_str) {
-               assert(0 && "bad url");
-               return NULL;
-       }
-
-       url = talloc_zero(ctx, struct pb_url);
-
-       if (!url)
-               return NULL;
-
-       si = pb_url_find_scheme(url_str);
-
-       url->scheme = si->scheme;
-       p = url_str + si->str_len + strlen("://");
-
-       url->full = talloc_strdup(url, url_str);
-
-       if (url->scheme == pb_url_file) {
-               url->port = NULL;
-               url->host = NULL;
-               url->path = talloc_strdup(url, p);
-       } else {
-               int len;
-               const char *col;
-               const char *path;
-
-               path = strchr(p, '/');
-
-               if (!path) {
-                       pb_log("%s: parse path failed '%s'\n", __func__ , p);
-                       goto fail;
-               }
-
-               col = strchr(p, ':');
-
-               if (col) {
-                       len = path - col - 1;
-                       url->port = len ? talloc_strndup(url, col + 1, len)
-                               : NULL;
-                       len = col - p;
-                       url->host = len ? talloc_strndup(url, p, len) : NULL;
-               } else {
-                       url->port = NULL;
-                       url->host = talloc_strndup(url, p, path - p);
-               }
-
-               /* remove multiple leading slashes */
-               for (; *path && *(path+1) == '/'; path++)
-                       ;
-
-               url->path = talloc_strdup(url, path);
-       }
-
-       p = strrchr(url->path, '/');
-
-       if (p) {
-               p++;
-               url->dir = talloc_strndup(url, url->path, p - url->path);
-               url->file = talloc_strdup(url, p);
-       } else {
-               url->dir = NULL;
-               url->file = talloc_strdup(url, url->path);
-       }
-
-       pb_log(" scheme %d\n", url->scheme);
-       pb_log(" host '%s'\n", url->host);
-       pb_log(" port '%s'\n", url->port);
-       pb_log(" path '%s'\n", url->path);
-       pb_log(" dir '%s'\n", url->dir);
-       pb_log(" file '%s'\n", url->file);
-
-       return url;
-
-fail:
-       talloc_free(url);
-       return NULL;
-}
-
-const char *pb_url_scheme_name(enum pb_url_scheme scheme)
-{
-       unsigned int i;
-
-       for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++)
-               if (schemes[i].scheme == scheme)
-                       return schemes[i].str;
-
-       return NULL;
-}
diff --git a/ui/common/url.h b/ui/common/url.h
deleted file mode 100644 (file)
index 3cb7cd8..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- *  Copyright (C) 2009 Sony Computer Entertainment Inc.
- *  Copyright 2009 Sony Corp.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; version 2 of the License.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#if !defined(_PB_URL_PARSER_H)
-#define _PB_URL_PARSER_H
-
-/**
- * enum pb_url_scheme - Enumeration of the URL schemes we can handle.
- */
-
-enum pb_url_scheme {
-       pb_url_unknown = 0,
-       pb_url_file,
-       pb_url_ftp,
-       pb_url_http,
-       pb_url_https,
-       pb_url_nfs,
-       pb_url_sftp,
-       pb_url_tftp,
-};
-
-/**
- * struct pb_url - Parsed URL info.
- * @scheme: The enum pb_url_scheme for this URL.
- * @full: The full URL = scheme://host:port/path.
- * @host: The host part of URL.
- * @port: The port part of URL.
- * @path: The path part of URL.
- * @dir: The dir part of path.
- * @file: The file part of path.
- *
- * For info on URL syntax see:
- *  http://www.ietf.org/rfc/rfc3986.txt
- */
-
-struct pb_url {
-       enum pb_url_scheme scheme;
-       char *full;
-       char *host;
-       char *port;
-       char *path;
-       char *dir;
-       char *file;
-};
-
-struct pb_url *pb_url_parse(void *ctx, const char *url_str);
-
-const char *pb_url_scheme_name(enum pb_url_scheme scheme);
-
-#endif