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