예제 #1
0
        public HttpResponseMessage Vulcanize(string id)
        {
            var rootPath = HostingEnvironment.MapPath("~");
            var filePath = Path.Combine(rootPath, id);

            if (!File.Exists(filePath))
            {
                return(new HttpResponseMessage(HttpStatusCode.NotFound));
            }

            var mimeType = System.Web.MimeMapping.GetMimeMapping(id);

            if (mimeType == "text/html")
            {
                var html = Vulcanizer.Generate(id, File.ReadAllText(filePath), true);
                return(new HttpResponseMessage {
                    Content = new StringContent(html, Encoding.UTF8, mimeType)
                });
            }

            if (id.StartsWith("WebComponents", StringComparison.OrdinalIgnoreCase))
            {
                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read))
                };
                result.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType);

                return(result);
            }

            return(new HttpResponseMessage(HttpStatusCode.NotFound));
        }
예제 #2
0
        public HttpResponseMessage Get(string id = null, string branch = null)
        {
            if (UseWeb2Home && string.IsNullOrEmpty(id))
            {
                return new HttpResponseMessage {
                           Content = new StringContent(File.ReadAllText(HostingEnvironment.MapPath("~/index.html")), Encoding.UTF8, "text/html")
                }
            }
            ;

            var    extension = Path.GetExtension(id);
            string mediaType;

            if (extension == null || !mediaTypes.TryGetValue(extension, out mediaType))
            {
                return(new HttpResponseMessage(HttpStatusCode.NotFound));
            }

            if (UseWeb2Home)
            {
                if (id.ToLower() == "webcomponents-lite.js" || id.ToLower() == "webcomponents-lite.min.js")
                {
                    id = "Libs/webcomponentsjs/" + id;
                }

                // NOTE: In development we serve the files directly from disk
                var srcFolder = Path.Combine(Web2Home, branch + "/src");
                var filePath  = Path.Combine(srcFolder, id);
                switch (extension)
                {
                case ".html":
                    var html = Vulcanizer.Generate(id, File.ReadAllText(filePath), true, false, srcFolder);
                    return(new HttpResponseMessage {
                        Content = new StringContent(html, Encoding.UTF8, mediaTypes[extension])
                    }.AddVersion(id));

                case ".css":
                case ".js":
                {
                    var js = File.ReadAllText(filePath);
                    if (id == "Libs/Vidyano/vidyano.js" || id == "ServiceWorker/service-worker.js")
                    {
                        var lastUpdated = new DirectoryInfo(srcFolder).GetFiles("*.*", SearchOption.AllDirectories).OrderByDescending(f => f.LastWriteTime).First();
                        js = js.Replace("\"latest\"", "\"latest." + lastUpdated.LastWriteTime.ToLongTimeString().Replace(":", ".") + "\"");
                    }

                    return(new HttpResponseMessage {
                            Content = new StringContent(js, Encoding.UTF8, mediaTypes[extension])
                        });
                }
                }
            }

            var ifNoneMatch = Request.Headers.IfNoneMatch.FirstOrDefault();
            Tuple <string, string> cacheInfo;

            if (cache.TryGetValue(id, out cacheInfo) && ifNoneMatch?.Tag == cacheInfo.Item1)
            {
                return(new HttpResponseMessage(HttpStatusCode.NotModified).AddVersion(id));
            }

            string content;

            lock (syncRoot)
            {
                if (cacheInfo == null && !cache.TryGetValue(id, out cacheInfo))
                {
                    try
                    {
                        content = GetEmbeddedResource(id);

                        switch (extension)
                        {
                        case ".css":
                        case ".js":
                            break;

                        case ".html":
                            content = Vulcanizer.Generate(id, content, false, false);
                            break;

                        default:
                            return(new HttpResponseMessage(HttpStatusCode.BadRequest));
                        }

                        cacheInfo = Tuple.Create("\"" + GetSHA256(content) + "\"", content);
                        cache[id] = cacheInfo;
                    }
                    catch (FileNotFoundException fnfe)
                    {
                        Trace.WriteLine($"Missing path '{fnfe.FileName}' on '{id}'.");
                        return(new HttpResponseMessage(HttpStatusCode.NotFound));
                    }
                }
                else if (ifNoneMatch?.Tag == cacheInfo.Item1)
                {
                    return(new HttpResponseMessage(HttpStatusCode.NotModified));
                }
                else
                {
                    content = cacheInfo.Item2;
                }
            }

            HttpContent httpContent = new StringContent(content, utf8NoBom, mediaType);

            if (Compress)
            {
                var encoding = Request.Headers.AcceptEncoding.OrderByDescending(ae => ae.Quality).Select(ae => ae.Value).FirstOrDefault();
                if (encoding != null && (encoding == "gzip" || encoding == "deflate"))
                {
                    httpContent = new CompressedContent(httpContent, encoding);
                }
            }

            var response = new HttpResponseMessage {
                Content = httpContent
            }.AddVersion(id);

            response.Headers.ETag = new EntityTagHeaderValue(cacheInfo.Item1);

            return(response);
        }