示例#1
0
        internal static HttpRequestMessageBuilder GetEmptyRestRequest(HttpRequestMessageBuilder requestBuilder)
        {
            string serializedBody = Newtonsoft.Json.JsonConvert.SerializeObject("");

            requestBuilder.AddOrUpdateHeaderParameter("application/json; charset=utf-8", serializedBody);
            return(requestBuilder);
        }
示例#2
0
 /// <summary>
 /// Set 'X-Amz-Security-Token' http header.
 /// </summary>
 /// <param name="requestBuilder">Instantiated requestBuilder object</param>
 /// <param name="sessionToken">session token</param>
 private void SetSessionTokenHeader(HttpRequestMessageBuilder requestBuilder, string sessionToken)
 {
     if (!string.IsNullOrEmpty(sessionToken))
     {
         requestBuilder.AddOrUpdateHeaderParameter("X-Amz-Security-Token", sessionToken);
     }
 }
示例#3
0
        /// <summary>
        /// Set 'x-amz-content-sha256' http header.
        /// </summary>
        /// <param name="requestBuilder">Instantiated requestBuilder object</param>
        private void SetContentSha256(HttpRequestMessageBuilder requestBuilder)
        {
            if (this.isAnonymous)
            {
                return;
            }
            // No need to compute SHA256 if the endpoint scheme is https
            // or the command method is not a Post to delete multiple files
            var isMultiDeleteRequest = false;

            if (requestBuilder.Method == HttpMethod.Post)
            {
                isMultiDeleteRequest = requestBuilder.QueryParameters.Any(p => p.Key.Equals("delete", StringComparison.OrdinalIgnoreCase));
            }
            if (isSecure || isMultiDeleteRequest)
            {
                requestBuilder.AddOrUpdateHeaderParameter("x-amz-content-sha256", "UNSIGNED-PAYLOAD");
                return;
            }
            // For insecure, authenticated requests set sha256 header instead of MD5.
            if (requestBuilder.Method.Equals(HttpMethod.Put) ||
                requestBuilder.Method.Equals(HttpMethod.Post))
            {
                var body = requestBuilder.Content;
                if (body == null)
                {
                    requestBuilder.AddOrUpdateHeaderParameter("x-amz-content-sha256", sha256EmptyFileHash);
                    return;
                }
                var    sha256 = SHA256.Create();
                byte[] hash   = sha256.ComputeHash(body);
                string hex    = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower();
                requestBuilder.AddOrUpdateHeaderParameter("x-amz-content-sha256", hex);
            }
            else if (!isSecure && requestBuilder.Content != null)
            {
                var    md5  = MD5.Create();
                byte[] hash = md5.ComputeHash(Encoding.UTF8.GetBytes(requestBuilder.Content.ToString()));

                string base64 = Convert.ToBase64String(hash);
                requestBuilder.AddHeaderParameter("Content-Md5", base64);
            }
            else
            {
                requestBuilder.AddOrUpdateHeaderParameter("x-amz-content-sha256", sha256EmptyFileHash);
            }
        }
 internal override HttpRequestMessageBuilder BuildRequest(HttpRequestMessageBuilder requestMessageBuilder)
 {
     // ``us-east-1`` is not a valid location constraint according to amazon, so we skip it.
     if (!string.IsNullOrEmpty(this.Location) && this.Location != "us-east-1")
     {
         CreateBucketConfiguration config = new CreateBucketConfiguration(this.Location);
         string body = utils.MarshalXML(config, "http://s3.amazonaws.com/doc/2006-03-01/");
         requestMessageBuilder.AddXmlBody(body);
         requestMessageBuilder.AddOrUpdateHeaderParameter("Content-Md5",
                                                          utils.getMD5SumStr(Encoding.UTF8.GetBytes(body)));
     }
     if (this.ObjectLock)
     {
         requestMessageBuilder.AddOrUpdateHeaderParameter("X-Amz-Bucket-Object-Lock-Enabled", "true");
     }
     return(requestMessageBuilder);
 }
        internal override HttpRequestMessageBuilder BuildRequest(HttpRequestMessageBuilder requestMessageBuilder)
        {
            requestMessageBuilder.AddQueryParameter("object-lock", "");
            string body = utils.MarshalXML(new ObjectLockConfiguration(), "http://s3.amazonaws.com/doc/2006-03-01/");

            requestMessageBuilder.AddXmlBody(body);
            requestMessageBuilder.AddOrUpdateHeaderParameter("Content-Md5",
                                                             utils.getMD5SumStr(Encoding.UTF8.GetBytes(body)));
            return(requestMessageBuilder);
        }
 internal override HttpRequestMessageBuilder BuildRequest(HttpRequestMessageBuilder requestMessageBuilder)
 {
     if (!string.IsNullOrEmpty(this.MatchETag))
     {
         requestMessageBuilder.AddOrUpdateHeaderParameter("If-Match", this.MatchETag);
     }
     if (!string.IsNullOrEmpty(this.NotMatchETag))
     {
         requestMessageBuilder.AddOrUpdateHeaderParameter("If-None-Match", this.NotMatchETag);
     }
     if (this.ModifiedSince != default(DateTime))
     {
         requestMessageBuilder.AddOrUpdateHeaderParameter("If-Modified-Since", utils.To8601String(this.ModifiedSince));
     }
     if (this.UnModifiedSince != default(DateTime))
     {
         requestMessageBuilder.AddOrUpdateHeaderParameter("If-Unmodified-Since", utils.To8601String(this.UnModifiedSince));
     }
     return(requestMessageBuilder);
 }
