示例#1
0
 private string GetComponentFileContentInternal(DrapoComponent component, DrapoComponentFile file, string key)
 {
     if (file.ResourceType == DrapoResourceType.Embedded)
     {
         return(this.GetComponentFileContentInternalEmbedded(component, file, key));
     }
     throw new Exception("Drapo: resource type not supported");
 }
示例#2
0
 private string GetComponentFileLastModified(DrapoComponent component, DrapoComponentFile file, string key)
 {
     if (!this._cacheComponentFileLastModified.ContainsKey(key))
     {
         this._cacheComponentFileLastModified.TryAdd(key, this.GetComponentFileLastModifiedInternal(component, file, key));
     }
     return(this._cacheComponentFileLastModified[key]);
 }
示例#3
0
 private string GetComponentFileContentType(DrapoComponent component, DrapoComponentFile file, string key)
 {
     if (!this._cacheComponentFileContentType.ContainsKey(key))
     {
         this._cacheComponentFileContentType.TryAdd(key, this.GetComponentFileContentTypeInternal(component, file, key));
     }
     return(this._cacheComponentFileContentType[key]);
 }
示例#4
0
        public DrapoComponentFile CreateFile(string name, DrapoFileType type, DrapoResourceType resourceType, string path)
        {
            DrapoComponentFile file = new DrapoComponentFile();

            file.Name         = name;
            file.Type         = type;
            file.ResourceType = resourceType;
            file.Path         = path;
            this.Files.Add(file);
            return(file);
        }
示例#5
0
 private string GetComponentFileContentTypeInternal(DrapoComponent component, DrapoComponentFile file, string key)
 {
     if (file.Type == DrapoFileType.View)
     {
         return("text/html");
     }
     else if (file.Type == DrapoFileType.Script)
     {
         return("application/javascript");
     }
     else if (file.Type == DrapoFileType.Style)
     {
         return("text/css");
     }
     throw new Exception("Drapo: content type not supported");
 }
示例#6
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);
            }
        }
示例#7
0
 private string GetComponentFileLastModifiedInternal(DrapoComponent component, DrapoComponentFile file, string key)
 {
     if (file.ResourceType == DrapoResourceType.Embedded)
     {
         return(this._libLastModified);
     }
     throw new Exception("Drapo: can't find last modified");
 }
示例#8
0
        private string GetComponentFileContentInternalEmbedded(DrapoComponent component, DrapoComponentFile file, string key)
        {
            //We only support embedded drapo resources for now
            Dictionary <string, string> resources = GetResources();

            if (resources.ContainsKey(file.Path))
            {
                return(resources[file.Path]);
            }
            throw new Exception("Drapo: resource embedded not found");
        }
示例#9
0
 private string GetComponentFileEtagInternal(DrapoComponent component, DrapoComponentFile file, string key)
 {
     return(GenerateETag(Encoding.UTF8.GetBytes(this.GetComponentFileContent(component, file, key))));
 }
示例#10
0
 private string CreateKeyComponentFile(DrapoComponent component, DrapoComponentFile file)
 {
     return(string.Format("{0}:{1}", component.Name, file.Name));
 }
示例#11
0
        private bool IsComponentFile(HttpContext context, out DrapoComponent component, out DrapoComponentFile file)
        {
            component = null;
            file      = null;
            string url   = context.Request.Path.Value;
            int    index = url.IndexOf("/components/");

            if (index < 0)
            {
                return(false);
            }
            string[] split = url.Substring(index + 12).Split('/');
            if (split.Length != 2)
            {
                return(false);
            }
            component = this._options.Config.GetComponent(split[0]);
            if (component == null)
            {
                return(false);
            }
            file = component.GetFile(split[1]);
            if (file == null)
            {
                return(false);
            }
            return(file.ResourceType == DrapoResourceType.Embedded);
        }