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