]> git.sesse.net Git - vlc/blob - src/text/url.c
9283520e0a666f553e70c5e6135d889746f68fe0
[vlc] / src / text / url.c
1 /*****************************************************************************
2  * url.c: URL related functions
3  *****************************************************************************
4  * Copyright (C) 2006 VLC authors and VideoLAN
5  * Copyright (C) 2008-2012 Rémi Denis-Courmont
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 #ifdef _WIN32
31 # include <io.h>
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_url.h>
36 #include <vlc_fs.h>
37 #include <ctype.h>
38
39 /**
40  * Decodes an encoded URI component. See also decode_URI().
41  * \return decoded string allocated on the heap, or NULL on error.
42  */
43 char *decode_URI_duplicate (const char *str)
44 {
45     char *buf = strdup (str);
46     if (decode_URI (buf) == NULL)
47     {
48         free (buf);
49         buf = NULL;
50     }
51     return buf;
52 }
53
54 /**
55  * Decodes an encoded URI component in place.
56  * <b>This function does NOT decode entire URIs.</b> Instead, it decodes one
57  * component at a time (e.g. host name, directory, file name).
58  * Decoded URIs do not exist in the real world (see RFC3986 §2.4).
59  * Complete URIs are always "encoded" (or they are syntaxically invalid).
60  *
61  * Note that URI encoding is different from Javascript escaping. Especially,
62  * white spaces and Unicode non-ASCII code points are encoded differently.
63  *
64  * \param str nul-terminated URI component to decode
65  * \return str on success, NULL if it was not properly encoded
66  */
67 char *decode_URI (char *str)
68 {
69     char *in = str, *out = str;
70     if (in == NULL)
71         return NULL;
72
73     char c;
74     while ((c = *(in++)) != '\0')
75     {
76         if (c == '%')
77         {
78             char hex[3];
79
80             if (!(hex[0] = *(in++)) || !(hex[1] = *(in++)))
81                 return NULL;
82             hex[2] = '\0';
83             *(out++) = strtoul (hex, NULL, 0x10);
84         }
85         else
86             *(out++) = c;
87     }
88     *out = '\0';
89     return str;
90 }
91
92 static inline bool isurisafe (int c)
93 {
94     /* These are the _unreserved_ URI characters (RFC3986 §2.3) */
95     return ((unsigned char)(c - 'a') < 26)
96         || ((unsigned char)(c - 'A') < 26)
97         || ((unsigned char)(c - '0') < 10)
98         || (strchr ("-._~", c) != NULL);
99 }
100
101 static char *encode_URI_bytes (const char *str, size_t *restrict lenp)
102 {
103     char *buf = malloc (3 * *lenp + 1);
104     if (unlikely(buf == NULL))
105         return NULL;
106
107     char *out = buf;
108     for (size_t i = 0; i < *lenp; i++)
109     {
110         static const char hex[16] = "0123456789ABCDEF";
111         unsigned char c = str[i];
112
113         if (isurisafe (c))
114             *(out++) = c;
115         /* This is URI encoding, not HTTP forms:
116          * Space is encoded as '%20', not '+'. */
117         else
118         {
119             *(out++) = '%';
120             *(out++) = hex[c >> 4];
121             *(out++) = hex[c & 0xf];
122         }
123     }
124
125     *lenp = out - buf;
126     out = realloc (buf, *lenp + 1);
127     return likely(out != NULL) ? out : buf;
128 }
129
130 /**
131  * Encodes a URI component (RFC3986 §2).
132  *
133  * @param str nul-terminated UTF-8 representation of the component.
134  * @note Obviously, a URI containing nul bytes cannot be passed.
135  * @return encoded string (must be free()'d), or NULL for ENOMEM.
136  */
137 char *encode_URI_component (const char *str)
138 {
139     size_t len = strlen (str);
140     char *ret = encode_URI_bytes (str, &len);
141     if (likely(ret != NULL))
142         ret[len] = '\0';
143     return ret;
144 }
145
146 /**
147  * Builds a URL representation from a local file path.
148  * @param path path to convert (or URI to copy)
149  * @param scheme URI scheme to use (default is auto: "file", "fd" or "smb")
150  * @return a nul-terminated URI string (use free() to release it),
151  * or NULL in case of error (errno will be set accordingly)
152  */
153 char *vlc_path2uri (const char *path, const char *scheme)
154 {
155     if (path == NULL)
156     {
157         errno = EINVAL;
158         return NULL;
159     }
160     if (scheme == NULL && !strcmp (path, "-"))
161         return strdup ("fd://0"); // standard input
162     /* Note: VLC cannot handle URI schemes without double slash after the
163      * scheme name (such as mailto: or news:). */
164
165     char *buf;
166
167 #ifdef __OS2__
168     char p[strlen (path) + 1];
169
170     for (buf = p; *path; buf++, path++)
171         *buf = (*path == '/') ? DIR_SEP_CHAR : *path;
172     *buf = '\0';
173
174     path = p;
175 #endif
176
177 #if defined (_WIN32) || defined (__OS2__)
178     /* Drive letter */
179     if (isalpha ((unsigned char)path[0]) && (path[1] == ':'))
180     {
181         if (asprintf (&buf, "%s:///%c:", scheme ? scheme : "file",
182                       path[0]) == -1)
183             buf = NULL;
184         path += 2;
185 # warning Drive letter-relative path not implemented!
186         if (path[0] != DIR_SEP_CHAR)
187         {
188             errno = ENOTSUP;
189             return NULL;
190         }
191     }
192     else
193     if (!strncmp (path, "\\\\", 2))
194     {   /* Windows UNC paths */
195         /* \\host\share\path -> file://host/share/path */
196         size_t hostlen = strcspn (path + 2, DIR_SEP);
197
198         buf = malloc (7 + hostlen);
199         if (buf != NULL)
200             snprintf (buf, 7 + hostlen, "file://%s", path + 2);
201         path += 2 + hostlen;
202
203         if (path[0] == '\0')
204             return buf; /* Hostname without path */
205     }
206     else
207 #endif
208     if (path[0] != DIR_SEP_CHAR)
209     {   /* Relative path: prepend the current working directory */
210         char *cwd, *ret;
211
212         if ((cwd = vlc_getcwd ()) == NULL)
213             return NULL;
214         if (asprintf (&buf, "%s"DIR_SEP"%s", cwd, path) == -1)
215             buf = NULL;
216
217         free (cwd);
218         ret = (buf != NULL) ? vlc_path2uri (buf, scheme) : NULL;
219         free (buf);
220         return ret;
221     }
222     else
223     if (asprintf (&buf, "%s://", scheme ? scheme : "file") == -1)
224         buf = NULL;
225     if (buf == NULL)
226         return NULL;
227
228     /* Absolute file path */
229     assert (path[0] == DIR_SEP_CHAR);
230     do
231     {
232         size_t len = strcspn (++path, DIR_SEP);
233         path += len;
234
235         char *component = encode_URI_bytes (path - len, &len);
236         if (unlikely(component == NULL))
237         {
238             free (buf);
239             return NULL;
240         }
241         component[len] = '\0';
242
243         char *uri;
244         int val = asprintf (&uri, "%s/%s", buf, component);
245         free (component);
246         free (buf);
247         if (unlikely(val == -1))
248             return NULL;
249         buf = uri;
250     }
251     while (*path);
252
253     return buf;
254 }
255
256 /**
257  * Tries to convert a URI to a local (UTF-8-encoded) file path.
258  * @param url URI to convert
259  * @return NULL on error, a nul-terminated string otherwise
260  * (use free() to release it)
261  */
262 char *make_path (const char *url)
263 {
264     char *ret = NULL;
265     char *end;
266
267     char *path = strstr (url, "://");
268     if (path == NULL)
269         return NULL; /* unsupported scheme or invalid syntax */
270
271     end = memchr (url, '/', path - url);
272     size_t schemelen = ((end != NULL) ? end : path) - url;
273     path += 3; /* skip "://" */
274
275     /* Remove HTML anchor if present */
276     end = strchr (path, '#');
277     if (end)
278         path = strndup (path, end - path);
279     else
280         path = strdup (path);
281     if (unlikely(path == NULL))
282         return NULL; /* boom! */
283
284     /* Decode path */
285     decode_URI (path);
286
287     if (schemelen == 4 && !strncasecmp (url, "file", 4))
288     {
289 #if !defined (_WIN32) && !defined (__OS2__)
290         /* Leading slash => local path */
291         if (*path == '/')
292             return path;
293         /* Local path disguised as a remote one */
294         if (!strncasecmp (path, "localhost/", 10))
295             return memmove (path, path + 9, strlen (path + 9) + 1);
296 #else
297         /* cannot start with a space */
298         if (*path == ' ')
299             goto out;
300         for (char *p = strchr (path, '/'); p; p = strchr (p + 1, '/'))
301             *p = '\\';
302
303         /* Leading backslash => local path */
304         if (*path == '\\')
305             return memmove (path, path + 1, strlen (path + 1) + 1);
306         /* Local path disguised as a remote one */
307         if (!strncasecmp (path, "localhost\\", 10))
308             return memmove (path, path + 10, strlen (path + 10) + 1);
309         /* UNC path */
310         if (*path && asprintf (&ret, "\\\\%s", path) == -1)
311             ret = NULL;
312 #endif
313         /* non-local path :-( */
314     }
315     else
316     if (schemelen == 2 && !strncasecmp (url, "fd", 2))
317     {
318         int fd = strtol (path, &end, 0);
319
320         if (*end)
321             goto out;
322
323 #if !defined( _WIN32 ) && !defined( __OS2__ )
324         switch (fd)
325         {
326             case 0:
327                 ret = strdup ("/dev/stdin");
328                 break;
329             case 1:
330                 ret = strdup ("/dev/stdout");
331                 break;
332             case 2:
333                 ret = strdup ("/dev/stderr");
334                 break;
335             default:
336                 if (asprintf (&ret, "/dev/fd/%d", fd) == -1)
337                     ret = NULL;
338         }
339 #else
340         /* XXX: Does this work on WinCE? */
341         if (fd < 2)
342             ret = strdup ("CON");
343         else
344             ret = NULL;
345 #endif
346     }
347
348 out:
349     free (path);
350     return ret; /* unknown scheme */
351 }
352
353 static char *vlc_idna_to_ascii (const char *);
354
355 /**
356  * Splits an URL into parts.
357  * \param url structure of URL parts [OUT]
358  * \param str nul-terminated URL string to split
359  * \param opt if non-zero, character separating paths from options,
360  *            normally the question mark
361  * \note Use vlc_UrlClean() to free associated resources
362  * \bug Errors cannot be detected.
363  * \return nothing
364  */
365 void vlc_UrlParse (vlc_url_t *restrict url, const char *str, unsigned char opt)
366 {
367     url->psz_protocol = NULL;
368     url->psz_username = NULL;
369     url->psz_password = NULL;
370     url->psz_host = NULL;
371     url->i_port = 0;
372     url->psz_path = NULL;
373     url->psz_option = NULL;
374     url->psz_buffer = NULL;
375
376     if (str == NULL)
377         return;
378
379     char *buf = strdup (str);
380     if (unlikely(buf == NULL))
381         abort ();
382     url->psz_buffer = buf;
383
384     char *cur = buf, *next;
385
386     /* URL scheme */
387     next = buf;
388     while ((*next >= 'A' && *next <= 'Z') || (*next >= 'a' && *next <= 'z')
389         || (*next >= '0' && *next <= '9') || memchr ("+-.", *next, 3) != NULL)
390         next++;
391     /* This is not strictly correct. In principles, the scheme is always
392      * present in an absolute URL and followed by a colon. Depending on the
393      * URL scheme, the two subsequent slashes are not required.
394      * VLC uses a different scheme for historical compatibility reasons - the
395      * scheme is often implicit. */
396     if (!strncmp (next, "://", 3))
397     {
398         *next = '\0';
399         next += 3;
400         url->psz_protocol = cur;
401         cur = next;
402     }
403
404     /* Path */
405     next = strchr (cur, '/');
406     if (next != NULL)
407     {
408         *next = '\0'; /* temporary nul, reset to slash later */
409         url->psz_path = next;
410         if (opt && (next = strchr (next, opt)) != NULL)
411         {
412             *(next++) = '\0';
413             url->psz_option = next;
414         }
415     }
416     /*else
417         url->psz_path = "/";*/
418
419     /* User name */
420     next = strrchr (cur, '@');
421     if (next != NULL)
422     {
423         *(next++) = '\0';
424         url->psz_username = cur;
425         cur = next;
426
427         /* Password (obsolete) */
428         next = strchr (url->psz_username, ':');
429         if (next != NULL)
430         {
431             *(next++) = '\0';
432             url->psz_password = next;
433             decode_URI (url->psz_password);
434         }
435         decode_URI (url->psz_username);
436     }
437
438     /* Host name */
439     if (*cur == '[' && (next = strrchr (cur, ']')) != NULL)
440     {   /* Try IPv6 numeral within brackets */
441         *(next++) = '\0';
442         url->psz_host = strdup (cur + 1);
443
444         if (*next == ':')
445             next++;
446         else
447             next = NULL;
448     }
449     else
450     {
451         next = strchr (cur, ':');
452         if (next != NULL)
453             *(next++) = '\0';
454
455         url->psz_host = vlc_idna_to_ascii (cur);
456     }
457
458     /* Port number */
459     if (next != NULL)
460         url->i_port = atoi (next);
461
462     if (url->psz_path != NULL)
463         *url->psz_path = '/'; /* restore leading slash */
464 }
465
466 /**
467  * Releases resources allocated by vlc_UrlParse().
468  */
469 void vlc_UrlClean (vlc_url_t *restrict url)
470 {
471     free (url->psz_host);
472     free (url->psz_buffer);
473 }
474
475 #if defined (HAVE_IDN)
476 # include <idna.h>
477 #elif defined (_WIN32)
478 # include <windows.h>
479 # include <vlc_charset.h>
480 #endif
481
482 /**
483  * Converts a UTF-8 nul-terminated IDN to nul-terminated ASCII domain name.
484  * \param idn UTF-8 Internationalized Domain Name to convert
485  * \return a heap-allocated string or NULL on error.
486  */
487 static char *vlc_idna_to_ascii (const char *idn)
488 {
489 #if defined (HAVE_IDN)
490     char *adn;
491
492     if (idna_to_ascii_8z (idn, &adn, IDNA_ALLOW_UNASSIGNED) != IDNA_SUCCESS)
493         return NULL;
494     return adn;
495
496 #elif defined (_WIN32) && (_WIN32_WINNT >= 0x0601)
497     char *ret = NULL;
498
499     wchar_t *wide = ToWide (idn);
500     if (wide == NULL)
501         return NULL;
502
503     int len = IdnToAscii (IDN_ALLOW_UNASSIGNED, wide, -1, NULL, 0);
504     if (len == 0)
505         goto error;
506
507     wchar_t *buf = malloc (sizeof (*buf) * len);
508     if (unlikely(buf == NULL))
509         goto error;
510     if (!IdnToAscii (IDN_ALLOW_UNASSIGNED, wide, -1, buf, len))
511     {
512         free (buf);
513         goto error;
514     }
515     ret = FromWide (buf);
516     free (buf);
517 error:
518     free (wide);
519     return ret;
520
521 #else
522     /* No IDN support, filter out non-ASCII domain names */
523     for (const char *p = idn; *p; p++)
524         if (((unsigned char)*p) >= 0x80)
525             return NULL;
526
527     return strdup (idn);
528
529 #endif
530 }