Exemplo n.º 1
0
        private async Task <DrapoDynamic> IsRequestCustom(HttpContext context)
        {
            //Cookies
            string theme = null;
            string view  = null;
            Dictionary <string, string> cookieValues = this.ExtractCookieValues(context, this._options.Config.CookieName);

            if (cookieValues != null)
            {
                if (cookieValues.ContainsKey("theme"))
                {
                    theme = cookieValues["theme"];
                }
                if (cookieValues.ContainsKey("view"))
                {
                    view = cookieValues["view"];
                }
                if (theme == "default")
                {
                    theme = null;
                }
                if (view == "default")
                {
                    view = null;
                }
            }
            //Theme Or View
            DrapoDynamic dynamic = await IsRequestThemeOrView(context, theme, view);

            if (dynamic != null)
            {
                if (this._options.Config.HandlerCustom != null)
                {
                    dynamic = await this._options.Config.HandlerCustom(dynamic);
                }
                return(dynamic);
            }
            //Dynamic
            dynamic = await IsRequestDynamic(context, theme, view);

            if (dynamic != null)
            {
                if (this._options.Config.HandlerCustom != null)
                {
                    dynamic = await this._options.Config.HandlerCustom(dynamic);
                }
                return(dynamic);
            }
            return(null);
        }
Exemplo n.º 2
0
        private async Task <DrapoDynamic> IsRequestDynamic(HttpContext context, string theme, string view)
        {
            DrapoDynamic dynamic = new DrapoDynamic();

            if (!this.ExtractRequestDynamic(context, out string tag, out string code))
            {
                return(null);
            }
            for (int i = 0; i < this._options.Config.Dynamics.Count; i++)
            {
                DrapoDynamicHandler dynamicHandler = this._options.Config.Dynamics[i];
                if (dynamicHandler.Tag != tag)
                {
                    continue;
                }
                DrapoDynamicRequest request = new DrapoDynamicRequest();
                request.Code     = code;
                request.Context  = dynamicHandler.UseTheme ? theme : dynamicHandler.UseView ? view : null;
                request.Path     = context.Request.Path;
                request.PathBase = context.Request.PathBase;
                DrapoDynamicResponse response = await dynamicHandler.Handler(request);

                if (response == null)
                {
                    dynamic.Status = 404;
                    return(dynamic);
                }
                dynamic.Status  = response.Status ?? 200;
                dynamic.Headers = response.Headers;
                if (dynamic.Status == 302)
                {
                    return(dynamic);
                }
                dynamic.ETag         = GenerateETag(Encoding.UTF8.GetBytes(response.Content));
                dynamic.LastModified = response.LastModified.ToString("R");
                dynamic.ContentType  = dynamicHandler.ContentType;
                dynamic.ContentData  = response.Content;
                return(dynamic);
            }
            return(null);
        }
Exemplo n.º 3
0
        private async Task <DrapoDynamic> IsRequestCustomType(HttpContext context, string path, string custom, string extension, string contentType)
        {
            string    key  = string.Format("{0}{1}", custom, path);
            DrapoFile file = this.GetCacheFile(key);

            if (file == null)
            {
                file = CreateCacheFile(context, path, custom, extension, key);
            }
            if (!file.Exists)
            {
                return(null);
            }
            DrapoDynamic dynamic = new DrapoDynamic();

            dynamic.ETag         = file.ETag;
            dynamic.LastModified = file.LastModified;
            dynamic.ContentData  = file.ContentData;
            dynamic.ContentType  = contentType;
            return(await Task.FromResult <DrapoDynamic>(dynamic));
        }
