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 *****************************************************************************/
37 #include <vlc_common.h>
38 #include <vlc_memstream.h>
43 char *vlc_uri_decode_duplicate (const char *str
)
45 char *buf
= strdup (str
);
46 if (vlc_uri_decode (buf
) == NULL
)
54 char *vlc_uri_decode (char *str
)
56 char *in
= str
, *out
= str
;
61 while ((c
= *(in
++)) != '\0')
67 if (!(hex
[0] = *(in
++)) || !(hex
[1] = *(in
++)))
70 *(out
++) = strtoul (hex
, NULL
, 0x10);
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
))
109 for (size_t i
= 0; i
< *lenp
; i
++)
111 unsigned char c
= str
[i
];
115 /* This is URI encoding, not HTTP forms:
116 * Space is encoded as '%20', not '+'. */
120 *(out
++) = urihex
[c
>> 4];
121 *(out
++) = urihex
[c
& 0xf];
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
))
139 char *vlc_path2uri (const char *path
, const char *scheme
)
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:). */
154 char p
[strlen (path
) + 1];
156 for (buf
= p
; *path
; buf
++, path
++)
157 *buf
= (*path
== '/') ? DIR_SEP_CHAR
: *path
;
163 #if defined (_WIN32) || defined (__OS2__)
165 if (isalpha ((unsigned char)path
[0]) && (path
[1] == ':'))
167 if (asprintf (&buf
, "%s:///%c:", scheme
? scheme
: "file",
171 # warning Drive letter-relative path not implemented!
172 if (path
[0] != DIR_SEP_CHAR
)
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)
189 return buf
; /* Hostname without path */
193 if (path
[0] != DIR_SEP_CHAR
)
194 { /* Relative path: prepend the current working directory */
197 if ((cwd
= vlc_getcwd ()) == NULL
)
199 if (asprintf (&buf
, "%s"DIR_SEP
"%s", cwd
, path
) == -1)
203 ret
= (buf
!= NULL
) ? vlc_path2uri (buf
, scheme
) : NULL
;
208 if (asprintf (&buf
, "%s://", scheme
? scheme
: "file") == -1)
213 /* Absolute file path */
214 assert (path
[0] == DIR_SEP_CHAR
);
217 size_t len
= strcspn (++path
, DIR_SEP
);
220 char *component
= encode_URI_bytes (path
- len
, &len
);
221 if (unlikely(component
== NULL
))
226 component
[len
] = '\0';
229 int val
= asprintf (&uri
, "%s/%s", buf
, component
);
232 if (unlikely(val
== -1))
241 char *vlc_uri2path (const char *url
)
246 char *path
= strstr (url
, "://");
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! */
261 vlc_uri_decode (path
);
263 if (schemelen
== 4 && !strncasecmp (url
, "file", 4))
265 #if !defined (_WIN32) && !defined (__OS2__)
266 /* Leading slash => local 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);
273 /* cannot start with a space */
276 for (char *p
= strchr (path
, '/'); p
; p
= strchr (p
+ 1, '/'))
279 /* Leading backslash => local 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);
286 if (*path
&& asprintf (&ret
, "\\\\%s", path
) == -1)
289 /* non-local path :-( */
292 if (schemelen
== 2 && !strncasecmp (url
, "fd", 2))
294 int fd
= strtol (path
, &end
, 0);
299 #if !defined( _WIN32 ) && !defined( __OS2__ )
303 ret
= strdup ("/dev/stdin");
306 ret
= strdup ("/dev/stdout");
309 ret
= strdup ("/dev/stderr");
312 if (asprintf (&ret
, "/dev/fd/%d", fd
) == -1)
316 /* XXX: Does this work on WinCE? */
318 ret
= strdup ("CON");
324 return ret
; /* unknown scheme */
327 static char *vlc_idna_to_ascii (const char *);
330 static char *vlc_iri2uri(const char *iri
)
334 for (size_t i
= 0; iri
[i
] != '\0'; i
++)
336 unsigned char c
= iri
[i
];
344 if (unlikely((a
+ u
) > (SIZE_MAX
/ 4)))
350 char *uri
= malloc(a
+ 3 * u
+ 1), *p
;
351 if (unlikely(uri
== NULL
))
354 for (p
= uri
; *iri
!= '\0'; iri
++)
356 unsigned char c
= *iri
;
363 *(p
++) = urihex
[c
>> 4];
364 *(p
++) = urihex
[c
& 0xf];
372 static bool vlc_uri_component_validate(const char *str
, const char *extras
)
376 for (size_t i
= 0; str
[i
] != '\0'; i
++)
380 if (isurisafe(c
) || isurisubdelim(c
))
382 if (strchr(extras
, c
) != NULL
)
384 if (c
== '%' && isurihex(str
[i
+ 1]) && isurihex(str
[i
+ 2]))
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
;
411 url
->psz_path
= NULL
;
412 url
->psz_option
= NULL
;
413 url
->psz_buffer
= NULL
;
421 char *buf
= vlc_iri2uri(str
);
422 if (unlikely(buf
== NULL
))
424 url
->psz_buffer
= buf
;
426 char *cur
= buf
, *next
;
431 while ((*next
>= 'A' && *next
<= 'Z') || (*next
>= 'a' && *next
<= 'z')
432 || (*next
>= '0' && *next
<= '9') || memchr ("+-.", *next
, 3) != NULL
)
438 url
->psz_protocol
= cur
;
443 next
= strchr(cur
, '#');
448 url
->psz_fragment
= next
;
454 /* Query parameters */
455 next
= strchr(cur
, '?');
459 url
->psz_option
= next
;
463 if (strncmp(cur
, "//", 2) == 0)
468 next
= strchr(cur
, '/');
471 *next
= '\0'; /* temporary nul, reset to slash later */
472 url
->psz_path
= next
;
475 url->psz_path = "/";*/
478 next
= strrchr(cur
, '@');
482 url
->psz_username
= cur
;
485 /* Password (obsolete) */
486 next
= strchr(url
->psz_username
, ':');
490 url
->psz_password
= next
;
491 vlc_uri_decode(url
->psz_password
);
493 vlc_uri_decode(url
->psz_username
);
497 if (*cur
== '[' && (next
= strrchr(cur
, ']')) != NULL
)
498 { /* Try IPv6 numeral within brackets */
500 url
->psz_host
= strdup(cur
+ 1);
509 next
= strchr(cur
, ':');
513 url
->psz_host
= vlc_idna_to_ascii(vlc_uri_decode(cur
));
516 if (url
->psz_host
== NULL
)
519 if (!vlc_uri_host_validate(url
->psz_host
))
522 url
->psz_host
= NULL
;
528 if (next
!= NULL
&& *next
)
531 unsigned long port
= strtoul(next
, &end
, 10);
533 if (strchr("0123456789", *next
) == NULL
|| *end
|| port
> UINT_MAX
)
542 if (url
->psz_path
!= NULL
)
543 *url
->psz_path
= '/'; /* restore leading slash */
550 if (url
->psz_path
!= NULL
&& !vlc_uri_path_validate(url
->psz_path
))
552 url
->psz_path
= NULL
;
560 void vlc_UrlClean (vlc_url_t
*restrict url
)
562 free (url
->psz_host
);
563 free (url
->psz_buffer
);
569 * See IETF RFC3986 section 5.2.3 for details.
571 static char *vlc_uri_merge_paths(const char *base
, const char *ref
)
577 len
= asprintf(&str
, "/%s", ref
);
580 const char *end
= strrchr(base
, '/');
587 len
= asprintf(&str
, "%.*s%s", (int)(end
- base
), base
, ref
);
590 if (unlikely(len
== -1))
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)
613 if (strncmp(input
, "./", 2) == 0)
618 if (strncmp(input
, "/./", 3) == 0)
623 if (strcmp(input
, "/.") == 0)
628 if (strncmp(input
, "/../", 4) == 0)
631 output
= memrchr(str
, '/', output
- str
);
636 if (strcmp(input
, "/..") == 0)
639 output
= memrchr(str
, '/', output
- str
);
644 if (strcmp(input
, ".") == 0)
649 if (strcmp(input
, "..") == 0)
656 *(output
++) = *(input
++);
658 size_t len
= strcspn(input
, "/");
661 memmove(output
, input
, len
);
671 char *vlc_uri_compose(const vlc_url_t
*uri
)
673 struct vlc_memstream stream
;
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
);
691 vlc_memstream_puts(&stream
, enc
);
694 if (uri
->psz_password
!= NULL
)
696 enc
= vlc_uri_encode(uri
->psz_password
);
697 if (unlikely(enc
== NULL
))
700 vlc_memstream_printf(&stream
, ":%s", enc
);
703 vlc_memstream_putc(&stream
, '@');
708 if (strchr(uri
->psz_host
, ':') != NULL
)
709 fmt
= (uri
->i_port
!= 0) ? "[%s]:%d" : "[%s]";
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
))
727 if (vlc_memstream_close(&stream
) == 0)
732 char *vlc_uri_resolve(const char *base
, const char *ref
)
734 vlc_url_t base_uri
, rel_uri
;
736 char *pathbuf
= NULL
, *ret
= NULL
;
738 if (vlc_UrlParse(&rel_uri
, ref
))
740 vlc_UrlClean(&rel_uri
);
744 if (rel_uri
.psz_protocol
!= NULL
)
745 { /* Short circuit in case of absolute URI */
746 vlc_UrlClean(&rel_uri
);
750 vlc_UrlParse(&base_uri
, base
);
752 /* RFC3986 section 5.2.2 */
756 tgt_uri
.psz_protocol
= base_uri
.psz_protocol
;
758 if (rel_uri
.psz_host
!= NULL
)
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
;
774 if (rel_uri
.psz_path
[0] == '/')
777 pathbuf
= vlc_uri_merge_paths(base_uri
.psz_path
, rel_uri
.psz_path
);
778 if (unlikely(pathbuf
== NULL
))
781 tgt_uri
.psz_path
= pathbuf
;
785 if (tgt_uri
.psz_path
!= NULL
)
786 vlc_uri_remove_dot_segments(tgt_uri
.psz_path
);
788 ret
= vlc_uri_compose(&tgt_uri
);
791 vlc_UrlClean(&base_uri
);
792 vlc_UrlClean(&rel_uri
);
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
, ":/?#[]@"))
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;
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
);
822 vlc_memstream_printf(&stream
, "%%%02hhX", c
);
825 if (vlc_memstream_close(&stream
))
830 #if defined (HAVE_IDN)
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"));
847 int (WINAPI
*IdnToAsciiReal
)(DWORD
, LPCWSTR
, int, LPWSTR
, int);
850 IdnToAsciiReal
= GetProcAddress(h
, "IdnToAscii");
851 if (IdnToAsciiReal
!= NULL
)
852 ret
= IdnToAsciiReal(flags
, str
, len
, buf
, size
);
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)
871 switch (idna_to_ascii_8z(idn
, &adn
, IDNA_ALLOW_UNASSIGNED
))
875 case IDNA_MALLOC_ERROR
:
878 case IDNA_DLOPEN_ERROR
:
886 #elif defined (_WIN32)
892 wchar_t *wide
= ToWide (idn
);
896 int len
= IdnToAscii (IDN_ALLOW_UNASSIGNED
, wide
, -1, NULL
, 0);
903 wchar_t *buf
= vlc_alloc (len
, sizeof (*buf
));
904 if (unlikely(buf
== NULL
))
906 if (!IdnToAscii (IDN_ALLOW_UNASSIGNED
, wide
, -1, buf
, len
))
912 ret
= FromWide (buf
);
919 /* No IDN support, filter out non-ASCII domain names */
920 for (const char *p
= idn
; *p
; p
++)
921 if (((unsigned char)*p
) >= 0x80)