Example #1
0
        /// <summary>
        /// Sets the value of the header with the given name.
        /// </summary>
        /// <param name="headerName">Name of the header.</param>
        /// <param name="headerValue">Value of the header.</param>
        public override void SetHeader(string headerName, string headerValue)
        {
#if DEBUG
            // Only content length header is set after firing SendingRequest2 event
            Debug.Assert(!this.sendingRequest2Fired || headerName == XmlConstants.HttpContentLength, "!this.sendingRequest2Fired || headerName == XmlConstants.HttpContentLength");
#endif
            Util.CheckArgumentNullAndEmpty(headerName, "headerName");
            HttpWebRequestMessage.SetHeaderValue(this.httpRequest, headerName, headerValue);
        }
Example #2
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));
                }
            }
        }