U4-2807 - better exception message on bogus domain

This commit is contained in:
Stephan
2013-09-07 10:24:52 +02:00
parent 39316a345f
commit c5197dbe17
2 changed files with 63 additions and 1 deletions

View File

@@ -28,6 +28,17 @@ namespace Umbraco.Core
[UmbracoWillObsolete("Do not use this constants. See IShortStringHelper.CleanStringForSafeAliasJavaScriptCode.")]
public const string UmbracoInvalidFirstCharacters = "01234567890";
private static readonly char[] ToCSharpHexDigitLower = "0123456789abcdef".ToCharArray();
private static readonly char[] ToCSharpEscapeChars;
static StringExtensions()
{
var escapes = new[] { "\aa", "\bb", "\ff", "\nn", "\rr", "\tt", "\vv", "\"\"", "\\\\", "??", "\00" };
ToCSharpEscapeChars = new char[escapes.Max(e => e[0]) + 1];
foreach (var escape in escapes)
ToCSharpEscapeChars[escape[0]] = escape[1];
}
internal static string ReplaceNonAlphanumericChars(this string input, char replacement)
{
//any character that is not alphanumeric, convert to a hyphen
@@ -1085,5 +1096,46 @@ namespace Umbraco.Core
return source;
}
/// <summary>
/// Converts a literal string into a C# expression.
/// </summary>
/// <param name="s">Current instance of the string.</param>
/// <returns>The string in a C# format.</returns>
public static string ToCSharpString(this string s)
{
if (s == null) return "<null>";
// http://stackoverflow.com/questions/323640/can-i-convert-a-c-sharp-string-value-to-an-escaped-string-literal
var sb = new StringBuilder(s.Length + 2);
for (var rp = 0; rp < s.Length; rp++)
{
var c = s[rp];
if (c < ToCSharpEscapeChars.Length && '\0' != ToCSharpEscapeChars[c])
sb.Append('\\').Append(ToCSharpEscapeChars[c]);
else if ('~' >= c && c >= ' ')
sb.Append(c);
else
sb.Append(@"\x")
.Append(ToCSharpHexDigitLower[c >> 12 & 0x0F])
.Append(ToCSharpHexDigitLower[c >> 8 & 0x0F])
.Append(ToCSharpHexDigitLower[c >> 4 & 0x0F])
.Append(ToCSharpHexDigitLower[c & 0x0F]);
}
return sb.ToString();
// requires full trust
/*
using (var writer = new StringWriter())
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
{
provider.GenerateCodeFromExpression(new CodePrimitiveExpression(s), writer, null);
return writer.ToString().Replace(string.Format("\" +{0}\t\"", Environment.NewLine), "");
}
*/
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using Umbraco.Core;
using umbraco.cms.businesslogic.web;
namespace Umbraco.Web.Routing
@@ -20,7 +21,16 @@ namespace Umbraco.Web.Routing
public DomainAndUri(Domain domain, string scheme)
{
Domain = domain;
Uri = new Uri(UriUtility.TrimPathEndSlash(UriUtility.StartWithScheme(domain.Name, scheme)));
try
{
Uri = new Uri(UriUtility.TrimPathEndSlash(UriUtility.StartWithScheme(domain.Name, scheme)));
}
catch (UriFormatException)
{
var name = domain.Name.ToCSharpString();
throw new ArgumentException(string.Format("Failed to parse invalid domain: node id={0}, hostname=\"{1}\"."
+ " Hostname should be a valid uri.", domain.RootNodeId, name), "domain");
}
}
/// <summary>