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;
41 static const struct pb_scheme_info schemes[] = {
43 .scheme = pb_url_file,
45 .str_len = sizeof("file") - 1,
50 .str_len = sizeof("ftp") - 1,
53 .scheme = pb_url_http,
55 .str_len = sizeof("http") - 1,
58 .scheme = pb_url_https,
60 .str_len = sizeof("https") - 1,
65 .str_len = sizeof("nfs") - 1,
68 .scheme = pb_url_sftp,
70 .str_len = sizeof("sftp") - 1,
73 .scheme = pb_url_tftp,
75 .str_len = sizeof("tftp") - 1,
79 static const struct pb_scheme_info *file_scheme = &schemes[0];
82 * pb_url_find_scheme - Find the pb_scheme_info for a URL string.
85 static const struct pb_scheme_info *pb_url_find_scheme(const char *url)
87 static const int sep_len = sizeof("://") - 1;
88 static const char *sep = "://";
89 unsigned int i, url_len;
91 url_len = strlen(url);
93 for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++) {
94 const struct pb_scheme_info *scheme = &schemes[i];
96 if (url_len < scheme->str_len + sep_len)
99 if (strncmp(url + scheme->str_len, sep, sep_len))
102 if (strncasecmp(url, scheme->str, scheme->str_len))
108 /* Assume this is a non-url local file. */
114 * pb_url_parse - Parse a remote file URL.
115 * @ctx: The talloc context to associate with the returned string.
117 * Returns a talloc'ed struct pb_url instance on success, or NULL on error.
120 struct pb_url *pb_url_parse(void *ctx, const char *url_str)
122 const struct pb_scheme_info *si;
126 pb_log("%s: '%s'\n", __func__, url_str);
128 if (!url_str || !*url_str) {
129 assert(0 && "bad url");
133 url = talloc_zero(ctx, struct pb_url);
138 si = pb_url_find_scheme(url_str);
140 url->scheme = si->scheme;
141 p = url_str + si->str_len + strlen("://");
143 url->full = talloc_strdup(url, url_str);
145 if (url->scheme == pb_url_file) {
148 url->path = talloc_strdup(url, p);
154 path = strchr(p, '/');
157 pb_log("%s: parse path failed '%s'\n", __func__ , p);
161 col = strchr(p, ':');
164 len = path - col - 1;
165 url->port = len ? talloc_strndup(url, col + 1, len)
168 url->host = len ? talloc_strndup(url, p, len) : NULL;
171 url->host = talloc_strndup(url, p, path - p);
173 url->path = talloc_strdup(url, path);
176 p = strrchr(url->path, '/');
180 url->dir = talloc_strndup(url, url->path, p - url->path);
181 url->file = talloc_strdup(url, p);
184 url->file = talloc_strdup(url, url->path);
187 pb_log(" scheme %d\n", url->scheme);
188 pb_log(" host '%s'\n", url->host);
189 pb_log(" port '%s'\n", url->port);
190 pb_log(" path '%s'\n", url->path);
191 pb_log(" dir '%s'\n", url->dir);
192 pb_log(" file '%s'\n", url->file);
201 const char *pb_url_scheme_name(enum pb_url_scheme scheme)
205 for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++)
206 if (schemes[i].scheme == scheme)
207 return schemes[i].str;