Esempio n. 1
0
        public IEsiFragment Parse(IReadOnlyDictionary <string, string> attributes, string body)
        {
            if (attributes == null)
            {
                throw new ArgumentNullException(nameof(attributes));
            }
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }

            var whenFragments =
                from match in WhenTagRegex.Matches(body).Cast <Match>()
                let test = match.Groups[1].Value
                           let innerBody = match.Groups[2].Value
                                           let innerFragment = _bodyParser.Parse(innerBody)
                                                               select new EsiWhenFragment(ParseTestExpression(test), innerFragment);

            var otherwiseMatch = OtherwiseTagRegex.Match(body);

            var otherwiseFragment = otherwiseMatch.Success
                ? _bodyParser.Parse(otherwiseMatch.Groups[1].Value)
                : new EsiIgnoreFragment();

            return(new EsiChooseFragment(whenFragments.ToArray(), otherwiseFragment));
        }
Esempio n. 2
0
        public IEsiFragment Parse(IReadOnlyDictionary <string, string> attributes, string body)
        {
            if (attributes == null)
            {
                throw new ArgumentNullException(nameof(attributes));
            }
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }

            var tags = TagRegex.Matches(body)
                       .Cast <Match>()
                       .ToDictionary(m => m.Groups[1].Value, m => m.Groups[2].Value, StringComparer.OrdinalIgnoreCase);

            var attemptFound = tags.TryGetValue("esi:attempt", out var attemptBody);
            var exceptFound  = tags.TryGetValue("esi:except", out var exceptBody);

            if (!attemptFound)
            {
                return(new EsiIgnoreFragment());
            }

            var attemptFragment = _bodyParser.Parse(attemptBody);
            var exceptFragment  = exceptFound
                ? _bodyParser.Parse(exceptBody)
                : new EsiIgnoreFragment();

            return(new EsiTryFragment(attemptFragment, exceptFragment));
        }
        private async Task <CacheResponse <IEsiFragment> > RequestAndParse(
            Uri uri, EsiExecutionContext executionContext)
        {
            var response = await _httpLoader.Get(uri, executionContext);

            response.EnsureSuccessStatusCode();

            var content = await response.Content.ReadAsStringAsync();

            var fragment = _esiBodyParser.Parse(content);

            return(CacheResponse.Create(fragment, response.Headers.CacheControl, response.Headers.Vary.ToList()));
        }
Esempio n. 4
0
        public async Task Invoke(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Response.StatusCode == 304 || context.Request.Headers.ContainsKey("X-Esi"))
            {
                await _next(context);

                return;
            }

            var executionContext = new EsiExecutionContext(
                context.Request.Headers.ToDictionary(), context.Request.GetVariablesFromContext());
            var pageUri = context.Request.GetPageUri();

            var(found, cachedResponse) = await _cache.TryGet <FragmentPageResponse>(pageUri, executionContext);

            IEsiFragment fragment;

            if (found)
            {
                context.Response.CopyHeaders(cachedResponse.Headers);
                fragment = cachedResponse.Fragment;
            }
            else
            {
                var acceptEncoding = context.Request.Headers[HeaderNames.AcceptEncoding];
                context.Request.Headers[HeaderNames.AcceptEncoding] = StringValues.Empty;
                var body = await _next.TryIntercept(context);

                context.Request.Headers[HeaderNames.AcceptEncoding] = acceptEncoding;

                if (body == null)
                {
                    return;
                }

                fragment = _parser.Parse(body);

                await StoreFragmentInCache(context, pageUri, executionContext, fragment);
            }

            var content = await _executor.Execute(fragment, executionContext);

            await context.Response.WriteAllAsync(content);
        }