X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=ui%2Fcommon%2Furl.c;h=4e4b9610cd96f1016a6d38aca78e258f5a97452c;hp=544eee2091a171f688907d6b6740c1ac350c9f93;hb=1b0b59295d0500764c5096753f7cd11bf3ab5df4;hpb=d29c9620cd853d9ead31075f237f53b9e6301a52 diff --git a/ui/common/url.c b/ui/common/url.c index 544eee2..4e4b961 100644 --- a/ui/common/url.c +++ b/ui/common/url.c @@ -38,62 +38,76 @@ struct pb_scheme_info { unsigned int str_len; }; -/** - * 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_str) -{ - static const struct pb_scheme_info a[] = { +static const struct pb_scheme_info schemes[] = { { .scheme = pb_url_file, - .str = "file://", - .str_len = sizeof("file://") - 1, + .str = "file", + .str_len = sizeof("file") - 1, }, { .scheme = pb_url_ftp, - .str = "ftp://", - .str_len = sizeof("ftp://") - 1, + .str = "ftp", + .str_len = sizeof("ftp") - 1, }, { .scheme = pb_url_http, - .str = "http://", - .str_len = sizeof("http://") - 1, + .str = "http", + .str_len = sizeof("http") - 1, }, { .scheme = pb_url_https, - .str = "https://", - .str_len = sizeof("https://") - 1, + .str = "https", + .str_len = sizeof("https") - 1, }, { .scheme = pb_url_nfs, - .str = "nfs://", - .str_len = sizeof("nfs://") - 1, + .str = "nfs", + .str_len = sizeof("nfs") - 1, }, { .scheme = pb_url_sftp, - .str = "sftp://", - .str_len = sizeof("sftp://") - 1, + .str = "sftp", + .str_len = sizeof("sftp") - 1, }, { .scheme = pb_url_tftp, - .str = "tftp://", - .str_len = sizeof("tftp://") - 1, + .str = "tftp", + .str_len = sizeof("tftp") - 1, }, - }; - static const struct pb_scheme_info file_scheme = { - .str = "", - .scheme = pb_url_file, - }; - unsigned int i; +}; + +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; - for (i = 0; i < sizeof(a) / sizeof(a[0]); i++) - if (!strncasecmp(url_str, a[i].str, a[i].str_len)) - return &a[i]; + 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; + return file_scheme; } /** @@ -124,7 +138,7 @@ struct pb_url *pb_url_parse(void *ctx, const char *url_str) si = pb_url_find_scheme(url_str); url->scheme = si->scheme; - p = url_str + si->str_len; + p = url_str + si->str_len + strlen("://"); url->full = talloc_strdup(url, url_str); @@ -156,6 +170,11 @@ struct pb_url *pb_url_parse(void *ctx, const char *url_str) 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); } @@ -183,3 +202,14 @@ 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; +}