1- using Microsoft . AspNetCore . Http ;
1+ using Microsoft . AspNetCore . Http ;
22using Microsoft . Extensions . Primitives ;
33using Newtonsoft . Json . Linq ;
4- using Ocelot . Configuration ;
4+ using Ocelot . Configuration ;
55using Ocelot . Configuration . File ;
6- using Ocelot . DownstreamRouteFinder . UrlMatcher ;
7- using Ocelot . Logging ;
8- using Ocelot . Middleware ;
6+ using Ocelot . DownstreamRouteFinder . UrlMatcher ;
7+ using Ocelot . Logging ;
8+ using Ocelot . Middleware ;
99using System . Collections ;
1010using Route = Ocelot . Configuration . Route ;
11-
11+
1212namespace Ocelot . Multiplexer ;
1313
1414public class MultiplexingMiddleware : OcelotMiddleware
1515{
1616 private readonly RequestDelegate _next ;
1717 private readonly IResponseAggregatorFactory _factory ;
1818 private const string RequestIdString = "RequestId" ;
19-
19+
2020 public MultiplexingMiddleware ( RequestDelegate next ,
2121 IOcelotLoggerFactory loggerFactory ,
2222 IResponseAggregatorFactory factory )
@@ -25,7 +25,7 @@ public MultiplexingMiddleware(RequestDelegate next,
2525 _factory = factory ;
2626 _next = next ;
2727 }
28-
28+
2929 public async Task Invoke ( HttpContext httpContext )
3030 {
3131 var downstreamRouteHolder = httpContext . Items . DownstreamRouteHolder ( ) ;
@@ -38,37 +38,37 @@ public async Task Invoke(HttpContext httpContext)
3838 await ProcessSingleRouteAsync ( httpContext , downstreamRoutes [ 0 ] ) ;
3939 return ;
4040 }
41-
41+
4242 // Case 2: if no downstream routes
4343 if ( downstreamRoutes . Count == 0 )
4444 {
4545 return ;
4646 }
47-
47+
4848 // Case 3: if multiple downstream routes
4949 var routeKeysConfigs = route . DownstreamRouteConfig ;
5050 if ( routeKeysConfigs == null || routeKeysConfigs . Count == 0 )
5151 {
5252 await ProcessRoutesAsync ( httpContext , route ) ;
5353 return ;
5454 }
55-
55+
5656 // Case 4: if multiple downstream routes with route keys
5757 var mainResponseContext = await ProcessMainRouteAsync ( httpContext , downstreamRoutes [ 0 ] ) ;
5858 if ( mainResponseContext == null )
5959 {
6060 return ;
6161 }
62-
62+
6363 var responsesContexts = await ProcessRoutesWithRouteKeysAsync ( httpContext , downstreamRoutes , routeKeysConfigs , mainResponseContext ) ;
6464 if ( responsesContexts . Length == 0 )
6565 {
6666 return ;
6767 }
68-
68+
6969 await MapResponsesAsync ( httpContext , route , mainResponseContext , responsesContexts ) ;
7070 }
71-
71+
7272 /// <summary>
7373 /// Helper method to determine if only the first downstream route should be processed.
7474 /// It is the case if the request is a websocket request or if there is only one downstream route.
@@ -78,7 +78,7 @@ public async Task Invoke(HttpContext httpContext)
7878 /// <returns>True if only the first downstream route should be processed.</returns>
7979 private static bool ShouldProcessSingleRoute ( HttpContext context , ICollection routes )
8080 => context . WebSockets . IsWebSocketRequest || routes . Count == 1 ;
81-
81+
8282 /// <summary>
8383 /// Processing a single downstream route (no route keys).
8484 /// In that case, no need to make copies of the http context.
@@ -89,9 +89,10 @@ private static bool ShouldProcessSingleRoute(HttpContext context, ICollection ro
8989 protected virtual Task ProcessSingleRouteAsync ( HttpContext context , DownstreamRoute route )
9090 {
9191 context . Items . UpsertDownstreamRoute ( route ) ;
92+ context . Items . SetAuthChallenge ( /*finished*/ context . Items . AuthChallenge ( ) ) ;
9293 return _next . Invoke ( context ) ;
9394 }
94-
95+
9596 /// <summary>
9697 /// Processing the downstream routes (no route keys).
9798 /// </summary>
@@ -105,7 +106,7 @@ private async Task ProcessRoutesAsync(HttpContext context, Route route)
105106 var contexts = await Task . WhenAll ( tasks ) ;
106107 await MapAsync ( context , route , new ( contexts ) ) ;
107108 }
108-
109+
109110 /// <summary>
110111 /// When using route keys, the first route is the main route and the rest are additional routes.
111112 /// Since we need to break if the main route response is null, we must process the main route first.
@@ -119,7 +120,7 @@ private async Task<HttpContext> ProcessMainRouteAsync(HttpContext context, Downs
119120 await _next . Invoke ( context ) ;
120121 return context ;
121122 }
122-
123+
123124 /// <summary>
124125 /// Processing the downstream routes with route keys except the main route that has already been processed.
125126 /// </summary>
@@ -133,7 +134,7 @@ protected virtual async Task<HttpContext[]> ProcessRoutesWithRouteKeysAsync(Http
133134 var processing = new List < Task < HttpContext > > ( ) ;
134135 var content = await mainResponse . Items . DownstreamResponse ( ) . Content . ReadAsStringAsync ( ) ;
135136 var jObject = JToken . Parse ( content ) ;
136-
137+
137138 foreach ( var downstreamRoute in routes . Skip ( 1 ) )
138139 {
139140 var matchAdvancedAgg = routeKeysConfigs . FirstOrDefault ( q => q . RouteKey == downstreamRoute . Key ) ;
@@ -142,13 +143,13 @@ protected virtual async Task<HttpContext[]> ProcessRoutesWithRouteKeysAsync(Http
142143 processing . AddRange ( ProcessRouteWithComplexAggregation ( matchAdvancedAgg , jObject , context , downstreamRoute ) ) ;
143144 continue ;
144145 }
145-
146+
146147 processing . Add ( ProcessRouteAsync ( context , downstreamRoute ) ) ;
147148 }
148-
149+
149150 return await Task . WhenAll ( processing ) ;
150151 }
151-
152+
152153 /// <summary>
153154 /// Mapping responses.
154155 /// </summary>
@@ -158,7 +159,7 @@ private Task MapResponsesAsync(HttpContext context, Route route, HttpContext mai
158159 contexts . AddRange ( responsesContexts ) ;
159160 return MapAsync ( context , route , contexts ) ;
160161 }
161-
162+
162163 /// <summary>
163164 /// Processing a route with aggregation.
164165 /// </summary>
@@ -173,7 +174,7 @@ private IEnumerable<Task<HttpContext>> ProcessRouteWithComplexAggregation(Aggreg
173174 tPnv . Add ( new PlaceholderNameAndValue ( '{' + matchAdvancedAgg . Parameter + '}' , value ) ) ;
174175 processing . Add ( ProcessRouteAsync ( httpContext , downstreamRoute , tPnv ) ) ;
175176 }
176-
177+
177178 return processing ;
178179 }
179180
@@ -186,11 +187,11 @@ private async Task<HttpContext> ProcessRouteAsync(HttpContext sourceContext, Dow
186187 var newHttpContext = await CreateThreadContextAsync ( sourceContext , route ) ;
187188 CopyItemsToNewContext ( newHttpContext , sourceContext , placeholders ) ;
188189 newHttpContext . Items . UpsertDownstreamRoute ( route ) ;
189-
190+
190191 await _next . Invoke ( newHttpContext ) ;
191192 return newHttpContext ;
192193 }
193-
194+
194195 /// <summary>
195196 /// Copying some needed parameters to the Http context items.
196197 /// </summary>
@@ -247,7 +248,7 @@ protected virtual async Task<HttpContext> CreateThreadContextAsync(HttpContext s
247248 target . Response . RegisterForDisposeAsync ( bodyStream ) ; // manage Stream lifetime by HttpResponse object
248249 return target ;
249250 }
250-
251+
251252 protected virtual Task MapAsync ( HttpContext httpContext , Route route , List < HttpContext > contexts )
252253 {
253254 if ( route . DownstreamRoute . Count == 1 )
@@ -282,4 +283,4 @@ protected virtual async Task<Stream> CloneRequestBodyAsync(HttpRequest request,
282283
283284 return targetBuffer ;
284285 }
285- }
286+ }
0 commit comments