2 * Copyright (C) 2009 Sony Computer Entertainment Inc.
3 * Copyright 2009 Sony Corp.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #if defined(HAVE_CONFIG_H)
28 #include "talloc/talloc.h"
32 * pb_scheme_info - Helper for parsing URLs.
35 struct pb_scheme_info {
36 enum pb_url_scheme scheme;
42 * pb_url_find_scheme - Find the pb_scheme_info for a URL string.
45 static const struct pb_scheme_info *pb_url_find_scheme(const char *url_str)
47 static const struct pb_scheme_info a[] = {
49 .scheme = pb_url_file,
51 .str_len = sizeof("file://") - 1,
56 .str_len = sizeof("ftp://") - 1,
59 .scheme = pb_url_http,
61 .str_len = sizeof("http://") - 1,
64 .scheme = pb_url_https,
66 .str_len = sizeof("https://") - 1,
71 .str_len = sizeof("nfs://") - 1,
74 .scheme = pb_url_sftp,
76 .str_len = sizeof("sftp://") - 1,
79 .scheme = pb_url_tftp,
81 .str_len = sizeof("tftp://") - 1,
84 static const struct pb_scheme_info file_scheme = {
86 .scheme = pb_url_file,
90 for (i = 0; i < sizeof(a) / sizeof(a[0]); i++)
91 if (!strncasecmp(url_str, a[i].str, a[i].str_len))
94 /* Assume this is a non-url local file. */
100 * pb_url_parse - Parse a remote file URL.
101 * @ctx: The talloc context to associate with the returned string.
103 * Returns a talloc'ed struct pb_url instance on success, or NULL on error.
106 struct pb_url *pb_url_parse(void *ctx, const char *url_str)
108 const struct pb_scheme_info *si;
112 pb_log("%s: '%s'\n", __func__, url_str);
114 if (!url_str || !*url_str) {
115 assert(0 && "bad url");
119 url = talloc_zero(ctx, struct pb_url);
124 si = pb_url_find_scheme(url_str);
126 url->scheme = si->scheme;
127 p = url_str + si->str_len;
129 url->full = talloc_strdup(url, url_str);
131 if (url->scheme == pb_url_file) {
134 url->path = talloc_strdup(url, p);
140 path = strchr(p, '/');
143 pb_log("%s: parse path failed '%s'\n", __func__ , p);
147 col = strchr(p, ':');
150 len = path - col - 1;
151 url->port = len ? talloc_strndup(url, col + 1, len)
154 url->host = len ? talloc_strndup(url, p, len) : NULL;
157 url->host = talloc_strndup(url, p, path - p);
159 url->path = talloc_strdup(url, path);
162 p = strrchr(url->path, '/');
166 url->dir = talloc_strndup(url, url->path, p - url->path);
167 url->file = talloc_strdup(url, p);
170 url->file = talloc_strdup(url, url->path);
173 pb_log(" scheme %d\n", url->scheme);
174 pb_log(" host '%s'\n", url->host);
175 pb_log(" port '%s'\n", url->port);
176 pb_log(" path '%s'\n", url->path);
177 pb_log(" dir '%s'\n", url->dir);
178 pb_log(" file '%s'\n", url->file);