gpt4 book ai didi

c# - 在单声道上获取带有转义斜杠的 Uri

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:17 26 4
gpt4 key购买 nike

更新:一个修复程序现已进入 mono .这是个好消息!

更新:添加了修复片段处理的逻辑。

我正在尝试使用 Uri 类在 Mono 上发送带有编码斜杠的请求。这基本上是这个 question 的 Mono 等价物: GETting a URL with an url-encoded slash

问题在于,与 .NET 类似的 Mono 将在构造 Uri 时取消转义它在 Uri 中找到的任何斜线。最初采用此逻辑是为了消除在路径经过转义编码但未检测到的情况下可能发生的漏洞。

在上一篇文章中有一个 hack,它显示了通过反射在底层 Uri 类上设置标志,强制保留转义的斜线。此行为已在 .NET 4.5 中得到修复,默认情况下允许转义斜线(正如我在评论中提到的)。

我试图在 Mono 上做同样的事情,但它失败了,因为 Uri 类的内部结构不同。我想出了这种方法来实现我想要的,它有效,但它非常 hacky。

类(class)计划 { 静态无效主要(字符串[]参数) { var uri = new Uri("http://www.yahoo.com/%2F?Foo=Bar%2F#frag "); UriHelper.ForceCanonicalPathAndQuery(uri); Console.WriteLine("uri.ToString() - "+ uri.ToString()); Console.WriteLine("uri.AbsoluteUri - "+ uri.AbsoluteUri); Console.WriteLine("uri.Host - "+ uri.Host); Console.WriteLine("uri.Query - "+ uri.Query); Console.WriteLine("uri.PathAndQuery - "+ uri.PathAndQuery); Console.WriteLine("uri.AbsolutePath - "+ uri.AbsolutePath); Console.WriteLine("uri.Fragment - "+ uri.Fragment);

public class UriHelper {
private static Type uriType = typeof(Uri);
private static FieldInfo sourceField;
private static FieldInfo queryField;
private static FieldInfo pathField;
private static FieldInfo cachedToStringField;
private static FieldInfo cachedAbsoluteUriField;

static UriHelper ()
{
sourceField = uriType.GetField ("source", BindingFlags.NonPublic | BindingFlags.Instance);
queryField = uriType.GetField ("query", BindingFlags.NonPublic | BindingFlags.Instance);
pathField = uriType.GetField ("path", BindingFlags.NonPublic | BindingFlags.Instance);
cachedToStringField = uriType.GetField ("cachedToString", BindingFlags.NonPublic | BindingFlags.Instance);
cachedAbsoluteUriField = uriType.GetField ("cachedAbsoluteUri", BindingFlags.NonPublic | BindingFlags.Instance);
}

public static void ForceCanonicalPathAndQuery(Uri uri)
{
var source = (string) sourceField.GetValue (uri);
cachedToStringField.SetValue (uri, source);
cachedAbsoluteUriField.SetValue (uri, source);
var fragPos = source.IndexOf ("#");
var queryPos = source.IndexOf ("?");
var start = source.IndexOf (uri.Host) + uri.Host.Length;
var pathEnd = queryPos == -1 ? fragPos : queryPos;
if (pathEnd == -1)
pathEnd = source.Length+1;
var path = queryPos > -1 ? source.Substring (start, pathEnd - start) : source.Substring (start);
pathField.SetValue (uri, path);
queryField.SetValue(uri, fragPos > -1 ? source.Substring(queryPos, fragPos - queryPos) : source.Substring(queryPos));
}
}

当您运行它时,它会输出以下内容:

uri.ToString() - http://www.yahoo.com/%2F?Foo=Bar%2F#frag
uri.AbsoluteUri - http://www.yahoo.com/%2F?Foo=Bar%2F#frag
uri.Host - www.yahoo.com
uri.Query - ?Foo=Bar%2F
uri.PathAndQuery - /%2F?Foo=Bar%2F
uri.AbsolutePath - /%2F
uri.Fragment - #frag

我对此一点都不满意,但它确实有效,至少对于获取 Uri 和发出查询的基本场景而言是这样。

我可能在 Uri 类中遗漏了一些东西,所以如果您有更好/更简单的方法来完成我在这里所做的事情,我将非常感激。

最佳答案

从最初的问题来看,似乎 MS.NET 的行为在 .NET 4.5 中发生了变化以修复错误。

事实上,这是单声道中的一个错误,因为它没有遵循 .NET 4.5 配置文件中的行为更改。似乎有人已经修复了这个错误并提出了拉取请求,问题是 Mono 团队中似乎没有人有时间审查它:https://github.com/mono/mono/pull/619

关于c# - 在单声道上获取带有转义斜杠的 Uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20769150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com