예제 #1
0
        private async Task <Response> SendAsync(Request request, TimeSpan?connectionTimeout, CancellationToken token)
        {
            try
            {
                using (var state = new DisposableState())
                {
                    state.Request = RequestMessageFactory.Create(request, token, log);
                    if (state.Request.Content is GenericContent content && socketTuner.CanTune)
                    {
                        state.Request.Content = new SocketTuningContent(content, socketTuner, log);
                    }

                    var handler = handlerProvider.Obtain(connectionTimeout);

                    state.Response = await SocketsHandlerInvoker.Invoke(handler, state.Request, token).ConfigureAwait(false);

                    var responseCode    = (ResponseCode)(int)state.Response.StatusCode;
                    var responseHeaders = ResponseHeadersConverter.Convert(state.Response);

                    if (request.Method == RequestMethods.Head)
                    {
                        return(new Response(responseCode, headers: responseHeaders));
                    }

                    var bodyReadResult = await bodyReader.ReadAsync(state.Response, token).ConfigureAwait(false);

                    if (bodyReadResult.ErrorCode.HasValue)
                    {
                        return(new Response(bodyReadResult.ErrorCode.Value));
                    }

                    if (bodyReadResult.Stream == null)
                    {
                        return(new Response(responseCode, bodyReadResult.Content, responseHeaders));
                    }

                    state.PreventNextDispose();

                    return(new Response(responseCode, null, responseHeaders, new DisposableBodyStream(bodyReadResult.Stream, state)));
                }
            }
            catch (Exception error)
            {
                var errorResponse = errorHandler.TryHandle(request, error, token, connectionTimeout);
                if (errorResponse == null)
                {
                    throw;
                }

                return(errorResponse);
            }
        }
예제 #2
0
        public void Create_When_Head_Or_Get_Should_Not_Add_Content(string method)
        {
            _request.Method = method;

            _underTest.Create(_request).Content.Should().BeNull();
        }
예제 #3
0
 private void Convert()
 {
     message = RequestMessageFactory.Create(request, CancellationToken.None, new ConsoleLog());
 }
예제 #4
0
 private void Convert()
 => message = RequestMessageFactory.Create(request, CancellationToken.None, new SynchronousConsoleLog());