gst-plugins-mythtv/myth_uri.c
author leo_sobral
Thu Sep 21 00:05:27 2006 +0100 (2006-09-21)
branchtrunk
changeset 3 265cdb1c59e3
parent 2 mythtv_plugin/myth_uri.c@bd3829c2e9c9
permissions -rwxr-xr-x
[svn r4] Renamed the mythtv GStreamer module name.
     1 /**
     2  *
     3  *    MythURI utils
     4  *  - Extracts and parses a URI char string, in according with the RFC 2396
     5  *    [http://www.ietf.org/rfc/rfc2396.txt]
     6  *
     7  *   @author Rosfran Borges (rosfran.borges@indt.org.br)
     8  *
     9  */
    10 
    11 #include "myth_uri.h"
    12 #include <glib.h>
    13 #include <string.h>
    14 #include <stdlib.h>
    15 
    16 static gint 
    17 myth_strstr( const gchar *haystack, const gchar *needle )
    18 {
    19 	
    20 	gchar *strPos;
    21 	
    22 	if (haystack == NULL || needle == NULL)
    23 		return -1;
    24 	strPos = strstr(haystack, needle);
    25 	if (strPos == NULL)
    26 		return -1;
    27 		
    28 	return (strPos - haystack);
    29 
    30 }
    31 
    32 static gboolean
    33 myth_uri_isabsolute( const MythURI *uri )
    34 {
    35 	gboolean ret = FALSE;
    36 	
    37 	g_return_val_if_fail( uri != NULL && uri->uri != NULL && uri->protocol != NULL, FALSE );
    38 	
    39 	if ( myth_strstr( uri->uri->str, MYTH_URI_PROTOCOL_DELIM ) == 0 || strlen(uri->protocol->str) > 0 )
    40 		ret = TRUE;
    41 		
    42 	return ret;	
    43 }
    44 
    45 static gint
    46 myth_strrchr( const gchar *str, const gchar *chars, const gint nchars )
    47 {
    48 
    49 	gint strLen;
    50 	gint i, j;
    51 	
    52 	if ( str == NULL || chars == NULL )
    53 		return -1;
    54 		
    55 	strLen = strlen( str );
    56 	for ( i= (strLen-1); 0 <= i; i-- ) {
    57 		for ( j=0; j<nchars; j++ ) {
    58 			if ( str[i] == chars[j] )
    59 				return i;
    60 		}
    61 	}
    62 
    63 	return -1;
    64 
    65 }
    66 
    67 static MythURI *
    68 myth_uri_init( )
    69 {
    70 	MythURI *uri = g_new0( MythURI, 1 );
    71 	uri->host = g_string_new("");
    72 	uri->fragment = g_string_new("");
    73 	uri->password = g_string_new("");
    74 	uri->path = g_string_new("");
    75 	uri->protocol = g_string_new("");
    76 	uri->query = g_string_new("");
    77 	uri->uri = g_string_new("");
    78 	uri->user = g_string_new("");
    79 	return uri;
    80 }
    81 
    82 const MythURI *
    83 myth_uri_new( gchar *value )
    84 {
    85 	
    86 	MythURI *uri = myth_uri_init();
    87 
    88 	gchar *protocol;
    89 	gint uriLen;
    90 	gint currIdx;
    91 	gint protoIdx;
    92 	gint atIdx;
    93 	gint colonIdx;
    94 	gint shashIdx;
    95 	gchar *host;
    96 	gint eblacketIdx;
    97 	GString *hostStr;
    98 	GString *portStr;
    99 	gint hostLen;
   100 	gint sharpIdx;
   101 	gint questionIdx;
   102 	gint queryLen;
   103 	
   104 	uriLen = strlen(value);
   105 	uri->uri = g_string_new( value );
   106 		
   107 	currIdx = 0;
   108 	
   109 	/*** Protocol ****/
   110 	protoIdx = myth_strstr( value, MYTH_URI_PROTOCOL_DELIM );
   111 	if (0 < protoIdx) {
   112 		uri->protocol = g_string_append_len( uri->protocol, value, protoIdx );
   113 		currIdx += protoIdx + strlen( MYTH_URI_PROTOCOL_DELIM );
   114 	}
   115 
   116 	/*** User (Password) ****/
   117 	atIdx = myth_strstr( value+currIdx, MYTH_URI_USER_DELIM );
   118 	if ( 0 < atIdx ) {
   119 		colonIdx = myth_strstr( value+currIdx, MYTH_URI_COLON_DELIM );
   120 
   121 		if (0 < colonIdx && colonIdx < atIdx) {
   122 			uri->user = g_string_append_len( uri->user, value+currIdx,  colonIdx );
   123 			uri->password = g_string_append_len( uri->password, value+currIdx+colonIdx+1, atIdx-(colonIdx+1) );
   124 		}
   125 		else 
   126 			uri->user = g_string_append_len( uri->user, value+currIdx, atIdx - currIdx );
   127 		currIdx += atIdx + 1;
   128 	}
   129 
   130 	/*** Host (Port) ****/
   131 	shashIdx = myth_strstr( value+currIdx, MYTH_URI_SLASH_DELIM );
   132 	if ( 0 < shashIdx )
   133 		uri->host = g_string_append_len( uri->host, value+currIdx, shashIdx );
   134 	else if ( myth_uri_isabsolute(uri) == TRUE )
   135 		uri->host = g_string_append_len( uri->host, value+currIdx, strlen(value) - currIdx );
   136 	host = g_strdup( myth_uri_gethost(uri) );
   137 	colonIdx = myth_strrchr( host, MYTH_URI_COLON_DELIM, 1 );
   138 	eblacketIdx = myth_strrchr( host, MYTH_URI_EBLACET_DELIM, 1 );
   139 	if ( ( 0 < colonIdx ) && ( eblacketIdx < colonIdx ) ) {
   140 		hostStr = g_string_new( host );
   141 
   142 		hostLen = hostStr->len;
   143 		/**** host ****/
   144 		uri->host = g_string_erase( uri->host, 0, hostLen );
   145 		uri->host = g_string_insert_len( uri->host, 0, hostStr->str, colonIdx );
   146 		//host = myth_uri_gethost( uri );
   147 		if (0 < hostLen) {
   148 			if (host[0] == '[' && host[hostLen-1] == ']')
   149 				uri->host = g_string_append_len( uri->host, hostStr->str+1,  colonIdx-2 );
   150 		}
   151 		/**** port ****/
   152 		portStr = g_string_new("");
   153 		portStr = g_string_append_len( portStr, hostStr->str+colonIdx+1, hostLen-colonIdx-1 );
   154 		uri->port = atoi( portStr->str );
   155 		g_string_free( portStr, TRUE );
   156 		g_string_free( hostStr, FALSE );
   157 	}
   158 	else {
   159 		uri->port = MYTH_URI_KNKOWN_PORT;
   160 		protocol = myth_uri_getprotocol(uri);
   161 		if ( strcmp(protocol, MYTH_URI_PROTOCOL_HTTP) == 0 )
   162 			uri->port = MYTH_URI_DEFAULT_HTTP_PORT;
   163 		if ( strcmp(protocol, MYTH_URI_PROTOCOL_FTP) == 0 )
   164 			uri->port = MYTH_URI_DEFAULT_FTP_PORT;
   165 	}
   166 	
   167 	if (shashIdx > 0) currIdx += shashIdx;
   168 	
   169 	/*
   170 		Handle relative URL
   171 	*/
   172 	if (myth_uri_isabsolute(uri) == FALSE)
   173 	{
   174 
   175 		if (shashIdx != 0)
   176 		{
   177 			/* Add slash delimiter at the beginning of the URL,
   178 			   if it doesn't exist 
   179 			*/
   180 			uri->path = g_string_new( MYTH_URI_SLASH_DELIM );
   181 		}
   182 		uri->path = g_string_append( uri->path, value );
   183 		
   184 	} else {
   185 		/* First set path simply to the rest of URI */
   186 		g_string_append_len( uri->path, value+currIdx,  uriLen-currIdx );
   187 	}
   188 		
   189 	/**** Path (Query/Fragment) ****/
   190 	sharpIdx = myth_strstr(value+currIdx, MYTH_URI_SHARP_DELIM);
   191 	if (0 < sharpIdx) {
   192 		uri->path = g_string_append_len( uri->path, value+currIdx, sharpIdx);
   193 		uri->fragment = g_string_append_len( uri->fragment, 
   194 			value+currIdx+sharpIdx+1, uriLen-(currIdx+sharpIdx+1));
   195 	}
   196 	
   197 	questionIdx = myth_strstr( value+currIdx, MYTH_URI_QUESTION_DELIM );
   198 	if ( 0 < questionIdx ) {
   199 		uri->path = g_string_append_len( uri->path, value+currIdx, questionIdx );
   200 		queryLen = uriLen-(currIdx+questionIdx+1);
   201 		if ( 0 < sharpIdx )
   202 			queryLen -= uriLen - (currIdx+sharpIdx+1);
   203 		uri->query = g_string_append_len( uri->query, value+currIdx+questionIdx+1, queryLen );
   204 	}
   205 	
   206 	return uri;
   207 
   208 }