]> git.ozlabs.org Git - petitboot/blob - ui/common/url.c
3e7b3840803e545e3ae35971beed8e9f0151c36f
[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)
86 {
87         static const int sep_len = sizeof("://") - 1;
88         static const char *sep = "://";
89         unsigned int i, url_len;
90
91         url_len = strlen(url);
92
93         for (i = 0; i < sizeof(schemes) / sizeof(schemes[0]); i++) {
94                 const struct pb_scheme_info *scheme = &schemes[i];
95
96                 if (url_len < scheme->str_len + sep_len)
97                         continue;
98
99                 if (strncmp(url + scheme->str_len, sep, sep_len))
100                         continue;
101
102                 if (strncasecmp(url, scheme->str, scheme->str_len))
103                         continue;
104
105                 return scheme;
106         }
107
108         /* Assume this is a non-url local file. */
109
110         return file_scheme;
111 }
112
113 /**
114  * pb_url_parse - Parse a remote file URL.
115  * @ctx: The talloc context to associate with the returned string.
116  *
117  * Returns a talloc'ed struct pb_url instance on success, or NULL on error.
118  */
119
120 struct pb_url *pb_url_parse(void *ctx, const char *url_str)
121 {
122         const struct pb_scheme_info *si;
123         struct pb_url *url;
124         const char *p;
125
126         pb_log("%s: '%s'\n", __func__, url_str);
127
128         if (!url_str || !*url_str) {
129                 assert(0 && "bad url");
130                 return NULL;
131         }
132
133         url = talloc_zero(ctx, struct pb_url);
134
135         if (!url)
136                 return NULL;
137
138         si = pb_url_find_scheme(url_str);
139
140         url->scheme = si->scheme;
141         p = url_str + si->str_len + strlen("://");
142
143         url->full = talloc_strdup(url, url_str);
144
145         if (url->scheme == pb_url_file) {
146                 url->port = NULL;
147                 url->host = NULL;
148                 url->path = talloc_strdup(url, p);
149         } else {
150                 int len;
151                 const char *col;
152                 const char *path;
153
154                 path = strchr(p, '/');
155
156                 if (!path) {
157                         pb_log("%s: parse path failed '%s'\n", __func__ , p);
158                         goto fail;
159                 }
160
161                 col = strchr(p, ':');
162
163                 if (col) {
164                         len = path - col - 1;
165                         url->port = len ? talloc_strndup(url, col + 1, len)
166                                 : NULL;
167                         len = col - p;
168                         url->host = len ? talloc_strndup(url, p, len) : NULL;
169                 } else {
170                         url->port = NULL;
171                         url->host = talloc_strndup(url, p, path - p);
172                 }
173                 url->path = talloc_strdup(url, path);
174         }
175
176         p = strrchr(url->path, '/');
177
178         if (p) {
179                 p++;
180                 url->dir = talloc_strndup(url, url->path, p - url->path);
181                 url->file = talloc_strdup(url, p);
182         } else {
183                 url->dir = NULL;
184                 url->file = talloc_strdup(url, url->path);
185         }
186
187         pb_log(" scheme %d\n", url->scheme);
188         pb_log(" host '%s'\n", url->host);
189         pb_log(" port '%s'\n", url->port);
190         pb_log(" path '%s'\n", url->path);
191         pb_log(" dir '%s'\n", url->dir);
192         pb_log(" file '%s'\n", url->file);
193
194         return url;
195
196 fail:
197         talloc_free(url);
198         return NULL;
199 }