Beispiel #1
0
        private SafeCurlSlistHandle SetRequestHeaders(SafeCurlHandle handle, HttpRequestMessage request)
        {
            SafeCurlSlistHandle retVal = new SafeCurlSlistHandle();

            if (request.Headers == null)
            {
                return(retVal);
            }

            HttpHeaders contentHeaders = null;

            if (request.Content != null)
            {
                SetChunkedModeForSend(request);

                // TODO: Content-Length header isn't getting correctly placed using ToString()
                // This is a bug in HttpContentHeaders that needs to be fixed.
                if (request.Content.Headers.ContentLength.HasValue)
                {
                    long contentLength = request.Content.Headers.ContentLength.Value;
                    request.Content.Headers.ContentLength = null;
                    request.Content.Headers.ContentLength = contentLength;
                }
                contentHeaders = request.Content.Headers;
            }

            bool gotReference = false;

            try
            {
                retVal.DangerousAddRef(ref gotReference);
                IntPtr rawHandle = IntPtr.Zero;

                if (request.Headers != null)
                {
                    // Add request headers
                    AddRequestHeaders(request.Headers, retVal, ref rawHandle);
                }

                if (contentHeaders != null)
                {
                    // Add content request headers
                    AddRequestHeaders(contentHeaders, retVal, ref rawHandle);
                }

                // Since libcurl always adds a Transfer-Encoding header, we need to explicitly block
                // it if caller specifically does not want to set the header
                if (request.Headers.TransferEncodingChunked.HasValue && !request.Headers.TransferEncodingChunked.Value)
                {
                    rawHandle = Interop.libcurl.curl_slist_append(rawHandle, NoTransferEncoding);

                    if (rawHandle == null)
                    {
                        throw new HttpRequestException(SR.net_http_client_execution_error);
                    }

                    retVal.SetHandle(rawHandle);
                }

                if (!retVal.IsInvalid)
                {
                    SetCurlOption(handle, CURLoption.CURLOPT_HTTPHEADER, rawHandle);
                }
            }
            finally
            {
                if (gotReference)
                {
                    retVal.DangerousRelease();
                }
            }

            return(retVal);
        }
Beispiel #2
0
        private SafeCurlSlistHandle SetRequestHeaders(SafeCurlHandle handle, HttpRequestMessage request)
        {
            SafeCurlSlistHandle retVal = new SafeCurlSlistHandle();

            if (request.Headers == null)
            {
                return(retVal);
            }

            HttpHeaders contentHeaders = null;

            if (request.Content != null)
            {
                SetChunkedModeForSend(request);

                // TODO: Content-Length header isn't getting correctly placed using ToString()
                // This is a bug in HttpContentHeaders that needs to be fixed.
                if (request.Content.Headers.ContentLength.HasValue)
                {
                    long contentLength = request.Content.Headers.ContentLength.Value;
                    request.Content.Headers.ContentLength = null;
                    request.Content.Headers.ContentLength = contentLength;
                }
                contentHeaders = request.Content.Headers;
            }

            string[] allHeaders = HeaderUtilities.DumpHeaders(request.Headers, contentHeaders)
                                  .Split(s_headerDelimiters, StringSplitOptions.RemoveEmptyEntries);
            bool gotReference = false;

            try
            {
                retVal.DangerousAddRef(ref gotReference);
                IntPtr rawHandle = IntPtr.Zero;
                for (int i = 0; i < allHeaders.Length; i++)
                {
                    string header = allHeaders[i].Trim();
                    if (header.Equals("{") || header.Equals("}"))
                    {
                        continue;
                    }
                    rawHandle = Interop.libcurl.curl_slist_append(rawHandle, header);
                    retVal.SetHandle(rawHandle);
                }

                // Since libcurl always adds a Transfer-Encoding header, we need to explicitly block
                // it if caller specifically does not want to set the header
                if (request.Headers.TransferEncodingChunked.HasValue && !request.Headers.TransferEncodingChunked.Value)
                {
                    rawHandle = Interop.libcurl.curl_slist_append(rawHandle, NoTransferEncoding);
                    retVal.SetHandle(rawHandle);
                }

                if (!retVal.IsInvalid)
                {
                    SetCurlOption(handle, CURLoption.CURLOPT_HTTPHEADER, rawHandle);
                }
            }
            finally
            {
                if (gotReference)
                {
                    retVal.DangerousRelease();
                }
            }

            return(retVal);
        }