demux: adaptive: handle obsolete http header line folding
[vlc.git] / src / text / url.c
blob1b602b599e8224c8178727b1e452d68b84f001c5
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
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.
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.
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 *****************************************************************************/
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33 #ifdef _WIN32
34 # include <io.h>
35 #endif
37 #include <vlc_common.h>
38 #include <vlc_memstream.h>
39 #include <vlc_url.h>
40 #include <vlc_fs.h>
41 #include <ctype.h>
43 char *vlc_uri_decode_duplicate (const char *str)
45 char *buf = strdup (str);
46 if (vlc_uri_decode (buf) == NULL)
48 free (buf);
49 buf = NULL;
51 return buf;
54 char *vlc_uri_decode (char *str)
56 char *in = str, *out = str;
57 if (in == NULL)
58 return NULL;
60 char c;
61 while ((c = *(in++)) != '\0')
63 if (c == '%')
65 char hex[3];
67 if (!(hex[0] = *(in++)) || !(hex[1] = *(in++)))
68 return NULL;
69 hex[2] = '\0';
70 *(out++) = strtoul (hex, NULL, 0x10);
72 else
73 *(out++) = c;
75 *out = '\0';
76 return str;
79 static bool isurisafe (int c)
81 /* These are the _unreserved_ URI characters (RFC3986 §2.3) */
82 return ((unsigned char)(c - 'a') < 26)
83 || ((unsigned char)(c - 'A') < 26)
84 || ((unsigned char)(c - '0') < 10)
85 || (strchr ("-._~", c) != NULL);
88 static bool isurisubdelim(int c)
90 return strchr("!$&'()*+,;=", c) != NULL;
93 static bool isurihex(int c)
94 { /* Same as isxdigit() but does not depend on locale and unsignedness */
95 return ((unsigned char)(c - '0') < 10)
96 || ((unsigned char)(c - 'A') < 6)
97 || ((unsigned char)(c - 'a') < 6);
100 static const char urihex[] = "0123456789ABCDEF";
102 static char *encode_URI_bytes (const char *str, size_t *restrict lenp)
104 char *buf = malloc (3 * *lenp + 1);
105 if (unlikely(buf == NULL))
106 return NULL;
108 char *out = buf;
109 for (size_t i = 0; i < *lenp; i++)
111 unsigned char c = str[i];
113 if (isurisafe (c))
114 *(out++) = c;
115 /* This is URI encoding, not HTTP forms:
116 * Space is encoded as '%20', not '+'. */
117 else
119 *(out++) = '%';
120 *(out++) = urihex[c >> 4];
121 *(out++) = urihex[c & 0xf];
125 *lenp = out - buf;
126 out = realloc (buf, *lenp + 1);
127 return likely(out != NULL) ? out : buf;
130 char *vlc_uri_encode (const char *str)
132 size_t len = strlen (str);
133 char *ret = encode_URI_bytes (str, &len);
134 if (likely(ret != NULL))
135 ret[len] = '\0';
136 return ret;
139 char *vlc_path2uri (const char *path, const char *scheme)
141 if (path == NULL)
143 errno = EINVAL;
144 return NULL;
146 if (scheme == NULL && !strcmp (path, "-"))
147 return strdup ("fd://0"); // standard input
148 /* Note: VLC cannot handle URI schemes without double slash after the
149 * scheme name (such as mailto: or news:). */
151 char *buf;
153 #ifdef __OS2__
154 char p[strlen (path) + 1];
156 for (buf = p; *path; buf++, path++)
157 *buf = (*path == '/') ? DIR_SEP_CHAR : *path;
158 *buf = '\0';
160 path = p;
161 #endif
163 #if defined (_WIN32) || defined (__OS2__)
164 /* Drive letter */
165 if (isalpha ((unsigned char)path[0]) && (path[1] == ':'))
167 if (asprintf (&buf, "%s:///%c:", scheme ? scheme : "file",
168 path[0]) == -1)
169 buf = NULL;
170 path += 2;
171 # warning Drive letter-relative path not implemented!
172 if (path[0] != DIR_SEP_CHAR)
174 errno = ENOTSUP;
175 return NULL;
178 else
179 if (!strncmp (path, "\\\\", 2))
180 { /* Windows UNC paths */
181 /* \\host\share\path -> file://host/share/path */
182 int hostlen = strcspn (path + 2, DIR_SEP);
184 if (asprintf (&buf, "file://%.*s", hostlen, path + 2) == -1)
185 buf = NULL;
186 path += 2 + hostlen;
188 if (path[0] == '\0')
189 return buf; /* Hostname without path */
191 else
192 #endif
193 if (path[0] != DIR_SEP_CHAR)
194 { /* Relative path: prepend the current working directory */
195 char *cwd, *ret;
197 if ((cwd = vlc_getcwd ()) == NULL)
198 return NULL;
199 if (asprintf (&buf, "%s"DIR_SEP"%s", cwd, path) == -1)
200 buf = NULL;
202 free (cwd);
203 ret = (buf != NULL) ? vlc_path2uri (buf, scheme) : NULL;
204 free (buf);
205 return ret;
207 else
208 if (asprintf (&buf, "%s://", scheme ? scheme : "file") == -1)
209 buf = NULL;
210 if (buf == NULL)
211 return NULL;
213 /* Absolute file path */
214 assert (path[0] == DIR_SEP_CHAR);
217 size_t len = strcspn (++path, DIR_SEP);
218 path += len;
220 char *component = encode_URI_bytes (path - len, &len);
221 if (unlikely(component == NULL))
223 free (buf);
224 return NULL;
226 component[len] = '\0';
228 char *uri;
229 int val = asprintf (&uri, "%s/%s", buf, component);
230 free (component);
231 free (buf);
232 if (unlikely(val == -1))
233 return NULL;
234 buf = uri;
236 while (*path);
238 return buf;
241 char *vlc_uri2path (const char *url)
243 char *ret = NULL;
244 char *end;
246 char *path = strstr (url, "://");
247 if (path == NULL)
248 return NULL; /* unsupported scheme or invalid syntax */
250 end = memchr (url, '/', path - url);
251 size_t schemelen = ((end != NULL) ? end : path) - url;
252 path += 3; /* skip "://" */
254 /* Remove request parameters and/or HTML anchor if present */
255 end = path + strcspn (path, "?#");
256 path = strndup (path, end - path);
257 if (unlikely(path == NULL))
258 return NULL; /* boom! */
260 /* Decode path */
261 vlc_uri_decode (path);
263 if (schemelen == 4 && !strncasecmp (url, "file", 4))
265 #if !defined (_WIN32) && !defined (__OS2__)
266 /* Leading slash => local path */
267 if (*path == '/')
268 return path;
269 /* Local path disguised as a remote one */
270 if (!strncasecmp (path, "localhost/", 10))
271 return memmove (path, path + 9, strlen (path + 9) + 1);
272 #else
273 /* cannot start with a space */
274 if (*path == ' ')
275 goto out;
276 for (char *p = strchr (path, '/'); p; p = strchr (p + 1, '/'))
277 *p = '\\';
279 /* Leading backslash => local path */
280 if (*path == '\\')
281 return memmove (path, path + 1, strlen (path + 1) + 1);
282 /* Local path disguised as a remote one */
283 if (!strncasecmp (path, "localhost\\", 10))
284 return memmove (path, path + 10, strlen (path + 10) + 1);
285 /* UNC path */
286 if (*path && asprintf (&ret, "\\\\%s", path) == -1)
287 ret = NULL;
288 #endif
289 /* non-local path :-( */
291 else
292 if (schemelen == 2 && !strncasecmp (url, "fd", 2))
294 int fd = strtol (path, &end, 0);
296 if (*end)
297 goto out;
299 #if !defined( _WIN32 ) && !defined( __OS2__ )
300 switch (fd)
302 case 0:
303 ret = strdup ("/dev/stdin");
304 break;
305 case 1:
306 ret = strdup ("/dev/stdout");
307 break;
308 case 2:
309 ret = strdup ("/dev/stderr");
310 break;
311 default:
312 if (asprintf (&ret, "/dev/fd/%d", fd) == -1)
313 ret = NULL;
315 #else
316 /* XXX: Does this work on WinCE? */
317 if (fd < 2)
318 ret = strdup ("CON");
319 #endif
322 out:
323 free (path);
324 return ret; /* unknown scheme */
327 static char *vlc_idna_to_ascii (const char *);
329 /* RFC3987 §3.1 */
330 static char *vlc_iri2uri(const char *iri)
332 size_t a = 0, u = 0;
334 for (size_t i = 0; iri[i] != '\0'; i++)
336 unsigned char c = iri[i];
338 if (c < 128)
339 a++;
340 else
341 u++;
344 if (unlikely((a + u) > (SIZE_MAX / 4)))
346 errno = ENOMEM;
347 return NULL;
350 char *uri = malloc(a + 3 * u + 1), *p;
351 if (unlikely(uri == NULL))
352 return NULL;
354 for (p = uri; *iri != '\0'; iri++)
356 unsigned char c = *iri;
358 if (c < 128)
359 *(p++) = c;
360 else
362 *(p++) = '%';
363 *(p++) = urihex[c >> 4];
364 *(p++) = urihex[c & 0xf];
368 *p = '\0';
369 return uri;
372 static bool vlc_uri_component_validate(const char *str, const char *extras)
374 assert(str != NULL);
376 for (size_t i = 0; str[i] != '\0'; i++)
378 int c = str[i];
380 if (isurisafe(c) || isurisubdelim(c))
381 continue;
382 if (strchr(extras, c) != NULL)
383 continue;
384 if (c == '%' && isurihex(str[i + 1]) && isurihex(str[i + 2]))
386 i += 2;
387 continue;
389 return false;
391 return true;
394 static bool vlc_uri_host_validate(const char *str)
396 return vlc_uri_component_validate(str, ":");
399 static bool vlc_uri_path_validate(const char *str)
401 return vlc_uri_component_validate(str, "/@:");
404 int vlc_UrlParse(vlc_url_t *restrict url, const char *str)
406 url->psz_protocol = NULL;
407 url->psz_username = NULL;
408 url->psz_password = NULL;
409 url->psz_host = NULL;
410 url->i_port = 0;
411 url->psz_path = NULL;
412 url->psz_option = NULL;
413 url->psz_buffer = NULL;
415 if (str == NULL)
417 errno = EINVAL;
418 return -1;
421 char *buf = vlc_iri2uri(str);
422 if (unlikely(buf == NULL))
423 return -1;
424 url->psz_buffer = buf;
426 char *cur = buf, *next;
427 int ret = 0;
429 /* URI scheme */
430 next = buf;
431 while ((*next >= 'A' && *next <= 'Z') || (*next >= 'a' && *next <= 'z')
432 || (*next >= '0' && *next <= '9') || memchr ("+-.", *next, 3) != NULL)
433 next++;
435 if (*next == ':')
437 *(next++) = '\0';
438 url->psz_protocol = cur;
439 cur = next;
442 /* Fragment */
443 next = strchr(cur, '#');
444 if (next != NULL)
446 #if 0 /* TODO */
447 *(next++) = '\0';
448 url->psz_fragment = next;
449 #else
450 *next = '\0';
451 #endif
454 /* Query parameters */
455 next = strchr(cur, '?');
456 if (next != NULL)
458 *(next++) = '\0';
459 url->psz_option = next;
462 /* Authority */
463 if (strncmp(cur, "//", 2) == 0)
465 cur += 2;
467 /* Path */
468 next = strchr(cur, '/');
469 if (next != NULL)
471 *next = '\0'; /* temporary nul, reset to slash later */
472 url->psz_path = next;
474 /*else
475 url->psz_path = "/";*/
477 /* User name */
478 next = strrchr(cur, '@');
479 if (next != NULL)
481 *(next++) = '\0';
482 url->psz_username = cur;
483 cur = next;
485 /* Password (obsolete) */
486 next = strchr(url->psz_username, ':');
487 if (next != NULL)
489 *(next++) = '\0';
490 url->psz_password = next;
491 vlc_uri_decode(url->psz_password);
493 vlc_uri_decode(url->psz_username);
496 /* Host name */
497 if (*cur == '[' && (next = strrchr(cur, ']')) != NULL)
498 { /* Try IPv6 numeral within brackets */
499 *(next++) = '\0';
500 url->psz_host = strdup(cur + 1);
502 if (*next == ':')
503 next++;
504 else
505 next = NULL;
507 else
509 next = strchr(cur, ':');
510 if (next != NULL)
511 *(next++) = '\0';
513 url->psz_host = vlc_idna_to_ascii(vlc_uri_decode(cur));
516 if (url->psz_host == NULL)
517 ret = -1;
518 else
519 if (!vlc_uri_host_validate(url->psz_host))
521 free(url->psz_host);
522 url->psz_host = NULL;
523 errno = EINVAL;
524 ret = -1;
527 /* Port number */
528 if (next != NULL && *next)
530 char* end;
531 unsigned long port = strtoul(next, &end, 10);
533 if (strchr("0123456789", *next) == NULL || *end || port > UINT_MAX)
535 errno = EINVAL;
536 ret = -1;
539 url->i_port = port;
542 if (url->psz_path != NULL)
543 *url->psz_path = '/'; /* restore leading slash */
545 else
547 url->psz_path = cur;
550 if (url->psz_path != NULL && !vlc_uri_path_validate(url->psz_path))
552 url->psz_path = NULL;
553 errno = EINVAL;
554 ret = -1;
557 return ret;
560 void vlc_UrlClean (vlc_url_t *restrict url)
562 free (url->psz_host);
563 free (url->psz_buffer);
567 * Merge paths
569 * See IETF RFC3986 section 5.2.3 for details.
571 static char *vlc_uri_merge_paths(const char *base, const char *ref)
573 char *str;
574 int len;
576 if (base == NULL)
577 len = asprintf(&str, "/%s", ref);
578 else
580 const char *end = strrchr(base, '/');
582 if (end != NULL)
583 end++;
584 else
585 end = base;
587 len = asprintf(&str, "%.*s%s", (int)(end - base), base, ref);
590 if (unlikely(len == -1))
591 str = NULL;
592 return str;
596 * Remove dot segments
598 * See IETF RFC3986 section 5.2.4 for details.
600 static char *vlc_uri_remove_dot_segments(char *str)
602 char *input = str, *output = str;
604 while (input[0] != '\0')
606 assert(output <= input);
608 if (strncmp(input, "../", 3) == 0)
610 input += 3;
611 continue;
613 if (strncmp(input, "./", 2) == 0)
615 input += 2;
616 continue;
618 if (strncmp(input, "/./", 3) == 0)
620 input += 2;
621 continue;
623 if (strcmp(input, "/.") == 0)
625 input[1] = '\0';
626 continue;
628 if (strncmp(input, "/../", 4) == 0)
630 input += 3;
631 output = memrchr(str, '/', output - str);
632 if (output == NULL)
633 output = str;
634 continue;
636 if (strcmp(input, "/..") == 0)
638 input[1] = '\0';
639 output = memrchr(str, '/', output - str);
640 if (output == NULL)
641 output = str;
642 continue;
644 if (strcmp(input, ".") == 0)
646 input++;
647 continue;
649 if (strcmp(input, "..") == 0)
651 input += 2;
652 continue;
655 if (input[0] == '/')
656 *(output++) = *(input++);
658 size_t len = strcspn(input, "/");
660 if (input != output)
661 memmove(output, input, len);
663 input += len;
664 output += len;
667 output[0] = '\0';
668 return str;
671 char *vlc_uri_compose(const vlc_url_t *uri)
673 struct vlc_memstream stream;
674 char *enc;
676 vlc_memstream_open(&stream);
678 if (uri->psz_protocol != NULL)
679 vlc_memstream_printf(&stream, "%s:", uri->psz_protocol);
681 if (uri->psz_host != NULL)
683 vlc_memstream_write(&stream, "//", 2);
685 if (uri->psz_username != NULL)
687 enc = vlc_uri_encode(uri->psz_username);
688 if (enc == NULL)
689 goto error;
691 vlc_memstream_puts(&stream, enc);
692 free(enc);
694 if (uri->psz_password != NULL)
696 enc = vlc_uri_encode(uri->psz_password);
697 if (unlikely(enc == NULL))
698 goto error;
700 vlc_memstream_printf(&stream, ":%s", enc);
701 free(enc);
703 vlc_memstream_putc(&stream, '@');
706 const char *fmt;
708 if (strchr(uri->psz_host, ':') != NULL)
709 fmt = (uri->i_port != 0) ? "[%s]:%d" : "[%s]";
710 else
711 fmt = (uri->i_port != 0) ? "%s:%d" : "%s";
712 /* No IDNA decoding here. Seems unnecessary, dangerous even. */
713 vlc_memstream_printf(&stream, fmt, uri->psz_host, uri->i_port);
716 if (uri->psz_path != NULL)
717 vlc_memstream_puts(&stream, uri->psz_path);
718 if (uri->psz_option != NULL)
719 vlc_memstream_printf(&stream, "?%s", uri->psz_option);
720 /* NOTE: fragment not handled currently */
722 if (vlc_memstream_close(&stream))
723 return NULL;
724 return stream.ptr;
726 error:
727 if (vlc_memstream_close(&stream) == 0)
728 free(stream.ptr);
729 return NULL;
732 char *vlc_uri_resolve(const char *base, const char *ref)
734 vlc_url_t base_uri, rel_uri;
735 vlc_url_t tgt_uri;
736 char *pathbuf = NULL, *ret = NULL;
738 if (vlc_UrlParse(&rel_uri, ref))
740 vlc_UrlClean(&rel_uri);
741 return NULL;
744 if (rel_uri.psz_protocol != NULL)
745 { /* Short circuit in case of absolute URI */
746 vlc_UrlClean(&rel_uri);
747 return strdup(ref);
750 vlc_UrlParse(&base_uri, base);
752 /* RFC3986 section 5.2.2 */
755 tgt_uri = rel_uri;
756 tgt_uri.psz_protocol = base_uri.psz_protocol;
758 if (rel_uri.psz_host != NULL)
759 break;
761 tgt_uri.psz_username = base_uri.psz_username;
762 tgt_uri.psz_password = base_uri.psz_password;
763 tgt_uri.psz_host = base_uri.psz_host;
764 tgt_uri.i_port = base_uri.i_port;
766 if (rel_uri.psz_path == NULL || rel_uri.psz_path[0] == '\0')
768 tgt_uri.psz_path = base_uri.psz_path;
769 if (rel_uri.psz_option == NULL)
770 tgt_uri.psz_option = base_uri.psz_option;
771 break;
774 if (rel_uri.psz_path[0] == '/')
775 break;
777 pathbuf = vlc_uri_merge_paths(base_uri.psz_path, rel_uri.psz_path);
778 if (unlikely(pathbuf == NULL))
779 goto error;
781 tgt_uri.psz_path = pathbuf;
783 while (0);
785 if (tgt_uri.psz_path != NULL)
786 vlc_uri_remove_dot_segments(tgt_uri.psz_path);
788 ret = vlc_uri_compose(&tgt_uri);
789 error:
790 free(pathbuf);
791 vlc_UrlClean(&base_uri);
792 vlc_UrlClean(&rel_uri);
793 return ret;
796 char *vlc_uri_fixup(const char *str)
798 /* Rule number one is do not change a (potentially) valid URI */
799 if (vlc_uri_component_validate(str, ":/?#[]@"))
800 return strdup(str);
802 bool encode_percent = false;
803 for (size_t i = 0; str[i] != '\0'; i++)
804 if (str[i] == '%' && !(isurihex(str[i+1]) && isurihex(str[i+2])))
806 encode_percent = true;
807 break;
810 struct vlc_memstream stream;
812 vlc_memstream_open(&stream);
814 for (size_t i = 0; str[i] != '\0'; i++)
816 unsigned char c = str[i];
818 if (isurisafe(c) || isurisubdelim(c) || (strchr(":/?#[]@", c) != NULL)
819 || (c == '%' && !encode_percent))
820 vlc_memstream_putc(&stream, c);
821 else
822 vlc_memstream_printf(&stream, "%%%02hhX", c);
825 if (vlc_memstream_close(&stream))
826 return NULL;
827 return stream.ptr;
830 #if defined (HAVE_IDN)
831 # include <idna.h>
832 #elif defined (_WIN32)
833 # include <windows.h>
834 # include <vlc_charset.h>
836 # if (_WIN32_WINNT < _WIN32_WINNT_VISTA)
837 # define IDN_ALLOW_UNASSIGNED 0x01
838 static int IdnToAscii(DWORD flags, LPCWSTR str, int len, LPWSTR buf, int size)
840 HMODULE h = LoadLibrary(_T("Normaliz.dll"));
841 if (h == NULL)
843 errno = ENOSYS;
844 return 0;
847 int (WINAPI *IdnToAsciiReal)(DWORD, LPCWSTR, int, LPWSTR, int);
848 int ret = 0;
850 IdnToAsciiReal = GetProcAddress(h, "IdnToAscii");
851 if (IdnToAsciiReal != NULL)
852 ret = IdnToAsciiReal(flags, str, len, buf, size);
853 else
854 errno = ENOSYS;
855 FreeLibrary(h);
856 return ret;
858 # endif
859 #endif
862 * Converts a UTF-8 nul-terminated IDN to nul-terminated ASCII domain name.
863 * \param idn UTF-8 Internationalized Domain Name to convert
864 * \return a heap-allocated string or NULL on error.
866 static char *vlc_idna_to_ascii (const char *idn)
868 #if defined (HAVE_IDN)
869 char *adn;
871 switch (idna_to_ascii_8z(idn, &adn, IDNA_ALLOW_UNASSIGNED))
873 case IDNA_SUCCESS:
874 return adn;
875 case IDNA_MALLOC_ERROR:
876 errno = ENOMEM;
877 return NULL;
878 case IDNA_DLOPEN_ERROR:
879 errno = ENOSYS;
880 return NULL;
881 default:
882 errno = EINVAL;
883 return NULL;
886 #elif defined (_WIN32)
887 char *ret = NULL;
889 if (idn[0] == '\0')
890 return strdup("");
892 wchar_t *wide = ToWide (idn);
893 if (wide == NULL)
894 return NULL;
896 int len = IdnToAscii (IDN_ALLOW_UNASSIGNED, wide, -1, NULL, 0);
897 if (len == 0)
899 errno = EINVAL;
900 goto error;
903 wchar_t *buf = malloc (sizeof (*buf) * len);
904 if (unlikely(buf == NULL))
905 goto error;
906 if (!IdnToAscii (IDN_ALLOW_UNASSIGNED, wide, -1, buf, len))
908 free (buf);
909 errno = EINVAL;
910 goto error;
912 ret = FromWide (buf);
913 free (buf);
914 error:
915 free (wide);
916 return ret;
918 #else
919 /* No IDN support, filter out non-ASCII domain names */
920 for (const char *p = idn; *p; p++)
921 if (((unsigned char)*p) >= 0x80)
923 errno = ENOSYS;
924 return NULL;
927 return strdup (idn);
929 #endif