- /// The Uri classes in .NET prior to 4.5 and Mono scrub through your Uris and modify them in order to prevent vulnerabilities, for
- /// example escaped slashes are unescaped. This scrubbing however prevents Uris that are inline with RFC 3986. Beyond that it prevents
- /// using .NET's HTTP clients (HttpClient and WebClient) to talk to APIs that require accessing resources using escaped
- /// slashes unless you are using .NET 4.5.
- ///
- /// This static class allows you to purify a Uri instance so that it remains untouched across all .NET runtime versions
- ///
- ///
- public static class Purifier
- {
-
- private static readonly bool hasBrokenDotNetUri;
-
- private static readonly bool isMono;
-
- static Purifier()
- {
- isMono = typeof(Uri).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic) == null;
- if (isMono)
- return;
-
- //ShouldUseLegacyV2Quirks was introduced in .net 4.5
- //Eventhough 4.5 is an inplace update of 4.0 this call will return
- //a different value if an application specifically targets 4.0 or 4.5+
- var legacyV2Quirks = typeof(UriParser).GetProperty("ShouldUseLegacyV2Quirks", BindingFlags.Static | BindingFlags.NonPublic);
- if (legacyV2Quirks == null)
- {
- hasBrokenDotNetUri = true; //neither 4.0 or 4.5
- return;
- }
- var isBrokenUri = (bool)legacyV2Quirks.GetValue(null, null);
- if (!isBrokenUri)
- return; //application targets 4.5
-
- //4.0 uses legacyV2quirks on the UriParser but you can set
- //