private void DoWebSocketUpgrade(IDictionary<string, object> acceptOptions, WebSocketFunc webSocketFunc)
        {
            if (webSocketFunc == null)
            {
                throw new ArgumentNullException("webSocketFunc");
            }

            _env.ResponseStatusCode = 101;
            _webSocketFunc = webSocketFunc;

            var options = new AspNetWebSocketOptions();
            options.SubProtocol = GetWebSocketSubProtocol(_env, acceptOptions);

            OnStart();
            _httpContext.AcceptWebSocketRequest(AcceptCallback, options);
        }
예제 #2
0
        public void ProcessRequest(HttpContext context)
        {
            string subProtocol = context.Request.QueryString["subprotocol"];

            if (context.Request.Url.Query == "?delay10sec")
            {
                Thread.Sleep(10000);
            }

            try
            {
                if (!context.IsWebSocketRequest)
                {
                    context.Response.StatusCode = 200;
                    context.Response.ContentType = "text/plain";
                    context.Response.Write("Not a websocket request");

                    return;
                }

                if (!string.IsNullOrEmpty(subProtocol))
                {
                    var wsOptions = new AspNetWebSocketOptions();
                    wsOptions.SubProtocol = subProtocol;

                    context.AcceptWebSocketRequest(ProcessWebSocketRequest, wsOptions);
                }
                else
                {
                    context.AcceptWebSocketRequest(ProcessWebSocketRequest);
                }
            }
            catch (Exception ex)
            {
                context.Response.StatusCode = 500;
                context.Response.StatusDescription = ex.Message;
            }
        }
예제 #3
0
 /// <summary>
 /// Accepts an <see cref="System.Web.WebSockets.AspNetWebSocket"/> request using the specified user function and options object.
 /// </summary>
 /// <param name="userFunc">The user function.</param>
 /// <param name="options">The options object.</param>
 public override void AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task> userFunc, AspNetWebSocketOptions options)
 {
     throw new NotSupportedException();
 }
 public override void AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task> userFunc,
     AspNetWebSocketOptions options)
 {
     _inner.AcceptWebSocketRequest(userFunc, options);
 }
예제 #5
0
        public void AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task> userFunc, AspNetWebSocketOptions options) {
            // Begin argument & state checking

            // We throw different error codes depending on the check that failed. Things that are
            // server configuration errors (WebSockets not enabled) or developer errors (called this
            // method with bad parameters) result in an appropriate exception type. Things that are
            // remote errors (e.g. bad parameters from the client) result in an HTTP 4xx.

            if (userFunc == null) {
                throw new ArgumentNullException("userFunc");
            }

            if (IsWebSocketRequestUpgrading) {
                // this method cannot be called multiple times
                throw new InvalidOperationException(SR.GetString(SR.WebSockets_AcceptWebSocketRequestCanOnlyBeCalledOnce));
            }

            // DevDiv #384514: Task<T> doesn't work correctly using the legacy SynchronizationContext setting. Since
            // WebSockets operation requires correct Task<T> behavior, we should forbid using the feature when legacy
            // mode is enabled.
            SynchronizationContextUtil.ValidateModeForWebSockets();

            switch (GetWebSocketInitStatus()) {
                case WebSocketInitStatus.RequiresIntegratedMode:
                    throw new PlatformNotSupportedException(SR.GetString(SR.Requires_Iis_Integrated_Mode));

                case WebSocketInitStatus.CannotCallFromBeginRequest:
                    throw new InvalidOperationException(SR.GetString(SR.WebSockets_CannotBeCalledDuringBeginRequest));

                case WebSocketInitStatus.NativeModuleNotEnabled:
                    throw new PlatformNotSupportedException(SR.GetString(SR.WebSockets_WebSocketModuleNotEnabled));

                case WebSocketInitStatus.NotAWebSocketRequest:
                    throw new HttpException((int)HttpStatusCode.BadRequest, SR.GetString(SR.WebSockets_NotAWebSocketRequest));

                case WebSocketInitStatus.CurrentRequestIsChildRequest:
                    throw new InvalidOperationException(SR.GetString(SR.WebSockets_CannotBeCalledDuringChildExecute));

                case WebSocketInitStatus.Success:
                    break;

                default:
                    // fallback error message - not a WebSocket request
                    throw new HttpException(SR.GetString(SR.WebSockets_UnknownErrorWhileAccepting));
            }

            if (CurrentNotification > RequestNotification.ExecuteRequestHandler) {
                // it is too late to call this method
                throw new InvalidOperationException(SR.GetString(SR.WebSockets_CannotBeCalledAfterHandlerExecute));
            }
            // End argument & state checking

            IIS7WorkerRequest wr = (IIS7WorkerRequest)_wr;

            // Begin options checking and parsing
            if (options != null && options.RequireSameOrigin) {
                if (!WebSocketUtil.IsSameOriginRequest(wr)) {
                    // use Forbidden (HTTP 403) since it's not an authentication error; it's a usage error
                    throw new HttpException((int)HttpStatusCode.Forbidden, SR.GetString(SR.WebSockets_OriginCheckFailed));
                }
            }

            string subprotocol = null;
            if (options != null && !String.IsNullOrEmpty(options.SubProtocol)) {
                // AspNetWebSocketOptions.set_SubProtocol() already checked that the provided value is valid
                subprotocol = options.SubProtocol;
            }

            if (subprotocol != null) {
                IList<string> incomingProtocols = WebSocketRequestedProtocols;
                if (incomingProtocols == null || !incomingProtocols.Contains(subprotocol, StringComparer.Ordinal)) {
                    // The caller requested a subprotocol that wasn't in the list of accepted protocols coming from the client.
                    // This is disallowed by the WebSockets protocol spec, Sec. 5.2.2 (#2).
                    throw new ArgumentException(SR.GetString(SR.WebSockets_SubProtocolCannotBeNegotiated, subprotocol), "options");
                }
            }
            // End options checking and parsing

            wr.AcceptWebSocket();

            // transition: Inactive -> AcceptWebSocketRequestCalled
            TransitionToWebSocketState(WebSocketTransitionState.AcceptWebSocketRequestCalled);

            Response.StatusCode = (int)HttpStatusCode.SwitchingProtocols; // 101
            if (subprotocol != null) {
                Response.AppendHeader("Sec-WebSocket-Protocol", subprotocol);
                _webSocketNegotiatedProtocol = subprotocol;
            }
            RootedObjects.WebSocketPipeline = new WebSocketPipeline(RootedObjects, this, userFunc, subprotocol);
        }
 public virtual void AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task> userFunc, AspNetWebSocketOptions options) {
     throw new NotImplementedException();
 }       
예제 #7
0
		public override void AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task> userFunc, AspNetWebSocketOptions options)
		{
			//TODO Missing AcceptWebSocketRequest for HttpListenerContext
		}
예제 #8
0
		/// <summary>
		/// Accepts an <see cref="T:System.Web.WebSockets.AspNetWebSocket"/> request using the specified user function and options object.
		/// </summary>
		/// <param name="userFunc">The user function.</param><param name="options">The options object.</param><exception cref="T:System.ArgumentNullException">The <paramref name="userFunc"/> parameter is null.</exception><exception cref="T:System.NotSupportedException">The request is not an <see cref="T:System.Web.WebSockets.AspNetWebSocket"/> request.</exception>
		public void AcceptWebSocketRequest(Func<AspNetWebSocketContext, Task> userFunc, AspNetWebSocketOptions options);