public async Task OnReceived(IMessageSender sender, HttpContext context) { var path = HttpUtility.UrlDecode(GetRoutePath(context.Request.Path.ToString())); var serviceRoute = await _serviceRouteProvider.GetRouteByPathRegex(path); IDictionary <string, object> parameters = context.Request.Query.ToDictionary(p => p.Key, p => (object)p.Value.ToString()); parameters.Remove("servicekey", out object serviceKey); StreamReader streamReader = new StreamReader(context.Request.Body); var data = await streamReader.ReadToEndAsync(); if (data.Length > 0) { parameters = _serializer.Deserialize <string, IDictionary <string, object> >(data) ?? new Dictionary <string, object>(); } if (String.Compare(serviceRoute.ServiceDescriptor.RoutePath, path, true) != 0) { var @params = RouteTemplateSegmenter.Segment(serviceRoute.ServiceDescriptor.RoutePath, path); foreach (var param in @params) { parameters.Add(param.Key, param.Value); } } if (context.Request.HasFormContentType) { var collection = await GetFormCollection(context.Request); parameters.Add("form", collection); await Received(sender, new TransportMessage(new HttpMessage { Parameters = parameters, RoutePath = serviceRoute.ServiceDescriptor.RoutePath, ServiceKey = serviceKey?.ToString() })); } else { if (context.Request.Method == "POST") { await Received(sender, new TransportMessage(new HttpMessage { Parameters = parameters, RoutePath = serviceRoute.ServiceDescriptor.RoutePath, ServiceKey = serviceKey?.ToString() })); } else { await Received(sender, new TransportMessage(new HttpMessage { Parameters = parameters, RoutePath = serviceRoute.ServiceDescriptor.RoutePath, ServiceKey = serviceKey?.ToString() })); } } }
protected override void ChannelRead0(IChannelHandlerContext ctx, IFullHttpRequest msg) { var data = new byte[msg.Content.ReadableBytes]; msg.Content.ReadBytes(data); Task.Run(async() => { var parameters = GetParameters(HttpUtility.UrlDecode(msg.Uri), out string path); path = AppConfig.MapRoutePathOptions.GetRoutePath(path, msg.Method.Name); var serviceRoute = await _serviceRouteProvider.GetRouteByPathOrRegexPath(path, msg.Method.Name.ToUpper()); if (serviceRoute == null) { throw new CPlatformException($"未能找到路径为{path}-{msg.Method.Name.ToUpper()}的路由信息", StatusCode.Http404EndpointStatusCode); } parameters.Remove("servicekey", out object serviceKey); if (data.Length > 0) { parameters = _serializer.Deserialize <string, IDictionary <string, object> >(System.Text.Encoding.ASCII.GetString(data)) ?? new Dictionary <string, object>(); } if (String.Compare(serviceRoute.ServiceDescriptor.RoutePath, path, true) != 0) { var @params = RouteTemplateSegmenter.Segment(serviceRoute.ServiceDescriptor.RoutePath, path); foreach (var param in @params) { parameters.Add(param.Key, param.Value); } } if (msg.Method.Name == "POST") { _readAction(ctx, new TransportMessage(new HttpMessage { Parameters = parameters, RoutePath = serviceRoute.ServiceDescriptor.RoutePath, HttpMethod = msg.Method.Name.ToUpper(), ServiceKey = serviceKey?.ToString() })); } else { _readAction(ctx, new TransportMessage(new HttpMessage { Parameters = parameters, RoutePath = serviceRoute.ServiceDescriptor.RoutePath, HttpMethod = msg.Method.Name.ToUpper(), ServiceKey = serviceKey?.ToString() })); } }); }
/// <summary> /// Channels the read0. /// </summary> /// <param name="ctx">The CTX.</param> /// <param name="msg">The MSG.</param> protected override void ChannelRead0(IChannelHandlerContext ctx, IFullHttpRequest msg) { var data = new byte[msg.Content.ReadableBytes]; msg.Content.ReadBytes(data); Task.Run(async() => { var parameters = GetParameters(HttpUtility.UrlDecode(msg.Uri), out var path); var serviceRoute = await _serviceRouteProvider.GetRouteByPathRegex(path); parameters.Remove("servicekey", out var serviceKey); if (data.Length > 0) { parameters = _serializer.Deserialize <string, IDictionary <string, object> >( Encoding.ASCII.GetString(data)) ?? new Dictionary <string, object>(); } if (string.Compare(serviceRoute.ServiceDescriptor.RoutePath, path, true) != 0) { var @params = RouteTemplateSegmenter.Segment(serviceRoute.ServiceDescriptor.RoutePath, path); foreach (var param in @params) { parameters.Add(param.Key, param.Value); } } if (msg.Method.Name == "POST") { _readAction(ctx, new TransportMessage(new HttpRequestMessage { Parameters = parameters, RoutePath = serviceRoute.ServiceDescriptor.RoutePath, ServiceKey = serviceKey?.ToString() })); } else { _readAction(ctx, new TransportMessage(new HttpRequestMessage { Parameters = parameters, RoutePath = serviceRoute.ServiceDescriptor.RoutePath, ServiceKey = serviceKey?.ToString() })); } }); }
public async Task OnReceived(IMessageSender sender, string messageId, HttpContext context, IEnumerable <IActionFilter> actionFilters) { var serviceRoute = context.Items["route"] as ServiceRoute; var path = (context.Items["path"] ?? HttpUtility.UrlDecode(GetRoutePath(context.Request.Path.ToString()))) as string; if (serviceRoute == null) { serviceRoute = await _serviceRouteProvider.GetRouteByPathRegex(path); } IDictionary <string, object> parameters = context.Request.Query.ToDictionary(p => p.Key, p => (object)p.Value.ToString()); object serviceKey = null; foreach (var key in _serviceKeys) { parameters.Remove(key, out object value); if (value != null) { serviceKey = value; break; } } if (String.Compare(serviceRoute.ServiceDescriptor.RoutePath, path, true) != 0) { var @params = RouteTemplateSegmenter.Segment(serviceRoute.ServiceDescriptor.RoutePath, path); foreach (var param in @params) { parameters.Add(param.Key, param.Value); } } var httpMessage = new HttpMessage { Parameters = parameters, RoutePath = serviceRoute.ServiceDescriptor.RoutePath, ServiceKey = serviceKey?.ToString() }; if (context.Request.HasFormContentType) { var collection = await GetFormCollection(context.Request); foreach (var item in collection) { httpMessage.Parameters.Add(item.Key, item.Value); } if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage }, sender, messageId, actionFilters)) { return; } httpMessage.Attachments = RpcContext.GetContext().GetContextParameters(); await Received(sender, new TransportMessage(messageId, httpMessage)); } else { StreamReader streamReader = new StreamReader(context.Request.Body); var data = await streamReader.ReadToEndAsync(); if (context.Request.Method == "POST") { var bodyParams = _serializer.Deserialize <string, IDictionary <string, object> >(data) ?? new Dictionary <string, object>(); foreach (var param in bodyParams) { httpMessage.Parameters.Add(param.Key, param.Value); } if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage }, sender, messageId, actionFilters)) { return; } httpMessage.Attachments = RpcContext.GetContext().GetContextParameters(); await Received(sender, new TransportMessage(messageId, httpMessage)); } else { if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage }, sender, messageId, actionFilters)) { return; } httpMessage.Attachments = RpcContext.GetContext().GetContextParameters(); await Received(sender, new TransportMessage(messageId, httpMessage)); } } await OnActionExecuted(context, httpMessage, actionFilters); }
public async Task <Surging.Core.ApiGateWay.ServiceResult <object> > Path([FromServices] IServicePartProvider servicePartProvider, string path, [FromBody] Dictionary <string, object> model) { string serviceKey = this.Request.Query["servicekey"]; path = path.IndexOf("/") < 0 ? $"/{path}" : path; if (model == null) { model = new Dictionary <string, object>(); } foreach (string n in this.Request.Query.Keys) { model[n] = this.Request.Query[n].ToString(); } Surging.Core.ApiGateWay.ServiceResult <object> result = Surging.Core.ApiGateWay.ServiceResult <object> .Create(false, null); path = String.Compare(path.ToLower(), GateWayAppConfig.TokenEndpointPath, true) == 0 ? GateWayAppConfig.AuthorizationRoutePath : path.ToLower(); var route = await _serviceRouteProvider.GetRouteByPathRegex(path); var httpMethods = route.ServiceDescriptor.HttpMethod(); if (!string.IsNullOrEmpty(httpMethods) && !httpMethods.Contains(Request.Method)) { return new Surging.Core.ApiGateWay.ServiceResult <object> { IsSucceed = false, StatusCode = (int)ServiceStatusCode.Http405Endpoint, Message = "405 HTTP Method Not Supported" } } ; if (!GetAllowRequest(route)) { return new Surging.Core.ApiGateWay.ServiceResult <object> { IsSucceed = false, StatusCode = (int)ServiceStatusCode.RequestError, Message = "Request error" } } ; if (servicePartProvider.IsPart(path)) { //result = ServiceResult<object>.Create(true, await servicePartProvider.Merge(path, model)); //result.StatusCode = (int)ServiceStatusCode.Success; var data = (string)await servicePartProvider.Merge(path, model); return(AuthenticationCommon.CreateServiceResult(data)); } else { var auth = await OnAuthorization(path, route, model); result = auth.result; if (auth.isSuccess) { if (path == GateWayAppConfig.AuthorizationRoutePath) { var oAuthUser = await _authorizationServerProvider.GenerateTokenCredential(model); if (oAuthUser.IsSucceed) { result = Surging.Core.ApiGateWay.ServiceResult <object> .Create(true, oAuthUser); result.StatusCode = (int)ServiceStatusCode.Success; } else { result = new Surging.Core.ApiGateWay.ServiceResult <object> { IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Entity = oAuthUser }; } } else { if (String.Compare(route.ServiceDescriptor.RoutePath, path, true) != 0) { var pamars = RouteTemplateSegmenter.Segment(route.ServiceDescriptor.RoutePath, path); foreach (KeyValuePair <string, object> item in pamars) { model.Add(item.Key, item.Value); } } if (!string.IsNullOrEmpty(serviceKey)) { //result = ServiceResult<object>.Create(true, await _serviceProxyProvider.Invoke<object>(model, route.ServiceDescriptor.RoutePath, serviceKey)); //result.StatusCode = (int)ServiceStatusCode.Success; var data = await _serviceProxyProvider.Invoke <object>(model, path, serviceKey); return(AuthenticationCommon.CreateServiceResult(data)); } else { //result = ServiceResult<object>.Create(true, await _serviceProxyProvider.Invoke<object>(model, route.ServiceDescriptor.RoutePath)); //result.StatusCode = (int)ServiceStatusCode.Success; var data = await _serviceProxyProvider.Invoke <object>(model, path); return(AuthenticationCommon.CreateServiceResult(data)); } } } } return(result); }
public async Task <ServiceResult <object> > Path([FromServices] IServicePartProvider servicePartProvider, string path, [FromBody] Dictionary <string, object> model) { string serviceKey = this.Request.Query["servicekey"]; path = path.IndexOf("/") < 0 ? $"/{path}" : path; if (model == null) { model = new Dictionary <string, object>(); } foreach (string n in this.Request.Query.Keys) { model[n] = this.Request.Query[n].ToString(); } ServiceResult <object> result = ServiceResult <object> .Create(false, null); path = String.Compare(path.ToLower(), GateWayAppConfig.TokenEndpointPath, true) == 0 ? GateWayAppConfig.AuthorizationRoutePath : path.ToLower(); var route = await _serviceRouteProvider.GetRouteByPathRegex(path); if (!GetAllowRequest(route)) { return new ServiceResult <object> { IsSucceed = false, StatusCode = (int)ServiceStatusCode.RequestError, Message = "Request error" } } ; if (servicePartProvider.IsPart(path)) { result = ServiceResult <object> .Create(true, await servicePartProvider.Merge(path, model)); result.StatusCode = (int)ServiceStatusCode.Success; } else { var auth = OnAuthorization(route, model); result = auth.Item2; if (auth.Item1) { if (path == GateWayAppConfig.AuthorizationRoutePath) { var token = await _authorizationServerProvider.GenerateTokenCredential(model); if (token != null) { result = ServiceResult <object> .Create(true, token); result.StatusCode = (int)ServiceStatusCode.Success; } else { result = new ServiceResult <object> { IsSucceed = false, StatusCode = (int)ServiceStatusCode.AuthorizationFailed, Message = "Invalid authentication credentials" }; } } else { if (String.Compare(route.ServiceDescriptor.RoutePath, path, true) != 0) { var pamars = RouteTemplateSegmenter.Segment(route.ServiceDescriptor.RoutePath, path); foreach (KeyValuePair <string, object> item in pamars) { model.Add(item.Key, item.Value); } } if (!string.IsNullOrEmpty(serviceKey)) { result = ServiceResult <object> .Create(true, await _serviceProxyProvider.Invoke <object>(model, route.ServiceDescriptor.RoutePath, serviceKey)); result.StatusCode = (int)ServiceStatusCode.Success; } else { result = ServiceResult <object> .Create(true, await _serviceProxyProvider.Invoke <object>(model, route.ServiceDescriptor.RoutePath)); result.StatusCode = (int)ServiceStatusCode.Success; } } } } return(result); }
public async Task OnReceived(IMessageSender sender, string messageId, HttpContext context, IEnumerable <IActionFilter> actionFilters) { var serviceRoute = context.Items["route"] as ServiceRoute; var path = (context.Items["path"] ?? HttpUtility.UrlDecode(GetRoutePath(context.Request.Path.ToString()))) as string; path = AppConfig.MapRoutePathOptions.GetRoutePath(path, context.Request.Method); if (serviceRoute == null) { var route = await _serviceRouteProvider.GetRouteByPathOrRegexPath(path, context.Request.Method); if (route == null) { throw new CPlatformException($"未能找到路径为{path}-{context.Request.Method}的路由信息", StatusCode.Http404EndpointStatusCode); } serviceRoute = route; } IDictionary <string, object> parameters = context.Request.Query.ToDictionary(p => p.Key, p => (object)p.Value.ToString()); object serviceKey = null; foreach (var key in _serviceKeys) { parameters.Remove(key, out object value); if (value != null) { serviceKey = value; break; } } if (!serviceRoute.ServiceDescriptor.RoutePath.Equals(path, StringComparison.OrdinalIgnoreCase)) { var @params = RouteTemplateSegmenter.Segment(serviceRoute.ServiceDescriptor.RoutePath, path); foreach (var param in @params) { parameters.Add(param.Key, param.Value); } } var httpMessage = new HttpMessage { Parameters = parameters, RoutePath = serviceRoute.ServiceDescriptor.RoutePath, HttpMethod = context.Request.Method, ServiceKey = serviceKey?.ToString() }; if (context.Request.HasFormContentType) { if (context.Request.ContentType == "multipart/form-data") { var collection = await GetFormCollection(context.Request); httpMessage.Parameters.Add("form", collection); } else { var formData = new Dictionary <string, object>(); foreach (var item in context.Request.Form.Keys) { formData.Add(item, context.Request.Form[item]); } httpMessage.Parameters.Add("form", formData); } if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage }, sender, messageId, actionFilters)) { return; } httpMessage.Attachments = GetHttpMessageAttachments(context); await Received(sender, new TransportMessage(messageId, httpMessage)); } else { StreamReader streamReader = new StreamReader(context.Request.Body); var data = await streamReader.ReadToEndAsync(); if (context.Request.Method != "GET") { var bodyParams = _serializer.Deserialize <string, IDictionary <string, object> >(data) ?? new Dictionary <string, object>(); foreach (var param in bodyParams) { httpMessage.Parameters.Add(param.Key, param.Value); } if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage }, sender, messageId, actionFilters)) { return; } httpMessage.Attachments = GetHttpMessageAttachments(context); await Received(sender, new TransportMessage(messageId, httpMessage)); } else { if (!await OnActionExecuting(new ActionExecutingContext { Context = context, Route = serviceRoute, Message = httpMessage }, sender, messageId, actionFilters)) { return; } httpMessage.Attachments = GetHttpMessageAttachments(context); await Received(sender, new TransportMessage(messageId, httpMessage)); } } await OnActionExecuted(context, httpMessage, actionFilters); }