4 * - Extracts and parses a URI char string, in according with the RFC 2396
5 * [http://www.ietf.org/rfc/rfc2396.txt]
7 * @author Rosfran Borges (rosfran.borges@indt.org.br)
17 myth_strstr( const gchar *haystack, const gchar *needle )
22 if (haystack == NULL || needle == NULL)
24 strPos = strstr(haystack, needle);
28 return (strPos - haystack);
33 myth_uri_isabsolute( const MythURI *uri )
37 g_return_val_if_fail( uri != NULL && uri->uri != NULL && uri->protocol != NULL, FALSE );
39 if ( myth_strstr( uri->uri->str, MYTH_URI_PROTOCOL_DELIM ) == 0 || strlen(uri->protocol->str) > 0 )
46 myth_strrchr( const gchar *str, const gchar *chars, const gint nchars )
52 if ( str == NULL || chars == NULL )
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] )
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("");
83 myth_uri_new( gchar *value )
86 MythURI *uri = myth_uri_init();
104 uriLen = strlen(value);
105 uri->uri = g_string_new( value );
110 protoIdx = myth_strstr( value, MYTH_URI_PROTOCOL_DELIM );
112 uri->protocol = g_string_append_len( uri->protocol, value, protoIdx );
113 currIdx += protoIdx + strlen( MYTH_URI_PROTOCOL_DELIM );
116 /*** User (Password) ****/
117 atIdx = myth_strstr( value+currIdx, MYTH_URI_USER_DELIM );
119 colonIdx = myth_strstr( value+currIdx, MYTH_URI_COLON_DELIM );
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) );
126 uri->user = g_string_append_len( uri->user, value+currIdx, atIdx - currIdx );
127 currIdx += atIdx + 1;
130 /*** Host (Port) ****/
131 shashIdx = myth_strstr( value+currIdx, MYTH_URI_SLASH_DELIM );
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 );
142 hostLen = hostStr->len;
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 );
148 if (host[0] == '[' && host[hostLen-1] == ']')
149 uri->host = g_string_append_len( uri->host, hostStr->str+1, colonIdx-2 );
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 );
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;
167 if (shashIdx > 0) currIdx += shashIdx;
172 if (myth_uri_isabsolute(uri) == FALSE)
177 /* Add slash delimiter at the beginning of the URL,
180 uri->path = g_string_new( MYTH_URI_SLASH_DELIM );
182 uri->path = g_string_append( uri->path, value );
185 /* First set path simply to the rest of URI */
186 g_string_append_len( uri->path, value+currIdx, uriLen-currIdx );
189 /**** Path (Query/Fragment) ****/
190 sharpIdx = myth_strstr(value+currIdx, MYTH_URI_SHARP_DELIM);
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));
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);
202 queryLen -= uriLen - (currIdx+sharpIdx+1);
203 uri->query = g_string_append_len( uri->query, value+currIdx+questionIdx+1, queryLen );