]> git.sesse.net Git - vlc/blob - include/vlc_url.h
oups, better put the prototype before using the function
[vlc] / include / vlc_url.h
1 /*****************************************************************************
2  * vlc_url.h: URL related macros
3  *****************************************************************************
4  * Copyright (C) 2002-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          RĂ©mi Denis-Courmont <rem # videolan.org>
9  *
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.
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 General Public License for more details.
19  *
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  *****************************************************************************/
24
25 #if !defined( __LIBVLC__ )
26   #error You are not libvlc or one of its plugins. You cannot include this file
27 #endif
28
29 #ifndef __VLC_URL_H
30 # define __VLC_URL_H
31
32 typedef struct
33 {
34     char *psz_protocol;
35     char *psz_username;
36     char *psz_password;
37     char *psz_host;
38     int  i_port;
39
40     char *psz_path;
41
42     char *psz_option;
43
44     char *psz_buffer; /* to be freed */
45 } vlc_url_t;
46
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 ) );
52
53 /*****************************************************************************
54  * vlc_UrlParse:
55  *****************************************************************************
56  * option : if != 0 then path is split at this char
57  *
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 )
62 {
63     char *psz_dup;
64     char *psz_parse;
65     char *p;
66
67     url->psz_protocol = NULL;
68     url->psz_username = NULL;
69     url->psz_password = NULL;
70     url->psz_host     = NULL;
71     url->i_port       = 0;
72     url->psz_path     = NULL;
73     url->psz_option   = NULL;
74
75     if( psz_url == NULL )
76     {
77         url->psz_buffer = NULL;
78         return;
79     }
80     url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
81
82     /* Search a valid protocol */
83     p  = strstr( psz_parse, ":/" );
84     if( p != NULL )
85     {
86         char *p2;
87         for( p2 = psz_parse; p2 < p; p2++ )
88         {
89 #define I(i,a,b) ( (a) <= (i) && (i) <= (b) )
90             if( !I(*p2, 'a', 'z' ) && !I(*p2, 'A', 'Z') && !I(*p2, '0', '9') && *p2 != '+' && *p2 != '-' && *p2 != '.' )
91             {
92                 p = NULL;
93                 break;
94             }
95 #undef I
96         }
97     }
98
99     if( p != NULL )
100     {
101         /* we have a protocol */
102
103         /* skip :// */
104         *p++ = '\0';
105         if( p[1] == '/' )
106             p += 2;
107         url->psz_protocol = psz_parse;
108         psz_parse = p;
109     }
110     p = strchr( psz_parse, '@' );
111     if( p != NULL )
112     {
113         /* We have a login */
114         url->psz_username = psz_parse;
115         *p++ = '\0';
116
117         psz_parse = strchr( psz_parse, ':' );
118         if( psz_parse != NULL )
119         {
120             /* We have a password */
121             *psz_parse++ = '\0';
122             url->psz_password = psz_parse;
123             decode_URI( url->psz_password );
124         }
125         decode_URI( url->psz_username );
126         psz_parse = p;
127     }
128
129     p = strchr( psz_parse, '/' );
130     if( !p || psz_parse < p )
131     {
132         char *p2;
133
134         /* We have a host[:port] */
135         url->psz_host = strdup( psz_parse );
136         if( p )
137         {
138             url->psz_host[p - psz_parse] = '\0';
139         }
140
141         if( *url->psz_host == '[' )
142         {
143             /* Ipv6 address */
144             p2 = strchr( url->psz_host, ']' );
145             if( p2 )
146             {
147                 p2 = strchr( p2, ':' );
148             }
149         }
150         else
151         {
152             p2 = strchr( url->psz_host, ':' );
153         }
154         if( p2 )
155         {
156             *p2++ = '\0';
157             url->i_port = atoi( p2 );
158         }
159     }
160     psz_parse = p;
161
162     /* Now parse psz_path and psz_option */
163     if( psz_parse )
164     {
165         url->psz_path = psz_parse;
166         if( option != '\0' )
167         {
168             p = strchr( url->psz_path, option );
169             if( p )
170             {
171                 *p++ = '\0';
172                 url->psz_option = p;
173             }
174         }
175     }
176 }
177
178 /*****************************************************************************
179  * vlc_UrlClean:
180  *****************************************************************************/
181 static inline void vlc_UrlClean( vlc_url_t *url )
182 {
183     if( url->psz_buffer ) free( url->psz_buffer );
184     if( url->psz_host )   free( url->psz_host );
185
186     url->psz_protocol = NULL;
187     url->psz_username = NULL;
188     url->psz_password = NULL;
189     url->psz_host     = NULL;
190     url->i_port       = 0;
191     url->psz_path     = NULL;
192     url->psz_option   = NULL;
193
194     url->psz_buffer   = NULL;
195 }
196
197
198 static inline char *vlc_UrlEncode( const char *psz_url )
199 {
200     /* FIXME: do not encode / : ? and & _when_ not needed */
201     return encode_URI_component( psz_url );
202 }
203
204 #include <ctype.h>
205
206 /** Check whether a given string is not a valid URL and must hence be
207  *  encoded */
208 static inline int vlc_UrlIsNotEncoded( const char *psz_url )
209 {
210     const char *ptr;
211
212     for( ptr = psz_url; *ptr; ptr++ )
213     {
214         char c = *ptr;
215
216         if( c == '%' )
217         {
218             if( !isxdigit( ptr[1] ) || !isxdigit( ptr[2] ) )
219                 return 1; /* not encoded */
220             ptr += 2;
221         }
222         else
223         if(  ( (unsigned char)( c - 'a' ) < 26 )
224           || ( (unsigned char)( c - 'A' ) < 26 )
225           || ( (unsigned char)( c - '0' ) < 10 )
226           || ( strchr( "-_.", c ) != NULL ) )
227             return 1;
228     }
229     return 0; /* looks fine - but maybe it is not encoded */
230 }
231
232
233 #endif