2008-03-05 09:52:00 -05:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Microsoft Research Singularity
|
|
|
|
//
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
//
|
|
|
|
// Note:
|
2008-11-17 18:29:00 -05:00
|
|
|
// This file was ported from the Coriolis codebase to Singularity.
|
2008-03-05 09:52:00 -05:00
|
|
|
//
|
|
|
|
|
|
|
|
namespace System.Net
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// support class for Validation related stuff.
|
|
|
|
//
|
|
|
|
internal class ValidationHelper {
|
|
|
|
|
|
|
|
public static string [] EmptyArray = new string[0];
|
|
|
|
|
|
|
|
internal static readonly char[] InvalidMethodChars =
|
|
|
|
new char[]{
|
|
|
|
' ',
|
|
|
|
'\r',
|
|
|
|
'\n',
|
|
|
|
'\t'
|
|
|
|
};
|
|
|
|
|
|
|
|
// invalid characters that cannot be found in a valid method-verb or http header
|
|
|
|
internal static readonly char[] InvalidParamChars =
|
|
|
|
new char[]{
|
|
|
|
'(',
|
|
|
|
')',
|
|
|
|
'<',
|
|
|
|
'>',
|
|
|
|
'@',
|
|
|
|
',',
|
|
|
|
';',
|
|
|
|
':',
|
|
|
|
'\\',
|
|
|
|
'"',
|
|
|
|
'\'',
|
|
|
|
'/',
|
|
|
|
'[',
|
|
|
|
']',
|
|
|
|
'?',
|
|
|
|
'=',
|
|
|
|
'{',
|
|
|
|
'}',
|
|
|
|
' ',
|
|
|
|
'\t',
|
|
|
|
'\r',
|
|
|
|
'\n'};
|
|
|
|
|
|
|
|
public static string [] MakeEmptyArrayNull(string [] stringArray) {
|
2008-11-17 18:29:00 -05:00
|
|
|
if (stringArray == null || stringArray.Length == 0) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return null;
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
return stringArray;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string MakeStringNull(string stringValue) {
|
2008-11-17 18:29:00 -05:00
|
|
|
if (stringValue == null || stringValue.Length == 0) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return null;
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
return stringValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string MakeStringEmpty(string stringValue) {
|
2008-11-17 18:29:00 -05:00
|
|
|
if (stringValue == null || stringValue.Length == 0) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return String.Empty;
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
return stringValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int HashCode(object objectValue) {
|
|
|
|
if (objectValue == null) {
|
|
|
|
return -1;
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
return objectValue.GetHashCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static string ExceptionMessage(Exception exception) {
|
2008-11-17 18:29:00 -05:00
|
|
|
if (exception == null) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return string.Empty;
|
|
|
|
}
|
2008-11-17 18:29:00 -05:00
|
|
|
if (exception.InnerException == null) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return exception.Message;
|
|
|
|
}
|
|
|
|
return exception.Message + " (" + ExceptionMessage(exception.InnerException) + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string ToString(object objectValue) {
|
|
|
|
if (objectValue == null) {
|
|
|
|
return "(null)";
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else if (objectValue is string && ((string)objectValue).Length == 0) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return "(string.empty)";
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else if (objectValue is Exception) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return ExceptionMessage(objectValue as Exception);
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else if (objectValue is IntPtr) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return "0x" + String.Format("{0x}", ((IntPtr)objectValue));
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
return objectValue.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static string HashString(object objectValue) {
|
|
|
|
if (objectValue == null) {
|
|
|
|
return "(null)";
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else if (objectValue is string && ((string)objectValue).Length == 0) {
|
2008-03-05 09:52:00 -05:00
|
|
|
return "(string.empty)";
|
2008-11-17 18:29:00 -05:00
|
|
|
}
|
|
|
|
else {
|
2008-03-05 09:52:00 -05:00
|
|
|
return objectValue.GetHashCode().ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool IsInvalidHttpString(string! stringValue) {
|
|
|
|
return stringValue.IndexOfAny(InvalidParamChars)!=-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool IsBlankString(string stringValue) {
|
|
|
|
return stringValue==null || stringValue.Length==0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool ValidateUInt32(long address) {
|
|
|
|
// on false, API should throw new ArgumentOutOfRangeException("address");
|
|
|
|
return address>=0x00000000 && address<=0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool ValidateTcpPort(int port) {
|
|
|
|
// on false, API should throw new ArgumentOutOfRangeException("port");
|
|
|
|
return port>=IPEndPoint.MinPort && port<=IPEndPoint.MaxPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool ValidateRange(int actual, int fromAllowed, int toAllowed) {
|
|
|
|
// on false, API should throw new ArgumentOutOfRangeException("argument");
|
|
|
|
return actual>=fromAllowed && actual<=toAllowed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool ValidateRange(long actual, long fromAllowed, long toAllowed) {
|
|
|
|
// on false, API should throw new ArgumentOutOfRangeException("argument");
|
|
|
|
return actual>=fromAllowed && actual<=toAllowed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|