private static void AddKnownHeaders(IEnumerable <string> headers, bool asRequestHeader)
        {
            foreach (string header in headers)
            {
                HttpHeaderInfo headerInfo = null;
                if (knownHeadersInfos.TryGetValue(header, out headerInfo))
                {
                    if (headerInfo.IsContentHeader)
                    {
                        // this header is actually a content header so continue
                        continue;
                    }
                }

                if (headerInfo == null)
                {
                    headerInfo = new HttpHeaderInfo(header);
                    knownHeadersInfos.TryAdd(headerInfo.Name, headerInfo);
                }

                if (asRequestHeader)
                {
                    headerInfo.IsRequestHeader = true;
                }
                else
                {
                    headerInfo.IsResponseHeader = true;
                }
            }
        }
Exemple #2
0
 private static void EnsureNotRequestHeader(HttpHeaderInfo headerInfo)
 {
     if (!headerInfo.IsResponseHeader && !headerInfo.IsContentHeader && headerInfo.IsResponseHeader)
     {
         throw FxTrace.Exception.AsError(
                   new InvalidOperationException(
                       SR.GetString(SR.RequestHeaderWithResponseHeadersCollection, headerInfo.Name)));
     }
 }
Exemple #3
0
        internal static void RemoveHeader(this HttpResponseMessage httpResponseMessage, string header)
        {
            Fx.Assert(httpResponseMessage != null, "The 'httpResponseMessage' parameter should never be null.");
            Fx.Assert(!string.IsNullOrWhiteSpace(header), "The 'header' parameter should never be null or whitespace.");

            HttpHeaderInfo headerInfo = HttpHeaderInfo.Create(header);

            EnsureNotRequestHeader(headerInfo);
            RemoveHeader(httpResponseMessage, headerInfo);
        }
        internal static IEnumerable <string> GetHeader(this HttpRequestMessage httpRequestMessage, string header)
        {
            Fx.Assert(httpRequestMessage != null, "The 'httpRequestMessage' parameter should never be null.");
            Fx.Assert(!string.IsNullOrWhiteSpace(header), "The 'header' parameter should never be null or whitespace.");

            HttpHeaderInfo headerInfo = HttpHeaderInfo.Create(header);

            EnsureNotResponseHeader(headerInfo);
            return(GetHeader(httpRequestMessage, headerInfo));
        }
        public static HttpHeaderInfo Create(string headerName)
        {
            Fx.Assert(!string.IsNullOrWhiteSpace(headerName), "The 'headerName' should not be null or whitespace.");

            HttpHeaderInfo headerInfo;
            if (!knownHeadersInfos.TryGetValue(headerName, out headerInfo))
            {
                headerInfo = new HttpHeaderInfo(headerName, true);           
            }

            return headerInfo;
        }
        public static HttpHeaderInfo Create(string headerName)
        {
            Fx.Assert(!string.IsNullOrWhiteSpace(headerName), "The 'headerName' should not be null or whitespace.");

            HttpHeaderInfo headerInfo;

            if (!knownHeadersInfos.TryGetValue(headerName, out headerInfo))
            {
                headerInfo = new HttpHeaderInfo(headerName, true);
            }

            return(headerInfo);
        }
Exemple #7
0
        private static void RemoveHeader(HttpResponseMessage httpResponseMessage, HttpHeaderInfo headerInfo)
        {
            Fx.Assert(httpResponseMessage != null, "The 'httpResponseMessage' parameter should never be null.");
            Fx.Assert(headerInfo != null, "The 'headerInfo' parameter should never be null.");
            Fx.Assert(headerInfo.IsResponseHeader || headerInfo.IsContentHeader, "The 'headerInfo' parameter should be either a response or content header.");

            if (headerInfo.IsResponseHeader)
            {
                headerInfo.TryRemoveHeader(httpResponseMessage.Headers);
            }

            if (headerInfo.IsContentHeader && httpResponseMessage.Content != null)
            {
                headerInfo.TryRemoveHeader(httpResponseMessage.Content.Headers);
            }
        }
