]> git.sesse.net Git - vlc/blob - src/text/url.c
dsm: change item b_net and i_type
[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 #endif
344     }
345
346 out:
347     free (path);
348     return ret; /* unknown scheme */
349 }
350
351 static char *vlc_idna_to_ascii (const char *);
352
353 /**
354  * Splits an URL into parts.
355  * \param url structure of URL parts [OUT]
356  * \param str nul-terminated URL string to split
357  * \param opt if non-zero, character separating paths from options,
358  *            normally the question mark
359  * \note Use vlc_UrlClean() to free associated resources
360  * \bug Errors cannot be detected.
361  * \return nothing
362  */
363 void vlc_UrlParse (vlc_url_t *restrict url, const char *str, unsigned char opt)
364 {
365     url->psz_protocol = NULL;
366     url->psz_username = NULL;
367     url->psz_password = NULL;
368     url->psz_host = NULL;
369     url->i_port = 0;
370     url->psz_path = NULL;
371     url->psz_option = NULL;
372     url->psz_buffer = NULL;
373
374     if (str == NULL)
375         return;
376
377     char *buf = strdup (str);
378     if (unlikely(buf == NULL))
379         abort ();
380     url->psz_buffer = buf;
381
382     char *cur = buf, *next;
383
384     /* URL scheme */
385     next = buf;
386     while ((*next >= 'A' && *next <= 'Z') || (*next >= 'a' && *next <= 'z')
387         || (*next >= '0' && *next <= '9') || memchr ("+-.", *next, 3) != NULL)
388         next++;
389     /* This is not strictly correct. In principles, the scheme is always
390      * present in an absolute URL and followed by a colon. Depending on the
391      * URL scheme, the two subsequent slashes are not required.
392      * VLC uses a different scheme for historical compatibility reasons - the
393      * scheme is often implicit. */
394     if (!strncmp (next, "://", 3))
395     {
396         *next = '\0';
397         next += 3;
398         url->psz_protocol = cur;
399         cur = next;
400     }
401
402     /* Path */
403     next = strchr (cur, '/');
404     if (next != NULL)
405     {
406         *next = '\0'; /* temporary nul, reset to slash later */
407         url->psz_path = next;
408         if (opt && (next = strchr (next + 1, opt)) != NULL)
409         {
410             *(next++) = '\0';
411             url->psz_option = next;
412         }
413     }
414     /*else
415         url->psz_path = "/";*/
416
417     /* User name */
418     next = strrchr (cur, '@');
419     if (next != NULL)
420     {
421         *(next++) = '\0';
422         url->psz_username = cur;
423         cur = next;
424
425         /* Password (obsolete) */
426         next = strchr (url->psz_username, ':');
427         if (next != NULL)
428         {
429             *(next++) = '\0';
430             url->psz_password = next;
431             decode_URI (url->psz_password);
432         }
433         decode_URI (url->psz_username);
434     }
435
436     /* Host name */
437     if (*cur == '[' && (next = strrchr (cur, ']')) != NULL)
438     {   /* Try IPv6 numeral within brackets */
439         *(next++) = '\0';
440         url->psz_host = strdup (cur + 1);
441
442         if (*next == ':')
443             next++;
444         else
445             next = NULL;
446     }
447     else
448     {
449         next = strchr (cur, ':');
450         if (next != NULL)
451             *(next++) = '\0';
452
453         url->psz_host = vlc_idna_to_ascii (cur);
454     }
455
456     /* Port number */
457     if (next != NULL)
458         url->i_port = atoi (next);
459
460     if (url->psz_path != NULL)
461         *url->psz_path = '/'; /* restore leading slash */
462 }
463
464 /**
465  * Releases resources allocated by vlc_UrlParse().
466  */
467 void vlc_UrlClean (vlc_url_t *restrict url)
468 {
469     free (url->psz_host);
470     free (url->psz_buffer);
471 }
472
473 #if defined (HAVE_IDN)
474 # include <idna.h>
475 #elif defined (_WIN32)
476 # include <windows.h>
477 # include <vlc_charset.h>
478 #endif
479
480 /**
481  * Converts a UTF-8 nul-terminated IDN to nul-terminated ASCII domain name.
482  * \param idn UTF-8 Internationalized Domain Name to convert
483  * \return a heap-allocated string or NULL on error.
484  */
485 static char *vlc_idna_to_ascii (const char *idn)
486 {
487 #if defined (HAVE_IDN)
488     char *adn;
489
490     if (idna_to_ascii_8z (idn, &adn, IDNA_ALLOW_UNASSIGNED) != IDNA_SUCCESS)
491         return NULL;
492     return adn;
493
494 #elif defined (_WIN32) && (_WIN32_WINNT >= 0x0601)
495     char *ret = NULL;
496
497     wchar_t *wide = ToWide (idn);
498     if (wide == NULL)
499         return NULL;
500
501     int len = IdnToAscii (IDN_ALLOW_UNASSIGNED, wide, -1, NULL, 0);
502     if (len == 0)
503         goto error;
504
505     wchar_t *buf = malloc (sizeof (*buf) * len);
506     if (unlikely(buf == NULL))
507         goto error;
508     if (!IdnToAscii (IDN_ALLOW_UNASSIGNED, wide, -1, buf, len))
509     {
510         free (buf);
511         goto error;
512     }
513     ret = FromWide (buf);
514     free (buf);
515 error:
516     free (wide);
517     return ret;
518
519 #else
520     /* No IDN support, filter out non-ASCII domain names */
521     for (const char *p = idn; *p; p++)
522         if (((unsigned char)*p) >= 0x80)
523             return NULL;
524
525     return strdup (idn);
526
527 #endif
528 }