public static AppFunc Middleware(AppFunc app) { return(env => { var request = new Request(env); string opaqueSupport = request.Get <string>("opaque.Support"); OpaqueUpgrade opaqueUpgrade = request.Get <OpaqueUpgrade>("opaque.Upgrade"); string websocketSupport = request.Get <string>("websocket.Support"); WebSocketAccept webSocketAccept = request.Get <WebSocketAccept>("websocket.Accept"); if (opaqueSupport == "opaque.Upgrade" && // If we have opaque support opaqueUpgrade != null && websocketSupport != "websocket.Accept" && // and no current websocket support webSocketAccept == null) // and this request is a websocket request... { // This middleware is adding support for websockets. env["websocket.Support"] = "websocket.Accept"; if (IsWebSocketRequest(env)) { IDictionary <string, object> acceptOptions = null; WebSocketFunc webSocketFunc = null; // Announce websocket support env["websocket.Accept"] = new WebSocketAccept( (options, callback) => { acceptOptions = options; webSocketFunc = callback; env[OwinConstants.ResponseStatusCode] = 101; }); return app(env).Then(() => { Response response = new Response(env); if (response.StatusCode == 101 && webSocketFunc != null) { SetWebSocketResponseHeaders(env, acceptOptions); opaqueUpgrade(acceptOptions, opaqueEnv => { WebSocketLayer webSocket = new WebSocketLayer(opaqueEnv); return webSocketFunc(webSocket.Environment) .Then(() => webSocket.CleanupAsync()); }); } }); } } // else return app(env); }); }
public static AppFunc Middleware(AppFunc app) { return(env => { string opaqueSupport = env.Get <string>("opaque.Support"); string websocketSupport = env.Get <string>("websocket.Support"); if (opaqueSupport == "OpaqueStreamFunc" && websocketSupport != "WebSocketFunc" && IsWebSocketRequest(env)) { // Announce websocket support env["websocket.Support"] = "WebSocketFunc"; return app(env).Then(() => { Response response = new Response(env); if (response.StatusCode == 101 && env.Get <WebSocketFunc>("websocket.Func") != null) { SetWebSocketResponseHeaders(env); WebSocketFunc wsBody = env.Get <WebSocketFunc>("websocket.Func"); OpaqueStreamFunc opaqueBody = (incoming, outgoing) => { WebSocketLayer webSocket = new WebSocketLayer(incoming, outgoing); return wsBody(webSocket.SendAsync, webSocket.ReceiveAsync, webSocket.CloseAsync) .Then(() => webSocket.CleanupAsync()); }; env["opaque.Func"] = opaqueBody; } }); } else { return app(env); } }); }
public static AppFunc Middleware(AppFunc app) { return env => { string opaqueSupport = env.Get<string>("opaque.Support"); OpaqueUpgrade opaqueUpgrade = env.Get<OpaqueUpgrade>("opaque.Upgrade"); string websocketSupport = env.Get<string>("websocket.Support"); WebSocketAccept webSocketAccept = env.Get<WebSocketAccept>("websocket.Accept"); if (opaqueSupport == "opaque.Upgrade" // If we have opaque support && opaqueUpgrade != null && websocketSupport != "websocket.Accept" // and no current websocket support && webSocketAccept == null) // and this request is a websocket request... { // This middleware is adding support for websockets. env["websocket.Support"] = "websocket.Accept"; if (IsWebSocketRequest(env)) { IDictionary<string, object> acceptOptions = null; WebSocketFunc webSocketFunc = null; // Announce websocket support env["websocket.Accept"] = new WebSocketAccept( (options, callback) => { acceptOptions = options; webSocketFunc = callback; env[OwinConstants.ResponseStatusCode] = 101; }); return app(env).Then(() => { Response response = new Response(env); if (response.StatusCode == 101 && webSocketFunc != null) { SetWebSocketResponseHeaders(env, acceptOptions); opaqueUpgrade(acceptOptions, opaqueEnv => { WebSocketLayer webSocket = new WebSocketLayer(opaqueEnv); return webSocketFunc(webSocket.Environment) .Then(() => webSocket.CleanupAsync()); }); } }); } } // else return app(env); }; }
public static AppFunc Middleware(AppFunc app) { return env => { string opaqueSupport = env.Get<string>("opaque.Support"); string websocketSupport = env.Get<string>("websocket.Support"); if (opaqueSupport == "OpaqueStreamFunc" && websocketSupport != "WebSocketFunc" && IsWebSocketRequest(env)) { // Announce websocket support env["websocket.Support"] = "WebSocketFunc"; return app(env).Then(() => { Response response = new Response(env); if (response.StatusCode == 101 && env.Get<WebSocketFunc>("websocket.Func") != null) { SetWebSocketResponseHeaders(env); WebSocketFunc wsBody = env.Get<WebSocketFunc>("websocket.Func"); OpaqueStreamFunc opaqueBody = (incoming, outgoing) => { WebSocketLayer webSocket = new WebSocketLayer(incoming, outgoing); return wsBody(webSocket.SendAsync, webSocket.ReceiveAsync, webSocket.CloseAsync) .Then(() => webSocket.CleanupAsync()); }; env["opaque.Func"] = opaqueBody; } }); } else { return app(env); } }; }