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 static int vlc_UrlParseInner(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
;
414 url
->psz_pathbuffer
= NULL
;
422 char *buf
= vlc_iri2uri(str
);
423 if (unlikely(buf
== NULL
))
425 url
->psz_buffer
= buf
;
427 char *cur
= buf
, *next
;
432 while ((*next
>= 'A' && *next
<= 'Z') || (*next
>= 'a' && *next
<= 'z')
433 || (*next
>= '0' && *next
<= '9') || memchr ("+-.", *next
, 3) != NULL
)
439 url
->psz_protocol
= cur
;
444 next
= strchr(cur
, '#');
449 url
->psz_fragment
= next
;
455 /* Query parameters */
456 next
= strchr(cur
, '?');
460 url
->psz_option
= next
;
464 if (strncmp(cur
, "//", 2) == 0)
469 next
= strchr(cur
, '/');
472 *next
= '\0'; /* temporary nul, reset to slash later */
473 url
->psz_path
= next
;
476 url->psz_path = "/";*/
479 next
= strrchr(cur
, '@');
483 url
->psz_username
= cur
;
486 /* Password (obsolete) */
487 next
= strchr(url
->psz_username
, ':');
491 url
->psz_password
= next
;
492 vlc_uri_decode(url
->psz_password
);
494 vlc_uri_decode(url
->psz_username
);
498 if (*cur
== '[' && (next
= strrchr(cur
, ']')) != NULL
)
499 { /* Try IPv6 numeral within brackets */
501 url
->psz_host
= strdup(cur
+ 1);
510 next
= strchr(cur
, ':');
514 url
->psz_host
= vlc_idna_to_ascii(vlc_uri_decode(cur
));
517 if (url
->psz_host
== NULL
)
520 if (!vlc_uri_host_validate(url
->psz_host
))
523 url
->psz_host
= NULL
;
529 if (next
!= NULL
&& *next
)
532 unsigned long port
= strtoul(next
, &end
, 10);
534 if (strchr("0123456789", *next
) == NULL
|| *end
|| port
> UINT_MAX
)
543 if (url
->psz_path
!= NULL
)
544 *url
->psz_path
= '/'; /* restore leading slash */
554 int vlc_UrlParse(vlc_url_t
*url
, const char *str
)
556 int ret
= vlc_UrlParseInner(url
, str
);
558 if (url
->psz_path
!= NULL
&& !vlc_uri_path_validate(url
->psz_path
))
560 url
->psz_path
= NULL
;
567 static char *vlc_uri_fixup_inner(const char *str
, const char *extras
);
569 int vlc_UrlParseFixup(vlc_url_t
*url
, const char *str
)
571 int ret
= vlc_UrlParseInner(url
, str
);
573 static const char pathextras
[] = "/@:";
575 if (url
->psz_path
!= NULL
576 && !vlc_uri_component_validate(url
->psz_path
, pathextras
))
578 url
->psz_pathbuffer
= vlc_uri_fixup_inner(url
->psz_path
, pathextras
);
579 if (url
->psz_pathbuffer
== NULL
)
581 url
->psz_path
= NULL
;
587 url
->psz_path
= url
->psz_pathbuffer
;
588 assert(vlc_uri_path_validate(url
->psz_path
));
594 void vlc_UrlClean (vlc_url_t
*restrict url
)
596 free (url
->psz_host
);
597 free (url
->psz_buffer
);
598 free (url
->psz_pathbuffer
);
604 * See IETF RFC3986 section 5.2.3 for details.
606 static char *vlc_uri_merge_paths(const char *base
, const char *ref
)
612 len
= asprintf(&str
, "/%s", ref
);
615 const char *end
= strrchr(base
, '/');
622 len
= asprintf(&str
, "%.*s%s", (int)(end
- base
), base
, ref
);
625 if (unlikely(len
== -1))
631 * Remove dot segments
633 * See IETF RFC3986 section 5.2.4 for details.
635 static char *vlc_uri_remove_dot_segments(char *str
)
637 char *input
= str
, *output
= str
;
639 while (input
[0] != '\0')
641 assert(output
<= input
);
643 if (strncmp(input
, "../", 3) == 0)
648 if (strncmp(input
, "./", 2) == 0)
653 if (strncmp(input
, "/./", 3) == 0)
658 if (strcmp(input
, "/.") == 0)
663 if (strncmp(input
, "/../", 4) == 0)
666 output
= memrchr(str
, '/', output
- str
);
671 if (strcmp(input
, "/..") == 0)
674 output
= memrchr(str
, '/', output
- str
);
679 if (strcmp(input
, ".") == 0)
684 if (strcmp(input
, "..") == 0)
691 *(output
++) = *(input
++);
693 size_t len
= strcspn(input
, "/");
696 memmove(output
, input
, len
);
706 char *vlc_uri_compose(const vlc_url_t
*uri
)
708 struct vlc_memstream stream
;
711 vlc_memstream_open(&stream
);
713 if (uri
->psz_protocol
!= NULL
)
714 vlc_memstream_printf(&stream
, "%s:", uri
->psz_protocol
);
716 if (uri
->psz_host
!= NULL
)
718 vlc_memstream_write(&stream
, "//", 2);
720 if (uri
->psz_username
!= NULL
)
722 enc
= vlc_uri_encode(uri
->psz_username
);
726 vlc_memstream_puts(&stream
, enc
);
729 if (uri
->psz_password
!= NULL
)
731 enc
= vlc_uri_encode(uri
->psz_password
);
732 if (unlikely(enc
== NULL
))
735 vlc_memstream_printf(&stream
, ":%s", enc
);
738 vlc_memstream_putc(&stream
, '@');
743 if (strchr(uri
->psz_host
, ':') != NULL
)
744 fmt
= (uri
->i_port
!= 0) ? "[%s]:%d" : "[%s]";
746 fmt
= (uri
->i_port
!= 0) ? "%s:%d" : "%s";
747 /* No IDNA decoding here. Seems unnecessary, dangerous even. */
748 vlc_memstream_printf(&stream
, fmt
, uri
->psz_host
, uri
->i_port
);
751 if (uri
->psz_path
!= NULL
)
752 vlc_memstream_puts(&stream
, uri
->psz_path
);
753 if (uri
->psz_option
!= NULL
)
754 vlc_memstream_printf(&stream
, "?%s", uri
->psz_option
);
755 /* NOTE: fragment not handled currently */
757 if (vlc_memstream_close(&stream
))
762 if (vlc_memstream_close(&stream
) == 0)
767 char *vlc_uri_resolve(const char *base
, const char *ref
)
769 vlc_url_t base_uri
, rel_uri
;
771 char *pathbuf
= NULL
, *ret
= NULL
;
773 if (vlc_UrlParse(&rel_uri
, ref
))
775 vlc_UrlClean(&rel_uri
);
779 if (rel_uri
.psz_protocol
!= NULL
)
780 { /* Short circuit in case of absolute URI */
781 vlc_UrlClean(&rel_uri
);
785 vlc_UrlParse(&base_uri
, base
);
787 /* RFC3986 section 5.2.2 */
791 tgt_uri
.psz_protocol
= base_uri
.psz_protocol
;
793 if (rel_uri
.psz_host
!= NULL
)
796 tgt_uri
.psz_username
= base_uri
.psz_username
;
797 tgt_uri
.psz_password
= base_uri
.psz_password
;
798 tgt_uri
.psz_host
= base_uri
.psz_host
;
799 tgt_uri
.i_port
= base_uri
.i_port
;
801 if (rel_uri
.psz_path
== NULL
|| rel_uri
.psz_path
[0] == '\0')
803 tgt_uri
.psz_path
= base_uri
.psz_path
;
804 if (rel_uri
.psz_option
== NULL
)
805 tgt_uri
.psz_option
= base_uri
.psz_option
;
809 if (rel_uri
.psz_path
[0] == '/')
812 pathbuf
= vlc_uri_merge_paths(base_uri
.psz_path
, rel_uri
.psz_path
);
813 if (unlikely(pathbuf
== NULL
))
816 tgt_uri
.psz_path
= pathbuf
;
820 if (tgt_uri
.psz_path
!= NULL
)
821 vlc_uri_remove_dot_segments(tgt_uri
.psz_path
);
823 ret
= vlc_uri_compose(&tgt_uri
);
826 vlc_UrlClean(&base_uri
);
827 vlc_UrlClean(&rel_uri
);
831 static char *vlc_uri_fixup_inner(const char *str
, const char *extras
)
833 assert(str
&& extras
);
835 bool encode_percent
= false;
836 for (size_t i
= 0; str
[i
] != '\0'; i
++)
837 if (str
[i
] == '%' && !(isurihex(str
[i
+1]) && isurihex(str
[i
+2])))
839 encode_percent
= true;
843 struct vlc_memstream stream
;
845 vlc_memstream_open(&stream
);
847 for (size_t i
= 0; str
[i
] != '\0'; i
++)
849 unsigned char c
= str
[i
];
851 if (isurisafe(c
) || isurisubdelim(c
) || (strchr(extras
, c
) != NULL
)
852 || (c
== '%' && !encode_percent
))
853 vlc_memstream_putc(&stream
, c
);
855 vlc_memstream_printf(&stream
, "%%%02hhX", c
);
858 if (vlc_memstream_close(&stream
))
863 char *vlc_uri_fixup(const char *str
)
865 static const char extras
[] = ":/?#[]@";
867 /* Rule number one is do not change a (potentially) valid URI */
868 if (vlc_uri_component_validate(str
, extras
))
871 return vlc_uri_fixup_inner(str
, extras
);
874 #if defined (HAVE_IDN)
876 #elif defined (_WIN32)
877 # include <windows.h>
878 # include <vlc_charset.h>
882 * Converts a UTF-8 nul-terminated IDN to nul-terminated ASCII domain name.
883 * \param idn UTF-8 Internationalized Domain Name to convert
884 * \return a heap-allocated string or NULL on error.
886 static char *vlc_idna_to_ascii (const char *idn
)
888 #if defined (HAVE_IDN)
891 switch (idna_to_ascii_8z(idn
, &adn
, IDNA_ALLOW_UNASSIGNED
))
895 case IDNA_MALLOC_ERROR
:
898 case IDNA_DLOPEN_ERROR
:
906 #elif defined (_WIN32)
912 wchar_t *wide
= ToWide (idn
);
916 int len
= IdnToAscii (IDN_ALLOW_UNASSIGNED
, wide
, -1, NULL
, 0);
923 wchar_t *buf
= vlc_alloc (len
, sizeof (*buf
));
924 if (unlikely(buf
== NULL
))
926 if (!IdnToAscii (IDN_ALLOW_UNASSIGNED
, wide
, -1, buf
, len
))
932 ret
= FromWide (buf
);
939 /* No IDN support, filter out non-ASCII domain names */
940 for (const char *p
= idn
; *p
; p
++)
941 if (((unsigned char)*p
) >= 0x80)