mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 22:08:17 +08:00
moved logic to request mapper and public setter gone
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
namespace Ocelot.Request.Mapper
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Responses;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public interface IRequestMapper
|
||||
{
|
||||
Task<Response<HttpRequestMessage>> Map(HttpRequest request);
|
||||
Task<Response<HttpRequestMessage>> Map(HttpRequest request, DownstreamReRoute downstreamReRoute);
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Responses;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -15,14 +16,14 @@
|
||||
{
|
||||
private readonly string[] _unsupportedHeaders = { "host" };
|
||||
|
||||
public async Task<Response<HttpRequestMessage>> Map(HttpRequest request)
|
||||
public async Task<Response<HttpRequestMessage>> Map(HttpRequest request, DownstreamReRoute downstreamReRoute)
|
||||
{
|
||||
try
|
||||
{
|
||||
var requestMessage = new HttpRequestMessage()
|
||||
{
|
||||
Content = await MapContent(request),
|
||||
Method = MapMethod(request),
|
||||
Method = MapMethod(request, downstreamReRoute),
|
||||
RequestUri = MapUri(request)
|
||||
};
|
||||
|
||||
@ -71,8 +72,13 @@
|
||||
}
|
||||
}
|
||||
|
||||
private HttpMethod MapMethod(HttpRequest request)
|
||||
private HttpMethod MapMethod(HttpRequest request, DownstreamReRoute downstreamReRoute)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(downstreamReRoute?.DownstreamHttpMethod))
|
||||
{
|
||||
return new HttpMethod(downstreamReRoute.DownstreamHttpMethod);
|
||||
}
|
||||
|
||||
return new HttpMethod(request.Method);
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ namespace Ocelot.Request.Middleware
|
||||
|
||||
public HttpRequestHeaders Headers { get; }
|
||||
|
||||
public string Method { get; set; }
|
||||
public string Method { get; }
|
||||
|
||||
public string OriginalString { get; }
|
||||
|
||||
|
@ -24,7 +24,7 @@ namespace Ocelot.Request.Middleware
|
||||
|
||||
public async Task Invoke(DownstreamContext context)
|
||||
{
|
||||
var downstreamRequest = await _requestMapper.Map(context.HttpContext.Request);
|
||||
var downstreamRequest = await _requestMapper.Map(context.HttpContext.Request, context.DownstreamReRoute);
|
||||
|
||||
if (downstreamRequest.IsError)
|
||||
{
|
||||
@ -34,11 +34,6 @@ namespace Ocelot.Request.Middleware
|
||||
|
||||
context.DownstreamRequest = _creator.Create(downstreamRequest.Data);
|
||||
|
||||
if (!string.IsNullOrEmpty(context.DownstreamReRoute?.DownstreamHttpMethod))
|
||||
{
|
||||
context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod;
|
||||
}
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user