gmyth/src/gmyth_uri.c
branchtrunk
changeset 698 9019388af980
parent 692 d9c6cce2b167
child 701 2f28edb4d804
     1.1 --- a/gmyth/src/gmyth_uri.c	Fri May 18 21:15:39 2007 +0100
     1.2 +++ b/gmyth/src/gmyth_uri.c	Wed May 23 16:11:29 2007 +0100
     1.3 @@ -10,23 +10,23 @@
     1.4   * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
     1.5   * @author Rosfran Borges <rosfran.borges@indt.org.br>
     1.6   *
     1.7 - *//*
     1.8 - * 
     1.9 - * This program is free software; you can redistribute it and/or modify
    1.10 - * it under the terms of the GNU Lesser General Public License as published by
    1.11 - * the Free Software Foundation; either version 2 of the License, or
    1.12 - * (at your option) any later version.
    1.13 - *
    1.14 - * This program is distributed in the hope that it will be useful,
    1.15 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 - * GNU General Public License for more details.
    1.18 - *
    1.19 - * You should have received a copy of the GNU Lesser General Public License
    1.20 - * along with this program; if not, write to the Free Software
    1.21 - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.22 - */
    1.23 - 
    1.24 +                                                                                                                                                                                                                                                                                                                                                                                              *//*
    1.25 +                                                                                                                                                                                                 * 
    1.26 +                                                                                                                                                                                                 * This program is free software; you can redistribute it and/or modify
    1.27 +                                                                                                                                                                                                 * it under the terms of the GNU Lesser General Public License as published by
    1.28 +                                                                                                                                                                                                 * the Free Software Foundation; either version 2 of the License, or
    1.29 +                                                                                                                                                                                                 * (at your option) any later version.
    1.30 +                                                                                                                                                                                                 *
    1.31 +                                                                                                                                                                                                 * This program is distributed in the hope that it will be useful,
    1.32 +                                                                                                                                                                                                 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.33 +                                                                                                                                                                                                 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.34 +                                                                                                                                                                                                 * GNU General Public License for more details.
    1.35 +                                                                                                                                                                                                 *
    1.36 +                                                                                                                                                                                                 * You should have received a copy of the GNU Lesser General Public License
    1.37 +                                                                                                                                                                                                 * along with this program; if not, write to the Free Software
    1.38 +                                                                                                                                                                                                 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.39 +                                                                                                                                                                                                 */
    1.40 +
    1.41  #ifdef HAVE_CONFIG_H
    1.42  #include "config.h"
    1.43  #endif
    1.44 @@ -40,90 +40,89 @@
    1.45  
    1.46  #include "gmyth_debug.h"
    1.47  
    1.48 -static void gmyth_uri_class_init          (GMythURIClass *klass);
    1.49 -static void gmyth_uri_init                (GMythURI *object);
    1.50 +static void gmyth_uri_class_init (GMythURIClass * klass);
    1.51 +static void gmyth_uri_init (GMythURI * object);
    1.52  
    1.53 -static void gmyth_uri_dispose  (GObject *object);
    1.54 -static void gmyth_uri_finalize (GObject *object);
    1.55 +static void gmyth_uri_dispose (GObject * object);
    1.56 +static void gmyth_uri_finalize (GObject * object);
    1.57  
    1.58 -static void gmyth_uri_parser_setup_and_new(GMythURI *uri, const gchar *value);
    1.59 -static gchar* gmyth_uri_print_field(const GString* field);
    1.60 +static void gmyth_uri_parser_setup_and_new (GMythURI * uri,
    1.61 +    const gchar * value);
    1.62 +static gchar *gmyth_uri_print_field (const GString * field);
    1.63  
    1.64 -G_DEFINE_TYPE(GMythURI, gmyth_uri, G_TYPE_OBJECT)
    1.65 -
    1.66 -static void
    1.67 -gmyth_uri_class_init (GMythURIClass *klass)
    1.68 +G_DEFINE_TYPE (GMythURI, gmyth_uri, G_TYPE_OBJECT)
    1.69 +     static void gmyth_uri_class_init (GMythURIClass * klass)
    1.70  {
    1.71 -	GObjectClass *gobject_class;
    1.72 +    GObjectClass *gobject_class;
    1.73  
    1.74      gobject_class = (GObjectClass *) klass;
    1.75 -	
    1.76 -    gobject_class->dispose  = gmyth_uri_dispose;
    1.77 -    gobject_class->finalize = gmyth_uri_finalize;	
    1.78 +
    1.79 +    gobject_class->dispose = gmyth_uri_dispose;
    1.80 +    gobject_class->finalize = gmyth_uri_finalize;
    1.81  }
    1.82  
    1.83  static void
    1.84 -gmyth_uri_init (GMythURI *gmyth_uri)
    1.85 +gmyth_uri_init (GMythURI * gmyth_uri)
    1.86  {
    1.87  }
    1.88  
    1.89  static void
    1.90 -gmyth_uri_dispose  (GObject *object)
    1.91 +gmyth_uri_dispose (GObject * object)
    1.92  {
    1.93 -	GMythURI *gmyth_uri = GMYTH_URI(object);
    1.94 +    GMythURI *gmyth_uri = GMYTH_URI (object);
    1.95  
    1.96 -	if ( gmyth_uri->host != NULL ) {
    1.97 -		g_string_free( gmyth_uri->host, TRUE );
    1.98 -		gmyth_uri->host = NULL;
    1.99 -	}
   1.100 -	
   1.101 -	if ( gmyth_uri->protocol != NULL ) {
   1.102 -		g_string_free( gmyth_uri->protocol, TRUE );
   1.103 -		gmyth_uri->protocol = NULL;
   1.104 -	}
   1.105 -	
   1.106 -	if ( gmyth_uri->path != NULL ) {
   1.107 -		g_string_free( gmyth_uri->path, TRUE );
   1.108 -		gmyth_uri->path = NULL;
   1.109 -	}
   1.110 -	
   1.111 -	if ( gmyth_uri->fragment != NULL ) {
   1.112 -		g_string_free( gmyth_uri->fragment, TRUE );
   1.113 -		gmyth_uri->fragment = NULL;
   1.114 -	}
   1.115 -	
   1.116 -	if ( gmyth_uri->user != NULL ) {
   1.117 -		g_string_free( gmyth_uri->user, TRUE );
   1.118 -		gmyth_uri->user = NULL;
   1.119 -	}
   1.120 -	
   1.121 -	if ( gmyth_uri->password != NULL ) {
   1.122 -		g_string_free( gmyth_uri->password, TRUE );
   1.123 -		gmyth_uri->password = NULL;
   1.124 -	}
   1.125 -	
   1.126 -	if ( gmyth_uri->query != NULL ) {
   1.127 -		g_string_free( gmyth_uri->query, TRUE );
   1.128 -		gmyth_uri->query = NULL;
   1.129 -	}	
   1.130 +    if (gmyth_uri->host != NULL) {
   1.131 +        g_string_free (gmyth_uri->host, TRUE);
   1.132 +        gmyth_uri->host = NULL;
   1.133 +    }
   1.134  
   1.135 -	if ( gmyth_uri->uri != NULL ) {
   1.136 -		g_string_free( gmyth_uri->uri, TRUE );
   1.137 -		gmyth_uri->uri = NULL;
   1.138 -	}	
   1.139 +    if (gmyth_uri->protocol != NULL) {
   1.140 +        g_string_free (gmyth_uri->protocol, TRUE);
   1.141 +        gmyth_uri->protocol = NULL;
   1.142 +    }
   1.143  
   1.144 +    if (gmyth_uri->path != NULL) {
   1.145 +        g_string_free (gmyth_uri->path, TRUE);
   1.146 +        gmyth_uri->path = NULL;
   1.147 +    }
   1.148  
   1.149 -	G_OBJECT_CLASS (gmyth_uri_parent_class)->dispose (object);
   1.150 +    if (gmyth_uri->fragment != NULL) {
   1.151 +        g_string_free (gmyth_uri->fragment, TRUE);
   1.152 +        gmyth_uri->fragment = NULL;
   1.153 +    }
   1.154 +
   1.155 +    if (gmyth_uri->user != NULL) {
   1.156 +        g_string_free (gmyth_uri->user, TRUE);
   1.157 +        gmyth_uri->user = NULL;
   1.158 +    }
   1.159 +
   1.160 +    if (gmyth_uri->password != NULL) {
   1.161 +        g_string_free (gmyth_uri->password, TRUE);
   1.162 +        gmyth_uri->password = NULL;
   1.163 +    }
   1.164 +
   1.165 +    if (gmyth_uri->query != NULL) {
   1.166 +        g_string_free (gmyth_uri->query, TRUE);
   1.167 +        gmyth_uri->query = NULL;
   1.168 +    }
   1.169 +
   1.170 +    if (gmyth_uri->uri != NULL) {
   1.171 +        g_string_free (gmyth_uri->uri, TRUE);
   1.172 +        gmyth_uri->uri = NULL;
   1.173 +    }
   1.174 +
   1.175 +
   1.176 +    G_OBJECT_CLASS (gmyth_uri_parent_class)->dispose (object);
   1.177  }
   1.178  
   1.179  static void
   1.180 -gmyth_uri_finalize (GObject *object)
   1.181 +gmyth_uri_finalize (GObject * object)
   1.182  {
   1.183 -	//GMythURI *gmyth_uri = GMYTH_URI(object);
   1.184 +    //GMythURI *gmyth_uri = GMYTH_URI(object);
   1.185  
   1.186 -	g_signal_handlers_destroy (object);
   1.187 +    g_signal_handlers_destroy (object);
   1.188  
   1.189 -	G_OBJECT_CLASS (gmyth_uri_parent_class)->finalize (object);
   1.190 +    G_OBJECT_CLASS (gmyth_uri_parent_class)->finalize (object);
   1.191  }
   1.192  
   1.193  /** 
   1.194 @@ -132,10 +131,10 @@
   1.195   * @return a new instance of GMythURI.
   1.196   */
   1.197  GMythURI *
   1.198 -gmyth_uri_new (void) 
   1.199 +gmyth_uri_new (void)
   1.200  {
   1.201      GMythURI *gmyth_uri = GMYTH_URI (g_object_new (GMYTH_URI_TYPE, NULL));
   1.202 -    
   1.203 +
   1.204      return gmyth_uri;
   1.205  }
   1.206  
   1.207 @@ -147,12 +146,12 @@
   1.208   * @return a new instance of GMythURI.
   1.209   */
   1.210  GMythURI *
   1.211 -gmyth_uri_new_with_value (const gchar *uri_str) 
   1.212 +gmyth_uri_new_with_value (const gchar * uri_str)
   1.213  {
   1.214      GMythURI *gmyth_uri = GMYTH_URI (g_object_new (GMYTH_URI_TYPE, NULL));
   1.215 -    
   1.216 +
   1.217      gmyth_uri_parser_setup_and_new (gmyth_uri, uri_str);
   1.218 -    
   1.219 +
   1.220      return gmyth_uri;
   1.221  }
   1.222  
   1.223 @@ -165,19 +164,19 @@
   1.224   * @return The starting offset to the given substring, or <code>-1</code> if the
   1.225   * 				 haystack function parameter doesn't contains the needle string argument.
   1.226   */
   1.227 -static gint 
   1.228 -gmyth_strstr (const gchar *haystack, const gchar *needle)
   1.229 +static gint
   1.230 +gmyth_strstr (const gchar * haystack, const gchar * needle)
   1.231  {
   1.232 -	
   1.233 -	gchar *strPos;
   1.234 -	
   1.235 -	if (haystack == NULL || needle == NULL)
   1.236 -		return -1;
   1.237 -	strPos = strstr(haystack, needle);
   1.238 -	if (strPos == NULL)
   1.239 -		return -1;
   1.240 -		
   1.241 -	return (strPos - haystack);
   1.242 +
   1.243 +    gchar *strPos;
   1.244 +
   1.245 +    if (haystack == NULL || needle == NULL)
   1.246 +        return -1;
   1.247 +    strPos = strstr (haystack, needle);
   1.248 +    if (strPos == NULL)
   1.249 +        return -1;
   1.250 +
   1.251 +    return (strPos - haystack);
   1.252  
   1.253  }
   1.254  
   1.255 @@ -189,16 +188,18 @@
   1.256   * @return <code>true</code>, if the URI is absolute.
   1.257   */
   1.258  static gboolean
   1.259 -gmyth_uri_isabsolute (const GMythURI *uri)
   1.260 +gmyth_uri_isabsolute (const GMythURI * uri)
   1.261  {
   1.262 -	gboolean ret = FALSE;
   1.263 -	
   1.264 -	g_return_val_if_fail( uri != NULL && uri->uri != NULL && uri->protocol != NULL, FALSE );
   1.265 -	
   1.266 -	if ( gmyth_strstr( uri->uri->str, GMYTH_URI_PROTOCOL_DELIM ) == 0 || strlen(uri->protocol->str) > 0 )
   1.267 -		ret = TRUE;
   1.268 -		
   1.269 -	return ret;	
   1.270 +    gboolean ret = FALSE;
   1.271 +
   1.272 +    g_return_val_if_fail (uri != NULL && uri->uri != NULL
   1.273 +        && uri->protocol != NULL, FALSE);
   1.274 +
   1.275 +    if (gmyth_strstr (uri->uri->str, GMYTH_URI_PROTOCOL_DELIM) == 0
   1.276 +        || strlen (uri->protocol->str) > 0)
   1.277 +        ret = TRUE;
   1.278 +
   1.279 +    return ret;
   1.280  }
   1.281  
   1.282  /** 
   1.283 @@ -215,34 +216,34 @@
   1.284   *         or <code>-1</code> if the no character of the list could be found.
   1.285   */
   1.286  static gint
   1.287 -gmyth_strrchr( const gchar *str, const gchar *chars, const gint nchars )
   1.288 +gmyth_strrchr (const gchar * str, const gchar * chars, const gint nchars)
   1.289  {
   1.290  
   1.291 -	gint strLen;
   1.292 -	gint i, j;
   1.293 -	
   1.294 -	if ( str == NULL || chars == NULL )
   1.295 -		return -1;
   1.296 -		
   1.297 -	strLen = strlen( str );
   1.298 -	for ( i= (strLen-1); 0 <= i; i-- ) {
   1.299 -		for ( j=0; j<nchars; j++ ) {
   1.300 -			if ( str[i] == chars[j] )
   1.301 -				return i;
   1.302 -		}
   1.303 -	}
   1.304 +    gint strLen;
   1.305 +    gint i, j;
   1.306  
   1.307 -	return -1;
   1.308 +    if (str == NULL || chars == NULL)
   1.309 +        return -1;
   1.310 +
   1.311 +    strLen = strlen (str);
   1.312 +    for (i = (strLen - 1); 0 <= i; i--) {
   1.313 +        for (j = 0; j < nchars; j++) {
   1.314 +            if (str[i] == chars[j])
   1.315 +                return i;
   1.316 +        }
   1.317 +    }
   1.318 +
   1.319 +    return -1;
   1.320  
   1.321  }
   1.322  
   1.323 -static gchar*
   1.324 -gmyth_uri_print_field( const GString* field )
   1.325 +static gchar *
   1.326 +gmyth_uri_print_field (const GString * field)
   1.327  {
   1.328 -	if ( field != NULL && field->str != NULL && strlen(field->str) > 0 )
   1.329 -		return field->str;
   1.330 -	else
   1.331 -		return "";		
   1.332 +    if (field != NULL && field->str != NULL && strlen (field->str) > 0)
   1.333 +        return field->str;
   1.334 +    else
   1.335 +        return "";
   1.336  }
   1.337  
   1.338  /** 
   1.339 @@ -253,135 +254,146 @@
   1.340   *
   1.341   */
   1.342  static void
   1.343 -gmyth_uri_parser_setup_and_new( GMythURI *uri, const gchar *value )
   1.344 +gmyth_uri_parser_setup_and_new (GMythURI * uri, const gchar * value)
   1.345  {
   1.346 -	
   1.347 -	gint 		uriLen;
   1.348 -	gint 		currIdx;
   1.349 -	gint 		protoIdx;
   1.350 -	gint 		atIdx;
   1.351 -	gint 		colonIdx;
   1.352 -	gint 		shashIdx;
   1.353 -	gint 		eIdx;
   1.354 -	gchar       *host;
   1.355 -	gint 		eblacketIdx;
   1.356 -	gint 		hostLen;
   1.357 -	gint 		sharpIdx;
   1.358 -	/*
   1.359 -	gint 		questionIdx;
   1.360 -	gint 		queryLen;
   1.361 -	*/
   1.362 -	
   1.363 -	uriLen = strlen(value);
   1.364 -	uri->uri = g_string_new( value );
   1.365 -		
   1.366 -	currIdx = 0;
   1.367 -	
   1.368 -	/*** Protocol ****/
   1.369 -	protoIdx = gmyth_strstr (value, GMYTH_URI_PROTOCOL_DELIM);
   1.370 -	if (0 < protoIdx) {
   1.371 +
   1.372 +    gint uriLen;
   1.373 +    gint currIdx;
   1.374 +    gint protoIdx;
   1.375 +    gint atIdx;
   1.376 +    gint colonIdx;
   1.377 +    gint shashIdx;
   1.378 +    gint eIdx;
   1.379 +    gchar *host;
   1.380 +    gint eblacketIdx;
   1.381 +    gint hostLen;
   1.382 +    gint sharpIdx;
   1.383 +
   1.384 +    /*
   1.385 +       gint                 questionIdx;
   1.386 +       gint                 queryLen;
   1.387 +     */
   1.388 +
   1.389 +    uriLen = strlen (value);
   1.390 +    uri->uri = g_string_new (value);
   1.391 +
   1.392 +    currIdx = 0;
   1.393 +
   1.394 +        /*** Protocol ****/
   1.395 +    protoIdx = gmyth_strstr (value, GMYTH_URI_PROTOCOL_DELIM);
   1.396 +    if (0 < protoIdx) {
   1.397          uri->protocol = g_string_new_len (value, protoIdx);
   1.398 -		currIdx += protoIdx + strlen( GMYTH_URI_PROTOCOL_DELIM );
   1.399 -	}
   1.400 +        currIdx += protoIdx + strlen (GMYTH_URI_PROTOCOL_DELIM);
   1.401 +    }
   1.402  
   1.403 -	/*** User (Password) ****/
   1.404 -	atIdx = gmyth_strstr( value+currIdx, GMYTH_URI_USER_DELIM );
   1.405 -	if ( 0 < atIdx ) {
   1.406 -		colonIdx = gmyth_strstr( value+currIdx, GMYTH_URI_COLON_DELIM );
   1.407 +        /*** User (Password) ****/
   1.408 +    atIdx = gmyth_strstr (value + currIdx, GMYTH_URI_USER_DELIM);
   1.409 +    if (0 < atIdx) {
   1.410 +        colonIdx = gmyth_strstr (value + currIdx, GMYTH_URI_COLON_DELIM);
   1.411  
   1.412 -		if (0 < colonIdx && colonIdx < atIdx) {
   1.413 -            uri->user = g_string_new_len (value+currIdx, colonIdx);
   1.414 -            uri->password = g_string_new_len (value+currIdx+colonIdx+1, atIdx - (colonIdx+1));
   1.415 -		}
   1.416 -		else
   1.417 -            uri->user = g_string_new_len (value+currIdx, atIdx - currIdx);
   1.418 -		currIdx += atIdx + 1;
   1.419 -	}
   1.420 +        if (0 < colonIdx && colonIdx < atIdx) {
   1.421 +            uri->user = g_string_new_len (value + currIdx, colonIdx);
   1.422 +            uri->password =
   1.423 +                g_string_new_len (value + currIdx + colonIdx + 1,
   1.424 +                atIdx - (colonIdx + 1));
   1.425 +        } else
   1.426 +            uri->user = g_string_new_len (value + currIdx, atIdx - currIdx);
   1.427 +        currIdx += atIdx + 1;
   1.428 +    }
   1.429  
   1.430 -	/*** Host (Port) ****/
   1.431 -	shashIdx = gmyth_strstr( value+currIdx, GMYTH_URI_SLASH_DELIM );
   1.432 -	if (0 < shashIdx)
   1.433 -        uri->host = g_string_new_len (value+currIdx, shashIdx);
   1.434 -	else if ( gmyth_uri_isabsolute(uri) == TRUE )
   1.435 -        uri->host = g_string_new_len (value+currIdx, strlen (value) - currIdx);
   1.436 +        /*** Host (Port) ****/
   1.437 +    shashIdx = gmyth_strstr (value + currIdx, GMYTH_URI_SLASH_DELIM);
   1.438 +    if (0 < shashIdx)
   1.439 +        uri->host = g_string_new_len (value + currIdx, shashIdx);
   1.440 +    else if (gmyth_uri_isabsolute (uri) == TRUE)
   1.441 +        uri->host =
   1.442 +            g_string_new_len (value + currIdx, strlen (value) - currIdx);
   1.443  
   1.444 -	host = gmyth_uri_get_host(uri);
   1.445 -	colonIdx = gmyth_strrchr (host, GMYTH_URI_COLON_DELIM, 1);
   1.446 -	eblacketIdx = gmyth_strrchr (host, GMYTH_URI_EBLACET_DELIM, 1);
   1.447 -	if ( ( 0 < colonIdx ) && ( eblacketIdx < colonIdx ) ) {
   1.448 +    host = gmyth_uri_get_host (uri);
   1.449 +    colonIdx = gmyth_strrchr (host, GMYTH_URI_COLON_DELIM, 1);
   1.450 +    eblacketIdx = gmyth_strrchr (host, GMYTH_URI_EBLACET_DELIM, 1);
   1.451 +    if ((0 < colonIdx) && (eblacketIdx < colonIdx)) {
   1.452          GString *portStr = NULL;
   1.453 -		GString *hostStr = g_string_new  (host != NULL ? host : "");
   1.454 +        GString *hostStr = g_string_new (host != NULL ? host : "");
   1.455  
   1.456 -		hostLen = hostStr->len;
   1.457 -		/**** host ****/
   1.458 -		uri->host = g_string_erase (uri->host, 0, hostLen);
   1.459 -		uri->host = g_string_insert_len (uri->host, 0, hostStr->str, colonIdx);
   1.460 -		if (0 < hostLen) {
   1.461 -			if (host[0] == '[' && host[hostLen-1] == ']')
   1.462 -                uri->host = g_string_new_len (hostStr->str+1, colonIdx-2);
   1.463 -		}
   1.464 -		/**** port ****/
   1.465 -		portStr = g_string_new_len (hostStr->str+colonIdx+1, hostLen-colonIdx-1);
   1.466 -		uri->port = (gint)g_ascii_strtoull( portStr->str, NULL, 10 );
   1.467 -		g_string_free (portStr, TRUE);
   1.468 -		g_string_free (hostStr, TRUE);
   1.469 -	}
   1.470 -	else {
   1.471 -        const gchar* protocol = gmyth_uri_get_protocol(uri);
   1.472 -		uri->port = GMYTH_URI_KNKOWN_PORT;
   1.473 -		if ( strcmp(protocol, GMYTH_URI_PROTOCOL_HTTP) == 0 )
   1.474 -			uri->port = GMYTH_URI_DEFAULT_HTTP_PORT;
   1.475 -		if ( strcmp(protocol, GMYTH_URI_PROTOCOL_FTP) == 0 )
   1.476 -			uri->port = GMYTH_URI_DEFAULT_FTP_PORT;
   1.477 -	}
   1.478 -	
   1.479 -	if (shashIdx > 0) currIdx += shashIdx;
   1.480 -	
   1.481 -	/*
   1.482 -		Handle relative URL
   1.483 -	*/
   1.484 -	if (gmyth_uri_isabsolute(uri) == FALSE)
   1.485 -	{
   1.486 +        hostLen = hostStr->len;
   1.487 +                /**** host ****/
   1.488 +        uri->host = g_string_erase (uri->host, 0, hostLen);
   1.489 +        uri->host = g_string_insert_len (uri->host, 0, hostStr->str, colonIdx);
   1.490 +        if (0 < hostLen) {
   1.491 +            if (host[0] == '[' && host[hostLen - 1] == ']')
   1.492 +                uri->host = g_string_new_len (hostStr->str + 1, colonIdx - 2);
   1.493 +        }
   1.494 +                /**** port ****/
   1.495 +        portStr =
   1.496 +            g_string_new_len (hostStr->str + colonIdx + 1,
   1.497 +            hostLen - colonIdx - 1);
   1.498 +        uri->port = (gint) g_ascii_strtoull (portStr->str, NULL, 10);
   1.499 +        g_string_free (portStr, TRUE);
   1.500 +        g_string_free (hostStr, TRUE);
   1.501 +    } else {
   1.502 +        const gchar *protocol = gmyth_uri_get_protocol (uri);
   1.503  
   1.504 -		if (shashIdx != 0)
   1.505 -		{
   1.506 -			/* Add slash delimiter at the beginning of the URL,
   1.507 -			   if it doesn't exist 
   1.508 -			*/
   1.509 -			uri->path = g_string_new( GMYTH_URI_SLASH_DELIM );
   1.510 -		}
   1.511 -		uri->path = g_string_append( uri->path, value );
   1.512 -		
   1.513 -	} else {
   1.514 -		/* First set path simply to the rest of URI */
   1.515 -		uri->path = g_string_new_len (value+currIdx,  uriLen-currIdx );
   1.516 -	}
   1.517 -	
   1.518 -	//gmyth_debug( "uri value:  %s", value );
   1.519 -	uri->query = g_string_new ( g_strstr_len( value, strlen(value), GMYTH_URI_QUESTION_DELIM ) );
   1.520 -	
   1.521 -	eIdx = gmyth_strstr( value+currIdx, GMYTH_URI_QUESTION_DELIM );
   1.522 -	
   1.523 -	if ( 0 < eIdx ) {
   1.524 -		uri->query = g_string_new ( g_strstr_len( value, strlen(value), GMYTH_URI_QUESTION_DELIM ) );
   1.525 -		gmyth_debug( "query = %s", uri->query->str );
   1.526 -	}
   1.527 -	
   1.528 -	/**** Path (Query/Fragment) ****/
   1.529 -	sharpIdx = gmyth_strstr(value+currIdx, GMYTH_URI_E_DELIM);
   1.530 -	if (0 < sharpIdx) {
   1.531 -		uri->path = g_string_append_len( uri->path, value+currIdx, sharpIdx);
   1.532 -		uri->fragment = g_string_new_len (value+currIdx+sharpIdx+1, uriLen-(currIdx+sharpIdx+1));
   1.533 -	}	
   1.534 +        uri->port = GMYTH_URI_KNKOWN_PORT;
   1.535 +        if (strcmp (protocol, GMYTH_URI_PROTOCOL_HTTP) == 0)
   1.536 +            uri->port = GMYTH_URI_DEFAULT_HTTP_PORT;
   1.537 +        if (strcmp (protocol, GMYTH_URI_PROTOCOL_FTP) == 0)
   1.538 +            uri->port = GMYTH_URI_DEFAULT_FTP_PORT;
   1.539 +    }
   1.540  
   1.541 -	gmyth_debug( "[%s] GMythURI: host = %s, port = %d, path = %s, query = %s, fragment = %s, "\
   1.542 -      			 "user = %s, password = %s.\n", __FUNCTION__, 
   1.543 -                 gmyth_uri_print_field( uri->host ), uri->port,
   1.544 -			     gmyth_uri_print_field( uri->path ), 
   1.545 -                 gmyth_uri_print_field( uri->query ), 
   1.546 -                 gmyth_uri_print_field( uri->fragment ), 
   1.547 -                 gmyth_uri_print_field ( uri->user ), 
   1.548 -                 gmyth_uri_print_field( uri->password ) );
   1.549 +    if (shashIdx > 0)
   1.550 +        currIdx += shashIdx;
   1.551 +
   1.552 +    /*
   1.553 +       Handle relative URL
   1.554 +     */
   1.555 +    if (gmyth_uri_isabsolute (uri) == FALSE) {
   1.556 +
   1.557 +        if (shashIdx != 0) {
   1.558 +            /* Add slash delimiter at the beginning of the URL,
   1.559 +               if it doesn't exist 
   1.560 +             */
   1.561 +            uri->path = g_string_new (GMYTH_URI_SLASH_DELIM);
   1.562 +        }
   1.563 +        uri->path = g_string_append (uri->path, value);
   1.564 +
   1.565 +    } else {
   1.566 +        /* First set path simply to the rest of URI */
   1.567 +        uri->path = g_string_new_len (value + currIdx, uriLen - currIdx);
   1.568 +    }
   1.569 +
   1.570 +    //gmyth_debug( "uri value:  %s", value );
   1.571 +    uri->query =
   1.572 +        g_string_new (g_strstr_len
   1.573 +        (value, strlen (value), GMYTH_URI_QUESTION_DELIM));
   1.574 +
   1.575 +    eIdx = gmyth_strstr (value + currIdx, GMYTH_URI_QUESTION_DELIM);
   1.576 +
   1.577 +    if (0 < eIdx) {
   1.578 +        uri->query =
   1.579 +            g_string_new (g_strstr_len
   1.580 +            (value, strlen (value), GMYTH_URI_QUESTION_DELIM));
   1.581 +        gmyth_debug ("query = %s", uri->query->str);
   1.582 +    }
   1.583 +
   1.584 +        /**** Path (Query/Fragment) ****/
   1.585 +    sharpIdx = gmyth_strstr (value + currIdx, GMYTH_URI_E_DELIM);
   1.586 +    if (0 < sharpIdx) {
   1.587 +        uri->path = g_string_append_len (uri->path, value + currIdx, sharpIdx);
   1.588 +        uri->fragment =
   1.589 +            g_string_new_len (value + currIdx + sharpIdx + 1,
   1.590 +            uriLen - (currIdx + sharpIdx + 1));
   1.591 +    }
   1.592 +
   1.593 +    gmyth_debug
   1.594 +        ("[%s] GMythURI: host = %s, port = %d, path = %s, query = %s, fragment = %s, "
   1.595 +        "user = %s, password = %s.\n", __FUNCTION__,
   1.596 +        gmyth_uri_print_field (uri->host), uri->port,
   1.597 +        gmyth_uri_print_field (uri->path),
   1.598 +        gmyth_uri_print_field (uri->query),
   1.599 +        gmyth_uri_print_field (uri->fragment),
   1.600 +        gmyth_uri_print_field (uri->user),
   1.601 +        gmyth_uri_print_field (uri->password));
   1.602  
   1.603  }
   1.604  
   1.605 @@ -394,10 +406,11 @@
   1.606   * @return <code>true</code>, if these two URI instances are equals.
   1.607   */
   1.608  gboolean
   1.609 -gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 )
   1.610 +gmyth_uri_is_equals (GMythURI * uri1, GMythURI * uri2)
   1.611  {
   1.612 -	return ( g_ascii_strcasecmp( gmyth_uri_get_host( uri1 ), gmyth_uri_get_host( uri2 ) ) == 0 &&
   1.613 -				gmyth_uri_get_port( uri1 ) == gmyth_uri_get_port( uri2 ) );
   1.614 +    return (g_ascii_strcasecmp
   1.615 +        (gmyth_uri_get_host (uri1), gmyth_uri_get_host (uri2)) == 0
   1.616 +        && gmyth_uri_get_port (uri1) == gmyth_uri_get_port (uri2));
   1.617  }
   1.618  
   1.619  /** 
   1.620 @@ -408,25 +421,25 @@
   1.621   * @return <code>true</code>, if the URI points to LiveTV content.
   1.622   */
   1.623  gboolean
   1.624 -gmyth_uri_is_livetv( GMythURI* uri )
   1.625 +gmyth_uri_is_livetv (GMythURI * uri)
   1.626  {
   1.627 -	gboolean ret = TRUE;
   1.628 -    
   1.629 -	g_return_val_if_fail (uri != NULL, FALSE);
   1.630 -	g_return_val_if_fail (uri->uri != NULL, FALSE);
   1.631 +    gboolean ret = TRUE;
   1.632 +
   1.633 +    g_return_val_if_fail (uri != NULL, FALSE);
   1.634 +    g_return_val_if_fail (uri->uri != NULL, FALSE);
   1.635      g_return_val_if_fail (uri->uri->str != NULL, FALSE);
   1.636 -   
   1.637 +
   1.638      if ((strstr (uri->uri->str, "channel") == NULL) ||
   1.639 -        (strstr (uri->uri->str, "livetv") == NULL)) 
   1.640 +        (strstr (uri->uri->str, "livetv") == NULL))
   1.641          ret = FALSE;
   1.642  
   1.643 -	if (ret)
   1.644 -		gmyth_debug( "This URI is a LiveTV recording..." );
   1.645 +    if (ret)
   1.646 +        gmyth_debug ("This URI is a LiveTV recording...");
   1.647      else
   1.648 -        gmyth_debug( "This URI is a stored remote recording." );
   1.649 +        gmyth_debug ("This URI is a stored remote recording.");
   1.650  
   1.651 -	return ret;
   1.652 -	
   1.653 +    return ret;
   1.654 +
   1.655  }
   1.656  
   1.657  /** 
   1.658 @@ -437,34 +450,35 @@
   1.659   * @return The channel name, got from the substring "?channel=[channel_name]"
   1.660   * 				 of the URI string.
   1.661   */
   1.662 -gchar*
   1.663 -gmyth_uri_get_channel_name( GMythURI* uri )
   1.664 +gchar *
   1.665 +gmyth_uri_get_channel_name (GMythURI * uri)
   1.666  {
   1.667 -	gchar* channel = NULL;
   1.668 -	
   1.669 -	g_return_val_if_fail( uri != NULL && uri->uri != NULL && uri->uri->str != NULL, FALSE );
   1.670 -	
   1.671 -	gchar *channel_query = g_strstr_len( gmyth_uri_get_query( uri ), strlen( gmyth_uri_get_query( uri ) ), "channel" );
   1.672 -	
   1.673 -	if ( channel_query != NULL )
   1.674 -	{
   1.675 -		gchar **chan_key_value = g_strsplit( gmyth_uri_get_query( uri ), "=", 2 );
   1.676 -		
   1.677 -		/* gmyth_debug( "Channel tuple is [ %s, %s ]", chan_key_value[0], chan_key_value[1] ); */
   1.678 +    gchar *channel = NULL;
   1.679  
   1.680 -		if ( chan_key_value[1] != NULL && strlen( chan_key_value[1] ) > 0 )
   1.681 -		{
   1.682 -			channel = g_strdup( chan_key_value[1] ); 
   1.683 -		}
   1.684 +    g_return_val_if_fail (uri != NULL && uri->uri != NULL
   1.685 +        && uri->uri->str != NULL, FALSE);
   1.686  
   1.687 -		if ( chan_key_value != NULL )		
   1.688 -			g_strfreev( chan_key_value );
   1.689 -	}
   1.690 -	
   1.691 -	gmyth_debug( "Got channel decimal value from the URI: %s", channel );
   1.692 +    gchar *channel_query = g_strstr_len (gmyth_uri_get_query (uri),
   1.693 +        strlen (gmyth_uri_get_query (uri)),
   1.694 +        "channel");
   1.695  
   1.696 -	return channel;
   1.697 -	
   1.698 +    if (channel_query != NULL) {
   1.699 +        gchar **chan_key_value = g_strsplit (gmyth_uri_get_query (uri), "=", 2);
   1.700 +
   1.701 +        /* gmyth_debug( "Channel tuple is [ %s, %s ]", chan_key_value[0], chan_key_value[1] ); */
   1.702 +
   1.703 +        if (chan_key_value[1] != NULL && strlen (chan_key_value[1]) > 0) {
   1.704 +            channel = g_strdup (chan_key_value[1]);
   1.705 +        }
   1.706 +
   1.707 +        if (chan_key_value != NULL)
   1.708 +            g_strfreev (chan_key_value);
   1.709 +    }
   1.710 +
   1.711 +    gmyth_debug ("Got channel decimal value from the URI: %s", channel);
   1.712 +
   1.713 +    return channel;
   1.714 +
   1.715  }
   1.716  
   1.717  /** 
   1.718 @@ -476,17 +490,16 @@
   1.719   * 				 of the URI string, or <code>-1</code> it if couldn't be converted.
   1.720   */
   1.721  gint
   1.722 -gmyth_uri_get_channel_num( GMythURI* uri )
   1.723 +gmyth_uri_get_channel_num (GMythURI * uri)
   1.724  {
   1.725 -	gchar *channel_name = gmyth_uri_get_channel_name( uri );
   1.726 -	
   1.727 -	if ( channel_name != NULL )
   1.728 -	{
   1.729 -		return g_ascii_strtoull( channel_name, NULL, 10 ); 
   1.730 -	}
   1.731 -	
   1.732 -	return -1;
   1.733 -	
   1.734 +    gchar *channel_name = gmyth_uri_get_channel_name (uri);
   1.735 +
   1.736 +    if (channel_name != NULL) {
   1.737 +        return g_ascii_strtoull (channel_name, NULL, 10);
   1.738 +    }
   1.739 +
   1.740 +    return -1;
   1.741 +
   1.742  }
   1.743  
   1.744  /** 
   1.745 @@ -497,37 +510,37 @@
   1.746   * @return <code>true</code>, if the URI points to a local file.
   1.747   */
   1.748  gboolean
   1.749 -gmyth_uri_is_local_file( const GMythURI* uri )
   1.750 +gmyth_uri_is_local_file (const GMythURI * uri)
   1.751  {
   1.752      gboolean ret = FALSE;
   1.753      gint len = -1;
   1.754 -    
   1.755 -    GString *hostname = gmyth_socket_get_local_hostname();
   1.756 -    
   1.757 -    g_return_val_if_fail( uri != NULL, FALSE );    
   1.758 -    
   1.759 -    len = strlen( gmyth_uri_get_host(uri) );
   1.760 -    
   1.761 -   // gmyth_debug("URI: host = %s, hostname = %s.", uri->host->str, hostname != NULL ? hostname->str : "[no hostname]");
   1.762 -    
   1.763 -    ret = ( NULL != hostname && ( g_ascii_strncasecmp( uri->host->str, 
   1.764 -              (hostname)->str, len ) == 0 ) /*|| 
   1.765 -        ( g_ascii_strncasecmp( gmyth_uri_get_host(uri), gmyth_socket_get_primary_addr(), len ) == 0 ) */ );
   1.766 -    
   1.767 -    if ( ret )
   1.768 -        gmyth_debug( "This URI is a local file..." );
   1.769 +
   1.770 +    GString *hostname = gmyth_socket_get_local_hostname ();
   1.771 +
   1.772 +    g_return_val_if_fail (uri != NULL, FALSE);
   1.773 +
   1.774 +    len = strlen (gmyth_uri_get_host (uri));
   1.775 +
   1.776 +    // gmyth_debug("URI: host = %s, hostname = %s.", uri->host->str, hostname != NULL ? hostname->str : "[no hostname]");
   1.777 +
   1.778 +    ret = (NULL != hostname && (g_ascii_strncasecmp (uri->host->str, (hostname)->str, len) == 0)    /*|| 
   1.779 +                                                                                                       ( g_ascii_strncasecmp( gmyth_uri_get_host(uri), gmyth_socket_get_primary_addr(), len ) == 0 ) */
   1.780 +        );
   1.781 +
   1.782 +    if (ret)
   1.783 +        gmyth_debug ("This URI is a local file...");
   1.784      else
   1.785 -        gmyth_debug( "This URI is NOT a local file..." );
   1.786 -    
   1.787 +        gmyth_debug ("This URI is NOT a local file...");
   1.788 +
   1.789      return ret;
   1.790 -    
   1.791 +
   1.792  }
   1.793  
   1.794 -char*
   1.795 -gmyth_uri_to_string (const GMythURI* uri)
   1.796 +char *
   1.797 +gmyth_uri_to_string (const GMythURI * uri)
   1.798  {
   1.799      g_return_val_if_fail (uri != NULL, NULL);
   1.800      g_return_val_if_fail (uri->uri != NULL, NULL);
   1.801 -    
   1.802 -    return g_strdup (uri->uri->str);	
   1.803 +
   1.804 +    return g_strdup (uri->uri->str);
   1.805  }