HttpContextServiceHost.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataWeb / Server / System / Data / Services / HttpContextServiceHost.cs / 1 / HttpContextServiceHost.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
//      Provides an HttpContext-based implementation.
//  
// 
// @owner  [....]
//--------------------------------------------------------------------- 

namespace System.Data.Services
{
    #region Namespaces. 

    using System; 
    using System.Collections.Generic; 
    using System.Diagnostics;
    using System.IO; 
    using System.Net;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Web;
    using System.Text; 
    using System.Xml;
 
    #endregion Namespaces. 

    ///  
    /// Provides access to the environment for a DataService, including information about the current request, based
    /// on the current WebOperationContext.
    /// 
    internal class HttpContextServiceHost : IDataServiceHost 
    {
        #region Private fields. 
 
        /// Message sent to server.
        private readonly Stream incomingMessageBody; 

        /// The WCF-based operation context.
        private readonly WebOperationContext operationContext;
 
        /// Whether an error was found when processing this request.
        private bool errorFound; 
 
        #endregion Private fields.
 
        #region Constructors.

        /// 
        /// Initializes a new System.Data.Services.HttpContextServiceHost instance. 
        /// 
        /// Incoming message body to process. 
        internal HttpContextServiceHost(Stream messageBody) 
        {
            // We capture the current context at initialization time rather 
            // than accessing it repeatedly.
            this.incomingMessageBody = messageBody;
            this.operationContext = WebOperationContext.Current;
            if (this.operationContext == null) 
            {
                throw new InvalidOperationException(Strings.HttpContextServiceHost_WebOperationContextCurrentMissing); 
            } 
        }
 
        #endregion Constructors.

        #region Properties.
 
        /// 
        /// Gets the character set encoding that the client requested, 
        /// possibly null. 
        /// 
        string IDataServiceHost.RequestAcceptCharSet 
        {
            get
            {
                // Returns a string that contains the comma-separated list of values 
                // associated with the specified key, if found; otherwise, null.
                return this.operationContext.IncomingRequest.Headers[HttpRequestHeader.AcceptCharset]; 
            } 
        }
 
        /// Gets or sets the HTTP MIME type of the output stream.
        string IDataServiceHost.ResponseContentType
        {
            get 
            {
                return this.operationContext.OutgoingResponse.ContentType; 
            } 

            set 
            {
                this.operationContext.OutgoingResponse.ContentType = value;
            }
        } 

        /// Gets the HTTP MIME type of the input stream. 
        string IDataServiceHost.RequestContentType 
        {
            get 
            {
                return this.operationContext.IncomingRequest.ContentType;
            }
        } 

        ///  
        /// Gets a comma-separated list of client-supported MIME Accept types. 
        /// 
        string IDataServiceHost.RequestAccept 
        {
            get
            {
                return this.operationContext.IncomingRequest.Accept; 
            }
        } 
 
        /// 
        /// Gets the HTTP data transfer method (such as GET, POST, or HEAD) used by the client. 
        /// 
        string IDataServiceHost.RequestHttpMethod
        {
            get 
            {
                string result; 
                string[] methodValues = this.operationContext.IncomingRequest.Headers.GetValues(XmlConstants.HttpXMethod); 
                if (methodValues == null || methodValues.Length == 0)
                { 
                    result = this.operationContext.IncomingRequest.Method;
                }
                else if (methodValues.Length == 1)
                { 
                    result = methodValues[0];
                    if (this.operationContext.IncomingRequest.Method != XmlConstants.HttpMethodPost) 
                    { 
                        throw DataServiceException.CreateBadRequestError(Strings.HttpContextServiceHost_XMethodNotUsingPost);
                    } 

                    if (result != XmlConstants.HttpMethodDelete && result != XmlConstants.HttpMethodPut && result != XmlConstants.HttpMethodMerge)
                    {
                        throw DataServiceException.CreateBadRequestError(Strings.HttpContextServiceHost_XMethodIncorrectValue(result)); 
                    }
                } 
                else 
                {
                    throw DataServiceException.CreateBadRequestError(Strings.HttpContextServiceHost_XMethodIncorrectCount(methodValues.Length)); 
                }

                return result;
            } 
        }
 