Exemplo n.º 4
0
        public async Task Invoke(HttpContext context)
        {
            DrapoComponent     component = null;
            DrapoComponentFile file      = null;
            DrapoDynamic       dynamic   = null;

            //Activator
            if (this.IsActivator(context))
            {
                //JS
                bool isCache = ((context.Request.Headers.Keys.Contains("If-None-Match")) && (context.Request.Headers["If-None-Match"].ToString() == this._libETag));
                context.Response.OnStarting(state =>
                {
                    var httpContext = (HttpContext)state;
                    httpContext.Response.StatusCode      = isCache ? (int)HttpStatusCode.NotModified : (int)HttpStatusCode.OK;
                    httpContext.Response.Headers["ETag"] = new[] { this._libETag };
                    httpContext.Response.Headers.Add("Last-Modified", new[] { this._libLastModified });
                    httpContext.Response.Headers.Add("Cache-Control", new[] { "no-cache" });
                    httpContext.Response.Headers.Add("Content-Type", new[] { "application/javascript" });
                    AppendHeaderContainerId(httpContext);
                    return(Task.FromResult(0));
                }, context);
                if (!isCache)
                {
                    await context.Response.WriteAsync(this._libContent);
                }
            }
            else if (this.IsConfig(context))
            {
                //Config
                bool isCache = ((context.Request.Headers.Keys.Contains("If-None-Match")) && (context.Request.Headers["If-None-Match"].ToString() == this._configETag));
                context.Response.OnStarting(state =>
                {
                    var httpContext = (HttpContext)state;
                    httpContext.Response.StatusCode      = isCache ? (int)HttpStatusCode.NotModified : (int)HttpStatusCode.OK;
                    httpContext.Response.Headers["ETag"] = new[] { this._configETag };
                    httpContext.Response.Headers.Add("Last-Modified", new[] { this._libLastModified });
                    httpContext.Response.Headers.Add("Cache-Control", new[] { "no-cache" });
                    httpContext.Response.Headers.Add("Content-Type", new[] { "application/json" });
                    AppendHeaderContainerId(httpContext);
                    return(Task.FromResult(0));
                }, context);
                if (!isCache)
                {
                    await context.Response.WriteAsync(this._configContent);
                }
            }
            else if (this.IsComponentFile(context, out component, out file))
            {
                //Component File
                string key     = this.CreateKeyComponentFile(component, file);
                string eTag    = this.GetComponentFileEtag(component, file, key);
                bool   isCache = ((context.Request.Headers.Keys.Contains("If-None-Match")) && (context.Request.Headers["If-None-Match"].ToString() == eTag));
                context.Response.OnStarting(state =>
                {
                    var httpContext = (HttpContext)state;
                    httpContext.Response.StatusCode      = isCache ? (int)HttpStatusCode.NotModified : (int)HttpStatusCode.OK;
                    httpContext.Response.Headers["ETag"] = new[] { eTag };
                    httpContext.Response.Headers.Add("Last-Modified", new[] { this.GetComponentFileLastModified(component, file, key) });
                    httpContext.Response.Headers.Add("Cache-Control", new[] { "no-cache" });
                    httpContext.Response.Headers.Add("Content-Type", new[] { this.GetComponentFileContentType(component, file, key) });
                    AppendHeaderContainerId(httpContext);
                    return(Task.FromResult(0));
                }, context);
                if (!isCache)
                {
                    await context.Response.WriteAsync(this.GetComponentFileContent(component, file, key));
                }
            }
            else if ((dynamic = await this.IsRequestCustom(context)) != null)
            {
                //Custom
                bool isCache = ((context.Request.Headers.Keys.Contains("If-None-Match")) && (context.Request.Headers["If-None-Match"].ToString() == dynamic.ETag));
                context.Response.OnStarting(state =>
                {
                    var httpContext = (HttpContext)state;
                    httpContext.Response.StatusCode = isCache ? (int)HttpStatusCode.NotModified : dynamic.Status;
                    if (!string.IsNullOrEmpty(dynamic.ETag))
                    {
                        httpContext.Response.Headers["ETag"] = new[] { dynamic.ETag }
                    }
                    ;
                    if (!string.IsNullOrEmpty(dynamic.LastModified))
                    {
                        httpContext.Response.Headers.Add("Last-Modified", new[] { dynamic.LastModified });
                    }
                    httpContext.Response.Headers.Add("Cache-Control", new[] { "no-cache" });
                    if (!string.IsNullOrEmpty(dynamic.ContentType))
                    {
                        httpContext.Response.Headers.Add("Content-Type", new[] { dynamic.ContentType });
                    }
                    if (dynamic.Headers != null)
                    {
                        foreach (KeyValuePair <string, string> entry in dynamic.Headers)
                        {
                            httpContext.Response.Headers.Add(entry.Key, new[] { entry.Value });
                        }
                    }
                    AppendHeaderContainerId(httpContext);
                    return(Task.FromResult(0));
                }, context);
                if ((!isCache) && (!string.IsNullOrEmpty(dynamic.ContentData)))
                {
                    await context.Response.WriteAsync(dynamic.ContentData, Encoding.UTF8);
                }
            }
            else
            {
                if (HasHeaderContainerId())
                {
                    context.Response.OnStarting(state =>
                    {
                        var httpContext = (HttpContext)state;
                        AppendHeaderContainerId(httpContext);
                        return(Task.FromResult(0));
                    }, context);
                }
                await _next.Invoke(context);
            }
        }