4 * @file gmyth/gmyth_uri.c
6 * @brief <p> GMythURI utils
7 * - Extracts and parses a URI char string, in according with the RFC 2396
8 * [http://www.ietf.org/rfc/rfc2396.txt]
10 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
11 * @author Rosfran Borges <rosfran.borges@indt.org.br>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include "gmyth_uri.h"
35 #include "gmyth_socket.h"
41 #include "gmyth_debug.h"
43 static void gmyth_uri_class_init (GMythURIClass *klass);
44 static void gmyth_uri_init (GMythURI *object);
46 static void gmyth_uri_dispose (GObject *object);
47 static void gmyth_uri_finalize (GObject *object);
49 static void gmyth_uri_parser_setup_and_new(GMythURI *uri, const gchar *value);
50 static gchar* gmyth_uri_print_field(const GString* field);
52 G_DEFINE_TYPE(GMythURI, gmyth_uri, G_TYPE_OBJECT)
55 gmyth_uri_class_init (GMythURIClass *klass)
57 GObjectClass *gobject_class;
59 gobject_class = (GObjectClass *) klass;
61 gobject_class->dispose = gmyth_uri_dispose;
62 gobject_class->finalize = gmyth_uri_finalize;
66 gmyth_uri_init (GMythURI *gmyth_uri)
71 gmyth_uri_dispose (GObject *object)
73 GMythURI *gmyth_uri = GMYTH_URI(object);
75 if ( gmyth_uri->host != NULL ) {
76 g_string_free( gmyth_uri->host, TRUE );
77 gmyth_uri->host = NULL;
80 if ( gmyth_uri->protocol != NULL ) {
81 g_string_free( gmyth_uri->protocol, TRUE );
82 gmyth_uri->protocol = NULL;
85 if ( gmyth_uri->path != NULL ) {
86 g_string_free( gmyth_uri->path, TRUE );
87 gmyth_uri->path = NULL;
90 if ( gmyth_uri->fragment != NULL ) {
91 g_string_free( gmyth_uri->fragment, TRUE );
92 gmyth_uri->fragment = NULL;
95 if ( gmyth_uri->user != NULL ) {
96 g_string_free( gmyth_uri->user, TRUE );
97 gmyth_uri->user = NULL;
100 if ( gmyth_uri->password != NULL ) {
101 g_string_free( gmyth_uri->password, TRUE );
102 gmyth_uri->password = NULL;
105 if ( gmyth_uri->query != NULL ) {
106 g_string_free( gmyth_uri->query, TRUE );
107 gmyth_uri->query = NULL;
110 if ( gmyth_uri->uri != NULL ) {
111 g_string_free( gmyth_uri->uri, TRUE );
112 gmyth_uri->uri = NULL;
116 G_OBJECT_CLASS (gmyth_uri_parent_class)->dispose (object);
120 gmyth_uri_finalize (GObject *object)
122 //GMythURI *gmyth_uri = GMYTH_URI(object);
124 g_signal_handlers_destroy (object);
126 G_OBJECT_CLASS (gmyth_uri_parent_class)->finalize (object);
130 * Creates a new instance of GMythURI.
132 * @return a new instance of GMythURI.
137 GMythURI *gmyth_uri = GMYTH_URI (g_object_new (GMYTH_URI_TYPE, NULL));
143 * Creates a new instance of GMythURI.
145 * @param uri_str The URI string representing this URI instance.
147 * @return a new instance of GMythURI.
150 gmyth_uri_new_with_value (const gchar *uri_str)
152 GMythURI *gmyth_uri = GMYTH_URI (g_object_new (GMYTH_URI_TYPE, NULL));
154 gmyth_uri_parser_setup_and_new (gmyth_uri, uri_str);
160 * Gets the starting offset of a substring inside a given string.
162 * @param haystack The given string to be searched for patterns.
163 * @param needle The substring that should be matched over the haystack.
165 * @return The starting offset to the given substring, or <code>-1</code> if the
166 * haystack function parameter doesn't contains the needle string argument.
169 gmyth_strstr (const gchar *haystack, const gchar *needle)
174 if (haystack == NULL || needle == NULL)
176 strPos = strstr(haystack, needle);
180 return (strPos - haystack);
185 * Checks if a URI is absolute.
187 * @param uri The GMythURI instance.
189 * @return <code>true</code>, if the URI is absolute.
192 gmyth_uri_isabsolute (const GMythURI *uri)
194 gboolean ret = FALSE;
196 g_return_val_if_fail( uri != NULL && uri->uri != NULL && uri->protocol != NULL, FALSE );
198 if ( gmyth_strstr( uri->uri->str, GMYTH_URI_PROTOCOL_DELIM ) == 0 || strlen(uri->protocol->str) > 0 )
205 * Searches for the first reverse character occurrence, from a given
206 * list of characters, inside a given string.
208 * @param str The given string to be searched for characters occurrence.
209 * @param chars The characters list. If this string returns 4 on strlen, there are
210 * four possible characters to be matched.
211 * @param nchars The number of characters to be matched, which has at most
214 * @return The starting offset to the first character occurrence,
215 * or <code>-1</code> if the no character of the list could be found.
218 gmyth_strrchr( const gchar *str, const gchar *chars, const gint nchars )
224 if ( str == NULL || chars == NULL )
227 strLen = strlen( str );
228 for ( i= (strLen-1); 0 <= i; i-- ) {
229 for ( j=0; j<nchars; j++ ) {
230 if ( str[i] == chars[j] )
240 gmyth_uri_print_field( const GString* field )
242 if ( field != NULL && field->str != NULL && strlen(field->str) > 0 )
249 * Parses a URI string into a GMythURI instance.
251 * @param uri The GMythURI instance.
252 * @param value The URI string to be parsed.
256 gmyth_uri_parser_setup_and_new( GMythURI *uri, const gchar *value )
275 uriLen = strlen(value);
276 uri->uri = g_string_new( value );
281 protoIdx = gmyth_strstr (value, GMYTH_URI_PROTOCOL_DELIM);
283 uri->protocol = g_string_new_len (value, protoIdx);
284 currIdx += protoIdx + strlen( GMYTH_URI_PROTOCOL_DELIM );
287 /*** User (Password) ****/
288 atIdx = gmyth_strstr( value+currIdx, GMYTH_URI_USER_DELIM );
290 colonIdx = gmyth_strstr( value+currIdx, GMYTH_URI_COLON_DELIM );
292 if (0 < colonIdx && colonIdx < atIdx) {
293 uri->user = g_string_new_len (value+currIdx, colonIdx);
294 uri->password = g_string_new_len (value+currIdx+colonIdx+1, atIdx - (colonIdx+1));
297 uri->user = g_string_new_len (value+currIdx, atIdx - currIdx);
298 currIdx += atIdx + 1;
301 /*** Host (Port) ****/
302 shashIdx = gmyth_strstr( value+currIdx, GMYTH_URI_SLASH_DELIM );
304 uri->host = g_string_new_len (value+currIdx, shashIdx);
305 else if ( gmyth_uri_isabsolute(uri) == TRUE )
306 uri->host = g_string_new_len (value+currIdx, strlen (value) - currIdx);
308 host = gmyth_uri_get_host(uri);
309 colonIdx = gmyth_strrchr (host, GMYTH_URI_COLON_DELIM, 1);
310 eblacketIdx = gmyth_strrchr (host, GMYTH_URI_EBLACET_DELIM, 1);
311 if ( ( 0 < colonIdx ) && ( eblacketIdx < colonIdx ) ) {
312 GString *portStr = NULL;
313 GString *hostStr = g_string_new (host != NULL ? host : "");
315 hostLen = hostStr->len;
317 uri->host = g_string_erase (uri->host, 0, hostLen);
318 uri->host = g_string_insert_len (uri->host, 0, hostStr->str, colonIdx);
320 if (host[0] == '[' && host[hostLen-1] == ']')
321 uri->host = g_string_new_len (hostStr->str+1, colonIdx-2);
324 portStr = g_string_new_len (hostStr->str+colonIdx+1, hostLen-colonIdx-1);
325 uri->port = (gint)g_ascii_strtoull( portStr->str, NULL, 10 );
326 g_string_free (portStr, TRUE);
327 g_string_free (hostStr, TRUE);
330 const gchar* protocol = gmyth_uri_get_protocol(uri);
331 uri->port = GMYTH_URI_KNKOWN_PORT;
332 if ( strcmp(protocol, GMYTH_URI_PROTOCOL_HTTP) == 0 )
333 uri->port = GMYTH_URI_DEFAULT_HTTP_PORT;
334 if ( strcmp(protocol, GMYTH_URI_PROTOCOL_FTP) == 0 )
335 uri->port = GMYTH_URI_DEFAULT_FTP_PORT;
338 if (shashIdx > 0) currIdx += shashIdx;
343 if (gmyth_uri_isabsolute(uri) == FALSE)
348 /* Add slash delimiter at the beginning of the URL,
351 uri->path = g_string_new( GMYTH_URI_SLASH_DELIM );
353 uri->path = g_string_append( uri->path, value );
356 /* First set path simply to the rest of URI */
357 uri->path = g_string_new_len (value+currIdx, uriLen-currIdx );
360 //gmyth_debug( "uri value: %s", value );
361 uri->query = g_string_new ( g_strstr_len( value, strlen(value), GMYTH_URI_QUESTION_DELIM ) );
363 eIdx = gmyth_strstr( value+currIdx, GMYTH_URI_QUESTION_DELIM );
366 uri->query = g_string_new ( g_strstr_len( value, strlen(value), GMYTH_URI_QUESTION_DELIM ) );
367 gmyth_debug( "query = %s", uri->query->str );
370 /**** Path (Query/Fragment) ****/
371 sharpIdx = gmyth_strstr(value+currIdx, GMYTH_URI_E_DELIM);
373 uri->path = g_string_append_len( uri->path, value+currIdx, sharpIdx);
374 uri->fragment = g_string_new_len (value+currIdx+sharpIdx+1, uriLen-(currIdx+sharpIdx+1));
377 gmyth_debug( "[%s] GMythURI: host = %s, port = %d, path = %s, query = %s, fragment = %s, "\
378 "user = %s, password = %s.\n", __FUNCTION__,
379 gmyth_uri_print_field( uri->host ), uri->port,
380 gmyth_uri_print_field( uri->path ),
381 gmyth_uri_print_field( uri->query ),
382 gmyth_uri_print_field( uri->fragment ),
383 gmyth_uri_print_field ( uri->user ),
384 gmyth_uri_print_field( uri->password ) );
389 * Compares 2 URI instances, and checks them for equality.
391 * @param uri The first GMythURI instance for comparison.
392 * @param uri The second GMythURI instance for comparison.
394 * @return <code>true</code>, if these two URI instances are equals.
397 gmyth_uri_is_equals( GMythURI* uri1, GMythURI* uri2 )
399 return ( g_ascii_strcasecmp( gmyth_uri_get_host( uri1 ), gmyth_uri_get_host( uri2 ) ) == 0 &&
400 gmyth_uri_get_port( uri1 ) == gmyth_uri_get_port( uri2 ) );
404 * Checks if the URI instance represents a LiveTV recording.
406 * @param uri The GMythURI instance.
408 * @return <code>true</code>, if the URI points to LiveTV content.
411 gmyth_uri_is_livetv( GMythURI* uri )
415 g_return_val_if_fail (uri != NULL, FALSE);
416 g_return_val_if_fail (uri->uri != NULL, FALSE);
417 g_return_val_if_fail (uri->uri->str != NULL, FALSE);
419 if ((strstr (uri->uri->str, "channel") == NULL) ||
420 (strstr (uri->uri->str, "livetv") == NULL))
424 gmyth_debug( "This URI is a LiveTV recording..." );
426 gmyth_debug( "This URI is a stored remote recording." );
433 * Gets the channel name fro a URI instance.
435 * @param uri The GMythURI instance.
437 * @return The channel name, got from the substring "?channel=[channel_name]"
441 gmyth_uri_get_channel_name( GMythURI* uri )
443 gchar* channel = NULL;
445 g_return_val_if_fail( uri != NULL && uri->uri != NULL && uri->uri->str != NULL, FALSE );
447 gchar *channel_query = g_strstr_len( gmyth_uri_get_query( uri ), strlen( gmyth_uri_get_query( uri ) ), "channel" );
449 if ( channel_query != NULL )
451 gchar **chan_key_value = g_strsplit( gmyth_uri_get_query( uri ), "=", 2 );
453 /* gmyth_debug( "Channel tuple is [ %s, %s ]", chan_key_value[0], chan_key_value[1] ); */
455 if ( chan_key_value[1] != NULL && strlen( chan_key_value[1] ) > 0 )
457 channel = g_strdup( chan_key_value[1] );
460 if ( chan_key_value != NULL )
461 g_strfreev( chan_key_value );
464 gmyth_debug( "Got channel decimal value from the URI: %s", channel );
471 * Gets the channel number from a URI instance.
473 * @param uri The GMythURI instance.
475 * @return The channel number, got from the substring "?channel=[channel_number]"
476 * of the URI string, or <code>-1</code> it if couldn't be converted.
479 gmyth_uri_get_channel_num( GMythURI* uri )
481 gchar *channel_name = gmyth_uri_get_channel_name( uri );
483 if ( channel_name != NULL )
485 return g_ascii_strtoull( channel_name, NULL, 10 );
493 * Checks if the URI instance represents a reference to a local file.
495 * @param uri The GMythURI instance.
497 * @return <code>true</code>, if the URI points to a local file.
500 gmyth_uri_is_local_file( const GMythURI* uri )
502 gboolean ret = FALSE;
505 GString *hostname = gmyth_socket_get_local_hostname();
507 g_return_val_if_fail( uri != NULL, FALSE );
509 len = strlen( gmyth_uri_get_host(uri) );
511 // gmyth_debug("URI: host = %s, hostname = %s.", uri->host->str, hostname != NULL ? hostname->str : "[no hostname]");
513 ret = ( NULL != hostname && ( g_ascii_strncasecmp( uri->host->str,
514 (hostname)->str, len ) == 0 ) /*||
515 ( g_ascii_strncasecmp( gmyth_uri_get_host(uri), gmyth_socket_get_primary_addr(), len ) == 0 ) */ );
518 gmyth_debug( "This URI is a local file..." );
520 gmyth_debug( "This URI is NOT a local file..." );
527 gmyth_uri_to_string (const GMythURI* uri)
529 g_return_val_if_fail (uri != NULL, NULL);
530 g_return_val_if_fail (uri->uri != NULL, NULL);
532 return g_strdup (uri->uri->str);