Feature/proxy reason phrase (#618)

* #599 started work to proxy reason phrase

* #599 test for aggregator
This commit is contained in:
Tom Pallister
2018-09-12 19:48:56 +01:00
committed by GitHub
parent 0b9ff92549
commit 669ece07b2
13 changed files with 446 additions and 335 deletions

View File

@ -1,30 +1,33 @@
using System.Collections.Generic;
using System.Net;
namespace Ocelot.Cache
{
public class CachedResponse
{
public CachedResponse(
HttpStatusCode statusCode,
Dictionary<string, IEnumerable<string>> headers,
string body,
Dictionary<string, IEnumerable<string>> contentHeaders
)
{
StatusCode = statusCode;
Headers = headers ?? new Dictionary<string, IEnumerable<string>>();
ContentHeaders = contentHeaders ?? new Dictionary<string, IEnumerable<string>>();
Body = body ?? "";
}
public HttpStatusCode StatusCode { get; private set; }
public Dictionary<string, IEnumerable<string>> Headers { get; private set; }
public Dictionary<string, IEnumerable<string>> ContentHeaders { get; private set; }
public string Body { get; private set; }
}
}
using System.Collections.Generic;
using System.Net;
namespace Ocelot.Cache
{
public class CachedResponse
{
public CachedResponse(
HttpStatusCode statusCode,
Dictionary<string, IEnumerable<string>> headers,
string body,
Dictionary<string, IEnumerable<string>> contentHeaders,
string reasonPhrase
)
{
StatusCode = statusCode;
Headers = headers ?? new Dictionary<string, IEnumerable<string>>();
ContentHeaders = contentHeaders ?? new Dictionary<string, IEnumerable<string>>();
Body = body ?? "";
ReasonPhrase = reasonPhrase;
}
public HttpStatusCode StatusCode { get; private set; }
public Dictionary<string, IEnumerable<string>> Headers { get; private set; }
public Dictionary<string, IEnumerable<string>> ContentHeaders { get; private set; }
public string Body { get; private set; }
public string ReasonPhrase { get; private set; }
}
}

View File

@ -87,7 +87,7 @@
streamContent.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
return new DownstreamResponse(streamContent, cached.StatusCode, cached.Headers.ToList());
return new DownstreamResponse(streamContent, cached.StatusCode, cached.Headers.ToList(), cached.ReasonPhrase);
}
internal async Task<CachedResponse> CreateCachedResponse(DownstreamResponse response)
@ -109,7 +109,7 @@
var contentHeaders = response?.Content?.Headers.ToDictionary(v => v.Key, v => v.Value);
var cached = new CachedResponse(statusCode, headers, body, contentHeaders);
var cached = new CachedResponse(statusCode, headers, body, contentHeaders, response.ReasonPhrase);
return cached;
}
}

View File

@ -7,25 +7,27 @@ namespace Ocelot.Middleware
{
public class DownstreamResponse
{
public DownstreamResponse(HttpContent content, HttpStatusCode statusCode, List<Header> headers)
public DownstreamResponse(HttpContent content, HttpStatusCode statusCode, List<Header> headers, string reasonPhrase)
{
Content = content;
StatusCode = statusCode;
Headers = headers ?? new List<Header>();
ReasonPhrase = reasonPhrase;
}
public DownstreamResponse(HttpResponseMessage response)
:this(response.Content, response.StatusCode, response.Headers.Select(x => new Header(x.Key, x.Value)).ToList())
:this(response.Content, response.StatusCode, response.Headers.Select(x => new Header(x.Key, x.Value)).ToList(), response.ReasonPhrase)
{
}
public DownstreamResponse(HttpContent content, HttpStatusCode statusCode, IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers)
:this(content, statusCode, headers.Select(x => new Header(x.Key, x.Value)).ToList())
public DownstreamResponse(HttpContent content, HttpStatusCode statusCode, IEnumerable<KeyValuePair<string, IEnumerable<string>>> headers, string reasonPhrase)
:this(content, statusCode, headers.Select(x => new Header(x.Key, x.Value)).ToList(), reasonPhrase)
{
}
public HttpContent Content { get; }
public HttpStatusCode StatusCode { get; }
public List<Header> Headers { get; }
public string ReasonPhrase {get;}
}
}

View File

@ -46,7 +46,7 @@ namespace Ocelot.Middleware.Multiplexer
Headers = {ContentType = new MediaTypeHeaderValue("application/json")}
};
originalContext.DownstreamResponse = new DownstreamResponse(stringContent, HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>());
originalContext.DownstreamResponse = new DownstreamResponse(stringContent, HttpStatusCode.OK, new List<KeyValuePair<string, IEnumerable<string>>>(), "cannot return from aggregate..which reason phrase would you use?");
}
private static void MapAggregateError(DownstreamContext originalContext, List<DownstreamContext> downstreamContexts, int i)

View File

@ -3,6 +3,7 @@ using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.Primitives;
using Ocelot.Headers;
using Ocelot.Middleware;
@ -45,6 +46,8 @@ namespace Ocelot.Responder
context.Response.StatusCode = (int)response.StatusCode;
context.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = response.ReasonPhrase;
using(content)
{
if (response.StatusCode != HttpStatusCode.NotModified && context.Response.ContentLength != 0)