public Task Invoke(IDictionary <string, object> environment) { var serverRequest = new ServerRequest(environment); var serverResponse = new ServerResponse(environment); var hostContext = new HostContext(serverRequest, serverResponse); // Add CORS support var origins = serverRequest.RequestHeaders.GetHeaders("Origin"); if (origins != null && origins.Any(origin => !String.IsNullOrEmpty(origin))) { serverResponse.ResponseHeaders["Access-Control-Allow-Origin"] = origins; serverResponse.ResponseHeaders["Access-Control-Allow-Credentials"] = AllowCredentialsTrue; } hostContext.Items[HostConstants.SupportsWebSockets] = LazyInitializer.EnsureInitialized( ref _supportWebSockets, ref _supportWebSocketsInitialized, ref _supportWebSocketsLock, () => environment.SupportsWebSockets()); hostContext.Items[HostConstants.ShutdownToken] = environment.GetShutdownToken(); hostContext.Items[HostConstants.DebugMode] = environment.GetIsDebugEnabled(); serverRequest.DisableRequestBuffering(); serverResponse.DisableResponseBuffering(); _connection.Initialize(_resolver, hostContext); return(_connection.ProcessRequestAsync(hostContext)); }
private static AppDelegate ExecuteConnection(Func <IDictionary <string, object>, PersistentConnection> factory) { return((environment, result, fault) => { // Read the request body then process the request. ParseBodyAsync(environment).ContinueWith(task => { if (task.IsFaulted) { // There was an error reading the body fault(task.Exception); } else { var request = new OwinRequest(environment, task.Result); var origin = request.Headers["Origin"]; var response = new OwinResponse(result, origin); var hostContext = new HostContext(request, response); try { PersistentConnection connection = factory(environment); connection .ProcessRequestAsync(hostContext) .ContinueWith(innerTask => { if (innerTask.IsFaulted) { fault(innerTask.Exception); } else { response.End().Catch(); } }); } catch (Exception ex) { fault(ex); } } }); }); }
public override Task ProcessRequestAsync(HttpContextBase context) #endif { // https://developer.mozilla.org/En/HTTP_Access_Control string origin = context.Request.Headers["Origin"]; if (!String.IsNullOrEmpty(origin)) { context.Response.AddHeader("Access-Control-Allow-Origin", origin); context.Response.AddHeader("Access-Control-Allow-Credentials", "true"); } var request = new AspNetRequest(context); var response = new AspNetResponse(context); var hostContext = new HostContext(request, response); // Determine if the client should bother to try a websocket request #if NET45 hostContext.Items[HostConstants.SupportsWebSockets] = HttpRuntime.IISVersion != null && HttpRuntime.IISVersion.Major >= 8 && !String.IsNullOrEmpty(context.Request.ServerVariables[WebSocketVersionServerVariable]); #else hostContext.Items[HostConstants.SupportsWebSockets] = false; #endif // Set the debugging flag hostContext.Items[HostConstants.DebugMode] = context.IsDebuggingEnabled; // Set the host shutdown token hostContext.Items[HostConstants.ShutdownToken] = AppDomainTokenSource.Token; // Stick the context in here so transports or other asp.net specific logic can // grab at it. hostContext.Items["System.Web.HttpContext"] = context; // Initialize the connection _connection.Initialize(_resolver, hostContext); try { return(_connection.ProcessRequestAsync(hostContext)); } catch (NotSupportedException) // WebSockets not supported { context.Response.StatusCode = 501; // HTTP 501 Not Implemented return(TaskAsyncHelper.Empty); } }
public override Task ProcessRequestAsync(HttpContextBase context) { var request = new AspNetRequest(context.Request); var response = new AspNetResponse(context); var hostContext = new HostContext(request, response, context.User); // Determine if the client should bother to try a websocket request hostContext.Items[HostConstants.SupportsWebSockets] = _hasAcceptWebSocketRequest.Value; // Set the debugging flag hostContext.Items[HostConstants.DebugMode] = context.IsDebuggingEnabled; // Stick the context in here so transports or other asp.net specific logic can // grab at it. hostContext.Items["System.Web.HttpContext"] = context; return(_connection.ProcessRequestAsync(hostContext)); }
public Task Invoke(IDictionary <string, object> env) { var serverRequest = new ServerRequest(env); var serverResponse = new ServerResponse(env); var hostContext = new HostContext(serverRequest, serverResponse); var origins = serverRequest.RequestHeaders.GetHeaders("Origin"); if (origins != null && origins.Any(origin => !String.IsNullOrEmpty(origin))) { serverResponse.ResponseHeaders["Access-Control-Allow-Origin"] = origins; serverResponse.ResponseHeaders["Access-Control-Allow-Credentials"] = AllowCredentialsTrue; } hostContext.Items[HostConstants.SupportsWebSockets] = env.ContainsKey(OwinConstants.WebSocketSupport); serverRequest.DisableRequestBuffering(); serverResponse.DisableResponseBuffering(); _connection.Initialize(_resolver, hostContext); return(_connection.ProcessRequestAsync(hostContext)); }