Recipe14.4.Forming and Validating an Absolute Uri


Recipe 14.4. Forming and Validating an Absolute Uri

Problem

You have a base URI of the form http://www.oreilly.com and a relative URI of the form hello%20world.htm; you want to form an absolute URI from them and ensure it is correctly formed.

Solution

Use the Uri class to combine a base URI and a relative URI via a constructor overload that takes the base and relative paths and then use the functions on the Uri class to validate it, as shown in Example 14-2.

Example 14-2. CreateAndVerifyAbsoluteUri method

 public static Uri CreateAndVerifyAbsoluteUri(string uriBaseString,                             string uriRelativeString) {     try     {         // Make the base URI.         Uri baseUri = new Uri(uriBaseString,UriKind.Absolute);         // Make the relative URI.         Uri relativeUri = new Uri(uriRelativeString, UriKind.Relative);         // Create the full URI by combining the base and relative.         Uri absoluteUri = new Uri(baseUri, relativeUri);         // Verify we did make an absolute URI.         if(absoluteUri.IsAbsoluteUri==false)             throw new UriFormatException(                 "Could not form absolute Uri from " +                 baseUri.OriginalString + " and " +                 relativeUri.OriginalString);         // Make sure our original base URI is a base URI for the new         // absolute URI.         if(baseUri.IsBaseOf(absoluteUri)==false)             throw new UriFormatException(                 "Base Uri was invalid for newly formed Absolute Uri: " +                 baseUri.OriginalString + " and " +                 absoluteUri.OriginalString);         // Get the relative URI from the difference between the base         // and the absolute URIs.         Uri relCheckUri = baseUri.MakeRelativeUri(absoluteUri);         // This new relative URI should equal our previous URI.         if(relCheckUri != relativeUri)             throw new UriFormatException(                 "Could not make equivalent relative Uri from new " +                 "Absolute Uri: " +                 relCheckUri.OriginalString + " and " +                 absoluteUri.OriginalString);         Uri newAbsoluteUri = new Uri(baseUri, relCheckUri);         // Check that the new and the original match.         if(Uri.Compare(absoluteUri, newAbsoluteUri,             UriComponents.AbsoluteUri, UriFormat.Unescaped,             StringComparison.InvariantCulture) != 0)         {             throw new UriFormatException(                 "New Absolute Uri did not equal originally formed " +                 "Absolute Uri: " +                 baseUri.OriginalString + " and " +                 absoluteUri.OriginalString);             }         // It's OK, send it.         return absoluteUri;     }     catch (ArgumentNullException e)     {         // uriString is a null reference (Nothing in Visual Basic).         Console.WriteLine("URI string object is a null reference: {0}", e);     }     catch (UriFormatException e)     {         Console.WriteLine("URI formatting error: {0}", e);     }     return null; } // … Uri myUri = CreateAndVerifyAbsoluteUri("http://www.oreilly.com",                        "hello%20world.htm"); // Displays http://www.oreilly.com/hello world.htm. Console.WriteLine(myUri.AbsoluteUri); 

Discussion

The System.Net.Uri class has a constructor overload that allows you to create a URI from a base Uri and a relative Uri. This creates the absolute URI and places it in the Uri.AbsoluteUri property. If there are two strings for the base path and relative path, escaping/unescaping can also be controlled through another overload of the Uri constructor that takes a UriKind as the last parameter (UriKind), but care needs to be taken here: if you unescape the Uri, it will put the URI into a form more readable by a human but no longer usable as a URI (this is because any spaces that were escaped as %20 will now be considered whitespace).

Here are the error conditions that can cause a UriFormatException to be thrown when using the Uri constructor that takes a string for the path and a UriKind to control escaping:

  • The passed string contains a relative URI, and the UriKind is Absolute.

  • The passed string contains an absolute URI, and the UriKind is Relative.

There are various ways that URI creation can fail. A URI might fail the IsAbsoluteUri check if the authority is not provided as part of the base URI. The IsBaseOf method on a URI might fail if the current URI is not identical to the internal base URI, given that all items after the last slash are ignored on the full URI.

See Also

See the "Uri Class" topic in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net