Exemple #1
0
        public static bool IfNoneMatch <TRegion>(this HttpRequest request, string cacheKey, ICacheRegion <TRegion> cache,
                                                 out byte[] stream, out IActionResult result)
        {
            var headers = request.GetTypedHeaders();

            foreach (var etag in headers.IfNoneMatch)
            {
                if (etag.IsWeak)
                {
                    if (!etag.Tag.Equals(cacheKey.WeakETag(false, cache)))
                    {
                        continue;
                    }
                    result = new StatusCodeResult((int)HttpStatusCode.NotModified);
                    stream = default;
                    return(false);
                }

                if (!cache.TryGetValue(cacheKey, out stream) || !etag.Tag.Equals(stream.StrongETag(cache)))
                {
                    continue;
                }
                result = new StatusCodeResult((int)HttpStatusCode.NotModified);
                return(false);
            }

            result = default;
            stream = default;
            return(true);
        }
        public async Task <IActionResult> GetSyndicationFeed([FromRoute] string controller,
                                                             [FromHeader(Name = Constants.HeaderNames.Accept)]
                                                             string contentType, [FromFilter] Encoding encoding, [FromRoute] string ns, [FromRoute] ulong rs,
                                                             [FromQuery(Name = "q")] string query = default)
        {
            var mediaType = contentType?.ToLowerInvariant().Trim();
            var charset   = encoding.WebName;
            var queryUrl  = Request.GetEncodedUrl();

            var cacheKey = $"{mediaType}:{charset}:{queryUrl}";

            if (!Request.IsContentStale(cacheKey, _feeds, out var stream, out var lastModified, out var result))
            {
                return(result);
            }

            if (stream != default || _feeds.TryGetValue(cacheKey, out stream))
            {
                return(ServeFeed(cacheKey, stream, mediaType, charset, lastModified));
            }

            var(records, _) = await QueryAsync(controller, ns, rs, query, CancellationToken);

            if (!SyndicationGenerator.TryBuildFeedAsync(queryUrl, ns, rs, records, mediaType, encoding, out stream,
                                                        out lastModified))
            {
                return(new UnsupportedMediaTypeResult());
            }

            _feeds.Set(cacheKey, stream);
            _feeds.Set($"{cacheKey}:{HeaderNames.LastModified}", lastModified);

            return(ServeFeed(cacheKey, stream, mediaType, charset, lastModified));
        }
        public static ulong GetSeed <T>(this ICacheRegion <T> cache)
        {
            if (!cache.TryGetValue(nameof(GetSeed), out ulong seed))
            {
                cache.Set(nameof(GetSeed), seed = BitConverter.ToUInt64(Encoding.UTF8.GetBytes(typeof(T).Name), 0));
            }

            return(seed);
        }
Exemple #4
0
        public static bool IfModifiedSince <TRegion>(this HttpRequest request, string cacheKey,
                                                     ICacheRegion <TRegion> cache, out DateTimeOffset?lastModified, out IActionResult result)
        {
            var headers         = request.GetTypedHeaders();
            var ifModifiedSince = headers.IfModifiedSince;

            if (cache.TryGetValue($"{cacheKey}:{HeaderNames.LastModified}", out lastModified) &&
                lastModified <= ifModifiedSince)
            {
                result = new StatusCodeResult((int)HttpStatusCode.NotModified);
                return(false);
            }

            result = default;
            return(true);
        }