Tuesday, August 25, 2009

Converting string for Safe Rewrite URL

public class SafeURL
{
///
/// A regex that matches all characters that would return false when passed to
///

private static readonly Regex SpecialChars = new Regex(@"[^\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nd}]");

private static readonly Regex EmbeddedEncodedValue = new Regex(@"-[0-9A-F]{1,8}-");

///
/// URL Encode and Replace URL characters
///

/// Original URL
/// URL
public static string UrlEncode(string url)
{
if(url == null) return null;

// replace special chars.
string result = SpecialChars.Replace
(url, match =>
{
StringBuilder buf = new StringBuilder();
buf.Append("-");
// we must convert to UTF8 first to make sure we're representing the bytes
// appropriately.
foreach (byte c in Encoding.UTF8.GetBytes(match.Value))
{
buf.Append(c.ToString("X2"));
}
buf.Append("-");
return buf.ToString();
});
// and now we're ok to use proper URL encoding
result = HttpUtility.UrlEncode(result);

return result;
}

///
/// Replace and Decode URL charaters
///

/// URL
/// Original URL
public static string UrlDecode(string url)
{
if (url == null) return null;

// decode url encoding
string result = HttpUtility.UrlDecode(url);

// replace special chars.
result = EmbeddedEncodedValue.Replace
(result, match =>
{
string utf8 = match.Value.Trim('-');
int count = utf8.Length / 2;
byte[] bytes = new byte[count];
for(int i = 0; i < count; i++)
{
bytes[i] = Byte.Parse(utf8.Substring(i * 2, 2), NumberStyles.HexNumber);
}
return Encoding.UTF8.GetString(bytes);
});

return result;
}
}

No comments: