private static bool MatchesBeatPulseRequestPath(this HttpContext context, BeatPulseOptions options)
        {
            var routeValues     = new RouteValueDictionary();
            var templateMatcher = GetTemplateMatcher(options);

            return(templateMatcher.TryMatch(context.Request.Path, routeValues));
        }
        public static Task WriteLivenessMessage(this HttpResponse response, BeatPulseOptions options, OutputLivenessMessage message)
        {
            var defaultContentType = options.DetailedOutput ? "application/json" : "text/plain";

            const string noCacheOptions = "no-cache, no-store, must-revalidate";
            const string noCachePragma  = "no-cache";
            const string defaultExpires = "0";

            response.Headers["Content-Type"] = new[] { defaultContentType };

            if (options.CacheOutput)
            {
                if (options.CacheMode == CacheMode.Header || options.CacheMode == CacheMode.HeaderAndServerMemory)
                {
                    response.Headers["Cache-Control"] = new[] { $"public, max-age={options.CacheDuration}" };
                }
            }
            else
            {
                response.Headers["Cache-Control"] = new[] { noCacheOptions };
                response.Headers["Pragma"]        = new[] { noCachePragma };
                response.Headers["Expires"]       = new[] { defaultExpires };
            }

            response.StatusCode = message.Code;

            var content = options.DetailedOutput ? JsonConvert.SerializeObject(message)
                : Enum.GetName(typeof(HttpStatusCode), message.Code);

            return(response.WriteAsync(content));
        }
Esempio n. 3
0
 public BeatPulseMiddleware(RequestDelegate next, IEnumerable <IBeatPulseAuthenticationFilter> authenticationFilters, BeatPulseOptions options)
 {
     _next    = next ?? throw new ArgumentNullException(nameof(next));
     _options = options;
     _authenticationFilters = authenticationFilters;
     _cache = new ConcurrentDictionary <string, OutputLivenessMessage>();
 }
        public static string GetBeatPulseRequestPath(this HttpContext context, BeatPulseOptions options)
        {
            var routeValues     = new RouteValueDictionary();
            var templateMatcher = GetTemplateMatcher(options);

            templateMatcher.TryMatch(context.Request.Path, routeValues);
            return(routeValues[BeatPulseKeys.BEATPULSE_PATH_SEGMENT_NAME].ToString());
        }
        private static TemplateMatcher GetTemplateMatcher(BeatPulseOptions options)
        {
            var templateMatcher = new TemplateMatcher(TemplateParser.Parse($"{options.BeatPulsePath}/{{{BeatPulseKeys.BEATPULSE_PATH_SEGMENT_NAME}}}"),
                                                      new RouteValueDictionary()
            {
                { BeatPulseKeys.BEATPULSE_PATH_SEGMENT_NAME, string.Empty }
            });

            return(templateMatcher);
        }
Esempio n. 6
0
 public BeatPulseMiddleware(RequestDelegate next, BeatPulseOptions options)
 {
     _next            = next ?? throw new ArgumentNullException(nameof(next));
     _options         = options;
     _cache           = new ConcurrentDictionary <string, OutputMessage>();
     _templateMatcher = new TemplateMatcher(TemplateParser.Parse($"{options.BeatPulsePath}/{{{BeatPulseKeys.BEATPULSE_PATH_SEGMENT_NAME}}}"),
                                            new RouteValueDictionary()
     {
         { BeatPulseKeys.BEATPULSE_PATH_SEGMENT_NAME, string.Empty }
     });                                                                                             //match template for uri like /hc/{segment}
 }
Esempio n. 7
0
        bool TryFromCache(string path, BeatPulseOptions options, out OutputLivenessMessage message)
        {
            message = null;

            if (options.CacheOutput &&
                options.CacheMode.UseServerMemory() &&
                _cache.TryGetValue(path, out message))
            {
                var seconds = (DateTime.UtcNow - message.EndAtUtc).TotalSeconds;

                if (options.CacheDuration > seconds)
                {
                    return(true);
                }
                else
                {
                    _cache.TryRemove(path, out OutputLivenessMessage removed);

                    return(false);
                }
            }

            return(false);
        }
 public static bool IsBeatPulseRequest(this HttpContext context, BeatPulseOptions options)
 {
     return(context.Request.Method == HttpMethods.Get &&
            context.MatchesBeatPulseRequestPath(options));
 }
Esempio n. 9
0
 public BeatPulseFilter(BeatPulseOptions options)
 {
     _options = options;
 }