public MediatorCore() { reqHandler = new HandleClientRequests(this); if (theCore == null) { theCore = this; } }
private async Task HandleClientRequest(HttpContext context) { HttpRequest request = context.Request; HttpResponse response = context.Response; if (shutdown) { response.StatusCode = 400; // BAD Request return; } try { if (request.Path == "/Mediator/" && context.WebSockets.IsWebSocketRequest) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); await HandleClientWebSocket(webSocket); return; } if (request.Method != "POST" || !HandleClientRequests.IsValid(request.Path)) { logger.Warn("Invalid request " + request.Path.ToUriComponent()); response.StatusCode = 400; // BAD Request return; } //logger.Info("Client request: " + request.Path); JObject obj = null; using (var body = request.Body) { using (var reader = new StreamReader(body, Encoding.UTF8)) { obj = await StdJson.JObjectFromReaderAsync(reader); //logger.Info("ReqStr: " + StdJson.ObjectToString(obj, true)); } } using (ReqResult result = await reqHandler.Handle(request.Path, obj)) { //logger.Info(result.AsString()); response.StatusCode = result.StatusCode; response.ContentLength = result.Bytes.Length; response.ContentType = "application/json"; try { await result.Bytes.CopyToAsync(response.Body); } catch (Exception) { response.StatusCode = 500; } } } catch (Exception exp) { response.StatusCode = 500; logger.Warn(exp.GetBaseException(), "Error handling client request"); } }
public MediatorCore() { if (theCore == null) { theCore = this; reqHandler = new HandleClientRequests(this); } else { reqHandler = new HandleClientRequests(this); // only for compiler so that reqHandler is never null } }
private async Task HandleClientRequest(HttpContext context) { HttpRequest request = context.Request; HttpResponse response = context.Response; string path = request.Path; if (shutdown) { if (HandleClientRequests.IsLogout(path)) { response.StatusCode = 200; return; } response.StatusCode = 400; // BAD Request string methodName = path.StartsWith(HandleClientRequests.PathPrefix) ? path.Substring(HandleClientRequests.PathPrefix.Length) : path; byte[] bytes = Encoding.UTF8.GetBytes($"Can not respond to {methodName} request because system is shutting down."); _ = response.Body.WriteAsync(bytes, 0, bytes.Length); return; } try { if (path == HandleClientRequests.PathPrefix && context.WebSockets.IsWebSocketRequest) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); await HandleClientWebSocket(webSocket); return; } if (request.Method != "POST" || !mapRequests.TryGetValue(path, out ReqDef? reqDef)) { logger.Warn("Invalid request " + request.Path.ToUriComponent()); response.StatusCode = 400; // BAD Request return; } //logger.Info("Client request: " + request.Path); RequestBase obj = await GetRequestObject(request, reqDef); using (ReqResult result = await reqHandler.Handle(obj)) { //logger.Info(result.AsString()); response.StatusCode = result.StatusCode; response.ContentLength = result.Bytes.Length; response.ContentType = result.ContentType; try { await result.Bytes.CopyToAsync(response.Body); } catch (Exception) { response.StatusCode = 500; } } } catch (Exception exp) { response.StatusCode = 500; logger.Warn(exp.GetBaseException(), "Error handling client request"); } }