]> git.ozlabs.org Git - petitboot/blob - lib/url/url.c
lib/pb-config: Implement config_set
[petitboot] / lib / url / 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         bool has_host;
40 };
41
42 static const struct pb_scheme_info schemes[] = {
43         {
44                 .scheme = pb_url_file,
45                 .str = "file",
46                 .str_len = sizeof("file") - 1,
47                 .has_host = false,
48         },
49         {
50                 .scheme = pb_url_ftp,
51                 .str = "ftp",
52                 .str_len = sizeof("ftp") - 1,
53                 .has_host = true,
54         },
55         {
56                 .scheme = pb_url_http,
57                 .str = "http",
58                 .str_len = sizeof("http") - 1,
59                 .has_host = true,
60         },
61         {
62                 .scheme = pb_url_https,
63                 .str = "https",
64                 .str_len = sizeof("https") - 1,
65                 .has_host = true,
66         },
67         {
68                 .scheme = pb_url_nfs,
69                 .str = "nfs",
70                 .str_len = sizeof("nfs") - 1,
71                 .has_host = true,
72         },
73         {
74                 .scheme = pb_url_sftp,
75                 .str = "sftp",
76                 .str_len = sizeof("sftp") - 1,
77                 .has_host = true,
78         },
79         {
80                 .scheme = pb_url_tftp,
81                 .str = "tftp",
82                 .str_len = sizeof("tftp") - 1,
83                 .has_host = true,
84         },
85 };
86
87 static const struct pb_scheme_info *file_scheme = &schemes[0];
88
89 /**
90  * pb_url_find_scheme - Find the pb_scheme_info for a URL string.
91  */
92
93 static const struct pb_scheme_info *pb_url_scheme_info(
94                 enum pb_url_scheme scheme)
95 {
96         unsigned int i;
97
98         for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++) {
99                 const struct pb_scheme_info *info = &schemes[i];
100
101                 if (info->scheme == scheme)
102                         return info;
103
104         }
105         return NULL;
106 }
107
108 static const struct pb_scheme_info *pb_url_find_scheme(const char *url)
109 {
110         static const int sep_len = sizeof("://") - 1;
111         static const char *sep = "://";
112         unsigned int i, url_len;
113
114         url_len = strlen(url);
115
116         for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++) {
117                 const struct pb_scheme_info *scheme = &schemes[i];
118
119                 if (url_len < scheme->str_len + sep_len)
120                         continue;
121
122                 if (strncmp(url + scheme->str_len, sep, sep_len))
123                         continue;
124
125                 if (strncasecmp(url, scheme->str, scheme->str_len))
126                         continue;
127
128                 return scheme;
129         }
130
131         return NULL;
132 }
133
134 static void pb_url_parse_path(struct pb_url *url)
135 {
136         const char *p = strrchr(url->path, '/');
137
138         talloc_free(url->dir);
139         talloc_free(url->file);
140
141         if (p) {
142                 p++;
143                 url->dir = talloc_strndup(url, url->path, p - url->path);
144                 url->file = talloc_strdup(url, p);
145         } else {
146                 url->dir = NULL;
147                 url->file = talloc_strdup(url, url->path);
148         }
149 }
150
151 /**
152  * pb_url_parse - Parse a remote file URL.
153  * @ctx: The talloc context to associate with the returned string.
154  *
155  * Returns a talloc'ed struct pb_url instance on success, or NULL on error.
156  */
157
158 struct pb_url *pb_url_parse(void *ctx, const char *url_str)
159 {
160         const struct pb_scheme_info *si;
161         struct pb_url *url;
162         const char *p;
163
164         if (!url_str || !*url_str) {
165                 assert(0 && "bad url");
166                 return NULL;
167         }
168
169         url = talloc_zero(ctx, struct pb_url);
170
171         if (!url)
172                 return NULL;
173
174         si = pb_url_find_scheme(url_str);
175         if (si) {
176                 url->scheme = si->scheme;
177                 p = url_str + si->str_len + strlen("://");
178         } else {
179                 url->scheme = file_scheme->scheme;
180                 p = url_str;
181         }
182
183         url->full = talloc_strdup(url, url_str);
184
185         if (url->scheme == pb_url_file) {
186                 url->port = NULL;
187                 url->host = NULL;
188                 url->path = talloc_strdup(url, p);
189         } else {
190                 int len;
191                 const char *col;
192                 const char *path;
193
194                 path = strchr(p, '/');
195
196                 if (!path) {
197                         pb_log("%s: parse path failed '%s'\n", __func__ , p);
198                         goto fail;
199                 }
200
201                 col = strchr(p, ':');
202
203                 if (col) {
204                         len = path - col - 1;
205                         url->port = len ? talloc_strndup(url, col + 1, len)
206                                 : NULL;
207                         len = col - p;
208                         url->host = len ? talloc_strndup(url, p, len) : NULL;
209                 } else {
210                         url->port = NULL;
211                         url->host = talloc_strndup(url, p, path - p);
212                 }
213
214                 /* remove multiple leading slashes */
215                 for (; *path && *(path+1) == '/'; path++)
216                         ;
217
218                 url->path = talloc_strdup(url, path);
219         }
220
221         pb_url_parse_path(url);
222
223         return url;
224
225 fail:
226         talloc_free(url);
227         return NULL;
228 }
229
230 bool is_url(const char *str)
231 {
232         return strstr(str, "://") != NULL;
233 }
234
235 char *pb_url_to_string(struct pb_url *url)
236 {
237         const struct pb_scheme_info *scheme = pb_url_scheme_info(url->scheme);
238         assert(scheme);
239
240         return talloc_asprintf(url, "%s://%s%s", scheme->str,
241                         scheme->has_host ? url->host : "", url->path);
242 }
243
244 static void pb_url_update_full(struct pb_url *url)
245 {
246         talloc_free(url->full);
247         url->full = pb_url_to_string(url);
248 }
249
250 static struct pb_url *pb_url_copy(void *ctx, const struct pb_url *url)
251 {
252         struct pb_url *new_url;
253
254         new_url = talloc(ctx, struct pb_url);
255         new_url->scheme = url->scheme;
256         new_url->full = talloc_strdup(new_url, url->full);
257
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;
263
264         return new_url;
265 }
266
267 struct pb_url *pb_url_join(void *ctx, const struct pb_url *url, const char *s)
268 {
269         struct pb_url *new_url;
270
271         /* complete url: just parse all info from s */
272         if (is_url(s))
273                 return pb_url_parse(ctx, s);
274
275         new_url = pb_url_copy(ctx, url);
276
277         if (s[0] == '/') {
278                 /* absolute path: replace path of new_url */
279                 talloc_free(new_url->path);
280                 new_url->path = talloc_strdup(new_url, s);
281
282         } else {
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);
287                 talloc_free(tmp);
288         }
289
290         /* replace ->dir and ->file with components from ->path */
291         pb_url_parse_path(new_url);
292
293         /* and re-generate the full URL */
294         pb_url_update_full(new_url);
295
296         return new_url;
297 }
298
299 const char *pb_url_scheme_name(enum pb_url_scheme scheme)
300 {
301         const struct pb_scheme_info *info = pb_url_scheme_info(scheme);
302         return info ? info->str : NULL;
303 }