示例#1
0
// Assuming formating like...
//  http://user:[email protected]/directory/somefile.php?param=1&param=2
CPropertyBag CIpAddress::ParseUrl( oexCSTR pUrl, oexUINT uMaxBufferSize )
{
	// Ensure a valid pointer
    if ( !pUrl )
        return CPropertyBag();

	// NULL terminated string? tsk, tsk...
	if ( 0 >= uMaxBufferSize )
		uMaxBufferSize = zstr::Length( pUrl );

	// Anything to parse?
	if ( !uMaxBufferSize )
		return CPropertyBag();

	// Propety bag object
    CPropertyBag pb;

	// Read into a string object
    CStr str( pUrl, uMaxBufferSize );

//	pb[ oexT( "scheme" ) ].ToString() = str.Parse( oexT( "://" ) );

	// Read in the scheme
	oexINT nScheme = str.FindSubStr( oexT( "://" ), 3 );
	if ( 0 <= nScheme && nScheme == str.FindSubStr( oexT( ":" ), 1 ) )
		pb[ oexT( "scheme" ) ].ToString() = str.SubStr( 0, nScheme ),
		str.LTrim( nScheme + 3 );

	// Trim off leading forward slashes
//	str.LTrim( oexT( ":" ) );
//	str.LTrim( oexT( "/" ) );

	// Is there a username / password?
	CStr tmp = str.Parse( oexT( "@" ) );
	if ( tmp.Length() )
	{
		// Skip the @
		str++;

		// Divide username and password
		CStr s = tmp.Parse( oexT( ":" ) );
		if ( s.Length () )
		{	pb[ oexT( "username" ) ].ToString() = s;
			tmp++; pb[ oexT( "password" ) ].ToString() = tmp;
		} // end if
		else
			pb[ oexT( "username" ) ].ToString() = tmp;

	} // end if

	// Parse the host
	tmp = str.Parse( oexT( "/" ) );
	if ( tmp.Length() )
	{
		CStr s = tmp.Parse( oexT( ":" ) );
		if ( s.Length () )
		{	pb[ oexT( "host" ) ].ToString() = s;
			tmp++; pb[ oexT( "port" ) ].ToString() = tmp;
		} // end if
		else
			pb[ oexT( "host" ) ].ToString() = tmp;

	} // end if

	// Grab the next token
	tmp = str.Parse( oexT( "?" ) );
	if ( tmp.Length() )
	{
		// Host or path?
		if ( !pb.IsKey( oexT( "host" ) ) )
			pb[ oexT( "host" ) ].ToString() = tmp;
		else
			pb[ oexT( "path" ) ].ToString() = tmp;

		// Parse extra part
		if ( str.Length() )
		{
			// Trim separator if any
			if ( oexT( '?' ) == *str.Ptr() )
			{
				str.LTrim( 1 );

				// Anything left over?
				if ( str.Length() )				
					pb[ oexT( "extra" ) ].ToString() = str.Parse( oexT( "#" ) );

				// Check for fragment
				if ( oexT( '#' ) == *str.Ptr() )
				{
					// Strip off sep
					str.LTrim( 1 );

					// Check for fragment
					if ( str.Length() )
						pb[ oexT( "fragment" ) ].ToString() = str;

				} // end if

				// Then it's all the extra
				else
					pb[ oexT( "extra" ) ].ToString() += str;

			} // end if

		} // end if
		
	} // end if

	// Whatever is left is the host if not yet parsed
	else if ( !pb.IsKey( oexT( "host" ) ) )
	{	CStr s = str.Parse( oexT( ":" ) );
		if ( s.Length () )
		{	pb[ oexT( "host" ) ].ToString() = s;
			str++; pb[ oexT( "port" ) ].ToString() = str;
		} // end if
		else
			pb[ oexT( "host" ) ].ToString() = str;
	} // end else if

	// Then whatever is left is the path
	else
		pb[ oexT( "path" ) ].ToString() = str;

	return pb;
}