vlc_url.h: Username and password passed in the URL can contain encoded @ : and /...
[vlc.git] / include / vlc_url.h
blobb501b3ae2d355cad01df22229cfbdbd763e07915
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 typedef struct
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 */
45 } vlc_url_t;
47 /*****************************************************************************
48 * vlc_UrlParse:
49 *****************************************************************************
50 * option : if != 0 then path is split at this char
52 * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
53 *****************************************************************************/
54 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
55 char option )
57 char *psz_dup;
58 char *psz_parse;
59 char *p;
61 url->psz_protocol = NULL;
62 url->psz_username = NULL;
63 url->psz_password = NULL;
64 url->psz_host = NULL;
65 url->i_port = 0;
66 url->psz_path = NULL;
67 url->psz_option = NULL;
69 if( psz_url == NULL )
71 url->psz_buffer = NULL;
72 return;
74 url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
76 /* Search a valid protocol */
77 p = strstr( psz_parse, ":/" );
78 if( p != NULL )
80 char *p2;
81 for( p2 = psz_parse; p2 < p; p2++ )
83 #define I(i,a,b) ( (a) <= (i) && (i) <= (b) )
84 if( !I(*p2, 'a', 'z' ) && !I(*p2, 'A', 'Z') && !I(*p2, '0', '9') && *p2 != '+' && *p2 != '-' && *p2 != '.' )
86 p = NULL;
87 break;
89 #undef I
93 if( p != NULL )
95 /* we have a protocol */
97 /* skip :// */
98 *p++ = '\0';
99 if( p[1] == '/' )
100 p += 2;
101 url->psz_protocol = psz_parse;
102 psz_parse = p;
104 p = strchr( psz_parse, '@' );
105 if( p != NULL )
107 /* We have a login */
108 url->psz_username = psz_parse;
109 *p++ = '\0';
111 psz_parse = strchr( psz_parse, ':' );
112 if( psz_parse != NULL )
114 /* We have a password */
115 *psz_parse++ = '\0';
116 url->psz_password = psz_parse;
117 decode_URI( url->psz_password );
119 decode_URI( url->psz_username );
120 psz_parse = p;
123 p = strchr( psz_parse, '/' );
124 if( !p || psz_parse < p )
126 char *p2;
128 /* We have a host[:port] */
129 url->psz_host = strdup( psz_parse );
130 if( p )
132 url->psz_host[p - psz_parse] = '\0';
135 if( *url->psz_host == '[' )
137 /* Ipv6 address */
138 p2 = strchr( url->psz_host, ']' );
139 if( p2 )
141 p2 = strchr( p2, ':' );
144 else
146 p2 = strchr( url->psz_host, ':' );
148 if( p2 )
150 *p2++ = '\0';
151 url->i_port = atoi( p2 );
154 psz_parse = p;
156 /* Now parse psz_path and psz_option */
157 if( psz_parse )
159 url->psz_path = psz_parse;
160 if( option != '\0' )
162 p = strchr( url->psz_path, option );
163 if( p )
165 *p++ = '\0';
166 url->psz_option = p;
172 /*****************************************************************************
173 * vlc_UrlClean:
174 *****************************************************************************/
175 static inline void vlc_UrlClean( vlc_url_t *url )
177 if( url->psz_buffer ) free( url->psz_buffer );
178 if( url->psz_host ) free( url->psz_host );
180 url->psz_protocol = NULL;
181 url->psz_username = NULL;
182 url->psz_password = NULL;
183 url->psz_host = NULL;
184 url->i_port = 0;
185 url->psz_path = NULL;
186 url->psz_option = NULL;
188 url->psz_buffer = NULL;
191 VLC_EXPORT( char *, unescape_URI_duplicate, ( const char *psz ) );
192 VLC_EXPORT( void, unescape_URI, ( char *psz ) );
193 VLC_EXPORT( char *, decode_URI_duplicate, ( const char *psz ) );
194 VLC_EXPORT( void, decode_URI, ( char *psz ) );
195 VLC_EXPORT( char *, encode_URI_component, ( const char *psz ) );
197 static inline char *vlc_UrlEncode( const char *psz_url )
199 /* FIXME: do not encode / : ? and & _when_ not needed */
200 return encode_URI_component( psz_url );
203 #include <ctype.h>
205 /** Check whether a given string is not a valid URL and must hence be
206 * encoded */
207 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
209 const char *ptr;
211 for( ptr = psz_url; *ptr; ptr++ )
213 char c = *ptr;
215 if( c == '%' )
217 if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
218 return 1; /* not encoded */
219 ptr += 2;
221 else
222 if( ( (unsigned char)( c - 'a' ) < 26 )
223 || ( (unsigned char)( c - 'A' ) < 26 )
224 || ( (unsigned char)( c - '0' ) < 10 )
225 || ( strchr( "-_.", c ) != NULL ) )
226 return 1;
228 return 0; /* looks fine - but maybe it is not encoded */
232 #endif