        /// Gets the value of the If-Match header from the request made 
        string IDataServiceHost.RequestIfMatch
        { 
            get
            {
                return this.operationContext.IncomingRequest.Headers[HttpRequestHeader.IfMatch];
            } 
        }
 
        /// Gets the value of the If-None-Match header from the request made 
        string IDataServiceHost.RequestIfNoneMatch
        { 
            get
            {
                return this.operationContext.IncomingRequest.Headers[HttpRequestHeader.IfNoneMatch];
            } 
        }
 
        /// Gets the value for the MaxDataServiceVersion request header. 
        string IDataServiceHost.RequestMaxVersion
        { 
            get
            {
                return this.operationContext.IncomingRequest.Headers[XmlConstants.HttpMaxDataServiceVersion];
            } 
        }
 
        /// Gets the value for the DataServiceVersion request header. 
        string IDataServiceHost.RequestVersion
        { 
            get
            {
                return this.operationContext.IncomingRequest.Headers[XmlConstants.HttpDataServiceVersion];
            } 
        }
 
        /// Gets the absolute URI to the resource upon which to apply the request. 
        Uri IDataServiceHost.AbsoluteRequestUri
        { 
            get
            {
                UriTemplateMatch match = this.operationContext.IncomingRequest.UriTemplateMatch;
                Uri requestUri = WebUtil.ApplyHostHeader(match.RequestUri, this.HostHeader); 
                return requestUri;
            } 
        } 

        /// Gets or sets the Cache-Control header on the response. 
        string IDataServiceHost.ResponseCacheControl
        {
            get { return this.operationContext.OutgoingResponse.Headers[HttpResponseHeader.CacheControl]; }
            set { this.operationContext.OutgoingResponse.Headers[HttpResponseHeader.CacheControl] = value; } 
        }
 
        /// Gets/Sets the value of the ETag header on the outgoing response 
        string IDataServiceHost.ResponseETag
        { 
            get
            {
                return this.operationContext.OutgoingResponse.ETag;
            } 

            set 
            { 
                this.operationContext.OutgoingResponse.ETag = value;
            } 
        }

        /// Gets or sets the Location header on the response.
        string IDataServiceHost.ResponseLocation 
        {
            get { return this.operationContext.OutgoingResponse.Headers[HttpResponseHeader.Location]; } 
            set { this.operationContext.OutgoingResponse.Headers[HttpResponseHeader.Location] = value; } 
        }
 
        /// 
        /// Gets/Sets the status code for the request made.
        /// 
        int IDataServiceHost.ResponseStatusCode 
        {
            get 
            { 
                return (int) this.operationContext.OutgoingResponse.StatusCode;
            } 

            set
            {
                this.operationContext.OutgoingResponse.StatusCode = (HttpStatusCode) value; 
            }
        } 
 
        /// 
        /// Gets the  to be written to send a response 
        /// to the client.
        /// 
        Stream IDataServiceHost.ResponseStream
        { 
            get
            { 
                // The ResponseStream is not directly accessible - this would 
                // prevent WCF from streaming results back. For the WCF host,
                // a Message subclass should write directly in the OnBodyWrite 
                // method.
                throw Error.NotSupported();
            }
        } 

        /// Gets or sets the value for the DataServiceVersion response header. 
        string IDataServiceHost.ResponseVersion 
        {
            get { return this.operationContext.OutgoingResponse.Headers[XmlConstants.HttpDataServiceVersion]; } 
            set { this.operationContext.OutgoingResponse.Headers[XmlConstants.HttpDataServiceVersion] = value; }
        }

