Uri规范化压缩FTP方案

本文关键字:方案 FTP 压缩 规范化 Uri | 更新日期: 2023-09-27 18:24:02

https://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx

根据上面的参考,当指定ftp url时,uri类不应该压缩url。例如,以下ftp url:

Uri uri = new Uri("ftp://myUrl/%2E%2E/%2E%2E");
Console.WriteLine(uri.AbsoluteUri);
Console.WriteLine(uri.PathAndQuery);

应导致:

绝对Uri:"ftp://myUrl/%2E%2E/%2E%2E"

路径和查询:"/%2E%2E/%2E%2E"

但是,这不是我所看到的。当我使用.NET框架4.5.1执行上述代码时,我看到:

绝对Uri:"ftp://myUrl/"

路径和查询:"/"

此外,添加到我的app.config似乎没有任何效果:

<uri> 
    <schemeSettings>
        <add name="ftp" genericUriParserOptions="DontUnescapePathDotsAndSlashes" />
    </schemeSettings>
</uri>

由于有几个人能够重新创建这个问题,我已经用微软创建了一个错误报告。欢迎投票:

https://connect.microsoft.com/VisualStudio/feedback/details/2046491/uri-canonicalization-compacting-ftp-scheme

Uri规范化压缩FTP方案

使用Microsoft创建错误报告:https://connect.microsoft.com/VisualStudio/Feedback/Details/2046491

目前,我正在通过(通过反射进行黑客攻击)从Uri类中的UriParser对象中删除两个标志来解决这个问题:

当我的应用程序正在实例化时,我会调用下面的方法一次。实例化后,每个FTP Uri对象在解析时都将使用新的标志组合。

// CompressPath = 0x800000, // For an authority based Uri remove/compress /./ /../ in the path
// UnEscapeDotsAndSlashes = 0x2000000, // additionally unescape dots and slashes before doing path compression
/// <summary>
/// http://referencesource.microsoft.com/#System/net/System/_UriSyntax.cs
/// </summary>
public static void LeaveDotsAndSlashesEscaped() {
    Uri uri = new Uri("ftp://myUrl/%2E%2E/%2E%2E/");
    if (uri == null) {
        throw new ArgumentNullException("uri");
    }
    FieldInfo fieldInfo = uri.GetType().GetField("m_Syntax", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fieldInfo == null) {
        throw new MissingFieldException("'m_Syntax' field not found");
    }
    object uriParser = fieldInfo.GetValue(uri);
    fieldInfo = typeof(UriParser).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fieldInfo == null) {
        throw new MissingFieldException("'m_Flags' field not found");
    }
    object uriSyntaxFlags = fieldInfo.GetValue(uriParser);
    // Clear the flags that we don't want
    uriSyntaxFlags = (int)uriSyntaxFlags & ~0x2000000 & ~0x800000;
    fieldInfo.SetValue(uriParser, uriSyntaxFlags);
}