private static void BeginSetRequestContent()

in sdk/Common/Communication/ServiceClientImpl.cs [287:355]


        private static void BeginSetRequestContent(HttpWebRequest webRequest, ServiceRequest serviceRequest,
                                            OssAction asyncCallback, ClientConfiguration clientConfiguration, HttpAsyncResult result)
        {
            var data = serviceRequest.BuildRequestContent();

            if (data == null ||
                (serviceRequest.Method != HttpMethod.Put &&
                 serviceRequest.Method != HttpMethod.Post))
            {
                // Skip setting content body in this case.
                try
                {
                    asyncCallback();
                }
                catch(Exception e)
                {
                    result.WebRequest.Abort();
                    result.Complete(e);
                }

                return;
            }

            // Write data to the request stream.
            long userSetContentLength = -1;
            if (serviceRequest.Headers.ContainsKey(HttpHeaders.ContentLength))
                userSetContentLength = long.Parse(serviceRequest.Headers[HttpHeaders.ContentLength]);

            if (serviceRequest.UseChunkedEncoding || !data.CanSeek) // when data cannot seek, we have to use chunked encoding as there's no way to set the length
            {
                webRequest.SendChunked = true;
                webRequest.AllowWriteStreamBuffering = false; // when using chunked encoding, the data is likely big and thus not use write buffer;
            }
            else
            {
                long streamLength = data.Length - data.Position;
                webRequest.ContentLength = (userSetContentLength >= 0 &&
                    userSetContentLength <= streamLength) ? userSetContentLength : streamLength;
                if (webRequest.ContentLength > clientConfiguration.DirectWriteStreamThreshold)
                {
                    webRequest.AllowWriteStreamBuffering = false;
                }
            }

            webRequest.BeginGetRequestStream(
                    (ar) =>
                    {
                        try
                        {
                            using (var requestStream = webRequest.EndGetRequestStream(ar))
                            {
                                if (!webRequest.SendChunked)
                                {
                                    IoUtils.WriteTo(data, requestStream, webRequest.ContentLength);
                                }
                                else
                                {
                                    IoUtils.WriteTo(data, requestStream);
                                }
                            }
                            asyncCallback();
                        }
                        catch(Exception e)
                        {
                            result.WebRequest.Abort();
                            result.Complete(e);
                        }
                    }, null);
        }