-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUriUtilities.cs
98 lines (62 loc) · 3.35 KB
/
UriUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Reflection;
namespace Gsemac.Net {
public static class UriUtilities {
// Public members
public static bool SetFilePathCanonicalizationEnabled(bool enabled) {
// .NET 4.0's Uri implementation doesn't work correctly for URIs that contain trailing periods (fixed in .NET 4.5).
// https://stackoverflow.com/questions/856885/httpwebrequest-to-url-with-dot-at-the-end
// The solution below is the one given in the official bug report, as given here: https://stackoverflow.com/a/2285321 (Jon Davis)
MethodInfo getSyntax = typeof(UriParser).GetMethod("GetSyntax", BindingFlags.Static | BindingFlags.NonPublic);
FieldInfo flagsField = typeof(UriParser).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
if (getSyntax is null || flagsField is null)
return false;
foreach (string scheme in new[] { "http", "https" }) {
UriParser parser = (UriParser)getSyntax.Invoke(null, new object[] { scheme });
if (parser is null)
continue;
UriSyntaxFlags flags = (UriSyntaxFlags)flagsField.GetValue(parser);
if (enabled)
flags |= UriSyntaxFlags.CanonicalizeAsFilePath;
else
flags &= ~UriSyntaxFlags.CanonicalizeAsFilePath;
flagsField.SetValue(parser, flags);
}
return true;
}
public static bool SetPathCanonicalizationEnabled(Uri uri, bool enabled) {
// .NET 4.0's Uri implementation doesn't work correctly for URIs that contain escaped slashes (fixed in .NET 4.5).
// It attempts to unescape escaped slashes, which can cause problems for endpoints that expect them to be escaped:
// https://docs.microsoft.com/en-us/dotnet/api/system.uri?view=netcore-3.1#remarks
// See the following discussions of the issue:
// https://stackoverflow.com/q/59846369/5383169
// https://stackoverflow.com/q/781205/5383169
// The following solution is adapted from the one given here: https://stackoverflow.com/a/784937/5383169 (Rasmus Faber)
if (uri is null)
throw new ArgumentNullException(nameof(uri));
// We need to access the "PathAndQuery" property before setting the flag.
string _ = uri.PathAndQuery;
FieldInfo flagsField = typeof(Uri).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
if (flagsField is null)
return false;
UriFlags flags = (UriFlags)(ulong)flagsField.GetValue(uri);
if (enabled)
flags |= UriFlags.PathNotCanonical | UriFlags.QueryNotCanonical;
else
flags &= ~(UriFlags.PathNotCanonical | UriFlags.QueryNotCanonical);
flagsField.SetValue(uri, (ulong)flags);
return true;
}
// Private members
[Flags]
private enum UriSyntaxFlags {
CanonicalizeAsFilePath = 0x1000000,
}
// https://referencesource.microsoft.com/#System/net/System/URI.cs,a5f3168ce0003968,references
[Flags]
private enum UriFlags {
PathNotCanonical = 0x10,
QueryNotCanonical = 0x20,
}
}
}