示例#1
0
        public void GlobalSetup()
        {
            _negotiateResponse = new NegotiationResponse
            {
                ConnectionId        = "d100338e-8c01-4281-92c2-9a967fdeebcb",
                AvailableTransports = new List <AvailableTransport>
                {
                    new AvailableTransport
                    {
                        Transport       = "WebSockets",
                        TransferFormats = new List <string>
                        {
                            "Text",
                            "Binary"
                        }
                    }
                }
            };
            _stream = Stream.Null;

            _responseData1 = Encoding.UTF8.GetBytes("{\"connectionId\":\"123\",\"availableTransports\":[]}");
            _responseData2 = Encoding.UTF8.GetBytes("{\"url\": \"http://foo.com/chat\"}");
            _responseData3 = Encoding.UTF8.GetBytes("{\"url\": \"http://foo.com/chat\", \"accessToken\": \"token\"}");
            _responseData4 = Encoding.UTF8.GetBytes("{\"connectionId\":\"123\",\"availableTransports\":[{\"transport\":\"test\",\"transferFormats\":[]}]}");

            var writer = new MemoryBufferWriter();

            NegotiateProtocol.WriteResponse(_negotiateResponse, writer);
            _responseData5 = writer.ToArray();
        }
    private static void WriteNegotiatePayload(IBufferWriter <byte> writer, string?connectionId, string?connectionToken, HttpContext context, HttpConnectionDispatcherOptions options,
                                              int clientProtocolVersion, string?error)
    {
        var response = new NegotiationResponse();

        if (!string.IsNullOrEmpty(error))
        {
            response.Error = error;
            NegotiateProtocol.WriteResponse(response, writer);
            return;
        }

        response.Version             = clientProtocolVersion;
        response.ConnectionId        = connectionId;
        response.ConnectionToken     = connectionToken;
        response.AvailableTransports = new List <AvailableTransport>();

        if ((options.Transports & HttpTransportType.WebSockets) != 0 && ServerHasWebSockets(context.Features))
        {
            response.AvailableTransports.Add(_webSocketAvailableTransport);
        }

        if ((options.Transports & HttpTransportType.ServerSentEvents) != 0)
        {
            response.AvailableTransports.Add(_serverSentEventsAvailableTransport);
        }

        if ((options.Transports & HttpTransportType.LongPolling) != 0)
        {
            response.AvailableTransports.Add(_longPollingAvailableTransport);
        }

        NegotiateProtocol.WriteResponse(response, writer);
    }
示例#3
0
        public void WriteNegotiateResponseWithNullAvailableTransports()
        {
            using (MemoryBufferWriter writer = new MemoryBufferWriter())
            {
                NegotiateProtocol.WriteResponse(new NegotiationResponse(), writer);

                string json = Encoding.UTF8.GetString(writer.ToArray());

                Assert.Equal("{\"negotiateVersion\":0,\"availableTransports\":[]}", json);
            }
        }
示例#4
0
        public Task WriteResponse_MemoryBufferWriter()
        {
            var writer = new MemoryBufferWriter();

            try
            {
                NegotiateProtocol.WriteResponse(_negotiateResponse, writer);
                return(writer.CopyToAsync(_stream));
            }
            finally
            {
                writer.Reset();
            }
        }
示例#5
0
        public void WriteNegotiateResponseWithNullTransferFormats()
        {
            using (MemoryBufferWriter writer = new MemoryBufferWriter())
            {
                NegotiateProtocol.WriteResponse(new NegotiationResponse
                {
                    AvailableTransports = new List <AvailableTransport>
                    {
                        new AvailableTransport()
                    }
                }, writer);

                string json = Encoding.UTF8.GetString(writer.ToArray());

                Assert.Equal("{\"negotiateVersion\":0,\"availableTransports\":[{\"transport\":null,\"transferFormats\":[]}]}", json);
            }
        }
示例#6
0
        private async Task RedirectToService(HttpContext context, string hubName, IList <IAuthorizeData> authorizationData)
        {
            if (!await AuthorizeHelper.AuthorizeAsync(context, authorizationData))
            {
                return;
            }

            NegotiationResponse negotiateResponse = null;

            try
            {
                negotiateResponse = _negotiateHandler.Process(context, hubName);
            }
            catch (AzureSignalRAccessTokenTooLongException ex)
            {
                Log.NegotiateFailed(_logger, ex.Message);
                context.Response.StatusCode = 413;
                await HttpResponseWritingExtensions.WriteAsync(context.Response, ex.Message);

                return;
            }

            var writer = new MemoryBufferWriter();

            try
            {
                context.Response.ContentType = "application/json";
                NegotiateProtocol.WriteResponse(negotiateResponse, writer);
                // Write it out to the response with the right content length
                context.Response.ContentLength = writer.Length;
                await writer.CopyToAsync(context.Response.Body);
            }
            finally
            {
                writer.Reset();
            }
        }
        private async Task RedirectToService(HttpContext context, string hubName, IList <IAuthorizeData> authorizationData)
        {
            if (!await AuthorizeHelper.AuthorizeAsync(context, authorizationData))
            {
                return;
            }

            var negotiateResponse = _negotiateHandler.Process(context, hubName);

            var writer = new MemoryBufferWriter();

            try
            {
                context.Response.ContentType = "application/json";
                NegotiateProtocol.WriteResponse(negotiateResponse, writer);
                // Write it out to the response with the right content length
                context.Response.ContentLength = writer.Length;
                await writer.CopyToAsync(context.Response.Body);
            }
            finally
            {
                writer.Reset();
            }
        }
示例#8
0
        public static async Task RedirectToService(HttpContext context, string hubName, IList <IAuthorizeData> authorizationData)
        {
            var handler       = context.RequestServices.GetRequiredService <NegotiateHandler>();
            var loggerFactory = context.RequestServices.GetService <ILoggerFactory>();
            var logger        = loggerFactory.CreateLogger <ServiceRouteHelper>();

            if (authorizationData != null && !await AuthorizeHelper.AuthorizeAsync(context, authorizationData))
            {
                return;
            }

            NegotiationResponse negotiateResponse = null;

            try
            {
                negotiateResponse = handler.Process(context, hubName);

                if (context.Response.HasStarted)
                {
                    // Inner handler already write to context.Response, no need to continue with error case
                    return;
                }

                // Consider it as internal server error when we don't successfully get negotiate response
                if (negotiateResponse == null)
                {
                    var message = "Unable to get the negotiate endpoint";
                    Log.NegotiateFailed(logger, message);
                    context.Response.StatusCode = 500;
                    await context.Response.WriteAsync(message);

                    return;
                }
            }
            catch (AzureSignalRAccessTokenTooLongException ex)
            {
                Log.NegotiateFailed(logger, ex.Message);
                context.Response.StatusCode = 413;
                await context.Response.WriteAsync(ex.Message);

                return;
            }
            catch (AzureSignalRNotConnectedException e)
            {
                Log.NegotiateFailed(logger, e.Message);
                context.Response.StatusCode = 500;
                await context.Response.WriteAsync(e.Message);

                return;
            }

            var writer = new MemoryBufferWriter();

            try
            {
                context.Response.ContentType = "application/json";
                NegotiateProtocol.WriteResponse(negotiateResponse, writer);
                // Write it out to the response with the right content length
                context.Response.ContentLength = writer.Length;
                await writer.CopyToAsync(context.Response.Body);
            }
            finally
            {
                writer.Reset();
            }
        }