WebUtil.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataWeb / Client / System / Data / Services / Client / WebUtil.cs / 1305376 / WebUtil.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
// static utility functions
//  
//--------------------------------------------------------------------- 

namespace System.Data.Services.Client 
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO; 
    using System.Reflection;
    using System.Runtime.CompilerServices; 
#if !ASTORIA_LIGHT // Data.Services http stack 
    using System.Net;
#else 
    using System.Data.Services.Http;
#endif

    /// web utility functions 
    internal static partial class WebUtil
    { 
        ///  
        /// Whether DataServiceCollection<> type is available.
        ///  
        private static bool? dataServiceCollectionAvailable = null;

        /// 
        /// Returns true if DataServiceCollection<> type is available or false otherwise. 
        /// 
        private static bool DataServiceCollectionAvailable 
        { 
            get
            { 
                if (dataServiceCollectionAvailable == null)
                {
                    try
                    { 
                        dataServiceCollectionAvailable = GetDataServiceCollectionOfTType() != null;
                    } 
                    catch (FileNotFoundException) 
                    {
                        // the assembly or one of its dependencies (read: WindowsBase.dll) was not found. DataServiceCollection is not available. 
                        dataServiceCollectionAvailable = false;
                    }
                }
 
                Debug.Assert(dataServiceCollectionAvailable != null, "observableCollectionOfTAvailable must not be null here.");
 
                return (bool)dataServiceCollectionAvailable; 
            }
        } 

        /// copy from one stream to another
        /// input stream
        /// output stream 
        /// reusable buffer
        /// count of copied bytes 
        internal static long CopyStream(Stream input, Stream output, ref byte[] refBuffer) 
        {
            Debug.Assert(null != input, "null input stream"); 
            Debug.Assert(null != output, "null output stream");

            long total = 0;
            byte[] buffer = refBuffer; 
            if (null == buffer)
            { 
                refBuffer = buffer = new byte[1000]; 
            }
 
            int count = 0;
            while (input.CanRead && (0 < (count = input.Read(buffer, 0, buffer.Length))))
            {
                output.Write(buffer, 0, count); 
                total += count;
            } 
 
            return total;
        } 

        /// get response object from possible WebException
        /// exception to probe
        /// http web respose object from exception 
        internal static void GetHttpWebResponse(InvalidOperationException exception, ref HttpWebResponse response)
        { 
            if (null == response) 
            {
                WebException webexception = (exception as WebException); 
                if (null != webexception)
                {
                    response = (HttpWebResponse)webexception.Response;
                } 
            }
        } 
 
        /// is this a success status code
        /// status code 
        /// true if status is between 200-299
        internal static bool SuccessStatusCode(HttpStatusCode status)
        {
            return (200 <= (int)status && (int)status < 300); 
        }
 
        ///  
        /// turn the response object headers into a dictionary
        ///  
        /// response
        /// dictionary
        internal static Dictionary WrapResponseHeaders(HttpWebResponse response)
        { 
            Dictionary headers = new Dictionary(EqualityComparer.Default);
            if (null != response) 
            { 
                foreach (string name in response.Headers.AllKeys)
                { 
                    headers.Add(name, response.Headers[name]);
                }
            }
 
            return headers;
        } 
 
        /// 
        /// Applies headers in the dictionary to a web request. 
        /// This has a special case for some of the headers to set the properties on the request instead of using the Headers collection.
        /// 
        /// The dictionary with the headers to apply.
        /// The request to apply the headers to. 
        /// If set to true the Accept header will be ignored
        /// and the request.Accept property will not be touched. 
        internal static void ApplyHeadersToRequest(Dictionary headers, HttpWebRequest request, bool ignoreAcceptHeader) 
        {
            foreach (KeyValuePair header in headers) 
            {
                if (string.Equals(header.Key, XmlConstants.HttpRequestAccept, StringComparison.Ordinal))
                {
                    if (!ignoreAcceptHeader) 
                    {
                        request.Accept = header.Value; 
                    } 
                }
                else if (string.Equals(header.Key, XmlConstants.HttpContentType, StringComparison.Ordinal)) 
                {
                    request.ContentType = header.Value;
                }
                else 
                {
                    request.Headers[header.Key] = header.Value; 
                } 
            }
        } 

        /// 
        /// Checks if the given type is DataServiceCollection<> type.
        ///  
        /// Type to be checked.
        /// true if the provided type is DataServiceCollection<> or false otherwise. 
        internal static bool IsDataServiceCollectionType(Type t) 
        {
            if (DataServiceCollectionAvailable) 
            {
                return t == GetDataServiceCollectionOfTType();
            }
 
            return false;
        } 
 
        /// 
        /// Creates an instance of DataServiceCollection<> class using provided types. 
        /// 
        /// Types to be used for creating DataServiceCollection<> object.
        /// 
        /// Instance of DataServiceCollection<> class created using provided types or null if DataServiceCollection<> 
        /// type is not avaiable.
        ///  
        internal static Type GetDataServiceCollectionOfT(params Type[] typeArguments) 
        {
            if (DataServiceCollectionAvailable) 
            {
                Debug.Assert(
                    GetDataServiceCollectionOfTType() != null,
                    "DataServiceCollection is available so GetDataServiceCollectionOfTType() must not return null."); 

                return GetDataServiceCollectionOfTType().MakeGenericType(typeArguments); 
            } 

            return null; 
        }

        /// 
        /// Forces loading WindowsBase assembly. If WindowsBase assembly is not present JITter will throw an exception. 
        /// This method MUST NOT be inlined otherwise we won't be able to catch the exception by JITter in the caller.
        ///  
        /// typeof(DataServiceCollection<>) 
        [MethodImpl(MethodImplOptions.NoInlining)]
        private static Type GetDataServiceCollectionOfTType() 
        {
            return typeof(DataServiceCollection<>);
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
// static utility functions
//  
//--------------------------------------------------------------------- 

namespace System.Data.Services.Client 
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO; 
    using System.Reflection;
    using System.Runtime.CompilerServices; 
#if !ASTORIA_LIGHT // Data.Services http stack 
    using System.Net;
#else 
    using System.Data.Services.Http;
#endif

    /// web utility functions 
    internal static partial class WebUtil
    { 
        ///  
        /// Whether DataServiceCollection<> type is available.
        ///  
        private static bool? dataServiceCollectionAvailable = null;

        /// 
        /// Returns true if DataServiceCollection<> type is available or false otherwise. 
        /// 
        private static bool DataServiceCollectionAvailable 
        { 
            get
            { 
                if (dataServiceCollectionAvailable == null)
                {
                    try
                    { 
                        dataServiceCollectionAvailable = GetDataServiceCollectionOfTType() != null;
                    } 
                    catch (FileNotFoundException) 
                    {
                        // the assembly or one of its dependencies (read: WindowsBase.dll) was not found. DataServiceCollection is not available. 
                        dataServiceCollectionAvailable = false;
                    }
                }
 
                Debug.Assert(dataServiceCollectionAvailable != null, "observableCollectionOfTAvailable must not be null here.");
 
                return (bool)dataServiceCollectionAvailable; 
            }
        } 

        /// copy from one stream to another
        /// input stream
        /// output stream 
        /// reusable buffer
        /// count of copied bytes 
        internal static long CopyStream(Stream input, Stream output, ref byte[] refBuffer) 
        {
            Debug.Assert(null != input, "null input stream"); 
            Debug.Assert(null != output, "null output stream");

            long total = 0;
            byte[] buffer = refBuffer; 
            if (null == buffer)
            { 
                refBuffer = buffer = new byte[1000]; 
            }
 
            int count = 0;
            while (input.CanRead && (0 < (count = input.Read(buffer, 0, buffer.Length))))
            {
                output.Write(buffer, 0, count); 
                total += count;
            } 
 
            return total;
        } 

        /// get response object from possible WebException
        /// exception to probe
        /// http web respose object from exception 
        internal static void GetHttpWebResponse(InvalidOperationException exception, ref HttpWebResponse response)
        { 
            if (null == response) 
            {
                WebException webexception = (exception as WebException); 
                if (null != webexception)
                {
                    response = (HttpWebResponse)webexception.Response;
                } 
            }
        } 
 
        /// is this a success status code
        /// status code 
        /// true if status is between 200-299
        internal static bool SuccessStatusCode(HttpStatusCode status)
        {
            return (200 <= (int)status && (int)status < 300); 
        }
 
        ///  
        /// turn the response object headers into a dictionary
        ///  
        /// response
        /// dictionary
        internal static Dictionary WrapResponseHeaders(HttpWebResponse response)
        { 
            Dictionary headers = new Dictionary(EqualityComparer.Default);
            if (null != response) 
            { 
                foreach (string name in response.Headers.AllKeys)
                { 
                    headers.Add(name, response.Headers[name]);
                }
            }
 
            return headers;
        } 
 
        /// 
        /// Applies headers in the dictionary to a web request. 
        /// This has a special case for some of the headers to set the properties on the request instead of using the Headers collection.
        /// 
        /// The dictionary with the headers to apply.
        /// The request to apply the headers to. 
        /// If set to true the Accept header will be ignored
        /// and the request.Accept property will not be touched. 
        internal static void ApplyHeadersToRequest(Dictionary headers, HttpWebRequest request, bool ignoreAcceptHeader) 
        {
            foreach (KeyValuePair header in headers) 
            {
                if (string.Equals(header.Key, XmlConstants.HttpRequestAccept, StringComparison.Ordinal))
                {
                    if (!ignoreAcceptHeader) 
                    {
                        request.Accept = header.Value; 
                    } 
                }
                else if (string.Equals(header.Key, XmlConstants.HttpContentType, StringComparison.Ordinal)) 
                {
                    request.ContentType = header.Value;
                }
                else 
                {
                    request.Headers[header.Key] = header.Value; 
                } 
            }
        } 

        /// 
        /// Checks if the given type is DataServiceCollection<> type.
        ///  
        /// Type to be checked.
        /// true if the provided type is DataServiceCollection<> or false otherwise. 
        internal static bool IsDataServiceCollectionType(Type t) 
        {
            if (DataServiceCollectionAvailable) 
            {
                return t == GetDataServiceCollectionOfTType();
            }
 
            return false;
        } 
 
        /// 
        /// Creates an instance of DataServiceCollection<> class using provided types. 
        /// 
        /// Types to be used for creating DataServiceCollection<> object.
        /// 
        /// Instance of DataServiceCollection<> class created using provided types or null if DataServiceCollection<> 
        /// type is not avaiable.
        ///  
        internal static Type GetDataServiceCollectionOfT(params Type[] typeArguments) 
        {
            if (DataServiceCollectionAvailable) 
            {
                Debug.Assert(
                    GetDataServiceCollectionOfTType() != null,
                    "DataServiceCollection is available so GetDataServiceCollectionOfTType() must not return null."); 

                return GetDataServiceCollectionOfTType().MakeGenericType(typeArguments); 
            } 

            return null; 
        }

        /// 
        /// Forces loading WindowsBase assembly. If WindowsBase assembly is not present JITter will throw an exception. 
        /// This method MUST NOT be inlined otherwise we won't be able to catch the exception by JITter in the caller.
        ///  
        /// typeof(DataServiceCollection<>) 
        [MethodImpl(MethodImplOptions.NoInlining)]
        private static Type GetDataServiceCollectionOfTType() 
        {
            return typeof(DataServiceCollection<>);
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK