VLC  3.0.15
vlc_url.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vlc_url.h: URL related macros
3  *****************************************************************************
4  * Copyright (C) 2002-2006 VLC authors and VideoLAN
5  * $Id: b76cea9893c5c94731202694e49cdeff5d758cb6 $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  * Rémi Denis-Courmont
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 
25 #ifndef VLC_URL_H
26 # define VLC_URL_H
27 
28 /**
29  * \file
30  * This file defines functions for manipulating URL in vlc
31  *
32  * \ingroup strings
33  * @{
34  */
35 
36 /**
37  * Converts local path to URL.
38  *
39  * Builds a URL representation from a local UTF-8 null-terminated file path.
40  *
41  * @param path file path
42  * @param scheme URI scheme to use (default is auto: "file", "fd" or "smb")
43  * @return a heap-allocated URI string on success
44  * or NULL in case of error (errno will be set accordingly)
45  */
46 VLC_API char *vlc_path2uri(const char *path, const char *scheme) VLC_MALLOC;
47 
48 /**
49  * Converts a URI to a local path.
50  *
51  * Builds a local path (UTF-8-encoded null-terminated string) from a URI if
52  * the URI scheme allows.
53  *
54  * @param url URI
55  * @return a heap-allocated string or success
56  * or NULL on error
57  */
58 VLC_API char *vlc_uri2path(const char *url) VLC_MALLOC;
59 
60 /**
61  * Decodes an URI component in place.
62  *
63  * Decodes one null-terminated UTF-8 URI component to aa null-terminated UTF-8
64  * string in place.
65  *
66  * See also vlc_uri_decode_duplicate() for the not-in-place variant.
67  *
68  * \warning <b>This function does NOT decode entire URIs.</b>
69  * URI can only be decoded (and encoded) one component at a time
70  * (e.g. the host name, one directory, the file name).
71  * Complete URIs are always "encoded" (or they are syntaxically invalid).
72  * See IETF RFC3986, especially §2.4 for details.
73  *
74  * \note URI encoding is <b>different</b> from Javascript escaping. Especially,
75  * white spaces and Unicode non-ASCII code points are encoded differently.
76  *
77  * \param str null-terminated component
78  * \return str is returned on success. NULL if str was not properly encoded.
79  */
80 VLC_API char *vlc_uri_decode(char *str);
81 
82 /**
83  * Decodes an URI component.
84  *
85  * See also vlc_uri_decode() for the in-place variant.
86  *
87  * \return a heap-allocated string on success or NULL on error.
88  */
89 VLC_API char *vlc_uri_decode_duplicate(const char *str) VLC_MALLOC;
90 
91 /**
92  * Encodes a URI component.
93  *
94  * Substitutes URI-unsafe, URI delimiters and non-ASCII characters into their
95  * URI-encoded URI-safe representation. See also IETF RFC3986 §2.
96  *
97  * @param str nul-terminated UTF-8 representation of the component.
98  * @note Obviously, a URI containing nul bytes cannot be passed.
99  * @return heap-allocated string, or NULL if out of memory.
100  */
101 VLC_API char *vlc_uri_encode(const char *str) VLC_MALLOC;
102 
103 /**
104  * Composes an URI.
105  *
106  * Converts a decomposed/parsed URI structure (\ref vlc_url_t) into a
107  * nul-terminated URI literal string.
108  *
109  * See also IETF RFC3986 section 5.3 for details.
110  *
111  * \bug URI fragments (i.e. HTML anchors) are not handled
112  *
113  * \return a heap-allocated nul-terminated string or NULL if out of memory
114  */
116 
117 /**
118  * Resolves an URI reference.
119  *
120  * Resolves an URI reference relative to a base URI.
121  * If the reference is an absolute URI, then this function simply returns a
122  * copy of the URI reference.
123  *
124  * \param base base URI (as a nul-terminated string)
125  * \param ref URI reference (also as a nul-terminated string)
126  *
127  * \return a heap-allocated nul-terminated string representing the resolved
128  * absolute URI, or NULL if out of memory.
129  */
130 VLC_API char *vlc_uri_resolve(const char *base, const char *ref) VLC_MALLOC;
131 
132 /**
133  * Fixes up a URI string.
134  *
135  * Attempts to convert a nul-terminated string into a syntactically valid URI.
136  * If the string is, or may be, a syntactically valid URI, an exact copy is
137  * returned. In any case, the result will only contain URI-safe and URI
138  * delimiter characters (generic delimiters or sub-delimiters) and all percent
139  * signs will be followed by two hexadecimal characters.
140  *
141  * @return a heap-allocated string, or NULL if on out of memory.
142  */
143 VLC_API char *vlc_uri_fixup(const char *) VLC_MALLOC;
144 
145 struct vlc_url_t
146 {
150  char *psz_host;
151  unsigned i_port;
152  char *psz_path;
153  char *psz_option;
154 
155  char *psz_buffer; /* to be freed */
156  char *psz_pathbuffer; /* to be freed */
157 };
158 
159 /**
160  * Parses an URI or IRI.
161  *
162  * Extracts the following parts from an URI string:
163  * - scheme (i.e. protocol),
164  * - user (deprecated),
165  * - password (also deprecated),
166  * - host name or IP address literal,
167  * - port number,
168  * - path (including the filename preceded by any and all directories)
169  * - request parameters (excluding the leading question mark '?').
170  *
171  * The function accepts URIs, as well as UTF-8-encoded IRIs. For IRIs, the hier
172  * part (specifically, the host name) is assumed to be an IDN and is decoded to
173  * ASCII according, so it can be used for DNS resolution. If the host is an
174  * IPv6 address literal, brackets are stripped.
175  *
176  * Any missing part is set to nul. For historical reasons, the target structure
177  * is always initialized, even if parsing the URI string fails.
178  *
179  * On error, errno is set to one of the following value:
180  * - ENOMEM in case of memory allocation failure,
181  * - EINVAL in case of syntax error in the input string.
182  *
183  * \bug The URI fragment is discarded if present.
184  *
185  * \note This function allocates memory. vlc_UrlClean() must be used free
186  * associated the allocations, even if the function fails.
187  *
188  * \param url structure of URL parts [OUT]
189  * \param str nul-terminated URL string to split
190  * \retval 0 success
191  * \retval -1 failure
192  */
193 VLC_API int vlc_UrlParse(vlc_url_t *url, const char *str);
194 
195 /**
196  * Parses an URI or IRI and fix up the path part.
197  *
198  * \see vlc_UrlParse
199  * \see vlc_uri_fixup
200  */
201 VLC_API int vlc_UrlParseFixup(vlc_url_t *url, const char *str);
202 
203 /**
204  * Releases resources allocated by vlc_UrlParse().
205  */
207 
208 /** @} */
209 
210 #endif
vlc_uri_decode
char * vlc_uri_decode(char *str)
Decodes an URI component in place.
Definition: url.c:53
VLC_API
#define VLC_API
Definition: fourcc_gen.c:30
vlc_common.h
vlc_uri_compose
char * vlc_uri_compose(const vlc_url_t *)
Composes an URI.
Definition: url.c:706
vlc_uri_decode_duplicate
char * vlc_uri_decode_duplicate(const char *str)
Decodes an URI component.
Definition: url.c:42
vlc_url_t::psz_password
char * psz_password
Definition: vlc_url.h:149
vlc_uri2path
char * vlc_uri2path(const char *url)
Converts a URI to a local path.
Definition: url.c:240
vlc_url_t::psz_option
char * psz_option
Definition: vlc_url.h:153
vlc_url_t::psz_host
char * psz_host
Definition: vlc_url.h:150
vlc_url_t::psz_protocol
char * psz_protocol
Definition: vlc_url.h:147
vlc_url_t::psz_username
char * psz_username
Definition: vlc_url.h:148
vlc_path2uri
char * vlc_path2uri(const char *path, const char *scheme)
Converts local path to URL.
Definition: url.c:138
vlc_uri_encode
char * vlc_uri_encode(const char *str)
Encodes a URI component.
Definition: url.c:129
vlc_url_t
Definition: vlc_url.h:145
vlc_uri_fixup
char * vlc_uri_fixup(const char *)
Fixes up a URI string.
Definition: url.c:863
vlc_UrlParseFixup
int vlc_UrlParseFixup(vlc_url_t *url, const char *str)
Parses an URI or IRI and fix up the path part.
Definition: url.c:569
vlc_url_t::i_port
unsigned i_port
Definition: vlc_url.h:151
VLC_MALLOC
#define VLC_MALLOC
Definition: vlc_common.h:102
vlc_url_t::psz_pathbuffer
char * psz_pathbuffer
Definition: vlc_url.h:156
vlc_UrlParse
int vlc_UrlParse(vlc_url_t *url, const char *str)
Parses an URI or IRI.
Definition: url.c:554
vlc_UrlClean
void vlc_UrlClean(vlc_url_t *)
Releases resources allocated by vlc_UrlParse().
vlc_url_t::psz_path
char * psz_path
Definition: vlc_url.h:152
vlc_url_t::psz_buffer
char * psz_buffer
Definition: vlc_url.h:155
vlc_uri_resolve
char * vlc_uri_resolve(const char *base, const char *ref)
Resolves an URI reference.
Definition: url.c:767