1 /*****************************************************************************
2 * xurl.c: URL manipulation functions
3 *****************************************************************************
4 * Copyright (C) 2003-2004 Commonwealth Scientific and Industrial Research
5 * Organisation (CSIRO) Australia
6 * Copyright (C) 2004 the VideoLAN team
10 * Authors: Andre Pang <Andre.Pang@csiro.au>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
31 static char *streallocat( char *psz_string, char *psz_to_append );
34 static char *xurl_strdup( const char *psz_string );
36 #define xurl_strdup strdup
39 static char *XURL_FindHostname ( char *psz_url );
40 static char *XURL_FindPath ( char *psz_url );
41 static char *XURL_FindFragment ( char *psz_url );
44 char *XURL_Join( char *psz_url1, char *psz_url2 )
46 if( XURL_IsAbsolute( psz_url1 ) )
47 return XURL_Concat( psz_url1, psz_url2 );
49 return XURL_Concat( psz_url2, psz_url1 );
54 /* TODO: replace XURL_Concat's rel/absolute calculation with the one
55 * specified by RFC2396, and also test it on their test suite :) */
58 char *XURL_Concat( char *psz_url, char *psz_append )
60 char *psz_return_value = NULL;
62 if( XURL_IsAbsolute( psz_append ) == XURL_TRUE )
63 return strdup( psz_append );
65 if( XURL_IsAbsolute( psz_url ) )
67 if( XURL_HasAbsolutePath( psz_append ) )
71 psz_concat_url = XURL_GetSchemeAndHostname( psz_url );
73 psz_concat_url = streallocat( psz_concat_url, psz_append );
75 fprintf( stderr, "XURL_Concat: concat is \"%s\"\n",
78 psz_return_value = psz_concat_url;
82 /* psz_append is a relative URL */
85 /* strip off last path component */
86 psz_new_url = XURL_GetHead( psz_url );
87 psz_new_url = streallocat( psz_new_url, psz_append );
89 psz_return_value = psz_new_url;
94 /* not an absolute URL */
95 if( XURL_HasAbsolutePath( psz_append ) == XURL_FALSE )
97 char *psz_new_url = XURL_GetHead( psz_url );
99 psz_new_url = streallocat( psz_new_url, psz_append );
100 psz_return_value = psz_new_url;
104 /* URL to append has an absolute path -- just use that instead */
105 psz_return_value = xurl_strdup( psz_append );
109 return psz_return_value;
113 XURL_Bool XURL_IsAbsolute( char *psz_url )
115 if( XURL_FindHostname( psz_url ) == NULL )
118 fprintf( stderr, "XURL_IsAbsolute(%s) returning false\n", psz_url );
125 fprintf( stderr, "XURL_IsAbsolute(%s) returning true\n", psz_url );
132 XURL_Bool XURL_HasFragment( char *psz_url )
134 if( XURL_FindFragment( psz_url ) == NULL )
141 char *XURL_FindHostname( char *psz_url )
143 char *psz_return_value = NULL;
145 char *psz_scheme_separator = strstr( psz_url, "://" );
146 if( psz_scheme_separator != NULL)
148 char *psz_hostname = psz_scheme_separator + strlen( "://" );
149 if( *psz_hostname != '\0') psz_return_value = psz_hostname;
152 fprintf( stderr, "XURL_FindHostname(%s): returning \"%s\"\n",
153 psz_url, psz_return_value );
157 return psz_return_value;
161 XURL_Bool XURL_HasAbsolutePath( char *psz_url )
163 #ifdef XURL_WIN32_PATHING
164 if( psz_url[0] == '/' || psz_url[0] == '\\' )
166 if( psz_url[0] == '/' )
174 char *XURL_GetHostname( char *psz_url )
176 char *psz_return_value = NULL;
177 char *psz_hostname = XURL_FindHostname( psz_url );
179 if( psz_hostname != NULL )
181 char *psz_new_hostname;
182 size_t i_hostname_length;
184 char *psz_one_past_end_of_hostname = strchr( psz_hostname, '/' );
185 if( psz_one_past_end_of_hostname != NULL)
187 /* Found a '/' after the hostname, so copy characters between
188 * the hostname and the '/' to a new string */
189 i_hostname_length = psz_one_past_end_of_hostname -
194 /* Didn't find a '/', so copy from the start of the hostname
195 * until the end of the string */
196 i_hostname_length = strlen( psz_url ) - ( psz_hostname - psz_url );
199 /* Copy hostname to a new string */
200 psz_new_hostname = xurl_malloc( i_hostname_length );
201 if (psz_new_hostname == NULL) return NULL;
202 strncpy( psz_new_hostname, psz_hostname, i_hostname_length );
205 fprintf (stderr, "XURL_GetHostname: psz_new_hostname is \"%s\"\n",
208 psz_return_value = psz_new_hostname;
212 /* Didn't find a hostname */
216 return psz_return_value;
220 char *XURL_GetSchemeAndHostname( char *psz_url )
222 char *psz_scheme, *psz_hostname, *psz_scheme_and_hostname;
224 psz_scheme = XURL_GetScheme( psz_url );
225 if( psz_scheme == NULL ) return NULL;
227 psz_hostname = XURL_GetHostname( psz_url );
228 if( psz_hostname == NULL ) return NULL;
230 /* malloc +1 for the terminating '\0' */
231 psz_scheme_and_hostname = xurl_malloc(
232 strlen( psz_scheme ) + strlen( "://" ) +
233 strlen( psz_hostname ) + 1);
234 if( psz_scheme_and_hostname == NULL ) return NULL;
235 (void) strcpy( psz_scheme_and_hostname, psz_scheme );
236 (void) strcat( psz_scheme_and_hostname, "://" );
237 (void) strcat( psz_scheme_and_hostname, psz_hostname );
239 if (psz_scheme_and_hostname == NULL ) return NULL;
240 return psz_scheme_and_hostname;
244 char *XURL_FindFragment( char *psz_url )
246 char *pc_hash = NULL;
247 char *pc_return_value = NULL;
249 pc_hash = strchr( psz_url, '#' );
250 if( pc_hash != NULL )
252 pc_return_value = pc_hash;
255 return pc_return_value;
259 char *XURL_FindQuery( char *psz_url )
261 char *pc_question_mark = NULL;
262 char *pc_return_value = NULL;
264 pc_question_mark = strchr( psz_url, '?' );
265 if( pc_question_mark != NULL )
267 pc_return_value = pc_question_mark;
270 return pc_return_value;
274 char *XURL_GetScheme( char *psz_url )
277 size_t i_scheme_length;
280 if( XURL_IsAbsolute( psz_url ) == XURL_FALSE ) return strdup( "file" );
282 /* this strchr will always succeed since we have an absolute URL, and thus
284 psz_colon = strchr( psz_url, ':' );
286 i_scheme_length = psz_colon - psz_url;
288 new_scheme = xurl_malloc( i_scheme_length );
289 if( new_scheme == NULL ) return NULL;
291 strncpy( new_scheme, psz_url, i_scheme_length );
297 XURL_Bool XURL_IsFileURL( char *psz_url )
299 XURL_Bool b_return_value;
300 char *psz_scheme = XURL_GetScheme( psz_url );
302 if( strcasecmp( psz_scheme, "file" ) == 0 )
303 b_return_value = XURL_TRUE;
305 b_return_value = XURL_FALSE;
307 xurl_free( psz_scheme );
309 return b_return_value;
314 char *xurl_strdup( const char *psz_string )
317 char *psz_new_string;
319 if( !psz_string ) return NULL;
321 i_length = strlen( psz_string ) + 1;
322 psz_new_string = (char *) xurl_malloc( i_length );
323 if( psz_new_string == NULL ) return NULL;
325 memcpy( psz_new_string, psz_string, i_length );
327 return psz_new_string;
332 char *XURL_FindPath( char *psz_url )
334 char *psz_return_value = NULL;
336 if( XURL_IsAbsolute( psz_url ) == XURL_TRUE )
338 char *psz_start_of_hostname = XURL_FindHostname( psz_url );
339 if( psz_start_of_hostname != NULL )
341 char *psz_start_of_path = strchr( psz_start_of_hostname, '/' );
342 psz_return_value = psz_start_of_path;
347 if( XURL_HasAbsolutePath( psz_url ) == XURL_TRUE )
349 psz_return_value = psz_url;
353 return xurl_strdup (".");
357 return psz_return_value;
361 char *XURL_GetPath( char *psz_url )
363 char *psz_return_value = NULL;
364 char *psz_path = NULL;
365 char *pc_question_mark = NULL;
366 char *pc_fragment = NULL;
368 psz_path = xurl_strdup( XURL_FindPath( psz_url ) );
370 fprintf( stderr, "XURL_GetPath: XURL_FindPath returning \"%s\"\n",
373 psz_return_value = psz_path;
375 pc_question_mark = XURL_FindQuery( psz_path );
376 if( pc_question_mark != NULL )
378 int i_path_length = pc_question_mark - psz_path;
379 *( psz_path + i_path_length ) = '\0';
382 pc_fragment = XURL_FindFragment( psz_path );
383 if( pc_fragment != NULL )
386 fprintf( stderr, "XURL_GetPath: XURL_FindFragment returned \"%s\"\n",
389 int i_path_length = pc_fragment - psz_path;
390 *( psz_path + i_path_length ) = '\0';
394 fprintf( stderr, "XURL_GetPath returning \"%s\"\n", psz_return_value );
397 return psz_return_value;
401 char *XURL_GetHead( const char *psz_path )
406 /* kill everything up to the last / (including the /) */
407 #ifdef XURL_WIN32_PATHING
408 /* Windows: Try looking for a \ first; if we don't find one, look for / */
409 pc_last_slash = strrchr( psz_path, '\\' );
410 if( pc_last_slash == NULL )
411 pc_last_slash = strrchr( psz_path, '/' );
413 pc_last_slash = strrchr( psz_path, '/' );
415 if( pc_last_slash == NULL )
417 psz_path_head = xurl_strdup( psz_path );
421 size_t i_characters_until_last_slash;
423 i_characters_until_last_slash = pc_last_slash - psz_path;
424 psz_path_head = malloc(
425 ( i_characters_until_last_slash + 1 ) * sizeof(char) );
426 (void) strncpy( psz_path_head, psz_path,
427 i_characters_until_last_slash + 1 );
429 /* terminate the resulting string with '\0' */
431 i_characters_until_last_slash) = '\0';
434 /* append a trailing / */
435 streallocat( psz_path_head, "/" );
437 return psz_path_head;
441 char *XURL_GetWithoutFragment( char *psz_url )
443 char *psz_return_value = NULL;
446 psz_fragment = XURL_FindFragment( psz_url );
447 if( psz_fragment == NULL )
449 psz_return_value = xurl_strdup( psz_url );
453 size_t i_pre_fragment_length;
454 char *psz_without_fragment;
456 i_pre_fragment_length = psz_fragment - psz_url;
458 psz_without_fragment = xurl_malloc( i_pre_fragment_length + 1 );
459 if( psz_without_fragment == NULL )
461 psz_return_value = NULL;
465 memcpy( psz_without_fragment, psz_url, i_pre_fragment_length );
466 *( psz_without_fragment + i_pre_fragment_length ) = '\0';
467 psz_return_value = psz_without_fragment;
471 return psz_return_value;
475 char *streallocat( char *psz_string, char *psz_to_append )
477 size_t i_new_string_length = strlen( psz_string ) +
478 strlen( psz_to_append ) + 1;
480 psz_string = (char *) realloc( psz_string, i_new_string_length );
482 return strcat( psz_string, psz_to_append );