        /// Gets the absolute URI to the service. 
        Uri IDataServiceHost.AbsoluteServiceUri
        { 
            get 
            {
                UriTemplateMatch match = this.operationContext.IncomingRequest.UriTemplateMatch; 

                // We never want to consider the last segment of the base URI a 'document' type
                // of segment to be replaced, ie, http://foo/svc.svc should never remove svc.svc
                // from the path. 
                Uri baseUri = match.BaseUri;
                if (!String.IsNullOrEmpty(baseUri.Fragment)) 
                { 
                    throw new InvalidOperationException(Strings.HttpContextServiceHost_IncomingTemplateMatchFragment(baseUri));
                } 

                if (!String.IsNullOrEmpty(baseUri.Query))
                {
                    throw new InvalidOperationException(Strings.HttpContextServiceHost_IncomingTemplateMatchQuery(baseUri)); 
                }
 
                baseUri = WebUtil.EnsureLastSegmentEmpty(baseUri); 
                baseUri = WebUtil.ApplyHostHeader(baseUri, this.HostHeader);
                return baseUri; 
            }
        }

        ///  
        /// Gets the  from which the request data can be read from
        /// to the client. 
        ///  
        Stream IDataServiceHost.RequestStream
        { 
            [DebuggerStepThrough]
            get { return this.incomingMessageBody; }
        }
 
        /// Whether an error was found when processing this request.
        internal bool ErrorFound 
        { 
            get { return this.errorFound; }
        } 

        /// The value for the Host header in the request, possibly null.
        private string HostHeader
        { 
            get { return this.operationContext.IncomingRequest.Headers[HttpRequestHeader.Host]; }
        } 
 
        #endregion Properties.
 
        #region Methods.

        /// Gets the value for the specified item in the request query string.
        /// Item to return. 
        /// 
        /// The value for the specified item in the request query string; 
        /// null if  is not found. 
        /// 
        string IDataServiceHost.GetQueryStringItem(string item) 
        {
            Debug.Assert(item != null, "item != null");
            Debug.Assert(item.Trim() == item, "item.Trim() == item - otherwise, there are leading/trailing spaces in the name");
 
            System.Collections.Specialized.NameValueCollection collection = this.operationContext.IncomingRequest.UriTemplateMatch.QueryParameters;
            string[] values = collection.GetValues(item); 
            if (values == null || values.Length == 0) 
            {
                // Do a scan of arguments ignoring whitespace (SQLBUDT #555944). 
                string keyFound = null;
                foreach (string key in collection.Keys)
                {
                    if (key != null && StringComparer.OrdinalIgnoreCase.Equals(key.Trim(), item)) 
                    {
                        if (keyFound != null) 
                        { 
                            throw DataServiceException.CreateBadRequestError(Strings.HttpContextServiceHost_AmbiguousItemName(item, keyFound, key));
                        } 

                        keyFound = key;
                        values = collection.GetValues(key);
                    } 
                }
 
                if (values == null || values.Length == 0) 
                {
                    return null; 
                }
            }

            Debug.Assert(values != null && values.Length > 0, "values != null && values.Length > 0 - otherwise we should have returned already"); 
            if (values.Length == 1)
            { 
                return values[0]; 
            }
            else 
            {
                throw DataServiceException.CreateSyntaxError();
            }
        } 

        ///  
        /// Method to handle a data service exception during processing. 
        /// 
        /// Exception handling description. 
        void IDataServiceHost.ProcessException(HandleExceptionArgs args)
        {
            Debug.Assert(this.operationContext != null, "this.operationContext != null");
            this.errorFound = true; 
            if (!args.ResponseWritten)
            { 
                ((IDataServiceHost)this).ResponseStatusCode = args.ResponseStatusCode; 
                ((IDataServiceHost)this).ResponseContentType = args.ResponseContentType;
                if (args.ResponseAllowHeader != null) 
                {
                    this.operationContext.OutgoingResponse.Headers[HttpResponseHeader.Allow] = args.ResponseAllowHeader;
                }
            } 
        }
 
