1 /*****************************************************************************
2 * vlc_url.h: URL related macros
3 *****************************************************************************
4 * Copyright (C) 2002-2006 VLC authors and VideoLAN
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 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.
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.
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 *****************************************************************************/
30 * This file defines functions for manipulating URL in vlc
45 char *psz_buffer; /* to be freed */
48 VLC_API char * decode_URI_duplicate( const char *psz );
49 VLC_API char * decode_URI( char *psz );
50 VLC_API char * encode_URI_component( const char *psz );
51 VLC_API char * make_URI( const char *path, const char *scheme );
52 VLC_API char * make_path( const char *url );
54 /*****************************************************************************
56 *****************************************************************************
57 * option : if != 0 then path is split at this char
59 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
60 *****************************************************************************/
61 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
69 url->psz_protocol = NULL;
70 url->psz_username = NULL;
71 url->psz_password = NULL;
75 url->psz_option = NULL;
79 url->psz_buffer = NULL;
82 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
84 /* Search a valid protocol */
85 p = strstr( psz_parse, ":/" );
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 != '.' )
102 /* we have a protocol */
108 url->psz_protocol = psz_parse;
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;
119 psz_parse = strchr( psz_parse, ':' );
120 if( psz_parse != NULL )
122 /* We have a password */
124 url->psz_password = psz_parse;
125 decode_URI( url->psz_password );
127 decode_URI( url->psz_username );
131 p = strchr( psz_parse, '/' );
132 if( !p || psz_parse < p )
134 /* We have a host[:port] */
135 url->psz_host = strdup( psz_parse );
138 url->psz_host[p - psz_parse] = '\0';
141 if( *url->psz_host == '[' )
144 p2 = strchr( url->psz_host, ']' );
147 p2 = strchr( p2, ':' );
152 p2 = strchr( url->psz_host, ':' );
157 url->i_port = atoi( p2 );
162 /* Now parse psz_path and psz_option */
165 url->psz_path = psz_parse;
168 p = strchr( url->psz_path, option );
178 /*****************************************************************************
180 *****************************************************************************/
181 static inline void vlc_UrlClean( vlc_url_t *url )
183 free( url->psz_buffer );
184 free( url->psz_host );
186 url->psz_protocol = NULL;
187 url->psz_username = NULL;
188 url->psz_password = NULL;
189 url->psz_host = NULL;
191 url->psz_path = NULL;
192 url->psz_option = NULL;
194 url->psz_buffer = NULL;
199 /** Check whether a given string is not a valid URL and must hence be
201 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
205 for( ptr = psz_url; *ptr; ptr++ )
207 unsigned char c = *ptr;
211 if( !isxdigit( (unsigned char)ptr[1] )
212 || !isxdigit( (unsigned char)ptr[2] ) )
213 return 1; /* not encoded */
217 if( ( (unsigned char)( c - 'a' ) < 26 )
218 || ( (unsigned char)( c - 'A' ) < 26 )
219 || ( (unsigned char)( c - '0' ) < 10 )
220 || ( strchr( "-_.", c ) != NULL ) )
223 return 0; /* looks fine - but maybe it is not encoded */