public async Task Invoke(HttpContext context, IServiceProvider serviceProvider, CommandExecutor commandExecutor) { connectionManager.CheckExistingConnections(); string requestPath = context.Request.Path.Value.Substring(1).ToLowerInvariant(); if (context.Request.Method != "POST" || string.IsNullOrEmpty(requestPath)) { await next(context); return; } if (!AuthHelper.CheckApiAuth(context.Request.Headers["key"], context.Request.Headers["secret"], options)) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync(JsonHelper.Serialize(new WrongApiResponse())); return; } if (!requestPath.EndsWith("command")) { requestPath += "command"; } ConnectionBase connection = connectionManager.GetConnection(context); StreamReader sr = new StreamReader(context.Request.Body); string requestBody = await sr.ReadToEndAsync(); CommandBase command = JsonHelper.DeserializeCommand(requestBody); if (command != null) { if (command.GetType().Name.ToLowerInvariant() != requestPath) { context.Response.StatusCode = StatusCodes.Status400BadRequest; await context.Response.WriteAsync("The specified path did not match the command type"); return; } ResponseBase response = await commandExecutor.ExecuteCommand(command, serviceProvider.CreateScope().ServiceProvider, connection != null?connection.Information : new HttpInformation(context), logger, connection); if (response?.Error != null) { context.Response.StatusCode = StatusCodes.Status400BadRequest; } await context.Response.WriteAsync(JsonHelper.Serialize(response)); } else { context.Response.StatusCode = StatusCodes.Status400BadRequest; await context.Response.WriteAsync("Parsing of the command was not successful"); } }
public async Task Invoke(HttpContext context, ILogger <PollConnection> logger) { connectionManager.CheckExistingConnections(); if (!AuthHelper.CheckApiAuth(context.Request.Headers["key"], context.Request.Headers["secret"], options)) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync(JsonHelper.Serialize(new WrongApiResponse())); return; } if (context.Request.Method != "GET") { await next(context); return; } PollConnection connection; if (context.Request.Path.Value.ToLowerInvariant().EndsWith("/init")) { connection = new PollConnection(context); connectionManager.AddConnection(connection); await context.Response.WriteAsync(JsonHelper.Serialize(new ConnectionResponse() { ConnectionId = connection.Id })); logger.LogInformation("Created new poll connection"); } else { connection = (PollConnection)connectionManager.GetConnection(context); if (connection != null) { IEnumerable <object> messages = await connection.GetMessages(); await context.Response.WriteAsync(JsonHelper.Serialize(messages)); } else { context.Response.StatusCode = StatusCodes.Status404NotFound; await context.Response.WriteAsync("No connection was found."); } } }
public async Task Invoke(HttpContext context, CommandExecutor commandExecutor, IServiceProvider serviceProvider, ILogger <SSEConnection> logger) { SSEConnection connection = null; try { if (context.Request.Headers["Accept"] == "text/event-stream") { context.Response.Headers["Cache-Control"] = "no-cache"; context.Response.Headers["X-Accel-Buffering"] = "no"; context.Response.ContentType = "text/event-stream"; await context.Response.Body.FlushAsync(); connection = new SSEConnection(context); if (!AuthHelper.CheckApiAuth(context.Request.Query["key"], context.Request.Query["secret"], options)) { await connection.Send(new WrongApiResponse()); context.Response.Body.Close(); return; } connectionManager.AddConnection(connection); await connection.Send(new ConnectionResponse() { ConnectionId = connection.Id }); context.RequestAborted.WaitHandle.WaitOne(); } } catch (Exception ex) { await context.Response.WriteAsync(ex.Message); } finally { if (connection != null) { connectionManager.RemoveConnection(connection); } } }
public async Task Invoke(HttpContext context, CommandExecutor commandExecutor, IServiceProvider serviceProvider, ILogger <WebsocketConnection> logger) { if (context.WebSockets.IsWebSocketRequest) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); if (!AuthHelper.CheckApiAuth(context.Request.Query["key"], context.Request.Query["secret"], options)) { await webSocket.Send(new WrongApiResponse()); await webSocket.CloseAsync(WebSocketCloseStatus.PolicyViolation, "Wrong API key or secret", CancellationToken.None); return; } WebsocketConnection connection = new WebsocketConnection(webSocket, context); connectionManager.AddConnection(connection); await connection.Send(new ConnectionResponse() { ConnectionId = connection.Id }); while (webSocket.State == WebSocketState.Open || webSocket.State == WebSocketState.Connecting) { try { string message = await connection.Websocket.Receive(); if (!string.IsNullOrEmpty(message)) { _ = Task.Run(async() => { CommandBase command = JsonHelper.DeserializeCommand(message); if (command != null) { ResponseBase response = await commandExecutor.ExecuteCommand(command, serviceProvider.CreateScope().ServiceProvider, connection.Information, logger, connection); if (response != null) { await connection.Send(response); } } }); } } catch (OperationCanceledException) { break; } catch (Exception ex) { logger.LogError(ex.Message); } } connectionManager.RemoveConnection(connection); } }