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)
27 #include "talloc/talloc.h"
31 * pb_scheme_info - Helper for parsing URLs.
34 struct pb_scheme_info {
35 enum pb_url_scheme scheme;
41 static const struct pb_scheme_info schemes[] = {
43 .scheme = pb_url_file,
45 .str_len = sizeof("file") - 1,
51 .str_len = sizeof("ftp") - 1,
55 .scheme = pb_url_http,
57 .str_len = sizeof("http") - 1,
61 .scheme = pb_url_https,
63 .str_len = sizeof("https") - 1,
69 .str_len = sizeof("nfs") - 1,
73 .scheme = pb_url_sftp,
75 .str_len = sizeof("sftp") - 1,
79 .scheme = pb_url_tftp,
81 .str_len = sizeof("tftp") - 1,
86 static const struct pb_scheme_info *file_scheme = &schemes[0];
89 * pb_url_find_scheme - Find the pb_scheme_info for a URL string.
92 static const struct pb_scheme_info *pb_url_scheme_info(
93 enum pb_url_scheme scheme)
97 for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++) {
98 const struct pb_scheme_info *info = &schemes[i];
100 if (info->scheme == scheme)
107 static const struct pb_scheme_info *pb_url_find_scheme(const char *url)
109 static const int sep_len = sizeof("://") - 1;
110 static const char *sep = "://";
111 unsigned int i, url_len;
113 url_len = strlen(url);
115 for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++) {
116 const struct pb_scheme_info *scheme = &schemes[i];
118 if (url_len < scheme->str_len + sep_len)
121 if (strncmp(url + scheme->str_len, sep, sep_len))
124 if (strncasecmp(url, scheme->str, scheme->str_len))
133 static void pb_url_parse_path(struct pb_url *url)
135 const char *p = strrchr(url->path, '/');
137 talloc_free(url->dir);
138 talloc_free(url->file);
142 url->dir = talloc_strndup(url, url->path, p - url->path);
143 url->file = talloc_strdup(url, p);
146 url->file = talloc_strdup(url, url->path);
151 * pb_url_parse - Parse a remote file URL.
152 * @ctx: The talloc context to associate with the returned string.
154 * Returns a talloc'ed struct pb_url instance on success, or NULL on error.
157 struct pb_url *pb_url_parse(void *ctx, const char *url_str)
159 const struct pb_scheme_info *si;
163 if (!url_str || !*url_str) {
164 assert(0 && "bad url");
168 url = talloc_zero(ctx, struct pb_url);
173 si = pb_url_find_scheme(url_str);
175 url->scheme = si->scheme;
176 p = url_str + si->str_len + strlen("://");
178 url->scheme = file_scheme->scheme;
182 url->full = talloc_strdup(url, url_str);
184 if (url->scheme == pb_url_file) {
187 url->path = talloc_strdup(url, p);
193 path = strchr(p, '/');
196 pb_log_fn("parse path failed '%s'\n" , p);
200 col = strrchr(p, ':');
203 if (col && strchr(col , ']')) {
205 * This is likely an IPv6 address with no port.
206 * See https://www.ietf.org/rfc/rfc2732.txt
212 len = path - col - 1;
213 url->port = len ? talloc_strndup(url, col + 1, len)
221 if (p[0] == '[' && p[len - 1] == ']')
223 url->host = talloc_strndup(url, p + 1, len - 2);
226 url->host = talloc_strndup(url, p, len);
229 /* remove multiple leading slashes */
230 for (; *path && *(path+1) == '/'; path++)
233 url->path = talloc_strdup(url, path);
236 pb_url_parse_path(url);
245 bool is_url(const char *str)
247 return strstr(str, "://") != NULL;
250 int addr_scheme(const char *address)
252 uint8_t buf[sizeof(struct in6_addr)];
255 rc = inet_pton(AF_INET, address , buf);
258 rc = inet_pton(AF_INET6, address , buf);
265 char *pb_url_to_string(struct pb_url *url)
267 const struct pb_scheme_info *scheme = pb_url_scheme_info(url->scheme);
271 port = url->port ? talloc_asprintf(url, ":%s", url->port) : NULL;
273 if (scheme->has_host && addr_scheme(url->host) == AF_INET6)
274 str = talloc_asprintf(url, "%s://[%s]%s%s", scheme->str,
275 url->host, port ?: "", url->path);
277 str = talloc_asprintf(url, "%s://%s%s%s", scheme->str,
278 scheme->has_host ? url->host : "",
279 port ?: "", url->path);
285 static void pb_url_update_full(struct pb_url *url)
287 talloc_free(url->full);
288 url->full = pb_url_to_string(url);
291 struct pb_url *pb_url_copy(void *ctx, const struct pb_url *url)
293 struct pb_url *new_url;
295 new_url = talloc(ctx, struct pb_url);
296 new_url->scheme = url->scheme;
297 new_url->full = talloc_strdup(new_url, url->full);
299 new_url->host = url->host ? talloc_strdup(new_url, url->host) : NULL;
300 new_url->port = url->port ? talloc_strdup(new_url, url->port) : NULL;
301 new_url->path = url->path ? talloc_strdup(new_url, url->path) : NULL;
302 new_url->dir = url->dir ? talloc_strdup(new_url, url->dir) : NULL;
303 new_url->file = url->file ? talloc_strdup(new_url, url->file) : NULL;
308 struct pb_url *pb_url_join(void *ctx, const struct pb_url *url, const char *s)
310 struct pb_url *new_url;
312 /* complete url: just parse all info from s */
314 return pb_url_parse(ctx, s);
316 new_url = pb_url_copy(ctx, url);
319 /* absolute path: replace path of new_url */
320 talloc_free(new_url->path);
321 new_url->path = talloc_strdup(new_url, s);
324 /* relative path: join s to existing path. We know that
325 * url->dir ends with a slash. */
326 char *tmp = new_url->path;
327 new_url->path = talloc_asprintf(new_url, "%s%s", url->dir, s);
331 /* replace ->dir and ->file with components from ->path */
332 pb_url_parse_path(new_url);
334 /* and re-generate the full URL */
335 pb_url_update_full(new_url);
340 const char *pb_url_scheme_name(enum pb_url_scheme scheme)
342 const struct pb_scheme_info *info = pb_url_scheme_info(scheme);
343 return info ? info->str : NULL;