public async void CallService(HttpHeader request, RestServiceSession session) { var pathInfo = request.Path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var pathValueMatch = Regex.Match(pathInfo[1], "^([0-9A-Fa-f]{8}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{4}[-][0-9A-Fa-f]{12})$"); if (pathInfo.Length != 2) { Log.Message(LogTypes.Error, $"Got invalid path '{request.Path}'."); session.Dispose(); return; } Guid sessionGuid; if (!pathValueMatch.Success || !Guid.TryParse(pathValueMatch.Captures[0].Value, out sessionGuid)) { Log.Message(LogTypes.Error, $"Wrong path value for '{request.Host}{request.Path}'."); session.Dispose(); return; } if (!Manager.Session.Exists(sessionGuid)) { Log.Message(LogTypes.Error, $"Session with guid '{pathValueMatch.Captures[0].Value}' doesn't exists.."); session.Dispose(); return; } request.PathValue = sessionGuid; Dictionary <Tuple <string, string>, MethodInfo> restMethods; // TODO: Fix host configuration in RestServiceAttribute. //if (restServiceHandlers.TryGetValue(request.Host, out restMethods)) if (restServiceHandlers.TryGetValue("*", out restMethods)) { var methodInfo = Tuple.Create(request.Method, pathInfo[0]); MethodInfo method; if (restMethods.TryGetValue(methodInfo, out method)) { // TODO: Test for performance issues and problems with async/await. await Task.Run(() => method.Invoke(null, new object[] { request, session })); } else { Log.Message(LogTypes.Error, $"Got unhandled REST method/path '{methodInfo.Item1}/{methodInfo.Item2}' for '{request.Host}'."); } } else { Log.Message(LogTypes.Error, $"Got unhandled REST service for host '{request.Host}'."); } }
//[RestRoute(Method = "POST", Path = "login")] public static async void HandleLoginRequest(HttpHeader request, RestServiceSession session) { // TODO: Implement authentication. if (false) { } else { await session.Send(RestResponse.InvalidAccountOrCredentials); } }
public static async void HandleConnectRequest(HttpHeader request, RestServiceSession session) { // Login form is the same for all clients... await session.Send(RestResponse.LoginForm); }