Ejemplo n.º 1
0
        public AppFunc Invoke(AppFunc next) =>
        async environment =>
        {
            var requestFrame = environment.ReadFromEnvironmentRequest();

            if (StompCommand.IsConnectRequest(requestFrame.Command))
            {
                string stompAcceptVersion;
                var    stompProtocolVersion = requestFrame.Headers.TryGetValue("accept-version", out stompAcceptVersion)
                        ? stompAcceptVersion
                        : "1.0";

                if (environment.ContainsKey("stomp.protocolVersion"))
                {
                    environment["stomp.protocolVersion"] = stompProtocolVersion;
                }
                else
                {
                    environment.Add("stomp.protocolVersion", stompProtocolVersion);
                }
            }

            await next(environment);
        };
Ejemplo n.º 2
0
        public AppFunc Invoke(AppFunc next) =>
        async environment =>
        {
            var stompCommand = (string)environment["stomp.requestMethod"];

            if (StompCommand.IsConnectRequest(stompCommand))
            {
                string version;
                object versionObject;
                if (!environment.TryGetValue("stomp.protocolVersion", out versionObject))
                {
                    version = "1.0";
                }
                else
                {
                    version = versionObject as string;
                }

                if (Options.AcceptedVersions.Contains(version))
                {
                    var sessionId = Guid.NewGuid().ToString();
                    new StompFrame(StompCommand.CONNECTED, new Dictionary <string, string>
                    {
                        ["version"] = version,
                        ["session"] = sessionId
                    }).WriteToEnvironmentResponse(environment);

                    //Give other middleware a chance to disagree.
                    await next(environment);

                    //If there wasn't a disagreement.
                    if ((string)environment["stomp.responseMethod"] == StompCommand.CONNECTED)
                    {
                        Options.AddSession(sessionId);
                    }
                }
                else
                {
                    new StompFrame(StompCommand.ERROR, new Dictionary <string, string>
                    {
                        { "message", "No acceptable protocol version negotiated." }
                    }).WriteToEnvironmentResponse(environment);
                }
            }
            else
            {
                var headers = (ImmutableArray <KeyValuePair <string, string> >)environment["stomp.requestHeaders"];

                string sessionId;

                if (headers.TryGetValue("session", out sessionId) && !string.IsNullOrWhiteSpace(sessionId))
                {
                    if (Options.ValidateSession(sessionId))
                    {
                        await next(environment);
                    }
                    else
                    {
                        new StompFrame(StompCommand.ERROR, new Dictionary <string, string>
                        {
                            { "message", "Invalid or expired/rejected session identifier." }
                        }).WriteToEnvironmentResponse(environment);
                    }
                }
                else
                {
                    new StompFrame(StompCommand.ERROR, new Dictionary <string, string> {
                        { "message", "Session not established." }
                    }).WriteToEnvironmentResponse(environment);
                }
            }
        };