        /// Verifies that query parameters are valid. 
        internal void VerifyQueryParameters()
        { 
            HashSet namesFound = new HashSet(StringComparer.Ordinal);
            System.Collections.Specialized.NameValueCollection collection = this.operationContext.IncomingRequest.UriTemplateMatch.QueryParameters;
            for (int i = 0; i < collection.Count; i++)
            { 
                string name = collection.GetKey(i);
                if (name == null) 
                { 
                    // These are values of the form a&b&c, without '='. We just make sure they aren't system
                    // values at all. 
                    string[] values = collection.GetValues(i);
                    if (values != null)
                    {
                        for (int j = 0; j < values.Length; j++) 
                        {
                            string value = values[j].Trim(); 
                            if (value.Length > 0 && value[0] == '$') 
                            {
                                throw DataServiceException.CreateBadRequestError( 
                                    Strings.HttpContextServiceHost_QueryParameterMustBeSpecifiedOnce(value));
                            }
                        }
                    } 

                    continue; 
                } 

                name = name.Trim(); 
                if (!namesFound.Add(name))
                {
                    throw DataServiceException.CreateBadRequestError(
                        Strings.HttpContextServiceHost_QueryParameterMustBeSpecifiedOnce(name)); 
                }
 
                if (name.Length > 0 && name[0] == '$') 
                {
                    if (name != XmlConstants.HttpQueryStringExpand && 
                        name != XmlConstants.HttpQueryStringFilter &&
                        name != XmlConstants.HttpQueryStringOrderBy &&
                        name != XmlConstants.HttpQueryStringSkip &&
                        name != XmlConstants.HttpQueryStringTop) 
                    {
                        throw DataServiceException.CreateBadRequestError( 
                            Strings.HttpContextServiceHost_UnknownQueryParameter(name)); 
                    }
 
                    string[] values = collection.GetValues(i);
                    if (values == null || values.Length != 1)
                    {
                        throw DataServiceException.CreateBadRequestError( 
                            Strings.HttpContextServiceHost_QueryParameterMustBeSpecifiedOnce(name));
                    } 
                } 
            }
        } 

        #endregion Methods.
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//  
//      Provides an HttpContext-based implementation.
//  
// 
// @owner  [....]
//--------------------------------------------------------------------- 

namespace System.Data.Services
{
    #region Namespaces. 

    using System; 
    using System.Collections.Generic; 
    using System.Diagnostics;
    using System.IO; 
    using System.Net;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Web;
    using System.Text; 
    using System.Xml;
 
    #endregion Namespaces. 

    ///  
    /// Provides access to the environment for a DataService, including information about the current request, based
    /// on the current WebOperationContext.
    /// 
    internal class HttpContextServiceHost : IDataServiceHost 
    {
        #region Private fields. 
 
        /// Message sent to server.
        private readonly Stream incomingMessageBody; 

        /// The WCF-based operation context.
        private readonly WebOperationContext operationContext;
 
        /// Whether an error was found when processing this request.
        private bool errorFound; 
 
        #endregion Private fields.
 
        #region Constructors.

        /// 
        /// Initializes a new System.Data.Services.HttpContextServiceHost instance. 
        /// 
        /// Incoming message body to process. 
        internal HttpContextServiceHost(Stream messageBody) 
        {
            // We capture the current context at initialization time rather 
            // than accessing it repeatedly.
            this.incomingMessageBody = messageBody;
            this.operationContext = WebOperationContext.Current;
            if (this.operationContext == null) 
            {
                throw new InvalidOperationException(Strings.HttpContextServiceHost_WebOperationContextCurrentMissing); 
            } 
        }
 
        #endregion Constructors.

        #region Properties.
 
        /// 
        /// Gets the character set encoding that the client requested, 
        /// possibly null. 
        /// 
        string IDataServiceHost.RequestAcceptCharSet 
        {
            get
            {
                // Returns a string that contains the comma-separated list of values 
                // associated with the specified key, if found; otherwise, null.
                return this.operationContext.IncomingRequest.Headers[HttpRequestHeader.AcceptCharset]; 
            } 
        }
 
