public async Task Invoke(HttpContext context) { var serveruri = context.Request.Host; string path = context.Request.Path; if (path.StartsWith("/json/session", StringComparison.InvariantCultureIgnoreCase) && context.WebSockets.IsWebSocketRequest) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); var session = chromeSessionProvider.CreateSession(webSocket, path.Split('/').Last()); using (ChromeHostExtension.Sessions.Register(session)) { await session.Process(CancellationToken.None); } } else { switch (context.Request.Path) { case "/json/version": { var responseObj = chromeSessionProvider.GetProtocolVersion(); var response = JsonConvert.SerializeObject(responseObj); context.Response.Headers.Add("Content-Length", response.Length.ToString()); await context.Response.WriteAsync(response); break; } case "/json": case "/json/list": { var responseObj = chromeSessionProvider.GetSessionInstanceDescriptions(serveruri.Host, serveruri.Port.Value); var response = JsonConvert.SerializeObject(responseObj); context.Response.ContentType = "application/json; charset=UTF-8"; context.Response.Headers.Add("Content-Length", response.Length.ToString()); await context.Response.WriteAsync(response); break; } default: await next(context); break; } } }
public static async Task Start(ChromeProtocolSessions sessions, string listeningAddress, IChromeSessionProvider sessionProvider, CancellationToken cancellationToken) { try { using (var listener = new HttpListener()) { listener.Prefixes.Add(listeningAddress); listener.Start(); Console.Out.WriteLine("listening on " + listeningAddress); var listeningOnUri = new Uri(listeningAddress); while (!cancellationToken.IsCancellationRequested) { var context = await listener.GetContextAsync(); var path = context.Request.Url.AbsolutePath; if (path.StartsWith("/json/session/", StringComparison.InvariantCultureIgnoreCase) && context.Request.IsWebSocketRequest) { try { _ = Task.Run(async() => { var webSocketContext = await context.AcceptWebSocketAsync(null); var session = sessionProvider.CreateSession(webSocketContext.WebSocket, path.Split('/').Last()); using (sessions.Register(session)) { await session.Process(cancellationToken); } }); } catch (Exception e) { Console.Error.WriteLine(e); } } else { switch (context.Request.Url.AbsolutePath) { case "/json/version": { var responseString = JsonConvert.SerializeObject(sessionProvider.GetProtocolVersion()); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get a response stream and write the response to it. context.Response.ContentLength64 = buffer.Length; context.Response.OutputStream.Write(buffer, 0, buffer.Length); context.Response.OutputStream.Close(); break; } case "/json": case "/json/list": { var responseString = JsonConvert.SerializeObject(sessionProvider.GetSessionInstanceDescriptions(listeningOnUri.Host, listeningOnUri.Port)); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); context.Response.ContentType = "application/json; charset=UTF-8"; // Get a response stream and write the response to it. context.Response.ContentLength64 = buffer.Length; context.Response.OutputStream.Write(buffer, 0, buffer.Length); context.Response.OutputStream.Close(); break; } } } } } } catch (Exception e) { Console.Error.WriteLine(e); } }