示例#7
0
        private async Task <ResponseResult> ExecuteTaskCoreAsync(
            IEnumerable <ApiResponseErrorHandlingDelegate> errorHandlers,
            HttpRequestMessageBuilder requestMessageBuilder,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var startTime = DateTime.Now;

            // Logs full url when HTTPtracing is enabled.
            if (this.trace)
            {
                var fullUrl = requestMessageBuilder.RequestUri;
            }

            var v4Authenticator = new V4Authenticator(this.Secure,
                                                      this.AccessKey, this.SecretKey, this.Region,
                                                      this.SessionToken);

            requestMessageBuilder.AddOrUpdateHeaderParameter("Authorization",
                                                             v4Authenticator.Authenticate(requestMessageBuilder));

            HttpRequestMessage request = requestMessageBuilder.Request;

            ResponseResult responseResult;

            try
            {
                if (requestTimeout > 0)
                {
                    this.HttpClient.Timeout = new TimeSpan(0, 0, 0, 0, requestTimeout);
                }

                var response = await this.HttpClient.SendAsync(request,
                                                               HttpCompletionOption.ResponseHeadersRead, cancellationToken)
                               .ConfigureAwait(false);

                responseResult = new ResponseResult(request, response);
                if (requestMessageBuilder.ResponseWriter != null)
                {
                    requestMessageBuilder.ResponseWriter(responseResult.ContentStream);
                }
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e)
            {
                responseResult = new ResponseResult(request, e);
            }

            this.HandleIfErrorResponse(responseResult, errorHandlers, startTime);
            return(responseResult);
        }
        internal override HttpRequestMessageBuilder BuildRequest(HttpRequestMessageBuilder requestMessageBuilder)
        {
            requestMessageBuilder.AddQueryParameter("tagging", "");
            string body = this.BucketTags.MarshalXML();

            requestMessageBuilder.AddXmlBody(body);
            requestMessageBuilder.AddOrUpdateHeaderParameter("Content-Md5",
                                                             utils.getMD5SumStr(Encoding.UTF8.GetBytes(body)));

            //
            return(requestMessageBuilder);
        }
示例#9
0
        internal override HttpRequestMessageBuilder BuildRequest(HttpRequestMessageBuilder requestMessageBuilder)
        {
            VersioningConfiguration config = new VersioningConfiguration((this.CurrentVersioningStatus == VersioningStatus.Enabled));

            string body = utils.MarshalXML(config, "http://s3.amazonaws.com/doc/2006-03-01/");

            requestMessageBuilder.AddXmlBody(body);
            requestMessageBuilder.AddOrUpdateHeaderParameter("Content-Md5",
                                                             utils.getMD5SumStr(Encoding.UTF8.GetBytes(body)));

            requestMessageBuilder.AddQueryParameter("versioning", "");
            return(requestMessageBuilder);
        }
        internal override HttpRequestMessageBuilder BuildRequest(HttpRequestMessageBuilder requestMessageBuilder)
        {
            requestMessageBuilder.AddQueryParameter("lifecycle", "");
            string body = this.BucketLifecycle.MarshalXML();

            // Convert string to a byte array
            byte[] bodyInBytes = Encoding.ASCII.GetBytes(body);
            requestMessageBuilder.BodyParameters.Add("content-type", "text/xml");
            requestMessageBuilder.SetBody(bodyInBytes);
            requestMessageBuilder.AddOrUpdateHeaderParameter("Content-Md5",
                                                             utils.getMD5SumStr(bodyInBytes));

            return(requestMessageBuilder);
        }
        internal override HttpRequestMessageBuilder BuildRequest(HttpRequestMessageBuilder requestMessageBuilder)
        {
            requestMessageBuilder.AddQueryParameter("object-lock", "");
            string body = utils.MarshalXML(this.LockConfiguration, "http://s3.amazonaws.com/doc/2006-03-01/");

            // Convert string to a byte array
            // byte[] bodyInBytes = Encoding.ASCII.GetBytes(body);

            // requestMessageBuilder.BodyParameters.Add("content-type", "text/xml");
            // requestMessageBuilder.SetBody(bodyInBytes);
            //
            // string body = utils.MarshalXML(config, "http://s3.amazonaws.com/doc/2006-03-01/");
            requestMessageBuilder.AddXmlBody(body);
            requestMessageBuilder.AddOrUpdateHeaderParameter("Content-Md5",
                                                             utils.getMD5SumStr(Encoding.UTF8.GetBytes(body)));
            //
            return(requestMessageBuilder);
        }
示例#12
0
 /// <summary>
 /// Set 'Host' http header.
 /// </summary>
 /// <param name="requestBuilder">Instantiated requestBuilder object</param>
 /// <param name="hostUrl">Host url</param>
 private void SetHostHeader(HttpRequestMessageBuilder requestBuilder, string hostUrl)
 {
     requestBuilder.AddOrUpdateHeaderParameter("Host", hostUrl);
 }
示例#13
0
 /// <summary>
 /// Sets 'x-amz-date' http header.
 /// </summary>
 /// <param name="requestBuilder">Instantiated requestBuilder object</param>
 /// <param name="signingDate">Date for signature to be signed</param>
 private void SetDateHeader(HttpRequestMessageBuilder requestBuilder, DateTime signingDate)
 {
     requestBuilder.AddOrUpdateHeaderParameter("x-amz-date", signingDate.ToString("yyyyMMddTHHmmssZ"));
 }