[Fixes #2929] Moving IsLocalUrl implementation to UrlHelper

This commit is contained in:
Sebastien Ros 2015-09-01 18:30:04 -07:00
parent a0e0df87de
commit dd94d54e1d
3 changed files with 9 additions and 118 deletions

View File

@ -1,39 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.AspNet.Mvc.Internal
{
public static class UrlUtility
{
/// <summary>
/// Returns a value that indicates whether the URL is local. An URL with an absolute path is considered local
/// if it does not have a host/authority part. URLs using the virtual paths ('~/') are also local.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns><c>true</c> if the URL is local; otherwise, <c>false</c>.</returns>
/// <example>
/// <para>
/// For example, the following URLs are considered local:
/// /Views/Default/Index.html
/// ~/Index.html
/// </para>
/// <para>
/// The following URLs are non-local:
/// ../Index.html
/// http://www.contoso.com/
/// http://localhost/Index.html
/// </para>
/// </example>
public static bool IsLocalUrl(string url)
{
return
!string.IsNullOrEmpty(url) &&
// Allows "/" or "/foo" but not "//" or "/\".
((url[0] == '/' && (url.Length == 1 || (url[1] != '/' && url[1] != '\\'))) ||
// Allows "~/" or "~/foo".
(url.Length > 1 && url[0] == '~' && url[1] == '/'));
}
}
}

View File

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Actions;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.Internal;
@ -87,9 +86,16 @@ namespace Microsoft.AspNet.Mvc
}
/// <inheritdoc />
public bool IsLocalUrl(string url)
public virtual bool IsLocalUrl(string url)
{
return UrlUtility.IsLocalUrl(url);
return
!string.IsNullOrEmpty(url) &&
// Allows "/" or "/foo" but not "//" or "/\".
((url[0] == '/' && (url.Length == 1 || (url[1] != '/' && url[1] != '\\'))) ||
// Allows "~/" or "~/foo".
(url.Length > 1 && url[0] == '~' && url[1] == '/'));
}
/// <inheritdoc />

View File

@ -8,7 +8,6 @@ using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.Mvc.Actions;
using Microsoft.AspNet.Mvc.Internal;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;
@ -66,9 +65,6 @@ namespace Microsoft.AspNet.Mvc
Assert.Equal(expectedPath, path);
}
// UrlHelper.IsLocalUrl depends on the UrlUtility.IsLocalUrl method.
// To avoid duplicate tests, all the tests exercising IsLocalUrl verify
// both of UrlHelper.IsLocalUrl and UrlUtility.IsLocalUrl
[Theory]
[InlineData(null)]
[InlineData("")]
@ -83,12 +79,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -105,12 +95,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.True(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.True(result);
}
[Theory]
@ -126,12 +110,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.True(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.True(result);
}
[Theory]
@ -148,12 +126,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -171,12 +143,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -192,12 +158,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -213,12 +173,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -233,12 +187,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -256,12 +204,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -280,12 +222,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -305,12 +241,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Theory]
@ -328,12 +258,6 @@ namespace Microsoft.AspNet.Mvc
// Assert
Assert.False(result);
// Arrange & Act
result = UrlUtility.IsLocalUrl(url);
// Assert
Assert.False(result);
}
[Fact]