/// <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 { } } }
/// <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 { } } }
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)); }
/// <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)); }
/// <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)); }
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); }
/// <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")); } }
/// <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 { } } }
public void unsecured_requests_have_secured_equal_to_false() { Assert.That(_unsecured.Build().Secure, Is.False); }
public void get_verb_is_given() { Assert.That(_subject.Build().RequestHead, Is.StringStarting("GET ")); Assert.That(_subject.Build().Verb, Is.EqualTo("GET")); }
/// <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 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: */*")); }
public void correct_basic_authentication_string_is_used() { Assert.That(_subject.Build().RequestHead().Lines(), Contains.Item("Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==")); }
public void get_verb_is_given() { Assert.That(_subject.Build().RequestHead, Is.StringStarting(_verb + " ")); }
public void secured_requests_have_secured_equal_to_true() { Assert.That(_secured.Build().Secure, Is.True); }
public void post_verb_is_given() { Assert.That(_subject.Build().RequestHead(), Is.StringStarting("POST ")); }
public static Task <HttpResponseMessage> DispatchAsync(this IHttpRequestBuilder httpRequestBuilder) { var httpRequest = httpRequestBuilder.Build(); return(Client.SendAsync(httpRequest)); }