        /// Gets or sets the HTTP MIME type of the output stream.
        string IDataServiceHost.ResponseContentType
        {
            get 
            {
                return this.operationContext.OutgoingResponse.ContentType; 
            } 

            set 
            {
                this.operationContext.OutgoingResponse.ContentType = value;
            }
        } 

        /// Gets the HTTP MIME type of the input stream. 
        string IDataServiceHost.RequestContentType 
        {
            get 
            {
                return this.operationContext.IncomingRequest.ContentType;
            }
        } 

        ///  
        /// Gets a comma-separated list of client-supported MIME Accept types. 
        /// 
        string IDataServiceHost.RequestAccept 
        {
            get
            {
                return this.operationContext.IncomingRequest.Accept; 
            }
        } 
 
        /// 
        /// Gets the HTTP data transfer method (such as GET, POST, or HEAD) used by the client. 
        /// 
        string IDataServiceHost.RequestHttpMethod
        {
            get 
            {
                string result; 
                string[] methodValues = this.operationContext.IncomingRequest.Headers.GetValues(XmlConstants.HttpXMethod); 
                if (methodValues == null || methodValues.Length == 0)
                { 
                    result = this.operationContext.IncomingRequest.Method;
                }
                else if (methodValues.Length == 1)
                { 
                    result = methodValues[0];
                    if (this.operationContext.IncomingRequest.Method != XmlConstants.HttpMethodPost) 
                    { 
                        throw DataServiceException.CreateBadRequestError(Strings.HttpContextServiceHost_XMethodNotUsingPost);
                    } 

                    if (result != XmlConstants.HttpMethodDelete && result != XmlConstants.HttpMethodPut && result != XmlConstants.HttpMethodMerge)
                    {
                        throw DataServiceException.CreateBadRequestError(Strings.HttpContextServiceHost_XMethodIncorrectValue(result)); 
                    }
                } 
                else 
                {
                    throw DataServiceException.CreateBadRequestError(Strings.HttpContextServiceHost_XMethodIncorrectCount(methodValues.Length)); 
                }

                return result;
            } 
        }
 
        /// Gets the value of the If-Match header from the request made 
        string IDataServiceHost.RequestIfMatch
        { 
            get
            {
                return this.operationContext.IncomingRequest.Headers[HttpRequestHeader.IfMatch];
            } 
        }
 
        /// Gets the value of the If-None-Match header from the request made 
        string IDataServiceHost.RequestIfNoneMatch
        { 
            get
            {
                return this.operationContext.IncomingRequest.Headers[HttpRequestHeader.IfNoneMatch];
            } 
        }
 
        /// Gets the value for the MaxDataServiceVersion request header. 
        string IDataServiceHost.RequestMaxVersion
        { 
            get
            {
                return this.operationContext.IncomingRequest.Headers[XmlConstants.HttpMaxDataServiceVersion];
            } 
        }
 
        /// Gets the value for the DataServiceVersion request header. 
        string IDataServiceHost.RequestVersion
        { 
            get
            {
                return this.operationContext.IncomingRequest.Headers[XmlConstants.HttpDataServiceVersion];
            } 
        }
 
        /// Gets the absolute URI to the resource upon which to apply the request. 
        Uri IDataServiceHost.AbsoluteRequestUri
        { 
            get
            {
                UriTemplateMatch match = this.operationContext.IncomingRequest.UriTemplateMatch;
                Uri requestUri = WebUtil.ApplyHostHeader(match.RequestUri, this.HostHeader); 
                return requestUri;
            } 
        } 

        /// Gets or sets the Cache-Control header on the response. 
        string IDataServiceHost.ResponseCacheControl
        {
            get { return this.operationContext.OutgoingResponse.Headers[HttpResponseHeader.CacheControl]; }
            set { this.operationContext.OutgoingResponse.Headers[HttpResponseHeader.CacheControl] = value; } 
        }
 
