]> git.ozlabs.org Git - petitboot/blob - ui/common/url.c
ui/common/url: move pb_scheme_info array to file scope
[petitboot] / ui / common / url.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
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.
8  *
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.
13  *
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
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #define _GNU_SOURCE
24 #include <assert.h>
25 #include <string.h>
26
27 #include "log/log.h"
28 #include "talloc/talloc.h"
29 #include "url.h"
30
31 /**
32  * pb_scheme_info - Helper for parsing URLs.
33  */
34
35 struct pb_scheme_info {
36         enum pb_url_scheme scheme;
37         const char *str;
38         unsigned int str_len;
39 };
40
41 static const struct pb_scheme_info schemes[] = {
42         {
43                 .scheme = pb_url_file,
44                 .str = "file://",
45                 .str_len = sizeof("file://") - 1,
46         },
47         {
48                 .scheme = pb_url_ftp,
49                 .str = "ftp://",
50                 .str_len = sizeof("ftp://") - 1,
51         },
52         {
53                 .scheme = pb_url_http,
54                 .str = "http://",
55                 .str_len = sizeof("http://") - 1,
56         },
57         {
58                 .scheme = pb_url_https,
59                 .str = "https://",
60                 .str_len = sizeof("https://") - 1,
61         },
62         {
63                 .scheme = pb_url_nfs,
64                 .str = "nfs://",
65                 .str_len = sizeof("nfs://") - 1,
66         },
67         {
68                 .scheme = pb_url_sftp,
69                 .str = "sftp://",
70                 .str_len = sizeof("sftp://") - 1,
71         },
72         {
73                 .scheme = pb_url_tftp,
74                 .str = "tftp://",
75                 .str_len = sizeof("tftp://") - 1,
76         },
77 };
78
79 static const struct pb_scheme_info *file_scheme = &schemes[0];
80
81 /**
82  * pb_url_find_scheme - Find the pb_scheme_info for a URL string.
83  */
84
85 static const struct pb_scheme_info *pb_url_find_scheme(const char *url_str)
86 {
87         unsigned int i;
88
89         for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++)
90                 if (!strncasecmp(url_str, schemes[i].str, schemes[i].str_len))
91                         return &schemes[i];
92
93         /* Assume this is a non-url local file. */
94
95         return file_scheme;
96 }
97
98 /**
99  * pb_url_parse - Parse a remote file URL.
100  * @ctx: The talloc context to associate with the returned string.
101  *
102  * Returns a talloc'ed struct pb_url instance on success, or NULL on error.
103  */
104
105 struct pb_url *pb_url_parse(void *ctx, const char *url_str)
106 {
107         const struct pb_scheme_info *si;
108         struct pb_url *url;
109         const char *p;
110
111         pb_log("%s: '%s'\n", __func__, url_str);
112
113         if (!url_str || !*url_str) {
114                 assert(0 && "bad url");
115                 return NULL;
116         }
117
118         url = talloc_zero(ctx, struct pb_url);
119
120         if (!url)
121                 return NULL;
122
123         si = pb_url_find_scheme(url_str);
124
125         url->scheme = si->scheme;
126         p = url_str + si->str_len;
127
128         url->full = talloc_strdup(url, url_str);
129
130         if (url->scheme == pb_url_file) {
131                 url->port = NULL;
132                 url->host = NULL;
133                 url->path = talloc_strdup(url, p);
134         } else {
135                 int len;
136                 const char *col;
137                 const char *path;
138
139                 path = strchr(p, '/');
140
141                 if (!path) {
142                         pb_log("%s: parse path failed '%s'\n", __func__ , p);
143                         goto fail;
144                 }
145
146                 col = strchr(p, ':');
147
148                 if (col) {
149                         len = path - col - 1;
150                         url->port = len ? talloc_strndup(url, col + 1, len)
151                                 : NULL;
152                         len = col - p;
153                         url->host = len ? talloc_strndup(url, p, len) : NULL;
154                 } else {
155                         url->port = NULL;
156                         url->host = talloc_strndup(url, p, path - p);
157                 }
158                 url->path = talloc_strdup(url, path);
159         }
160
161         p = strrchr(url->path, '/');
162
163         if (p) {
164                 p++;
165                 url->dir = talloc_strndup(url, url->path, p - url->path);
166                 url->file = talloc_strdup(url, p);
167         } else {
168                 url->dir = NULL;
169                 url->file = talloc_strdup(url, url->path);
170         }
171
172         pb_log(" scheme %d\n", url->scheme);
173         pb_log(" host '%s'\n", url->host);
174         pb_log(" port '%s'\n", url->port);
175         pb_log(" path '%s'\n", url->path);
176         pb_log(" dir '%s'\n", url->dir);
177         pb_log(" file '%s'\n", url->file);
178
179         return url;
180
181 fail:
182         talloc_free(url);
183         return NULL;
184 }