Home > Net >  Create a forward agent middleware a piece of code don't quite understand, expert help explain.
Create a forward agent middleware a piece of code don't quite understand, expert help explain.

Time:04-09

 public class ProxyMiddleware 
{
//private ProxyHttpClient _proxyHttpClient;
Private const string CDN_HEADER_NAME="cache-control";
NotForwardedHttpHeaders=new private static readonly string [] [] {" Connection ", "the Host"};////Http header files used in the two attributes in the following code, the two state attribute is what meaning, if there are no these two properties in a header file, represent what meaning?
Private readonly RequestDelegate _next;
Private readonly ILogger _logger;

Public ProxyMiddleware (
RequestDelegate next,
ILogger The logger

)
{
_next=next;
_logger=logger;
//_proxyHttpClient=proxyHttpClient;
}

///& lt; Summary>
///by middleware, intercept access, detection prefix, and forward
///& lt;/summary>
///& lt; Param name="context" & gt;
///& lt; Param name="urlRewriter & gt;"
///& lt; Returns>
Public async Task Invoke (HttpContext context, IUrlRewriter urlRewriter, ProxyHttpClient ProxyHttpClient)
{
Var targetUri=await urlRewriter. RewriteUri (context);

If (targetUri!=null)
{
Var requestMessage=GenerateProxifiedRequest (context, targetUri);
Await SendAsync (context, requestMessage proxyHttpClient);

return;
}

Await _next (context);
}

Private async Task SendAsync (HttpContext context, HttpRequestMessage requestMessage, ProxyHttpClient ProxyHttpClient)
{


Using (var responseMessage=await proxyHttpClient. Client. SendAsync (requestMessage, HttpCompletionOption ResponseHeadersRead, context, RequestAborted))
{
The context. The Response. StatusCode=(int) responseMessage. StatusCode;

Foreach (var header in responseMessage. Headers)
{
Context, the Response Headers [header. Key]=header. The Value. The ToArray ();
}

Foreach (var header in responseMessage content. Headers)
{
Context, the Response Headers [header. Key]=header. The Value. The ToArray ();
}

The context, the Response Headers. Remove (" transfer - the encoding ");

if (! The context, the Response Headers. Either ContainsKey (CDN_HEADER_NAME))
{
The context, the Response Headers. The Add (CDN_HEADER_NAME, "no - the cache, the no - the store");
}

Await responseMessage. Content. CopyToAsync (context. The Response. The Body);
}
}

Private static HttpRequestMessage GenerateProxifiedRequest (HttpContext context, Uri targetUri)
{
Var requestMessage=new HttpRequestMessage ();
CopyRequestContentAndHeaders (context, requestMessage);
RequestMessage. RequestUri=targetUri;
RequestMessage. Headers. Host=targetUri. Host;
RequestMessage. Method=GetMethod (context) Request) Method).
Return requestMessage;
}

Private static void CopyRequestContentAndHeaders (HttpContext context, HttpRequestMessage requestMessage)
{
Var requestMethod=context. Request. Method;
if (! HttpMethods. IsGet (requestMethod) & amp; &
! HttpMethods. IsHead (requestMethod) & amp; &
! HttpMethods. IsDelete (requestMethod) & amp; &
! HttpMethods. IsTrace (requestMethod))
{
Var streamContent=new streamContent (context. Request. Body);
RequestMessage. The Content=streamContent;
}

Foreach (var header in the context, the Request Headers)
{
if (! NotForwardedHttpHeaders. The Contains (header. Key))////Http header file these two properties (connection, the Host) state of the two attributes is what meaning, if there are no these two properties in a header file, represent what meaning?
{
If (header. The Key!="the user-agent")//////the judgment, what does that mean?
{
if (! RequestMessage. Headers. TryAddWithoutValidation (header. The Key, the header. The Value. ToArray ()) & amp; & RequestMessage. The Content!=null)
{
RequestMessage. The Content? . Headers. TryAddWithoutValidation (header. The Key, the header. The Value. ToArray ());
}
}
The else
{
String userAgent=header. The Value. The Count & gt; 0? (the header. The Value [0] + "" + context. TraceIdentifier) : string. The Empty;

if (! RequestMessage. Headers. TryAddWithoutValidation (header. The Key, the userAgent) & amp; & RequestMessage. The Content!=null)
{
RequestMessage. The Content? . Headers. TryAddWithoutValidation (header. The Key, the userAgent);////in the main file attributes are more the size attribute, the head of attributes and what meaning, please combined with code to help explain,
}
}

}
}
}

Private static HttpMethod GetMethod (string method)
{
If (HttpMethods IsDelete (method)) return HttpMethod. Delete;
If (HttpMethods IsGet (method)) return HttpMethod. Get;
If (HttpMethods IsHead (method)) return HttpMethod. Head;
If (HttpMethods IsOptions (method)) return HttpMethod. The Options;
If (HttpMethods IsPost (method)) return HttpMethod. Post;
If (HttpMethods IsPut (method)) return HttpMethod. Put;
If (HttpMethods IsTrace (method)) return HttpMethod. Trace;
Return new HttpMethod (method);
}

CodePudding user response:

NotForwardedHttpHeaders=new private static readonly string [] [] {" Connection ", "the Host"};////Http header files used in the two attributes in the following code, the two state attribute is what meaning, if there are no these two properties in a header file, represent what meaning?

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related