1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/gst-plugins-mythtv/myth_uri.c Thu Sep 21 00:05:27 2006 +0100
1.3 @@ -0,0 +1,208 @@
1.4 +/**
1.5 + *
1.6 + * MythURI utils
1.7 + * - Extracts and parses a URI char string, in according with the RFC 2396
1.8 + * [http://www.ietf.org/rfc/rfc2396.txt]
1.9 + *
1.10 + * @author Rosfran Borges (rosfran.borges@indt.org.br)
1.11 + *
1.12 + */
1.13 +
1.14 +#include "myth_uri.h"
1.15 +#include <glib.h>
1.16 +#include <string.h>
1.17 +#include <stdlib.h>
1.18 +
1.19 +static gint
1.20 +myth_strstr( const gchar *haystack, const gchar *needle )
1.21 +{
1.22 +
1.23 + gchar *strPos;
1.24 +
1.25 + if (haystack == NULL || needle == NULL)
1.26 + return -1;
1.27 + strPos = strstr(haystack, needle);
1.28 + if (strPos == NULL)
1.29 + return -1;
1.30 +
1.31 + return (strPos - haystack);
1.32 +
1.33 +}
1.34 +
1.35 +static gboolean
1.36 +myth_uri_isabsolute( const MythURI *uri )
1.37 +{
1.38 + gboolean ret = FALSE;
1.39 +
1.40 + g_return_val_if_fail( uri != NULL && uri->uri != NULL && uri->protocol != NULL, FALSE );
1.41 +
1.42 + if ( myth_strstr( uri->uri->str, MYTH_URI_PROTOCOL_DELIM ) == 0 || strlen(uri->protocol->str) > 0 )
1.43 + ret = TRUE;
1.44 +
1.45 + return ret;
1.46 +}
1.47 +
1.48 +static gint
1.49 +myth_strrchr( const gchar *str, const gchar *chars, const gint nchars )
1.50 +{
1.51 +
1.52 + gint strLen;
1.53 + gint i, j;
1.54 +
1.55 + if ( str == NULL || chars == NULL )
1.56 + return -1;
1.57 +
1.58 + strLen = strlen( str );
1.59 + for ( i= (strLen-1); 0 <= i; i-- ) {
1.60 + for ( j=0; j<nchars; j++ ) {
1.61 + if ( str[i] == chars[j] )
1.62 + return i;
1.63 + }
1.64 + }
1.65 +
1.66 + return -1;
1.67 +
1.68 +}
1.69 +
1.70 +static MythURI *
1.71 +myth_uri_init( )
1.72 +{
1.73 + MythURI *uri = g_new0( MythURI, 1 );
1.74 + uri->host = g_string_new("");
1.75 + uri->fragment = g_string_new("");
1.76 + uri->password = g_string_new("");
1.77 + uri->path = g_string_new("");
1.78 + uri->protocol = g_string_new("");
1.79 + uri->query = g_string_new("");
1.80 + uri->uri = g_string_new("");
1.81 + uri->user = g_string_new("");
1.82 + return uri;
1.83 +}
1.84 +
1.85 +const MythURI *
1.86 +myth_uri_new( gchar *value )
1.87 +{
1.88 +
1.89 + MythURI *uri = myth_uri_init();
1.90 +
1.91 + gchar *protocol;
1.92 + gint uriLen;
1.93 + gint currIdx;
1.94 + gint protoIdx;
1.95 + gint atIdx;
1.96 + gint colonIdx;
1.97 + gint shashIdx;
1.98 + gchar *host;
1.99 + gint eblacketIdx;
1.100 + GString *hostStr;
1.101 + GString *portStr;
1.102 + gint hostLen;
1.103 + gint sharpIdx;
1.104 + gint questionIdx;
1.105 + gint queryLen;
1.106 +
1.107 + uriLen = strlen(value);
1.108 + uri->uri = g_string_new( value );
1.109 +
1.110 + currIdx = 0;
1.111 +
1.112 + /*** Protocol ****/
1.113 + protoIdx = myth_strstr( value, MYTH_URI_PROTOCOL_DELIM );
1.114 + if (0 < protoIdx) {
1.115 + uri->protocol = g_string_append_len( uri->protocol, value, protoIdx );
1.116 + currIdx += protoIdx + strlen( MYTH_URI_PROTOCOL_DELIM );
1.117 + }
1.118 +
1.119 + /*** User (Password) ****/
1.120 + atIdx = myth_strstr( value+currIdx, MYTH_URI_USER_DELIM );
1.121 + if ( 0 < atIdx ) {
1.122 + colonIdx = myth_strstr( value+currIdx, MYTH_URI_COLON_DELIM );
1.123 +
1.124 + if (0 < colonIdx && colonIdx < atIdx) {
1.125 + uri->user = g_string_append_len( uri->user, value+currIdx, colonIdx );
1.126 + uri->password = g_string_append_len( uri->password, value+currIdx+colonIdx+1, atIdx-(colonIdx+1) );
1.127 + }
1.128 + else
1.129 + uri->user = g_string_append_len( uri->user, value+currIdx, atIdx - currIdx );
1.130 + currIdx += atIdx + 1;
1.131 + }
1.132 +
1.133 + /*** Host (Port) ****/
1.134 + shashIdx = myth_strstr( value+currIdx, MYTH_URI_SLASH_DELIM );
1.135 + if ( 0 < shashIdx )
1.136 + uri->host = g_string_append_len( uri->host, value+currIdx, shashIdx );
1.137 + else if ( myth_uri_isabsolute(uri) == TRUE )
1.138 + uri->host = g_string_append_len( uri->host, value+currIdx, strlen(value) - currIdx );
1.139 + host = g_strdup( myth_uri_gethost(uri) );
1.140 + colonIdx = myth_strrchr( host, MYTH_URI_COLON_DELIM, 1 );
1.141 + eblacketIdx = myth_strrchr( host, MYTH_URI_EBLACET_DELIM, 1 );
1.142 + if ( ( 0 < colonIdx ) && ( eblacketIdx < colonIdx ) ) {
1.143 + hostStr = g_string_new( host );
1.144 +
1.145 + hostLen = hostStr->len;
1.146 + /**** host ****/
1.147 + uri->host = g_string_erase( uri->host, 0, hostLen );
1.148 + uri->host = g_string_insert_len( uri->host, 0, hostStr->str, colonIdx );
1.149 + //host = myth_uri_gethost( uri );
1.150 + if (0 < hostLen) {
1.151 + if (host[0] == '[' && host[hostLen-1] == ']')
1.152 + uri->host = g_string_append_len( uri->host, hostStr->str+1, colonIdx-2 );
1.153 + }
1.154 + /**** port ****/
1.155 + portStr = g_string_new("");
1.156 + portStr = g_string_append_len( portStr, hostStr->str+colonIdx+1, hostLen-colonIdx-1 );
1.157 + uri->port = atoi( portStr->str );
1.158 + g_string_free( portStr, TRUE );
1.159 + g_string_free( hostStr, FALSE );
1.160 + }
1.161 + else {
1.162 + uri->port = MYTH_URI_KNKOWN_PORT;
1.163 + protocol = myth_uri_getprotocol(uri);
1.164 + if ( strcmp(protocol, MYTH_URI_PROTOCOL_HTTP) == 0 )
1.165 + uri->port = MYTH_URI_DEFAULT_HTTP_PORT;
1.166 + if ( strcmp(protocol, MYTH_URI_PROTOCOL_FTP) == 0 )
1.167 + uri->port = MYTH_URI_DEFAULT_FTP_PORT;
1.168 + }
1.169 +
1.170 + if (shashIdx > 0) currIdx += shashIdx;
1.171 +
1.172 + /*
1.173 + Handle relative URL
1.174 + */
1.175 + if (myth_uri_isabsolute(uri) == FALSE)
1.176 + {
1.177 +
1.178 + if (shashIdx != 0)
1.179 + {
1.180 + /* Add slash delimiter at the beginning of the URL,
1.181 + if it doesn't exist
1.182 + */
1.183 + uri->path = g_string_new( MYTH_URI_SLASH_DELIM );
1.184 + }
1.185 + uri->path = g_string_append( uri->path, value );
1.186 +
1.187 + } else {
1.188 + /* First set path simply to the rest of URI */
1.189 + g_string_append_len( uri->path, value+currIdx, uriLen-currIdx );
1.190 + }
1.191 +
1.192 + /**** Path (Query/Fragment) ****/
1.193 + sharpIdx = myth_strstr(value+currIdx, MYTH_URI_SHARP_DELIM);
1.194 + if (0 < sharpIdx) {
1.195 + uri->path = g_string_append_len( uri->path, value+currIdx, sharpIdx);
1.196 + uri->fragment = g_string_append_len( uri->fragment,
1.197 + value+currIdx+sharpIdx+1, uriLen-(currIdx+sharpIdx+1));
1.198 + }
1.199 +
1.200 + questionIdx = myth_strstr( value+currIdx, MYTH_URI_QUESTION_DELIM );
1.201 + if ( 0 < questionIdx ) {
1.202 + uri->path = g_string_append_len( uri->path, value+currIdx, questionIdx );
1.203 + queryLen = uriLen-(currIdx+questionIdx+1);
1.204 + if ( 0 < sharpIdx )
1.205 + queryLen -= uriLen - (currIdx+sharpIdx+1);
1.206 + uri->query = g_string_append_len( uri->query, value+currIdx+questionIdx+1, queryLen );
1.207 + }
1.208 +
1.209 + return uri;
1.210 +
1.211 +}