From d186f0e1b62ccf3eb47372557b622ced1085afa7 Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Wed, 25 Jul 2018 08:34:58 +0100 Subject: [PATCH] #490 use byte content not stream (#498) --- src/Ocelot/Request/Mapper/IRequestMapper.cs | 2 +- src/Ocelot/Request/Mapper/RequestMapper.cs | 21 +++++++++++++++---- .../DownstreamRequestInitialiserMiddleware.cs | 2 +- ...streamRequestInitialiserMiddlewareTests.cs | 4 ++-- .../Request/Mapper/RequestMapperTests.cs | 5 +++-- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Ocelot/Request/Mapper/IRequestMapper.cs b/src/Ocelot/Request/Mapper/IRequestMapper.cs index 9d34cbb22..941a24f7d 100644 --- a/src/Ocelot/Request/Mapper/IRequestMapper.cs +++ b/src/Ocelot/Request/Mapper/IRequestMapper.cs @@ -7,6 +7,6 @@ public interface IRequestMapper { - Response Map(HttpRequest request); + Task> Map(HttpRequest request); } } diff --git a/src/Ocelot/Request/Mapper/RequestMapper.cs b/src/Ocelot/Request/Mapper/RequestMapper.cs index f417cc016..d0c458076 100644 --- a/src/Ocelot/Request/Mapper/RequestMapper.cs +++ b/src/Ocelot/Request/Mapper/RequestMapper.cs @@ -15,13 +15,13 @@ public class RequestMapper : IRequestMapper { private readonly string[] _unsupportedHeaders = { "host" }; - public Response Map(HttpRequest request) + public async Task> Map(HttpRequest request) { try { var requestMessage = new HttpRequestMessage() { - Content = MapContent(request), + Content = await MapContent(request), Method = MapMethod(request), RequestUri = MapUri(request) }; @@ -36,14 +36,15 @@ public Response Map(HttpRequest request) } } - private HttpContent MapContent(HttpRequest request) + private async Task MapContent(HttpRequest request) { if (request.Body == null || (request.Body.CanSeek && request.Body.Length <= 0)) { return null; } - var content = new StreamContent(request.Body); + // Never change this to StreamContent again, I forgot it doesnt work in #464. + var content = new ByteArrayContent(await ToByteArray(request.Body)); if(!string.IsNullOrEmpty(request.ContentType)) { @@ -95,5 +96,17 @@ private bool IsSupportedHeader(KeyValuePair header) { return !_unsupportedHeaders.Contains(header.Key.ToLower()); } + + private async Task ToByteArray(Stream stream) + { + using(stream) + { + using (var memStream = new MemoryStream()) + { + await stream.CopyToAsync(memStream); + return memStream.ToArray(); + } + } + } } } diff --git a/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs b/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs index 2bdbec9f8..4f3151d97 100644 --- a/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs +++ b/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs @@ -27,7 +27,7 @@ public DownstreamRequestInitialiserMiddleware(OcelotRequestDelegate next, public async Task Invoke(DownstreamContext context) { - var downstreamRequest = _requestMapper.Map(context.HttpContext.Request); + var downstreamRequest = await _requestMapper.Map(context.HttpContext.Request); if (downstreamRequest.IsError) { diff --git a/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs b/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs index 9c5fb9777..d571a8b72 100644 --- a/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs @@ -95,7 +95,7 @@ private void GivenTheMapperWillReturnAMappedRequest() _requestMapper .Setup(rm => rm.Map(It.IsAny())) - .Returns(_mappedRequest); + .ReturnsAsync(_mappedRequest); } private void GivenTheMapperWillReturnAnError() @@ -104,7 +104,7 @@ private void GivenTheMapperWillReturnAnError() _requestMapper .Setup(rm => rm.Map(It.IsAny())) - .Returns(_mappedRequest); + .ReturnsAsync(_mappedRequest); } private void WhenTheMiddlewareIsInvoked() diff --git a/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs b/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs index 17e35b644..9be76e3fd 100644 --- a/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs +++ b/test/Ocelot.UnitTests/Request/Mapper/RequestMapperTests.cs @@ -16,6 +16,7 @@ using System.IO; using System.Text; using System.Security.Cryptography; + using System.Threading.Tasks; public class RequestMapperTests { @@ -378,9 +379,9 @@ private void GivenTheInputRequestHasNullContent() _inputRequest.Body = null; } - private void WhenMapped() + private async Task WhenMapped() { - _mappedRequest = _requestMapper.Map(_inputRequest); + _mappedRequest = await _requestMapper.Map(_inputRequest); } private void ThenNoErrorIsReturned()