Skip to content

Commit a39a06c

Browse files
committed
Fix the problem of data loss when using rpccontext in the Rest protocol
1 parent e18dd8f commit a39a06c

6 files changed

Lines changed: 94 additions & 9 deletions

File tree

src/Surging.Core/Surging.Core.KestrelHttpServer/HttpMessageListener.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public async Task OnReceived(IMessageSender sender, TransportMessage message)
4747

4848
public async Task OnReceived(IMessageSender sender,string messageId, HttpContext context, IEnumerable<IActionFilter> actionFilters)
4949
{
50-
var serviceRoute = context.Items["route"] as ServiceRoute;
51-
52-
var path = (context.Items["path"]
50+
var serviceRoute = RestContext.GetContext().GetAttachment("route") as ServiceRoute;
51+
RestContext.GetContext().RemoveContextParameters("route");
52+
var path = (RestContext.GetContext().GetAttachment("path")
5353
?? HttpUtility.UrlDecode(GetRoutePath(context.Request.Path.ToString()))) as string;
5454
if (serviceRoute == null)
5555
{
@@ -88,7 +88,7 @@ public async Task OnReceived(IMessageSender sender,string messageId, HttpContext
8888
httpMessage.Parameters.Add("form", collection);
8989
if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage },
9090
sender, messageId, actionFilters)) return;
91-
httpMessage.Attachments = RpcContext.GetContext().GetContextParameters();
91+
httpMessage.Attachments = RestContext.GetContext().GetContextParameters();
9292
await Received(sender, new TransportMessage(messageId,httpMessage));
9393
}
9494
else
@@ -102,14 +102,14 @@ public async Task OnReceived(IMessageSender sender,string messageId, HttpContext
102102
httpMessage.Parameters.Add(param.Key, param.Value);
103103
if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage },
104104
sender, messageId, actionFilters)) return;
105-
httpMessage.Attachments = RpcContext.GetContext().GetContextParameters();
105+
httpMessage.Attachments = RestContext.GetContext().GetContextParameters();
106106
await Received(sender, new TransportMessage(messageId,httpMessage));
107107
}
108108
else
109109
{
110110
if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage },
111111
sender, messageId, actionFilters)) return;
112-
httpMessage.Attachments = RpcContext.GetContext().GetContextParameters();
112+
httpMessage.Attachments = RestContext.GetContext().GetContextParameters();
113113
await Received(sender, new TransportMessage(messageId,httpMessage));
114114
}
115115
}
@@ -151,7 +151,7 @@ public async Task<bool> OnAuthorization(HttpContext context, HttpServerMessageSe
151151
var path = HttpUtility.UrlDecode(GetRoutePath(context.Request.Path.ToString()));
152152
var serviceRoute = await _serviceRouteProvider.GetRouteByPathRegex(path);
153153
if (serviceRoute == null) serviceRoute = await _serviceRouteProvider.GetLocalRouteByPathRegex(path);
154-
context.Items.Add("route", serviceRoute);
154+
RestContext.GetContext().SetAttachment("route", serviceRoute);
155155
var filterContext = new AuthorizationFilterContext
156156
{
157157
Path = path,
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
using System.Collections.Concurrent;
4+
using System.Collections.Generic;
5+
using System.Runtime.CompilerServices;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Surging.Core.CPlatform.Utilities;
8+
using System.Linq;
9+
10+
namespace Surging.Core.KestrelHttpServer.Internal
11+
{
12+
public class RestContext
13+
{
14+
15+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
16+
public void SetAttachment(string key, object value)
17+
{
18+
Check.NotNull(serviceProvider, "serviceProvider");
19+
var htpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
20+
htpContextAccessor.HttpContext.Items.Add(key,value);
21+
}
22+
23+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24+
public object GetAttachment(string key)
25+
{
26+
Check.NotNull(serviceProvider, "serviceProvider");
27+
var htpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
28+
htpContextAccessor.HttpContext.Items.TryGetValue(key, out object value);
29+
return value;
30+
}
31+
32+
public void RemoveContextParameters(string key)
33+
{
34+
Check.NotNull(serviceProvider, "serviceProvider");
35+
var htpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
36+
if (htpContextAccessor.HttpContext.Items.ContainsKey(key))
37+
htpContextAccessor.HttpContext.Items.Remove(key);
38+
39+
}
40+
41+
public Dictionary<String, Object> GetContextParameters()
42+
{
43+
Check.NotNull(serviceProvider, "serviceProvider");
44+
var htpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
45+
return htpContextAccessor.HttpContext.Items.ToDictionary(p => p.Key.ToString(), m => m.Value);
46+
47+
}
48+
49+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50+
public void SetContextParameters(IDictionary<object, object> contextParameters)
51+
{
52+
Check.NotNull(serviceProvider, "serviceProvider");
53+
var htpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
54+
htpContextAccessor.HttpContext.Items= contextParameters;
55+
}
56+
57+
private static IServiceProvider serviceProvider;
58+
59+
internal void Initialize(IServiceProvider provider)
60+
{
61+
serviceProvider= provider;
62+
}
63+
64+
public static RestContext GetContext()
65+
{
66+
67+
return new RestContext();
68+
}
69+
70+
public static void RemoveContext()
71+
{
72+
Check.NotNull(serviceProvider, "serviceProvider");
73+
var htpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
74+
htpContextAccessor.HttpContext.Items.Clear();
75+
76+
}
77+
78+
79+
}
80+
}

src/Surging.Core/Surging.Core.KestrelHttpServer/KestrelHttpModule.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Surging.Core.KestrelHttpServer.Extensions;
1414
using Surging.Core.KestrelHttpServer.Filters;
1515
using Surging.Core.KestrelHttpServer.Filters.Implementation;
16+
using Surging.Core.KestrelHttpServer.Internal;
1617
using System.Net;
1718

1819
namespace Surging.Core.KestrelHttpServer
@@ -26,6 +27,7 @@ public override void Initialize(AppModuleContext context)
2627

2728
public virtual void Initialize(ApplicationInitializationContext builder)
2829
{
30+
RestContext.GetContext().Initialize(builder.Builder.ApplicationServices);
2931
}
3032

3133
public virtual void RegisterBuilder(WebHostContext context)

src/Surging.Core/Surging.Core.Stage/Filters/AuthorizationFilterAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Threading.Tasks;
1313
using Autofac;
1414
using System;
15+
using Surging.Core.KestrelHttpServer.Internal;
1516

1617
namespace Surging.Core.Stage.Filters
1718
{
@@ -44,7 +45,7 @@ public async Task OnAuthorization(AuthorizationFilterContext filterContext)
4445
else
4546
{
4647
var payload = _authorizationServerProvider.GetPayloadString(author);
47-
RpcContext.GetContext().SetAttachment("payload", payload);
48+
RestContext.GetContext().SetAttachment("payload", payload);
4849
}
4950
}
5051
else

src/Surging.Core/Surging.Core.Stage/Filters/IPFilterAttribute.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Surging.Core.CPlatform.Transport.Implementation;
55
using Surging.Core.KestrelHttpServer.Filters;
66
using Surging.Core.KestrelHttpServer.Filters.Implementation;
7+
using Surging.Core.KestrelHttpServer.Internal;
78
using Surging.Core.Stage.Internal;
89
using System;
910
using System.Collections.Generic;
@@ -30,7 +31,7 @@ public Task OnActionExecuted(ActionExecutedContext filterContext)
3031
public Task OnActionExecuting(ActionExecutingContext filterContext)
3132
{
3233
var address = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress;
33-
RpcContext.GetContext().SetAttachment("RemoteIpAddress", address.ToString());
34+
RestContext.GetContext().SetAttachment("RemoteIpAddress", address.ToString());
3435
if (_ipChecker.IsBlackIp(address,filterContext.Message.RoutePath))
3536
{
3637
filterContext.Result = new HttpResultMessage<object> { IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Message = "Your IP address is not allowed" };

src/Surging.Services/Surging.Services.Server/Surging.Services.Server.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<ProjectReference Include="..\..\Surging.Core\Surging.Core.Serilog\Surging.Core.Serilog.csproj" />
6363
<ProjectReference Include="..\..\Surging.Core\Surging.Core.ServiceHosting.Extensions\Surging.Core.ServiceHosting.Extensions.csproj" />
6464
<ProjectReference Include="..\..\Surging.Core\Surging.Core.ServiceHosting\Surging.Core.ServiceHosting.csproj" />
65+
<ProjectReference Include="..\..\Surging.Core\Surging.Core.Stage\Surging.Core.Stage.csproj" />
6566
<ProjectReference Include="..\..\Surging.Core\Surging.Core.Swagger\Surging.Core.Swagger.csproj" />
6667
<ProjectReference Include="..\..\Surging.Core\Surging.Core.System\Surging.Core.System.csproj" />
6768
<ProjectReference Include="..\..\Surging.Core\Surging.Core.Zookeeper\Surging.Core.Zookeeper.csproj" />

0 commit comments

Comments
 (0)