private static void SetHeaderValues(HttpWebRequestMessage requestMessage, Dictionary <string, string> cachedHeaders)
        {
            bool flag = true;

            System.Net.HttpWebRequest httpRequest = requestMessage.httpRequest;
            string method = requestMessage.Method;
            string str2   = null;

            cachedHeaders.TryGetValue("Content-Type", out str2);
            if (string.CompareOrdinal(method, "GET") != 0)
            {
                if (string.CompareOrdinal(method, "DELETE") == 0)
                {
                    httpRequest.ContentType   = null;
                    httpRequest.ContentLength = 0L;
                }
                else
                {
                    httpRequest.ContentType = str2;
                }
                if (requestMessage.requestInfo.UsePostTunneling && (string.CompareOrdinal(method, "POST") != 0))
                {
                    httpRequest.Headers["X-HTTP-Method"] = method;
                    method = "POST";
                    flag   = false;
                }
            }
            ICollection <string> allKeys = httpRequest.Headers.AllKeys;

            if (allKeys.Contains("If-Match"))
            {
                httpRequest.Headers.Remove(HttpRequestHeader.IfMatch);
            }
            if (flag && allKeys.Contains("X-HTTP-Method"))
            {
                httpRequest.Headers.Remove("X-HTTP-Method");
            }
            httpRequest.Method = method;
            if (requestMessage.HeadersToReset != null)
            {
                foreach (string str3 in requestMessage.HeadersToReset)
                {
                    SetHeaderValue(httpRequest, str3, cachedHeaders[str3]);
                }
            }
        }
Esempio n. 2
0
        internal DataServiceClientRequestMessage ShimDataServiceRequest(DataServiceClientRequestMessageArgs args)
        {
            DataServiceClientRequestMessage message = null;

            if (_dataServiceHandler != null)
            {
                message = _dataServiceHandler(args);
            }
            else
            {
                message = new HttpWebRequestMessage(args);
            }

            // apply proxy and credential settings on the core web request
            InitializeMessage(message);

            return message;
        }
