NTP Analyzer  0.8.2
Analyze the operation of time servers
UriExtensions.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Text;
26 
27 namespace Ntp.Common.Web
28 {
29  public static class UriExtensions
30  {
31  public static Uri Append(this Uri uri, params string[] paths)
32  {
33  string basePath;
34  UriKind uriKind;
35 
36  GetBase(uri, out basePath, out uriKind);
37  string resPath = paths.Aggregate(
38  basePath,
39  (current, path) => $"{current.TrimEnd('/')}/{path.TrimStart('/')}");
40  Uri newUri = new Uri(resPath, uriKind);
41 
42  return newUri;
43  }
44 
45  public static Uri Append(this Uri uri, Uri relativeUri, params string[] paths)
46  {
47  string relativeUriText = relativeUri.IsAbsoluteUri
48  ? relativeUri.AbsolutePath
49  : relativeUri.ToString();
50  var list = new List<string> {relativeUriText};
51  list.AddRange(paths);
52  return uri.Append(list.ToArray());
53  }
54 
55  public static Uri Append(this Uri uri, Uri relativeUri, string path)
56  {
57  string relativeUriText = relativeUri.IsAbsoluteUri
58  ? relativeUri.AbsolutePath
59  : relativeUri.ToString();
60  var list = new List<string> {relativeUriText, path};
61  return uri.Append(list.ToArray());
62  }
63 
64  public static Uri AppendExtension(this Uri uri, string extension)
65  {
66  string basePath;
67  UriKind uriKind;
68 
69  GetBase(uri, out basePath, out uriKind);
70  basePath = basePath.TrimEnd('/') + extension;
71  Uri newUri = new Uri(basePath, uriKind);
72 
73  return newUri;
74  }
75 
76  public static string DebugInfo(this Uri uri)
77  {
78  StringBuilder b = new StringBuilder();
79  b.AppendLine($"ToString(): {uri}");
80  b.AppendLine($"IsAbsoluteUri: {uri.IsAbsoluteUri}");
81  b.AppendLine($"OriginalString: {uri.OriginalString}");
82 
83  if (uri.IsAbsoluteUri)
84  {
85  b.AppendLine($"AbsolutePath: {uri.AbsolutePath}");
86  b.AppendLine($"AbsoluteUri: {uri.AbsoluteUri}");
87  b.AppendLine($"Authority: {uri.Authority}");
88  b.AppendLine($"DnsSafeHost: {uri.DnsSafeHost}");
89  b.AppendLine($"Fragment: {uri.Fragment}");
90  b.AppendLine($"Host: {uri.Host}");
91  b.AppendLine($"HostNameType: {uri.HostNameType}");
92  b.AppendLine($"IsDefaultPort {uri.IsDefaultPort}");
93  b.AppendLine($"IsFile: {uri.IsFile}");
94  b.AppendLine($"IsLoopback: {uri.IsLoopback}");
95  b.AppendLine($"IsUnc: {uri.IsUnc}");
96  b.AppendLine($"LocalPath: {uri.LocalPath}");
97  b.AppendLine($"PathAndQuery: {uri.PathAndQuery}");
98  b.AppendLine($"Port: {uri.Port}");
99  b.AppendLine($"Query: {uri.Query}");
100  b.AppendLine($"Scheme: {uri.Scheme}");
101  b.AppendLine($"Segments: {uri.Segments}");
102  b.AppendLine($"UserEscaped: {uri.UserEscaped}");
103  b.AppendLine($"UserInfo: {uri.UserInfo}");
104  }
105 
106  return b.ToString();
107  }
108 
109  public static string ToHtmlString(this Uri uri)
110  {
111  return Uri.EscapeUriString(uri.ToString());
112  }
113 
114  private static void GetBase(Uri uri, out string basePath, out UriKind uriKind)
115  {
116  string orig = uri.OriginalString.TrimStart();
117  if (orig.StartsWith("/") && !orig.StartsWith("//"))
118  {
119  basePath = uri.IsAbsoluteUri
120  ? uri.AbsolutePath
121  : uri.ToString();
122  uriKind = UriKind.Relative;
123  }
124  else
125  {
126  basePath = uri.ToString();
127  uriKind = UriKind.Absolute;
128  }
129  }
130  }
131 }
static void GetBase(Uri uri, out string basePath, out UriKind uriKind)
static Uri Append(this Uri uri, params string[] paths)
static Uri Append(this Uri uri, Uri relativeUri, params string[] paths)
static Uri Append(this Uri uri, Uri relativeUri, string path)
static string ToHtmlString(this Uri uri)
static string DebugInfo(this Uri uri)
static Uri AppendExtension(this Uri uri, string extension)
var b
Definition: bootstrap.min.js:6