Esempio n. 1
0
        public bool TryGetMatchedUpstreamContext(InvokeUpstreamParameters parameters, out UpstreamContext upstreamContext)
        {
            upstreamContext = null;
            var templates = _optionsMonitor.CurrentValue.Templates;

            if (templates == null)
            {
                return(false);
            }

            // order matters
            foreach (var templateItem in templates)
            {
                if (templateItem.IsMatch(parameters))
                {
                    upstreamContext     = new UpstreamContext();
                    upstreamContext.Uri = Utils.GetUpstreamUrl(templateItem.UrlTemplate, parameters.Hub, parameters.Category, parameters.Event);
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> TriggerAsync(UpstreamContext context, IHttpUpstreamPropertiesFeature upstreamProperties, InvokeUpstreamParameters parameters, Action <HttpRequestMessage> configureRequest = null, CancellationToken token = default)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, context.Uri);

            request.Headers.Add(Constants.Headers.AsrsConnectionIdHeader, upstreamProperties.ConnectionId);
            request.Headers.Add(Constants.Headers.AsrsHubNameHeader, parameters.Hub);
            request.Headers.Add(Constants.Headers.AsrsCategory, parameters.Category);
            request.Headers.Add(Constants.Headers.AsrsEvent, parameters.Event);

            // userId can be null when we allow anonymous login
            if (!string.IsNullOrEmpty(upstreamProperties.UserIdentifier))
            {
                request.Headers.Add(Constants.Headers.AsrsUserId, upstreamProperties.UserIdentifier);
            }

            if (upstreamProperties.ClaimStrings?.Count > 0)
            {
                request.Headers.Add(Constants.Headers.AsrsUserClaims, upstreamProperties.ClaimStrings);
            }

            if (!string.IsNullOrEmpty(upstreamProperties.QueryString))
            {
                request.Headers.Add(Constants.Headers.AsrsClientQueryString, upstreamProperties.QueryString);
            }

            var signature = upstreamProperties.GetSignatures(new string[] { AppBuilderExtensions.AccessKey });

            if (signature != null)
            {
                request.Headers.Add(Constants.Headers.AsrsSignature, signature);
            }

            configureRequest?.Invoke(request);

            return(await SafeAuthAndSendAsync(request, parameters.ToString(), token));
        }
        public static Task <HttpResponseMessage> TriggerAsync(this IHttpUpstreamTrigger trigger, UpstreamContext context, HubConnectionContext connectionContext, InvokeUpstreamParameters parameters, Action <HttpRequestMessage> configureRequest = null, CancellationToken token = default)
        {
            var upstreamFeatures = connectionContext.Features.Get <IHttpUpstreamPropertiesFeature>();

            if (upstreamFeatures == null)
            {
                // defensive code, should not happen
                throw new InvalidOperationException("IHttpUpstreamPropertiesFeature expected");
            }

            return(trigger.TriggerAsync(context, upstreamFeatures, parameters, configureRequest, token));
        }
        public static async Task <HttpResponseMessage> GetResponseAsync(this IHttpUpstreamTrigger httpUpstream, UpstreamContext context, HubConnectionContext connectionContext, ServerlessProtocol.InvocationMessage message, InvokeUpstreamParameters parameters, MediaTypeHeaderValue mediaType = null, CancellationToken token = default)
        {
            var upstream = connectionContext.Features.Get <IHttpUpstreamPropertiesFeature>();

            if (upstream == null)
            {
                // defensive code, should not happen
                throw new InvalidOperationException("IHttpUpstreamPropertiesFeature expected");
            }

            if (message.Payload.IsSingleSegment)
            {
                return(await httpUpstream.TriggerAsync(
                           context,
                           upstream,
                           parameters,
                           request =>
                {
                    request.Content = new ReadOnlyMemoryContent(message.Payload.First);
                    request.Content.Headers.ContentType = mediaType;
                },
                           token));
            }

            using (var owner = ExactSizeMemoryPool.Shared.Rent((int)message.Payload.Length))
            {
                message.Payload.CopyTo(owner.Memory.Span);

                return(await httpUpstream.TriggerAsync(
                           context,
                           upstream,
                           parameters,
                           request =>
                {
                    request.Content = new ReadOnlyMemoryContent(owner.Memory);
                    request.Content.Headers.ContentType = mediaType;
                },
                           token));
            }
        }