Exemplo n.º 1
0
        /// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The current http context</param>
        /// <returns>An async task</returns>
        public override async Task Invoke(HttpContext context)
        {
            if (!IsHandled(context) && !context.Request.Path.Value.StartsWith("/manager/assets/"))
            {
                var url = context.Request.Path.HasValue ? context.Request.Path.Value : "";

                var response = StartPageRouter.Invoke(api, url);
                if (response != null)
                {
                    context.Request.Path = new PathString(response.Route);

                    if (context.Request.QueryString.HasValue)
                    {
                        context.Request.QueryString = new QueryString(context.Request.QueryString.Value + "&" + response.QueryString);
                    }
                    else
                    {
                        context.Request.QueryString = new QueryString("?" + response.QueryString);
                    }
                }
            }
            await next.Invoke(context);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The current http context</param>
        /// <param name="api">The current api</param>
        /// <param name="service">The application service</param>
        /// <returns>An async task</returns>
        public override async Task Invoke(HttpContext context, IApi api, IApplicationService service)
        {
            if (!IsHandled(context) && !context.Request.Path.Value.StartsWith("/manager/assets/"))
            {
                var url        = context.Request.Path.HasValue ? context.Request.Path.Value : "";
                var siteId     = service.Site.Id;
                var authorized = true;

                var response = await StartPageRouter.InvokeAsync(api, url, siteId);

                if (response != null)
                {
                    _logger?.LogInformation($"Found startpage\n  Route: {response.Route}\n  Params: {response.QueryString}");

                    if (!response.IsPublished)
                    {
                        if (!context.User.HasClaim(Security.Permission.PagePreview, Security.Permission.PagePreview))
                        {
                            _logger?.LogInformation($"User not authorized to preview unpublished page");
                            authorized = false;
                        }
                    }

                    if (authorized)
                    {
                        service.PageId = response.PageId;

                        using (var config = new Config(api))
                        {
                            var headers = context.Response.GetTypedHeaders();

                            if (config.CacheExpiresPages > 0)
                            {
                                _logger?.LogInformation("Caching enabled. Setting MaxAge, LastModified & ETag");

                                headers.CacheControl = new CacheControlHeaderValue
                                {
                                    Public = true,
                                    MaxAge = TimeSpan.FromMinutes(config.CacheExpiresPages),
                                };

                                headers.ETag         = new EntityTagHeaderValue(response.CacheInfo.EntityTag);
                                headers.LastModified = response.CacheInfo.LastModified;
                            }
                            else
                            {
                                headers.CacheControl = new CacheControlHeaderValue
                                {
                                    NoCache = true
                                };
                            }
                        }

                        if (HttpCaching.IsCached(context, response.CacheInfo))
                        {
                            _logger?.LogInformation("Client has current version. Returning NotModified");

                            context.Response.StatusCode = 304;
                            return;
                        }
                        else
                        {
                            context.Request.Path = new PathString(response.Route);

                            if (context.Request.QueryString.HasValue)
                            {
                                context.Request.QueryString = new QueryString(context.Request.QueryString.Value + "&" + response.QueryString);
                            }
                            else
                            {
                                context.Request.QueryString = new QueryString("?" + response.QueryString);
                            }
                        }
                    }
                }
            }
            await _next.Invoke(context);
        }
        /// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The current http context</param>
        /// <returns>An async task</returns>
        public override async Task Invoke(HttpContext context)
        {
            if (!IsHandled(context) && !context.Request.Path.Value.StartsWith("/manager/assets/"))
            {
                var url = context.Request.Path.HasValue ? context.Request.Path.Value : "";

                var response = StartPageRouter.Invoke(api, url);
                if (response != null)
                {
                    if (logger != null)
                    {
                        logger.LogInformation($"Found startpage\n  Route: {response.Route}\n  Params: {response.QueryString}");
                    }

                    using (var config = new Config(api)) {
                        var headers = context.Response.GetTypedHeaders();

                        if (config.CacheExpiresPages > 0)
                        {
                            if (logger != null)
                            {
                                logger.LogInformation("Caching enabled. Setting MaxAge, LastModified & ETag");
                            }

                            headers.CacheControl = new CacheControlHeaderValue()
                            {
                                Public = true,
                                MaxAge = TimeSpan.FromMinutes(config.CacheExpiresPages),
                            };

                            headers.Headers["ETag"] = response.CacheInfo.EntityTag;
                            headers.LastModified    = response.CacheInfo.LastModified;
                        }
                        else
                        {
                            headers.CacheControl = new CacheControlHeaderValue()
                            {
                                NoCache = true
                            };
                        }
                    }

                    if (HttpCaching.IsCached(context, response.CacheInfo))
                    {
                        if (logger != null)
                        {
                            logger.LogInformation("Client has current version. Returning NotModified");
                        }

                        context.Response.StatusCode = 304;
                        return;
                    }
                    else
                    {
                        context.Request.Path = new PathString(response.Route);

                        if (context.Request.QueryString.HasValue)
                        {
                            context.Request.QueryString = new QueryString(context.Request.QueryString.Value + "&" + response.QueryString);
                        }
                        else
                        {
                            context.Request.QueryString = new QueryString("?" + response.QueryString);
                        }
                    }
                }
            }
            await next.Invoke(context);
        }