We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Net
- {
- using System.IO;
- using System.Reflection;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Globalization;
- using System.Net.Sockets;
- using System.Runtime.InteropServices;
- using System.Runtime.Versioning;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Security.Permissions;
- using System.Diagnostics;
- using System.Threading;
- using System.Security.Principal;
- using System.Security;
- using System.Net.Security;
- using System.Net.NetworkInformation;
- using System.Runtime.Serialization;
-
- static internal class IntPtrHelper
- {
-
- static internal IntPtr Add(IntPtr a, int b)
- {
- return (IntPtr)((long)a + (long)b);
- }
- }
-
- internal class InternalException : SystemException
- {
- internal InternalException()
- {
- GlobalLog.Assert("InternalException thrown.");
- }
-
- internal InternalException(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext)
- {
- }
- }
-
- static internal class NclUtilities
- {
- /// <devdoc>
- /// <para>
- /// Indicates true if the threadpool is low on threads,
- /// in this case we need to refuse to start new requests,
- /// and avoid blocking.
- /// </para>
- /// </devdoc>
- static internal bool IsThreadPoolLow()
- {
-
- int workerThreads;
- int completionPortThreads;
- ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
-
- return workerThreads < 2 || completionPortThreads < 2;
- }
-
- static internal bool HasShutdownStarted {
- get { return Environment.HasShutdownStarted || AppDomain.CurrentDomain.IsFinalizingForUnload(); }
- }
-
-
-
-
-
- private static ContextCallback s_ContextRelativeDemandCallback;
-
- static internal ContextCallback ContextRelativeDemandCallback {
- get {
- if (s_ContextRelativeDemandCallback == null)
- s_ContextRelativeDemandCallback = new ContextCallback(DemandCallback);
- return s_ContextRelativeDemandCallback;
- }
- }
-
- private static void DemandCallback(object state)
- {
- ((CodeAccessPermission)state).Demand();
- }
-
-
- static internal bool GuessWhetherHostIsLoopback(string host)
- {
- string hostLower = host.ToLowerInvariant();
- if (hostLower == "localhost" || hostLower == "loopback") {
- return true;
- }
-
- return false;
- }
-
- static internal bool IsFatal(Exception exception)
- {
- return exception != null && (exception is OutOfMemoryException || exception is StackOverflowException || exception is ThreadAbortException);
- }
-
-
- private static IPAddress[] _LocalAddresses;
- private static object _LocalAddressesLock;
-
- private const int HostNameBufferLength = 256;
- static internal string _LocalDomainName;
-
-
-
-
- private static IPHostEntry GetLocalHost()
- {
-
-
-
-
-
- if (Socket.SupportsIPv6) {
-
-
-
-
-
-
- StringBuilder hostname = new StringBuilder(HostNameBufferLength);
- SocketError errorCode = UnsafeNclNativeMethods.OSSOCK.gethostname(hostname, HostNameBufferLength);
-
- if (errorCode != SocketError.Success) {
- throw new SocketException();
- }
-
- return Dns.GetHostByName(hostname.ToString());
- }
- else {
-
-
-
- IntPtr nativePointer = UnsafeNclNativeMethods.OSSOCK.gethostbyname(null);
-
- if (nativePointer == IntPtr.Zero) {
- throw new SocketException();
- }
-
- return Dns.NativeToHostEntry(nativePointer);
- }
-
- }
-
- static internal IPAddress[] LocalAddresses {
- get {
- IPAddress[] local = _LocalAddresses;
- if (local != null) {
- return local;
- }
-
- lock (LocalAddressesLock) {
- local = _LocalAddresses;
- if (local != null) {
- return local;
- }
-
- List<IPAddress> localList = new List<IPAddress>();
-
- try {
- IPHostEntry hostEntry = GetLocalHost();
- if (hostEntry != null) {
- if (hostEntry.HostName != null) {
- int dot = hostEntry.HostName.IndexOf('.');
- if (dot != -1) {
- _LocalDomainName = hostEntry.HostName.Substring(dot);
- }
- }
-
- IPAddress[] ipAddresses = hostEntry.AddressList;
- if (ipAddresses != null) {
- foreach (IPAddress ipAddress in ipAddresses) {
- localList.Add(ipAddress);
- }
- }
- }
- }
- catch {
- }
-
- local = new IPAddress[localList.Count];
- int index = 0;
- foreach (IPAddress ipAddress in localList) {
- local[index] = ipAddress;
- index++;
- }
- _LocalAddresses = local;
-
- return local;
- }
- }
- }
-
- static internal bool IsAddressLocal(IPAddress ipAddress)
- {
- IPAddress[] localAddresses = NclUtilities.LocalAddresses;
- for (int i = 0; i < localAddresses.Length; i++) {
- if (ipAddress.Equals(localAddresses[i], false)) {
- return true;
- }
- }
- return false;
- }
-
- private static object LocalAddressesLock {
- get {
- if (_LocalAddressesLock == null) {
- Interlocked.CompareExchange(ref _LocalAddressesLock, new object(), null);
- }
- return _LocalAddressesLock;
- }
- }
- }
-
- static internal class NclConstants
- {
- static internal readonly object Sentinel = new object();
- static internal readonly object[] EmptyObjectArray = new object[0];
- static internal readonly Uri[] EmptyUriArray = new Uri[0];
-
- static internal readonly byte[] CRLF = new byte[] {(byte)'\r', (byte)'\n'};
- static internal readonly byte[] ChunkTerminator = new byte[] {(byte)'0', (byte)'\r', (byte)'\n', (byte)'\r', (byte)'\n'};
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- internal struct InterlockedGate
- {
- private int m_State;
-
- internal const int Open = 0;
-
- internal const int Held = 1;
-
- internal const int Triggered = 2;
-
- internal const int Closed = 3;
-
- #if DEBUG
-
- #endif
-
-
- internal void Reset()
- {
- m_State = Open;
- }
-
-
-
- internal bool Trigger(bool exclusive)
- {
- int gate = Interlocked.CompareExchange(ref m_State, Triggered, Open);
- if (exclusive && (gate == Held || gate == Triggered)) {
- GlobalLog.Assert("InterlockedGate::Trigger", "Gate already triggered.");
- throw new InternalException();
- }
- return gate == Open;
- }
-
-
-
-
-
-
-
-
- internal bool StartTrigger(bool exclusive)
- {
- int gate = Interlocked.CompareExchange(ref m_State, Held, Open);
- if (exclusive && (gate == Held || gate == Triggered)) {
- GlobalLog.Assert("InterlockedGate::StartTrigger", "Gate already triggered.");
- throw new InternalException();
- }
- return gate == Open;
- }
-
-
- internal void FinishTrigger()
- {
- int gate = Interlocked.CompareExchange(ref m_State, Triggered, Held);
- if (gate != Held) {
- GlobalLog.Assert("InterlockedGate::FinishTrigger", "Gate not held.");
- throw new InternalException();
- }
- }
-
-
- internal bool Complete()
- {
- int gate;
-
-
- while ((gate = Interlocked.CompareExchange(ref m_State, Closed, Triggered)) != Triggered) {
- if (gate == Closed) {
- return false;
- }
-
- if (gate == Open) {
- if (Interlocked.CompareExchange(ref m_State, Closed, Open) == Open) {
- return false;
- }
-
- continue;
- }
-
-
- Thread.SpinWait(1);
- }
-
- return true;
- }
- }
-
-
-
-
-
-
-
-
-
- static internal class ValidationHelper
- {
-
- public static string[] EmptyArray = new string[0];
-
- static internal readonly char[] InvalidMethodChars = new char[] {' ', '\r', '\n', '\t'};
-
-
- static internal readonly char[] InvalidParamChars = new char[] {'(', ')', '<', '>', '@', ',', ';', ':', '\\', '"',
- '\'', '/', '[', ']', '?', '=', '{', '}', ' ', '\t',
- '\r', '\n'};
-
- public static string[] MakeEmptyArrayNull(string[] stringArray)
- {
- if (stringArray == null || stringArray.Length == 0) {
- return null;
- }
- else {
- return stringArray;
- }
- }
-
- public static string MakeStringNull(string stringValue)
- {
- if (stringValue == null || stringValue.Length == 0) {
- return null;
- }
- else {
- return stringValue;
- }
- }
-
-
-
-
- public static string ExceptionMessage(Exception exception)
- {
- if (exception == null) {
- return string.Empty;
- }
- if (exception.InnerException == null) {
- return exception.Message;
- }
- return exception.Message + " (" + ExceptionMessage(exception.InnerException) + ")";
- }
-
- public static string ToString(object objectValue)
- {
- if (objectValue == null) {
- return "(null)";
- }
- else if (objectValue is string && ((string)objectValue).Length == 0) {
- return "(string.empty)";
- }
- else if (objectValue is Exception) {
- return ExceptionMessage(objectValue as Exception);
- }
- else if (objectValue is IntPtr) {
- return "0x" + ((IntPtr)objectValue).ToString("x");
- }
- else {
- return objectValue.ToString();
- }
- }
- public static string HashString(object objectValue)
- {
- if (objectValue == null) {
- return "(null)";
- }
- else if (objectValue is string && ((string)objectValue).Length == 0) {
- return "(string.empty)";
- }
- else {
- return objectValue.GetHashCode().ToString(NumberFormatInfo.InvariantInfo);
- }
- }
-
- 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 ValidateTcpPort(int port)
- {
-
- return port >= IPEndPoint.MinPort && port <= IPEndPoint.MaxPort;
- }
-
- public static bool ValidateRange(int actual, int fromAllowed, int toAllowed)
- {
-
- return actual >= fromAllowed && actual <= toAllowed;
- }
-
-
-
- }
-
- static internal class ExceptionHelper
- {
- static internal readonly WebPermission WebPermissionUnrestricted = new WebPermission(NetworkAccess.Connect);
- static internal readonly SecurityPermission UnmanagedPermission = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
- static internal readonly SocketPermission UnrestrictedSocketPermission = new SocketPermission(PermissionState.Unrestricted);
- static internal readonly SecurityPermission InfrastructurePermission = new SecurityPermission(SecurityPermissionFlag.Infrastructure);
- static internal readonly SecurityPermission ControlPolicyPermission = new SecurityPermission(SecurityPermissionFlag.ControlPolicy);
- static internal readonly SecurityPermission ControlPrincipalPermission = new SecurityPermission(SecurityPermissionFlag.ControlPrincipal);
-
- static internal NotImplementedException MethodNotImplementedException {
- get { return new NotImplementedException(SR.GetString(SR.net_MethodNotImplementedException)); }
- }
-
- static internal NotImplementedException PropertyNotImplementedException {
- get { return new NotImplementedException(SR.GetString(SR.net_PropertyNotImplementedException)); }
- }
-
- static internal NotSupportedException MethodNotSupportedException {
- get { return new NotSup