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 static const struct pb_scheme_info schemes[] = {
44 .scheme = pb_url_file,
46 .str_len = sizeof("file") - 1,
52 .str_len = sizeof("ftp") - 1,
56 .scheme = pb_url_http,
58 .str_len = sizeof("http") - 1,
62 .scheme = pb_url_https,
64 .str_len = sizeof("https") - 1,
70 .str_len = sizeof("nfs") - 1,
74 .scheme = pb_url_sftp,
76 .str_len = sizeof("sftp") - 1,
80 .scheme = pb_url_tftp,
82 .str_len = sizeof("tftp") - 1,
87 static const struct pb_scheme_info *file_scheme = &schemes[0];
90 * pb_url_find_scheme - Find the pb_scheme_info for a URL string.
93 static const struct pb_scheme_info *pb_url_scheme_info(
94 enum pb_url_scheme scheme)
98 for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++) {
99 const struct pb_scheme_info *info = &schemes[i];
101 if (info->scheme == scheme)
108 static const struct pb_scheme_info *pb_url_find_scheme(const char *url)
110 static const int sep_len = sizeof("://") - 1;
111 static const char *sep = "://";
112 unsigned int i, url_len;
114 url_len = strlen(url);
116 for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++) {
117 const struct pb_scheme_info *scheme = &schemes[i];
119 if (url_len < scheme->str_len + sep_len)
122 if (strncmp(url + scheme->str_len, sep, sep_len))
125 if (strncasecmp(url, scheme->str, scheme->str_len))
134 static void pb_url_parse_path(struct pb_url *url)
136 const char *p = strrchr(url->path, '/');
138 talloc_free(url->dir);
139 talloc_free(url->file);
143 url->dir = talloc_strndup(url, url->path, p - url->path);
144 url->file = talloc_strdup(url, p);
147 url->file = talloc_strdup(url, url->path);
152 * pb_url_parse - Parse a remote file URL.
153 * @ctx: The talloc context to associate with the returned string.
155 * Returns a talloc'ed struct pb_url instance on success, or NULL on error.
158 struct pb_url *pb_url_parse(void *ctx, const char *url_str)
160 const struct pb_scheme_info *si;
164 if (!url_str || !*url_str) {
165 assert(0 && "bad url");
169 url = talloc_zero(ctx, struct pb_url);
174 si = pb_url_find_scheme(url_str);
176 url->scheme = si->scheme;
177 p = url_str + si->str_len + strlen("://");
179 url->scheme = file_scheme->scheme;
183 url->full = talloc_strdup(url, url_str);
185 if (url->scheme == pb_url_file) {
188 url->path = talloc_strdup(url, p);
194 path = strchr(p, '/');
197 pb_log("%s: parse path failed '%s'\n", __func__ , p);
201 col = strchr(p, ':');
204 len = path - col - 1;
205 url->port = len ? talloc_strndup(url, col + 1, len)
208 url->host = len ? talloc_strndup(url, p, len) : NULL;
211 url->host = talloc_strndup(url, p, path - p);
214 /* remove multiple leading slashes */
215 for (; *path && *(path+1) == '/'; path++)
218 url->path = talloc_strdup(url, path);
221 pb_url_parse_path(url);
230 bool is_url(const char *str)
232 return strstr(str, "://") != NULL;
235 char *pb_url_to_string(struct pb_url *url)
237 const struct pb_scheme_info *scheme = pb_url_scheme_info(url->scheme);
240 return talloc_asprintf(url, "%s://%s%s", scheme->str,
241 scheme->has_host ? url->host : "", url->path);
244 static void pb_url_update_full(struct pb_url *url)
246 talloc_free(url->full);
247 url->full = pb_url_to_string(url);
250 static struct pb_url *pb_url_copy(void *ctx, const struct pb_url *url)
252 struct pb_url *new_url;
254 new_url = talloc(ctx, struct pb_url);
255 new_url->scheme = url->scheme;
256 new_url->full = talloc_strdup(new_url, url->full);
258 new_url->host = url->host ? talloc_strdup(new_url, url->host) : NULL;
259 new_url->port = url->port ? talloc_strdup(new_url, url->port) : NULL;
260 new_url->path = url->path ? talloc_strdup(new_url, url->path) : NULL;
261 new_url->dir = url->dir ? talloc_strdup(new_url, url->dir) : NULL;
262 new_url->file = url->file ? talloc_strdup(new_url, url->file) : NULL;
267 struct pb_url *pb_url_join(void *ctx, const struct pb_url *url, const char *s)
269 struct pb_url *new_url;
271 /* complete url: just parse all info from s */
273 return pb_url_parse(ctx, s);
275 new_url = pb_url_copy(ctx, url);
278 /* absolute path: replace path of new_url */
279 talloc_free(new_url->path);
280 new_url->path = talloc_strdup(new_url, s);
283 /* relative path: join s to existing path. We know that
284 * url->dir ends with a slash. */
285 char *tmp = new_url->path;
286 new_url->path = talloc_asprintf(new_url, "%s%s", url->dir, s);
290 /* replace ->dir and ->file with components from ->path */
291 pb_url_parse_path(new_url);
293 /* and re-generate the full URL */
294 pb_url_update_full(new_url);
299 const char *pb_url_scheme_name(enum pb_url_scheme scheme)
301 const struct pb_scheme_info *info = pb_url_scheme_info(scheme);
302 return info ? info->str : NULL;