        /// Gets/Sets the value of the ETag header on the outgoing response 
        string IDataServiceHost.ResponseETag
        { 
            get
            {
                return this.operationContext.OutgoingResponse.ETag;
            } 

            set 
            { 
                this.operationContext.OutgoingResponse.ETag = value;
            } 
        }

        /// Gets or sets the Location header on the response.
        string IDataServiceHost.ResponseLocation 
        {
            get { return this.operationContext.OutgoingResponse.Headers[HttpResponseHeader.Location]; } 
            set { this.operationContext.OutgoingResponse.Headers[HttpResponseHeader.Location] = value; } 
        }
 
        /// 
        /// Gets/Sets the status code for the request made.
        /// 
        int IDataServiceHost.ResponseStatusCode 
        {
            get 
            { 
                return (int) this.operationContext.OutgoingResponse.StatusCode;
            } 

            set
            {
                this.operationContext.OutgoingResponse.StatusCode = (HttpStatusCode) value; 
            }
        } 
 
        /// 
        /// Gets the  to be written to send a response 
        /// to the client.
        /// 
        Stream IDataServiceHost.ResponseStream
        { 
            get
            { 
                // The ResponseStream is not directly accessible - this would 
                // prevent WCF from streaming results back. For the WCF host,
                // a Message subclass should write directly in the OnBodyWrite 
                // method.
                throw Error.NotSupported();
            }
        } 

        /// Gets or sets the value for the DataServiceVersion response header. 
        string IDataServiceHost.ResponseVersion 
        {
            get { return this.operationContext.OutgoingResponse.Headers[XmlConstants.HttpDataServiceVersion]; } 
            set { this.operationContext.OutgoingResponse.Headers[XmlConstants.HttpDataServiceVersion] = value; }
        }

        /// Gets the absolute URI to the service. 
        Uri IDataServiceHost.AbsoluteServiceUri
        { 
            get 
            {
                UriTemplateMatch match = this.operationContext.IncomingRequest.UriTemplateMatch; 

                // We never want to consider the last segment of the base URI a 'document' type
                // of segment to be replaced, ie, http://foo/svc.svc should never remove svc.svc
                // from the path. 
                Uri baseUri = match.BaseUri;
                if (!String.IsNullOrEmpty(baseUri.Fragment)) 
                { 
                    throw new InvalidOperationException(Strings.HttpContextServiceHost_IncomingTemplateMatchFragment(baseUri));
                } 

                if (!String.IsNullOrEmpty(baseUri.Query))
                {
                    throw new InvalidOperationException(Strings.HttpContextServiceHost_IncomingTemplateMatchQuery(baseUri)); 
                }
 
                baseUri = WebUtil.EnsureLastSegmentEmpty(baseUri); 
                baseUri = WebUtil.ApplyHostHeader(baseUri, this.HostHeader);
                return baseUri; 
            }
        }

        ///  
        /// Gets the  from which the request data can be read from
        /// to the client. 
        ///  
        Stream IDataServiceHost.RequestStream
        { 
            [DebuggerStepThrough]
            get { return this.incomingMessageBody; }
        }
 
        /// Whether an error was found when processing this request.
        internal bool ErrorFound 
        { 
            get { return this.errorFound; }
        } 

        /// The value for the Host header in the request, possibly null.
        private string HostHeader
        { 
            get { return this.operationContext.IncomingRequest.Headers[HttpRequestHeader.Host]; }
        } 
 
        #endregion Properties.
 
        #region Methods.

