]> git.sesse.net Git - vlc/blob - include/vlc_url.h
make_URI: make a URI out of a path, if needed
[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 /**
29  * \file
30  * This file defines functions for manipulating URL in vlc
31  */
32
33 struct vlc_url_t
34 {
35     char *psz_protocol;
36     char *psz_username;
37     char *psz_password;
38     char *psz_host;
39     int  i_port;
40
41     char *psz_path;
42
43     char *psz_option;
44
45     char *psz_buffer; /* to be freed */
46 };
47
48 VLC_EXPORT( char *, unescape_URI_duplicate, ( const char *psz ) );
49 VLC_EXPORT( void, unescape_URI, ( char *psz ) );
50 VLC_EXPORT( char *, decode_URI_duplicate, ( const char *psz ) );
51 VLC_EXPORT( char *, decode_URI, ( char *psz ) );
52 VLC_EXPORT( char *, encode_URI_component, ( const char *psz ) );
53 VLC_EXPORT( char *, make_URI, ( const char *path ) );
54
55 /*****************************************************************************
56  * vlc_UrlParse:
57  *****************************************************************************
58  * option : if != 0 then path is split at this char
59  *
60  * format [protocol://[login[:password]@]][host[:port]]/path[OPTIONoption]
61  *****************************************************************************/
62 static inline void vlc_UrlParse( vlc_url_t *url, const char *psz_url,
63                                  char option )
64 {
65     char *psz_dup;
66     char *psz_parse;
67     char *p;
68     char *p2;
69
70     url->psz_protocol = NULL;
71     url->psz_username = NULL;
72     url->psz_password = NULL;
73     url->psz_host     = NULL;
74     url->i_port       = 0;
75     url->psz_path     = NULL;
76     url->psz_option   = NULL;
77
78     if( psz_url == NULL )
79     {
80         url->psz_buffer = NULL;
81         return;
82     }
83     url->psz_buffer = psz_parse = psz_dup = strdup( psz_url );
84
85     /* Search a valid protocol */
86     p  = strstr( psz_parse, ":/" );
87     if( p != NULL )
88     {
89         for( p2 = psz_parse; p2 < p; p2++ )
90         {
91 #define I(i,a,b) ( (a) <= (i) && (i) <= (b) )
92             if( !I(*p2, 'a', 'z' ) && !I(*p2, 'A', 'Z') && !I(*p2, '0', '9') && *p2 != '+' && *p2 != '-' && *p2 != '.' )
93             {
94                 p = NULL;
95                 break;
96             }
97 #undef I
98         }
99     }
100
101     if( p != NULL )
102     {
103         /* we have a protocol */
104
105         /* skip :// */
106         *p++ = '\0';
107         if( p[1] == '/' )
108             p += 2;
109         url->psz_protocol = psz_parse;
110         psz_parse = p;
111     }
112     p = strchr( psz_parse, '@' );
113     p2 = strchr( psz_parse, '/' );
114     if( p != NULL && ( p2 != NULL ? p < p2 : 1 ) )
115     {
116         /* We have a login */
117         url->psz_username = psz_parse;
118         *p++ = '\0';
119
120         psz_parse = strchr( psz_parse, ':' );
121         if( psz_parse != NULL )
122         {
123             /* We have a password */
124             *psz_parse++ = '\0';
125             url->psz_password = psz_parse;
126             decode_URI( url->psz_password );
127         }
128         decode_URI( url->psz_username );
129         psz_parse = p;
130     }
131
132     p = strchr( psz_parse, '/' );
133     if( !p || psz_parse < p )
134     {
135         /* We have a host[:port] */
136         url->psz_host = strdup( psz_parse );
137         if( p )
138         {
139             url->psz_host[p - psz_parse] = '\0';
140         }
141
142         if( *url->psz_host == '[' )
143         {
144             /* Ipv6 address */
145             p2 = strchr( url->psz_host, ']' );
146             if( p2 )
147             {
148                 p2 = strchr( p2, ':' );
149             }
150         }
151         else
152         {
153             p2 = strchr( url->psz_host, ':' );
154         }
155         if( p2 )
156         {
157             *p2++ = '\0';
158             url->i_port = atoi( p2 );
159         }
160     }
161     psz_parse = p;
162
163     /* Now parse psz_path and psz_option */
164     if( psz_parse )
165     {
166         url->psz_path = psz_parse;
167         if( option != '\0' )
168         {
169             p = strchr( url->psz_path, option );
170             if( p )
171             {
172                 *p++ = '\0';
173                 url->psz_option = p;
174             }
175         }
176     }
177 }
178
179 /*****************************************************************************
180  * vlc_UrlClean:
181  *****************************************************************************/
182 static inline void vlc_UrlClean( vlc_url_t *url )
183 {
184     free( url->psz_buffer );
185     free( url->psz_host );
186
187     url->psz_protocol = NULL;
188     url->psz_username = NULL;
189     url->psz_password = NULL;
190     url->psz_host     = NULL;
191     url->i_port       = 0;
192     url->psz_path     = NULL;
193     url->psz_option   = NULL;
194
195     url->psz_buffer   = NULL;
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 #endif