Update after filrs added and renamed
[vlc.git] / include / vlc_url.h
blob3d26ba8dd7412e4edcdf465166cffbbfc665a2c8
1 /*****************************************************************************
2 * vlc_url.h: URL related macros
3 *****************************************************************************
4 * Copyright (C) 2002-2006 the VideoLAN team
5 * $Id$
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * RĂ©mi Denis-Courmont <rem # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #if !defined( __LIBVLC__ )
26 #error You are not libvlc or one of its plugins. You cannot include this file
27 #endif
29 #ifndef __VLC_URL_H
30 # define __VLC_URL_H
32 struct vlc_url_t
34 char *psz_protocol;
35 char *psz_username;
36 char *psz_password;
37 char *psz_host;
38 int i_port;
40 char *psz_path;
42 char *psz_option;
44 char *psz_buffer; /* to be freed */
47 VLC_EXPORT( char *, unescape_URI_duplicate, ( const char *psz ) );
48 VLC_EXPORT( void, unescape_URI, ( char *psz ) );
49 VLC_EXPORT( char *, decode_URI_duplicate, ( const char *psz ) );
50 VLC_EXPORT( void, decode_URI, ( char *psz ) );
51 VLC_EXPORT( char *, encode_URI_component, ( const char *psz ) );
53 /*****************************************************************************
54 * vlc_UrlParse:
55 *****************************************************************************
56 * option : if != 0 then path is split at this char
58 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
59 *****************************************************************************/
60 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
61 char option )
63 char *psz_dup;
64 char *psz_parse;
65 char *p;
66 char *p2;
68 url->psz_protocol = NULL;
69 url->psz_username = NULL;
70 url->psz_password = NULL;
71 url->psz_host = NULL;
72 url->i_port = 0;
73 url->psz_path = NULL;
74 url->psz_option = NULL;
76 if( psz_url == NULL )
78 url->psz_buffer = NULL;
79 return;
81 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
83 /* Search a valid protocol */
84 p = strstr( psz_parse, ":/" );
85 if( p != NULL )
87 char *p2;
88 for( p2 = psz_parse; p2 < p; p2++ )
90 #define I(i,a,b) ( (a) <= (i) && (i) <= (b) )
91 if( !I(*p2, 'a', 'z' ) && !I(*p2, 'A', 'Z') && !I(*p2, '0', '9') && *p2 != '+' && *p2 != '-' && *p2 != '.' )
93 p = NULL;
94 break;
96 #undef I
100 if( p != NULL )
102 /* we have a protocol */
104 /* skip :// */
105 *p++ = '\0';
106 if( p[1] == '/' )
107 p += 2;
108 url->psz_protocol = psz_parse;
109 psz_parse = p;
111 p = strchr( psz_parse, '@' );
112 p2 = strchr( psz_parse, '/' );
113 if( p != NULL && ( p2 != NULL ? p < p2 : 1 ) )
115 /* We have a login */
116 url->psz_username = psz_parse;
117 *p++ = '\0';
119 psz_parse = strchr( psz_parse, ':' );
120 if( psz_parse != NULL )
122 /* We have a password */
123 *psz_parse++ = '\0';
124 url->psz_password = psz_parse;
125 decode_URI( url->psz_password );
127 decode_URI( url->psz_username );
128 psz_parse = p;
131 p = strchr( psz_parse, '/' );
132 if( !p || psz_parse < p )
134 char *p2;
136 /* We have a host[:port] */
137 url->psz_host = strdup( psz_parse );
138 if( p )
140 url->psz_host[p - psz_parse] = '\0';
143 if( *url->psz_host == '[' )
145 /* Ipv6 address */
146 p2 = strchr( url->psz_host, ']' );
147 if( p2 )
149 p2 = strchr( p2, ':' );
152 else
154 p2 = strchr( url->psz_host, ':' );
156 if( p2 )
158 *p2++ = '\0';
159 url->i_port = atoi( p2 );
162 psz_parse = p;
164 /* Now parse psz_path and psz_option */
165 if( psz_parse )
167 url->psz_path = psz_parse;
168 if( option != '\0' )
170 p = strchr( url->psz_path, option );
171 if( p )
173 *p++ = '\0';
174 url->psz_option = p;
180 /*****************************************************************************
181 * vlc_UrlClean:
182 *****************************************************************************/
183 static inline void vlc_UrlClean( vlc_url_t *url )
185 if( url->psz_buffer ) free( url->psz_buffer );
186 if( url->psz_host ) free( url->psz_host );
188 url->psz_protocol = NULL;
189 url->psz_username = NULL;
190 url->psz_password = NULL;
191 url->psz_host = NULL;
192 url->i_port = 0;
193 url->psz_path = NULL;
194 url->psz_option = NULL;
196 url->psz_buffer = NULL;
200 static inline char *vlc_UrlEncode( const char *psz_url )
202 /* FIXME: do not encode / : ? and & _when_ not needed */
203 return encode_URI_component( psz_url );
206 #include <ctype.h>
208 /** Check whether a given string is not a valid URL and must hence be
209 * encoded */
210 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
212 const char *ptr;
214 for( ptr = psz_url; *ptr; ptr++ )
216 char c = *ptr;
218 if( c == '%' )
220 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
221 return 1; /* not encoded */
222 ptr += 2;
224 else
225 if( ( (unsigned char)( c - 'a' ) < 26 )
226 || ( (unsigned char)( c - 'A' ) < 26 )
227 || ( (unsigned char)( c - '0' ) < 10 )
228 || ( strchr( "-_.", c ) != NULL ) )
229 return 1;
231 return 0; /* looks fine - but maybe it is not encoded */
235 #endif