Ejemplo n.º 1
0
 /// <summary>
 /// Request data from one resource and provide to another.
 /// This is done in a memory-efficient manner.
 /// </summary>
 /// <param name="loadRequest">Request that will provide body data (should be a GET or POST)</param>
 /// <param name="storeRequest">Request that will accept body data (should be a PUT or POST)</param>
 /// <exception cref="ShiftIt.Http.HttpTransferException">Response to the request was not a succesful HTTP status.</exception>
 /// <exception cref="System.Net.Sockets.SocketException">Low level transport exception occured.</exception>
 /// <exception cref="ShiftIt.Http.TimeoutException">A timeout occured during transfer.</exception>
 public void CrossLoad(IHttpRequest loadRequest, IHttpRequestBuilder storeRequest)
 {
     using (var getTx = RequestOrThrow(loadRequest)) // get source
     {
         var storeRq = storeRequest.Build(getTx.RawBodyStream, getTx.BodyReader.ExpectedLength);
         using (RequestOrThrow(storeRq)) // dispose of the response stream
         {
         }
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Request data from one resource and provide to another.
 /// This is done in a memory-efficient manner.
 /// </summary>
 /// <param name="loadRequest">Request that will provide body data (should be a GET or POST)</param>
 /// <param name="storeRequest">Request that will accept body data (should be a PUT or POST)</param>
 /// <param name="sendProgress">Optional: action that is updated with bytes transferred. No guarantees as to when.</param>
 /// <exception cref="ShiftIt.Http.HttpTransferException">Response to the request was not a succesful HTTP status.</exception>
 /// <exception cref="System.Net.Sockets.SocketException">Low level transport exception occured.</exception>
 /// <exception cref="ShiftIt.Http.TimeoutException">A timeout occured during transfer.</exception>
 public void CrossLoad(IHttpRequest loadRequest, IHttpRequestBuilder storeRequest, Action <long> sendProgress = null)
 {
     using (var getTx = RequestOrThrow(loadRequest))             // get source
     {
         var storeRq = storeRequest.Build(getTx.RawBodyStream, getTx.BodyReader.ExpectedLength);
         using (RequestOrThrow(storeRq))                 // dispose of the response stream
         {
         }
     }
 }
Ejemplo n.º 3
0
        public static async Task <TResponse> DispatchAsync <TResponse>(this IHttpRequestBuilder httpRequestBuilder)
        {
            var httpRequest = httpRequestBuilder.Build();
            var response    = await Client.SendAsync(httpRequest);

            var responseContent = await response.Content.ReadAsStringAsync();

            //TODO: consider a strategy for non-success responses
            //if (!task.Result.IsSuccessStatusCode) throw new Exception(result);
            return(JsonConvert.DeserializeObject <TResponse>(responseContent));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sends a request.
        /// </summary>
        /// <param name="requestBuilder">A request builder.</param>
        /// <param name="completionOption">A http completion option.</param>
        /// <returns>The http response.</returns>
        public async Task <HttpResponseMessage> SendAsync(
            IHttpRequestBuilder requestBuilder,
            HttpCompletionOption completionOption)
        {
            var retryAttribute = this.requestContext.MethodInfo.GetCustomAttribute <RetryAttribute>() ??
                                 this.requestContext.MethodInfo.DeclaringType.GetCustomAttribute <RetryAttribute>();

            if (retryAttribute != null)
            {
                RetryHandler retry = new RetryHandler()
                                     .RetryCount(retryAttribute.RetryCount)
                                     .WaitTime(TimeSpan.FromMilliseconds(retryAttribute.WaitTime))
                                     .MaxWaitTime(TimeSpan.FromMilliseconds(retryAttribute.MaxWaitTime))
                                     .DoubleWaitTimeOnRetry(retryAttribute.DoubleWaitTimeOnRetry);

                return(await retry.RetryAsync <HttpResponseMessage>(
                           () =>
                {
                    return this.SendAsync(
                        requestBuilder.Build(),
                        completionOption);
                },
                           (r) =>
                {
                    if (retryAttribute.HttpStatusCodesToRetry != null)
                    {
                        return !retryAttribute.HttpStatusCodesToRetry.Contains(r.StatusCode);
                    }

                    return true;
                })
                       .ConfigureAwait(false));
            }

            return(await this
                   .SendAsync(
                       requestBuilder.Build(),
                       completionOption)
                   .ConfigureAwait(false));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Request data from one resource and provide to another, calculating a
        /// hash of the cross-loaded data. This is done in a memory-efficient manner.
        /// If either source or destination return a non-success result (including redirects)
        /// an exception will be thrown
        /// </summary>
        /// <param name="loadRequest">Request that will provide body data (should be a GET or POST)</param>
        /// <param name="storeRequest">Request that will accept body data (should be a PUT or POST)</param>
        /// <param name="hashAlgorithmName">Name of hash algorithm to use (should be a name supported by <see cref="System.Security.Cryptography.HashAlgorithm"/>)</param>
        /// <exception cref="ShiftIt.Http.HttpTransferException">Response to the request was not a succesful HTTP status.</exception>
        /// <exception cref="System.Net.Sockets.SocketException">Low level transport exception occured.</exception>
        /// <exception cref="ShiftIt.Http.TimeoutException">A timeout occured during transfer.</exception>
        public byte[] CrossLoad(IHttpRequest loadRequest, IHttpRequestBuilder storeRequest, string hashAlgorithmName)
        {
            var hash = HashAlgorithm.Create(hashAlgorithmName);

            using (var getTx = RequestOrThrow(loadRequest))
            {
                var hashStream = new HashingReadStream(getTx.RawBodyStream, hash);
                var storeRq    = storeRequest.Build(hashStream, getTx.BodyReader.ExpectedLength);
                using (RequestOrThrow(storeRq))                 // dispose of the response stream
                {
                }
                return(hashStream.GetHashValue());
            }
        }
        public static async Task <TResponse> DispatchAsync <TResponse>(this IHttpRequestBuilder httpRequestBuilder, IExceptionParser exceptionParser = null)
        {
            Enforce.That.ExceptionParserHasBeenInitialized(ref exceptionParser);

            var httpRequest = httpRequestBuilder.Build();
            var response    = await _client.SendAsync(httpRequest);

            var responseContent = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                exceptionParser.ParseException(responseContent);
            }

            return(JsonConvert.DeserializeObject <TResponse>(responseContent));
        }
Ejemplo n.º 7
0
        public async Task <Stream> SendRequest(IHttpRequestBuilder requestBuilder, CancellationToken ct = default)
        {
            var request = requestBuilder.Build();

            var httpResponse = await _httpClient.SendAsync(request, ct);

            if (_unauthorizedAccessResponseCodes?.Any(code => code == httpResponse.StatusCode) == true)
            {
                throw new UnauthorizedAccessException("Unauthorized request");
            }

            var responseStream = await httpResponse.Content
                                 .ReadAsStreamAsync()
                                 .ConfigureAwait(false);

            return(responseStream);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Request data from one resource and provide to another, calculating a
        /// hash of the cross-loaded data. This is done in a memory-efficient manner.
        /// If either source or destination return a non-success result (including redirects)
        /// an exception will be thrown
        /// </summary>
        /// <param name="loadRequest">Request that will provide body data (should be a GET or POST)</param>
        /// <param name="storeRequest">Request that will accept body data (should be a PUT or POST)</param>
        /// <param name="hashAlgorithmName">Name of hash algorithm to use (should be a name supported by <see cref="System.Security.Cryptography.HashAlgorithm"/>)</param>
        /// <exception cref="ShiftIt.Http.HttpTransferException">Response to the request was not a succesful HTTP status.</exception>
        /// <exception cref="System.Net.Sockets.SocketException">Low level transport exception occured.</exception>
        /// <exception cref="ShiftIt.Http.TimeoutException">A timeout occured during transfer.</exception>
        public byte[] CrossLoad(IHttpRequest loadRequest, IHttpRequestBuilder storeRequest, string hashAlgorithmName)
        {
            if (loadRequest == null)
            {
                throw new ArgumentNullException(nameof(loadRequest));
            }
            if (storeRequest == null)
            {
                throw new ArgumentNullException(nameof(storeRequest));
            }

            var hash = HashAlgorithm.Create(hashAlgorithmName ?? "SHA256");

            using (var getTx = RequestOrThrow(loadRequest))
            {
                var hashStream = new HashingReadStream(getTx.RawBodyStream, hash);
                var storeRq    = storeRequest.Build(hashStream, getTx.BodyReader.ExpectedLength);
                using (RequestOrThrow(storeRq))                 // dispose of the response stream
                {
                }
                return(hashStream.GetHashValue() ?? throw new Exception("System hash function returned a null result"));
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Request data from one resource and provide to another.
        /// This is done in a memory-efficient manner.
        /// </summary>
        /// <param name="loadRequest">Request that will provide body data (should be a GET or POST)</param>
        /// <param name="storeRequest">Request that will accept body data (should be a PUT or POST)</param>
        /// <param name="sendProgress">Optional: action that is updated with bytes transferred. No guarantees as to when.</param>
        /// <exception cref="ShiftIt.Http.HttpTransferException">Response to the request was not a succesful HTTP status.</exception>
        /// <exception cref="System.Net.Sockets.SocketException">Low level transport exception occured.</exception>
        /// <exception cref="ShiftIt.Http.TimeoutException">A timeout occured during transfer.</exception>
        public void CrossLoad(IHttpRequest loadRequest, IHttpRequestBuilder storeRequest, Action <long> sendProgress = null)
        {
            if (loadRequest == null)
            {
                throw new ArgumentNullException(nameof(loadRequest));
            }
            if (storeRequest == null)
            {
                throw new ArgumentNullException(nameof(storeRequest));
            }

            using (var getTx = RequestOrThrow(loadRequest))             // get source
            {
                var rawStream = getTx.RawBodyStream;
                if (rawStream == null)
                {
                    throw new Exception("Body stream was null in request to " + loadRequest.Target);
                }
                var storeRq = storeRequest.Build(rawStream, getTx.BodyReader.ExpectedLength);
                using (RequestOrThrow(storeRq)) // dispose of the response stream
                {
                }
            }
        }
Ejemplo n.º 10
0
 public void unsecured_requests_have_secured_equal_to_false()
 {
     Assert.That(_unsecured.Build().Secure, Is.False);
 }
Ejemplo n.º 11
0
        public void get_verb_is_given()
        {
            Assert.That(_subject.Build().RequestHead, Is.StringStarting("GET "));

            Assert.That(_subject.Build().Verb, Is.EqualTo("GET"));
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Request data from one resource and provide to another, calculating a 
 /// hash of the cross-loaded data. This is done in a memory-efficient manner.
 /// If either source or destination return a non-success result (including redirects)
 /// an exception will be thrown
 /// </summary>
 /// <param name="loadRequest">Request that will provide body data (should be a GET or POST)</param>
 /// <param name="storeRequest">Request that will accept body data (should be a PUT or POST)</param>
 /// <param name="hashAlgorithmName">Name of hash algorithm to use (should be a name supported by <see cref="System.Security.Cryptography.HashAlgorithm"/>)</param>
 /// <exception cref="ShiftIt.Http.HttpTransferException">Response to the request was not a succesful HTTP status.</exception>
 /// <exception cref="System.Net.Sockets.SocketException">Low level transport exception occured.</exception>
 /// <exception cref="ShiftIt.Http.TimeoutException">A timeout occured during transfer.</exception>
 public byte[] CrossLoad(IHttpRequest loadRequest, IHttpRequestBuilder storeRequest, string hashAlgorithmName)
 {
     var hash = HashAlgorithm.Create(hashAlgorithmName);
     using (var getTx = RequestOrThrow(loadRequest))
     {
         var hashStream = new HashingReadStream(getTx.RawBodyStream, hash);
         var storeRq = storeRequest.Build(hashStream, getTx.BodyReader.ExpectedLength);
         using (RequestOrThrow(storeRq)) // dispose of the response stream
         {
         }
         return hashStream.GetHashValue();
     }
 }
Ejemplo n.º 13
0
 public void default_headers_are_written_correctly()
 {
     Assert.That(_subject.Build().RequestHead.Lines(), Contains.Item("Host: www.example.com:80"));
     Assert.That(_subject.Build().RequestHead.Lines(), Contains.Item("Accept: */*"));
 }
Ejemplo n.º 14
0
 public void correct_basic_authentication_string_is_used()
 {
     Assert.That(_subject.Build().RequestHead().Lines(),
                 Contains.Item("Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="));
 }
Ejemplo n.º 15
0
 public void get_verb_is_given()
 {
     Assert.That(_subject.Build().RequestHead, Is.StringStarting(_verb + " "));
 }
Ejemplo n.º 16
0
 public void secured_requests_have_secured_equal_to_true()
 {
     Assert.That(_secured.Build().Secure, Is.True);
 }
Ejemplo n.º 17
0
 public void post_verb_is_given()
 {
     Assert.That(_subject.Build().RequestHead(), Is.StringStarting("POST "));
 }
Ejemplo n.º 18
0
        public static Task <HttpResponseMessage> DispatchAsync(this IHttpRequestBuilder httpRequestBuilder)
        {
            var httpRequest = httpRequestBuilder.Build();

            return(Client.SendAsync(httpRequest));
        }