        /// Gets the value for the specified item in the request query string.
        /// Item to return. 
        /// 
        /// The value for the specified item in the request query string; 
        /// null if  is not found. 
        /// 
        string IDataServiceHost.GetQueryStringItem(string item) 
        {
            Debug.Assert(item != null, "item != null");
            Debug.Assert(item.Trim() == item, "item.Trim() == item - otherwise, there are leading/trailing spaces in the name");
 
            System.Collections.Specialized.NameValueCollection collection = this.operationContext.IncomingRequest.UriTemplateMatch.QueryParameters;
            string[] values = collection.GetValues(item); 
            if (values == null || values.Length == 0) 
            {
                // Do a scan of arguments ignoring whitespace (SQLBUDT #555944). 
                string keyFound = null;
                foreach (string key in collection.Keys)
                {
                    if (key != null && StringComparer.OrdinalIgnoreCase.Equals(key.Trim(), item)) 
                    {
                        if (keyFound != null) 
                        { 
                            throw DataServiceException.CreateBadRequestError(Strings.HttpContextServiceHost_AmbiguousItemName(item, keyFound, key));
                        } 

                        keyFound = key;
                        values = collection.GetValues(key);
                    } 
                }
 
                if (values == null || values.Length == 0) 
                {
                    return null; 
                }
            }

            Debug.Assert(values != null && values.Length > 0, "values != null && values.Length > 0 - otherwise we should have returned already"); 
            if (values.Length == 1)
            { 
                return values[0]; 
            }
            else 
            {
                throw DataServiceException.CreateSyntaxError();
            }
        } 

        ///  
        /// Method to handle a data service exception during processing. 
        /// 
        /// Exception handling description. 
        void IDataServiceHost.ProcessException(HandleExceptionArgs args)
        {
            Debug.Assert(this.operationContext != null, "this.operationContext != null");
            this.errorFound = true; 
            if (!args.ResponseWritten)
            { 
                ((IDataServiceHost)this).ResponseStatusCode = args.ResponseStatusCode; 
                ((IDataServiceHost)this).ResponseContentType = args.ResponseContentType;
                if (args.ResponseAllowHeader != null) 
                {
                    this.operationContext.OutgoingResponse.Headers[HttpResponseHeader.Allow] = args.ResponseAllowHeader;
                }
            } 
        }
 
        /// Verifies that query parameters are valid. 
        internal void VerifyQueryParameters()
        { 
            HashSet namesFound = new HashSet(StringComparer.Ordinal);
            System.Collections.Specialized.NameValueCollection collection = this.operationContext.IncomingRequest.UriTemplateMatch.QueryParameters;
            for (int i = 0; i < collection.Count; i++)
            { 
                string name = collection.GetKey(i);
                if (name == null) 
                { 
                    // These are values of the form a&b&c, without '='. We just make sure they aren't system
                    // values at all. 
                    string[] values = collection.GetValues(i);
                    if (values != null)
                    {
                        for (int j = 0; j < values.Length; j++) 
                        {
                            string value = values[j].Trim(); 
                            if (value.Length > 0 && value[0] == '$') 
                            {
                                throw DataServiceException.CreateBadRequestError( 
                                    Strings.HttpContextServiceHost_QueryParameterMustBeSpecifiedOnce(value));
                            }
                        }
                    } 

                    continue; 
                } 

                name = name.Trim(); 
                if (!namesFound.Add(name))
                {
                    throw DataServiceException.CreateBadRequestError(
                        Strings.HttpContextServiceHost_QueryParameterMustBeSpecifiedOnce(name)); 
                }
 
                if (name.Length > 0 && name[0] == '$') 
                {
                    if (name != XmlConstants.HttpQueryStringExpand && 
                        name != XmlConstants.HttpQueryStringFilter &&
                        name != XmlConstants.HttpQueryStringOrderBy &&
                        name != XmlConstants.HttpQueryStringSkip &&
                        name != XmlConstants.HttpQueryStringTop) 
                    {
                        throw DataServiceException.CreateBadRequestError( 
                            Strings.HttpContextServiceHost_UnknownQueryParameter(name)); 
                    }
 
                    string[] values = collection.GetValues(i);
                    if (values == null || values.Length != 1)
                    {
                        throw DataServiceException.CreateBadRequestError( 
                            Strings.HttpContextServiceHost_QueryParameterMustBeSpecifiedOnce(name));
                    } 
                } 
            }
        } 

        #endregion Methods.
    }
} 

// 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