Esempio n. 3
0
        public HttpWebRequestMessage(DataServiceClientRequestMessageArgs args)
            : base(args.ActualMethod)
        {
            Util.CheckArgumentNull(args, "args");
            Debug.Assert(args.RequestUri.IsAbsoluteUri, "request uri is not absolute uri");
            Debug.Assert(
                args.RequestUri.Scheme.Equals("http", StringComparison.OrdinalIgnoreCase) ||
                args.RequestUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase),
                "request uri is not for HTTP");
            this.effectiveHttpMethod = args.Method;
            this.requestUrl          = args.RequestUri;

            this.httpRequest = HttpWebRequestMessage.CreateRequest(this.ActualMethod, this.Url, args);

            // Now set the headers.
            foreach (var keyValue in args.Headers)
            {
                this.SetHeader(keyValue.Key, keyValue.Value);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Entry point for requests from OData.
        /// </summary>
        public DataServiceClientRequestMessage ShimDataService(DataServiceClientRequestMessageArgs args)
        {
            DataServiceClientRequestMessage message = null;

            InterceptDispatcher dispatcher = GetDispatcher(args.RequestUri);

            if (dispatcher != null && dispatcher.Initialized == true)
            {
                // Let the interceptor handle this
                message = new ShimDataServiceClientRequestMessage(this, args);
            }

            // If no interceptors want the message create a normal HttpWebRequestMessage
            if (message == null)
            {
                Log(String.Format(CultureInfo.InvariantCulture, "[V2 REQ] {0}", args.RequestUri.AbsoluteUri), ConsoleColor.Gray);
                message = new HttpWebRequestMessage(args);
            }

            return message;
        }
Esempio n. 5
0
        public DataServiceClientRequestMessage ShimDataService(DataServiceClientRequestMessageArgs args)
        {
            DataServiceClientRequestMessage message = null;

            InterceptDispatcher dispatcher = GetDispatcher(args.RequestUri);

            if (dispatcher != null && dispatcher.Initialized == true)
            {
                // Let the interceptor handle this
                message = new ShimDataServiceClientRequestMessage(this, args);
            }

            // If no interceptors want the message create a normal HttpWebRequestMessage
            if (message == null)
            {
                V3InteropTraceSources.ShimController.Verbose("request", "{0} {1}", args.Method, args.RequestUri.AbsoluteUri);
                message = new HttpWebRequestMessage(args);
            }

            return message;
        }
Esempio n. 6
0
        /// <summary>
        /// Fire SendingRequest event if its conditions are met.
        /// If the user has a handler for BuildingRequest, we will throw.
        /// If the user has no BuildingRequest handlers but does have a SendingRequest2 handler, we silently do not fire this event (this is shipped 5.0 behavior).
        /// </summary>
        private void FireSendingRequest()
        {
            // Do not fire SendingRequest event when user tries to wrap the HttpWebRequestMessage
            if (this.fireSendingRequestMethodCalled || this.requestInfo == null)
            {
                return;
            }

            // We need to set this before SendingRequest event is fired so that
            // GetStream method can throw if it is called from SendingRequest event.
            this.fireSendingRequestMethodCalled = true;

            HeaderCollection cachedHeaders = null;

            if (this.requestInfo.HasSendingRequestEventHandlers)
            {
                // Before firing SendingRequest event, we need to cache all the header values so that
                // we can reset them to the original values after SendingRequest event has been fired.
                cachedHeaders = new HeaderCollection();
                foreach (var header in this.Headers)
                {
                    cachedHeaders.SetHeader(header.Key, header.Value);
                }
#if PORTABLELIB
                cachedHeaders.SetHeader(XmlConstants.HttpContentLength, this.httpRequest.Headers[XmlConstants.HttpContentLength]);
#endif
#if !ASTORIA_LIGHT && !PORTABLELIB
                // Content-Length and accept header does not show up in the header collection at all.
                // Hence adding it explicitly, since we reset the content length header
                // after firing SendingRequest event
                cachedHeaders.SetHeader(XmlConstants.HttpContentLength, this.httpRequest.ContentLength.ToString(CultureInfo.InvariantCulture));
#endif
            }

            // Fires whenever a new HttpWebRequest has been created
            // The event fires early - before the client library sets many of its required property values.
            // This ensures the client library has the last say on the value of mandated properties
            // such as the HTTP verb  being used for the request.
            if (this.requestInfo.HasSendingRequestEventHandlers)
            {
                System.Net.WebHeaderCollection requestHeaders;
#if !ASTORIA_LIGHT
                requestHeaders = this.httpRequest.Headers;
                SendingRequestEventArgs args = new SendingRequestEventArgs(this.httpRequest, requestHeaders);
#else
                requestHeaders = this.httpRequest.CreateEmptyWebHeaderCollection();
                /* Also set header for SL, MaxDataServcieVersion is required header */
                foreach (var head in this.Headers)
                {
                    if (head.Key == XmlConstants.HttpMaxDataServiceVersion)
                    {
                        requestHeaders[XmlConstants.HttpMaxDataServiceVersion] = head.Value;
                        break;
                    }
                }

                SendingRequestEventArgs args = new SendingRequestEventArgs(null, requestHeaders);
#endif
                this.requestInfo.FireSendingRequest(args);

#if !ASTORIA_LIGHT
                if (!Object.ReferenceEquals(args.Request, this.httpRequest))
                {
                    this.httpRequest = (System.Net.HttpWebRequest)args.Request;
                }
#else
                // apply all headers to the request
                foreach (string key in requestHeaders.AllKeys)
                {
                    this.httpRequest.Headers[key] = requestHeaders[key];
                }
#endif
                HttpWebRequestMessage.SetHeaderValues(this, cachedHeaders, this.Method);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Set the header values on the request.
        /// </summary>
        /// <param name="requestMessage">IODataRequestMessage instance containing all the headers.</param>
        /// <param name="cachedHeaders">Dictionary of cached headers.</param>
        /// <param name="effectiveHttpMethod">The Effective http method.</param>
        private static void SetHeaderValues(HttpWebRequestMessage requestMessage, HeaderCollection cachedHeaders, string effectiveHttpMethod)
        {
            // DevNote(shank): We used to set request.AllowWriteStreamBuffering to true here. This is now removed
            //                 as it prevents customers from enabling HTTP streaming by setting it to false in the
            //                 SendingRequest event. This is because this method is called twice by CreateGetRequest(),
            //                 before *and* after the SendingRequest event (see DevNote in CreateGetRequest()).
            //                 We also do *not* set it to false by default for media resource requests. Enabling
            //                 streaming requires additional actions to be performed by the customer, e.g., sending
            //                 chunks if content-length is not specified, and pre-authenticating with a HEAD request.
#if !ASTORIA_LIGHT && !PORTABLELIB
            bool removeXMethod = true;
#endif
            Debug.Assert(requestMessage.requestInfo != null, "This method is called from FireSendingRequest. Hence requestInfo should never be null.");
            HttpWebRequest request     = requestMessage.httpRequest;
            string         method      = requestMessage.Method;
            string         contentType = null;
            cachedHeaders.TryGetHeader(XmlConstants.HttpContentType, out contentType);

            if (string.CompareOrdinal(effectiveHttpMethod, XmlConstants.HttpMethodGet) != 0)
            {
                if (string.CompareOrdinal(effectiveHttpMethod, XmlConstants.HttpMethodDelete) == 0)
                {
                    // In V2, we always use to over-write content type after sendingRequest event has been called.
                    // So doing the same for delete requests.
                    // TODO: no need to override this if it was set in building request; just override it was changed elsewhere?
                    Debug.Assert(String.IsNullOrEmpty(contentType), "Content-Type must not be specified for delete operation");
                    request.ContentType = null;
                    SetHttpWebRequestContentLength(request, 0);
                }
                else
                {
                    Debug.Assert(!String.IsNullOrEmpty(contentType), "Content-Type must be specified for non get and non delete operations");
                    request.ContentType = contentType;
                }

                if (requestMessage.requestInfo.UsePostTunneling && (string.CompareOrdinal(effectiveHttpMethod, XmlConstants.HttpMethodPost) != 0))
                {
                    Debug.Assert(effectiveHttpMethod != null, "expected an effectiveHttpMethod");
                    request.Headers[XmlConstants.HttpXMethod] = effectiveHttpMethod;
                    method = XmlConstants.HttpMethodPost;
#if !ASTORIA_LIGHT && !PORTABLELIB
                    removeXMethod = false;
#endif
                }
            }

            // We cannot assert that the content type for GET request is null, since in some scenarios (e.g. GetReadStream),
            // the user can pass the content type in the API and they can set the content type to something. In those
            // scenarios, we set the content type as specified by the user and not check for anything.
#if !ASTORIA_LIGHT && !PORTABLELIB
            // Removing the If-Match header so that if it was accidently written when it should not
            // have, it will get removed after the event. The correct value will be written again
            // when reseting the headers.
            request.Headers.Remove(HttpRequestHeader.IfMatch);

            // alternate HttpXMethod header doesn't work
            if (removeXMethod)
            {
                request.Headers.Remove(XmlConstants.HttpXMethod);
            }
#endif
            request.Method = method;

            // Reset the list of headers, if any
            if (requestMessage.headersToReset != null)
            {
                foreach (string headerName in requestMessage.headersToReset)
                {
#if ASTORIA_LIGHT || PORTABLELIB
                    // Ignoring content length header since its not supported in silverlight
                    if (string.Equals(headerName, XmlConstants.HttpContentLength, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }
#endif
                    HttpWebRequestMessage.SetHeaderValue(request, headerName, cachedHeaders.GetHeader(headerName));
                }
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Returns the value of the header with the given name.
 /// </summary>
 /// <param name="headerName">Name of the header.</param>
 /// <returns>Returns the value of the header with the given name.</returns>
 public override string GetHeader(string headerName)
 {
     Util.CheckArgumentNullAndEmpty(headerName, "headerName");
     return(HttpWebRequestMessage.GetHeaderValue(this.httpRequest, headerName));
 }
Esempio n. 9
0
 private static void SetHeaderValues(HttpWebRequestMessage requestMessage, Dictionary<string, string> cachedHeaders)
 {
     bool flag = true;
     System.Net.HttpWebRequest httpRequest = requestMessage.httpRequest;
     string method = requestMessage.Method;
     string str2 = null;
     cachedHeaders.TryGetValue("Content-Type", out str2);
     if (string.CompareOrdinal(method, "GET") != 0)
     {
         if (string.CompareOrdinal(method, "DELETE") == 0)
         {
             httpRequest.ContentType = null;
             httpRequest.ContentLength = 0L;
         }
         else
         {
             httpRequest.ContentType = str2;
         }
         if (requestMessage.requestInfo.UsePostTunneling && (string.CompareOrdinal(method, "POST") != 0))
         {
             httpRequest.Headers["X-HTTP-Method"] = method;
             method = "POST";
             flag = false;
         }
     }
     ICollection<string> allKeys = httpRequest.Headers.AllKeys;
     if (allKeys.Contains("If-Match"))
     {
         httpRequest.Headers.Remove(HttpRequestHeader.IfMatch);
     }
     if (flag && allKeys.Contains("X-HTTP-Method"))
     {
         httpRequest.Headers.Remove("X-HTTP-Method");
     }
     httpRequest.Method = method;
     if (requestMessage.HeadersToReset != null)
     {
         foreach (string str3 in requestMessage.HeadersToReset)
         {
             SetHeaderValue(httpRequest, str3, cachedHeaders[str3]);
         }
     }
 }