Ejemplo n.º 1
0
        public async Task Invoke(HttpContext context)
        {
            if (context.Request.Method != HttpMethods.Post ||
                !EnvelopeContentTypes.Contains(context.Request.ContentType) ||
                !_portEndPointOptions.TryGetValue(context.Connection.LocalPort, out var endPointOptions) ||
                !endPointOptions.ContainsPath(context.Request.Path))
            {
                await _next(context);

                return;
            }

            var identity = await AuthenticateAsync(context);

            if (identity == null)
            {
                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                await context.Response.CompleteAsync();

                return;
            }

            var envelope = await ReadEnvelopeAsync(context);

            var channel = new HttpContextChannel(
                context,
                _options.Value.LocalNode,
                identity.ToNode(),
                _envelopeSerializer);

            await HandleEnvelopeAsync(envelope, channel, context);
        }
Ejemplo n.º 2
0
        private async Task HandleEnvelopeAsync(Envelope?envelope, HttpContextChannel channel, HttpContext context)
        {
            switch (envelope)
            {
            case Message message:
                await _transportListener.OnMessageAsync(message, channel, context.RequestAborted);

                break;

            case Notification notification:
                await _transportListener.OnNotificationAsync(notification, channel, context.RequestAborted);

                break;

            case Command command:
                await _transportListener.OnCommandAsync(command, channel, context.RequestAborted);

                break;

            default:
                context.Response.StatusCode = StatusCodes.Status400BadRequest;
                break;
            }
        }