Esempio n. 1
0
        private static void SetRequestContent(HttpWebRequest webRequest, ServiceRequest serviceRequest,
                                              bool async, OssAction asyncCallback)
        {
            var data = serviceRequest.BuildRequestContent();

            if (data == null ||
                (serviceRequest.Method != HttpMethod.Put &&
                 serviceRequest.Method != HttpMethod.Post))
            {
                // Skip setting content body in this case.
                if (async)
                {
                    asyncCallback();
                }

                return;
            }

            // Write data to the request stream.
            long userSetContentLength = -1;

            if (serviceRequest.Headers.ContainsKey(HttpHeaders.ContentLength))
            {
                userSetContentLength = long.Parse(serviceRequest.Headers[HttpHeaders.ContentLength]);
            }

            long streamLength = data.Length - data.Position;

            webRequest.ContentLength = (userSetContentLength >= 0 &&
                                        userSetContentLength <= streamLength) ? userSetContentLength : streamLength;

            if (async)
            {
                webRequest.BeginGetRequestStream(
                    (ar) =>
                {
                    using (var requestStream = webRequest.EndGetRequestStream(ar))
                    {
                        IoUtils.WriteTo(data, requestStream, webRequest.ContentLength);
                    }
                    asyncCallback();
                }, null);
            }
            else
            {
                using (var requestStream = webRequest.GetRequestStream())
                {
                    IoUtils.WriteTo(data, requestStream, webRequest.ContentLength);
                }
            }
        }
        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);
        }