1 | // -----------------------------------------------------------------------
|
---|
2 | // <copyright file="UriUtilities.cs" company="Microsoft">
|
---|
3 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
---|
4 | // </copyright>
|
---|
5 | // -----------------------------------------------------------------------
|
---|
6 |
|
---|
7 | namespace Microsoft.Samples.Kinect.Webserver
|
---|
8 | {
|
---|
9 | using System;
|
---|
10 | using System.Diagnostics;
|
---|
11 |
|
---|
12 | /// <summary>
|
---|
13 | /// Static class that defines uri manipulation utilities.
|
---|
14 | /// </summary>
|
---|
15 | public static class UriUtilities
|
---|
16 | {
|
---|
17 | /// <summary>
|
---|
18 | /// Separator between URI path segments.
|
---|
19 | /// </summary>
|
---|
20 | public const string PathSeparator = "/";
|
---|
21 |
|
---|
22 | /// <summary>
|
---|
23 | /// Concatenate specified path segments at the end of specified URI.
|
---|
24 | /// </summary>
|
---|
25 | /// <param name="uri">
|
---|
26 | /// Absolute URI to serve as starting point of concatenation.
|
---|
27 | /// </param>
|
---|
28 | /// <param name="pathSegments">
|
---|
29 | /// Path segments to concatenate at the end of URI.
|
---|
30 | /// </param>
|
---|
31 | /// <returns>
|
---|
32 | /// URI that represents the combination of the specified uri and path segments.
|
---|
33 | /// May be null if uri segments could not be concatenated.
|
---|
34 | /// </returns>
|
---|
35 | public static Uri ConcatenateSegments(this Uri uri, params string[] pathSegments)
|
---|
36 | {
|
---|
37 | Uri result = uri;
|
---|
38 |
|
---|
39 | if (uri == null)
|
---|
40 | {
|
---|
41 | throw new ArgumentNullException("uri");
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (pathSegments == null)
|
---|
45 | {
|
---|
46 | throw new ArgumentNullException("pathSegments");
|
---|
47 | }
|
---|
48 |
|
---|
49 | for (int i = 0; i < pathSegments.Length; ++i)
|
---|
50 | {
|
---|
51 | var segment = pathSegments[i];
|
---|
52 |
|
---|
53 | if (segment == null)
|
---|
54 | {
|
---|
55 | throw new ArgumentException(@"One or more of the specified path segments is null", "pathSegments");
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (i < pathSegments.Length - 1)
|
---|
59 | {
|
---|
60 | // For each element other than the last element, make sure it ends in the
|
---|
61 | // path separator character so that it's treated as a path segment rather
|
---|
62 | // than an endpoint or resource (see CoInternetCombineIUri documentation
|
---|
63 | // for an explanation of standard URI combination behavior)
|
---|
64 | segment = segment.EndsWith(PathSeparator, StringComparison.OrdinalIgnoreCase) ? segment : (segment + PathSeparator);
|
---|
65 | }
|
---|
66 |
|
---|
67 | // Now call the standard URI class to take care of canonicalization and other
|
---|
68 | // combination functionality
|
---|
69 | var previous = result;
|
---|
70 | try
|
---|
71 | {
|
---|
72 | result = new Uri(previous, new Uri(segment.Trim(), UriKind.Relative));
|
---|
73 | }
|
---|
74 | catch (UriFormatException)
|
---|
75 | {
|
---|
76 | Trace.TraceError("Unable to concatenate uri \"{0}\" with path segment \"{1}\"", previous, segment);
|
---|
77 | result = null;
|
---|
78 | break;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | return result;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|