Exemple #8
0
        private static void AddHeader(HttpResponseMessage httpResponseMessage, HttpHeaderInfo headerInfo, string value)
        {
            Fx.Assert(httpResponseMessage != null, "The 'httpResponseMessage' parameter should never be null.");
            Fx.Assert(headerInfo != null, "The 'headerInfo' parameter should never be null.");
            Fx.Assert(headerInfo.IsResponseHeader || headerInfo.IsContentHeader, "The 'headerInfo' parameter should be either a response or content header.");

            if (headerInfo.IsResponseHeader)
            {
                if (headerInfo.TryAddHeader(httpResponseMessage.Headers, value))
                {
                    return;
                }
            }

            if (headerInfo.IsContentHeader)
            {
                CreateContentIfNull(httpResponseMessage);
                headerInfo.TryAddHeader(httpResponseMessage.Content.Headers, value);
            }
        }
        private static void AddHeader(HttpRequestMessage httpRequestMessage, HttpHeaderInfo headerInfo, string value)
        {
            Fx.Assert(httpRequestMessage != null, "The 'httpRequestMessage' parameter should never be null.");
            Fx.Assert(headerInfo != null, "The 'headerInfo' parameter should never be null.");
            Fx.Assert(headerInfo.IsRequestHeader || headerInfo.IsContentHeader, "The 'headerInfo' parameter should be either a request or content header.");

            if (headerInfo.IsRequestHeader)
            {
                if (headerInfo.TryAddHeader(httpRequestMessage.Headers, value))
                {
                    return;
                }
            }
            
            if (headerInfo.IsContentHeader)
            {
                CreateContentIfNull(httpRequestMessage);
                headerInfo.TryAddHeader(httpRequestMessage.Content.Headers, value);
            }  
        }
        private static void RemoveHeader(HttpRequestMessage httpRequestMessage, HttpHeaderInfo headerInfo)
        {
            Fx.Assert(httpRequestMessage != null, "The 'httpRequestMessage' parameter should never be null.");
            Fx.Assert(headerInfo != null, "The 'headerInfo' parameter should never be null.");
            Fx.Assert(headerInfo.IsRequestHeader || headerInfo.IsContentHeader, "The 'headerInfo' parameter should be either a request or content header.");

            if (headerInfo.IsRequestHeader)
            {
                headerInfo.TryRemoveHeader(httpRequestMessage.Headers);
            }

            if (headerInfo.IsContentHeader && httpRequestMessage.Content != null)
            {
                headerInfo.TryRemoveHeader(httpRequestMessage.Content.Headers);
            }
        }
        private static IEnumerable<string> GetHeader(HttpRequestMessage httpRequestMessage, HttpHeaderInfo headerInfo)
        {
            Fx.Assert(httpRequestMessage != null, "The 'httpRequestMessage' parameter should never be null.");
            Fx.Assert(headerInfo != null, "The 'headerInfo' parameter should never be null.");
            Fx.Assert(headerInfo.IsRequestHeader || headerInfo.IsContentHeader, "The 'headerInfo' parameter should be either a request or content header.");

            IEnumerable<string> values = null;

            if (headerInfo.IsRequestHeader)
            {
                values = headerInfo.TryGetHeader(httpRequestMessage.Headers);
            }

            if (values == null && 
                headerInfo.IsContentHeader && 
                httpRequestMessage.Content != null)
            {
                values = headerInfo.TryGetHeader(httpRequestMessage.Content.Headers);
            }

            return values;
        }
 private static void EnsureNotResponseHeader(HttpHeaderInfo headerInfo)
 {
     if (!headerInfo.IsRequestHeader && !headerInfo.IsContentHeader && headerInfo.IsResponseHeader)
     {
         throw FxTrace.Exception.AsError(
             new InvalidOperationException(
                 SR.GetString(SR.ResponseHeaderWithRequestHeadersCollection, headerInfo.Name)));
     }
 }
Exemple #13
0
        private static IEnumerable <string> GetHeader(HttpResponseMessage httpResponseMessage, HttpHeaderInfo headerInfo)
        {
            Fx.Assert(httpResponseMessage != null, "The 'httpResponseMessage' parameter should never be null.");
            Fx.Assert(headerInfo != null, "The 'headerInfo' parameter should never be null.");
            Fx.Assert(headerInfo.IsResponseHeader || headerInfo.IsContentHeader, "The 'headerInfo' parameter should be either a response or content header.");

            IEnumerable <string> values = null;

            if (headerInfo.IsResponseHeader)
            {
                values = headerInfo.TryGetHeader(httpResponseMessage.Headers);
            }

            if (values == null &&
                headerInfo.IsContentHeader &&
                httpResponseMessage.Content != null)
            {
                values = headerInfo.TryGetHeader(httpResponseMessage.Content.Headers);
            }

            return(values);
        }
        private static void AddKnownHeaders(IEnumerable<string> headers, bool asRequestHeader)
        {
            foreach (string header in headers)
            {
                HttpHeaderInfo headerInfo = null;
                if (knownHeadersInfos.TryGetValue(header, out headerInfo))
                {
                    if (headerInfo.IsContentHeader)
                    {
                        // this header is actually a content header so continue
                        continue;
                    }
                }

                if (headerInfo == null)
                {
                    headerInfo = new HttpHeaderInfo(header);
                    knownHeadersInfos.TryAdd(headerInfo.Name, headerInfo);
                }

                if (asRequestHeader)
                {
                    headerInfo.IsRequestHeader = true;
                }
                else
                {
                    headerInfo.IsResponseHeader = true;
                }
            }
        }