Commit 1e2944d5 authored by yangxiaodong's avatar yangxiaodong

remove unused files.

parent c83fa66a
using System;
using Microsoft.AspNetCore.Http;
namespace DotNetCore.CAP.Dashboard.GatewayProxy
{
public class HttpDataRepository : IRequestScopedDataRepository
{
private readonly IHttpContextAccessor _httpContextAccessor;
public HttpDataRepository(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public void Add<T>(string key, T value)
{
_httpContextAccessor.HttpContext.Items.Add(key, value);
}
public T Get<T>(string key)
{
object obj;
if (_httpContextAccessor.HttpContext.Items.TryGetValue(key, out obj))
{
return (T)obj;
}
throw new Exception($"Unable to find data for key: {key}");
}
}
}
namespace DotNetCore.CAP.Dashboard.GatewayProxy
{
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
public interface IRequestMapper
{
Task<HttpRequestMessage> Map(HttpRequest request);
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
namespace DotNetCore.CAP.Dashboard.GatewayProxy
{
public class RequestMapper : IRequestMapper
{
private readonly string[] _unsupportedHeaders = { "host" };
private const string SchemeDelimiter = "://";
public async Task<HttpRequestMessage> Map(HttpRequest request)
{
try
{
var requestMessage = new HttpRequestMessage()
{
Content = await MapContent(request),
Method = MapMethod(request),
RequestUri = MapUri(request)
};
MapHeaders(request, requestMessage);
return requestMessage;
}
catch (Exception ex)
{
throw new Exception($"Error when parsing incoming request, exception: {ex.Message}");
}
}
private async Task<HttpContent> MapContent(HttpRequest request)
{
if (request.Body == null)
{
return null;
}
var content = new ByteArrayContent(await ToByteArray(request.Body));
content.Headers.TryAddWithoutValidation("Content-Type", new[] {request.ContentType});
return content;
}
private HttpMethod MapMethod(HttpRequest request)
{
return new HttpMethod(request.Method);
}
private Uri MapUri(HttpRequest request)
{
return new Uri(GetEncodedUrl(request));
}
private void MapHeaders(HttpRequest request, HttpRequestMessage requestMessage)
{
foreach (var header in request.Headers)
{
//todo get rid of if..
if (IsSupportedHeader(header))
{
requestMessage.Headers.TryAddWithoutValidation(header.Key, header.Value.ToArray());
}
}
}
private async Task<byte[]> ToByteArray(Stream stream)
{
using (stream)
{
using (var memStream = new MemoryStream())
{
await stream.CopyToAsync(memStream);
return memStream.ToArray();
}
}
}
private bool IsSupportedHeader(KeyValuePair<string, StringValues> header)
{
return !_unsupportedHeaders.Contains(header.Key.ToLower());
}
/// <summary>
/// Combines the given URI components into a string that is properly encoded for use in HTTP headers.
/// Note that unicode in the HostString will be encoded as punycode.
/// </summary>
/// <param name="scheme">http, https, etc.</param>
/// <param name="host">The host portion of the uri normally included in the Host header. This may include the port.</param>
/// <param name="pathBase">The first portion of the request path associated with application root.</param>
/// <param name="path">The portion of the request path that identifies the requested resource.</param>
/// <param name="query">The query, if any.</param>
/// <param name="fragment">The fragment, if any.</param>
/// <returns></returns>
public string BuildAbsolute(
string scheme,
HostString host,
PathString pathBase = new PathString(),
PathString path = new PathString(),
QueryString query = new QueryString(),
FragmentString fragment = new FragmentString())
{
if (scheme == null)
{
throw new ArgumentNullException(nameof(scheme));
}
var combinedPath = (pathBase.HasValue || path.HasValue) ? (pathBase + path).ToString() : "/";
var encodedHost = host.ToString();
var encodedQuery = query.ToString();
var encodedFragment = fragment.ToString();
// PERF: Calculate string length to allocate correct buffer size for StringBuilder.
var length = scheme.Length + SchemeDelimiter.Length + encodedHost.Length
+ combinedPath.Length + encodedQuery.Length + encodedFragment.Length;
return new StringBuilder(length)
.Append(scheme)
.Append(SchemeDelimiter)
.Append(encodedHost)
.Append(combinedPath)
.Append(encodedQuery)
.Append(encodedFragment)
.ToString();
}
/// <summary>
/// Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers
/// and other HTTP operations.
/// </summary>
/// <param name="request">The request to assemble the uri pieces from.</param>
/// <returns></returns>
public string GetEncodedUrl(HttpRequest request)
{
return BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
}
}
}
namespace DotNetCore.CAP.Dashboard.GatewayProxy
{
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
public class DownstreamRequestInitialiserMiddleware : GatewayProxyMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
private readonly IRequestMapper _requestMapper;
public DownstreamRequestInitialiserMiddleware(RequestDelegate next,
ILoggerFactory loggerFactory,
IRequestScopedDataRepository requestScopedDataRepository,
IRequestMapper requestMapper)
:base(requestScopedDataRepository)
{
_next = next;
_logger = loggerFactory.CreateLogger<DownstreamRequestInitialiserMiddleware>();
_requestMapper = requestMapper;
}
public async Task Invoke(HttpContext context)
{
_logger.LogDebug("started calling request builder middleware");
var downstreamRequest = await _requestMapper.Map(context.Request);
SetDownstreamRequest(downstreamRequest);
_logger.LogDebug("calling next middleware");
await _next.Invoke(context);
_logger.LogDebug("succesfully called next middleware");
}
}
}
\ No newline at end of file
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace DotNetCore.CAP.Dashboard.GatewayProxy
{
public class HttpRequestBuilderMiddleware : GatewayProxyMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger _logger;
public HttpRequestBuilderMiddleware(RequestDelegate next,
ILoggerFactory loggerFactory,
IRequestScopedDataRepository requestScopedDataRepository )
:base(requestScopedDataRepository)
{
_next = next;
_logger = loggerFactory.CreateLogger<HttpRequestBuilderMiddleware>();
}
public async Task Invoke(HttpContext context)
{
_logger.LogDebug("started calling request builder middleware");
_logger.LogDebug("setting upstream request");
SetUpstreamRequestForThisRequest(DownstreamRequest);
_logger.LogDebug("calling next middleware");
await _next.Invoke(context);
_logger.LogDebug("succesfully called next middleware");
}
}
}
\ No newline at end of file
using Microsoft.AspNetCore.Builder;
namespace DotNetCore.CAP.Dashboard.GatewayProxy.Request.Middleware
{
public static class HttpRequestBuilderMiddlewareExtensions
{
public static IApplicationBuilder UseHttpRequestBuilderMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<HttpRequestBuilderMiddleware>();
}
public static IApplicationBuilder UseDownstreamRequestInitialiser(this IApplicationBuilder builder)
{
return builder.UseMiddleware<DownstreamRequestInitialiserMiddleware>();
}
}
}
\ No newline at end of file
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
namespace DotNetCore.CAP.Dashboard.GatewayProxy.Requester.Middleware
{
public class HttpRequesterMiddleware : GatewayProxyMiddleware
{
private readonly RequestDelegate _next;
private readonly IHttpRequester _requester;
private readonly ILogger _logger;
public HttpRequesterMiddleware(RequestDelegate next,
ILoggerFactory loggerFactory,
IHttpRequester requester,
IRequestScopedDataRepository requestScopedDataRepository)
:base(requestScopedDataRepository)
{
_next = next;
_requester = requester;
_logger = loggerFactory.CreateLogger<HttpRequesterMiddleware>();
}
public async Task Invoke(HttpContext context)
{
_logger.LogDebug("started calling requester middleware");
var response = await _requester.GetResponse(Request);
_logger.LogDebug("setting http response message");
SetHttpResponseMessageThisRequest(response);
_logger.LogDebug("returning to calling middleware");
}
}
}
\ No newline at end of file
using Microsoft.AspNetCore.Builder;
namespace DotNetCore.CAP.Dashboard.GatewayProxy.Requester.Middleware
{
public static class HttpRequesterMiddlewareExtensions
{
public static IApplicationBuilder UseHttpRequesterMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<HttpRequesterMiddleware>();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment