Example #1
0
        public static string GetTemplateScript(Content content, string skin, string category)
        {
            var response      = HttpContext.Current.Response;
            var cacheSettings = HttpHeaderTools.GetCacheHeaderSetting(content.Path, "File", "js");
            var maxAge        = TimeSpan.FromSeconds(cacheSettings.HasValue ? cacheSettings.Value : 60);

            response.ContentType = "application/javascript";

            var script = HtmlTemplateCache.GetScript(content.Path, skin, category);

            if (script.FromCache)
            {
                HttpHeaderTools.EndResponseForClientCache(script.CacheDate, false);

                // If the response should end because there was no change, return an empty result
                // instead of flushing and ending the request to avoid a thread abort exception.
                if (response.StatusCode == 304)
                {
                    HttpHeaderTools.SetCacheControlHeaders(HttpCacheability.Public, script.CacheDate, maxAge);
                    return(string.Empty);
                }
            }

            HttpHeaderTools.SetCacheControlHeaders(HttpCacheability.Public, script.CacheDate, maxAge);

            return(script.Script);
        }
Example #2
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        public virtual void ProcessRequestCore(HttpContextBase context)
        {
            var requestedNode = RequestedNode;
            var propertyName  = PropertyName;

            if (string.IsNullOrEmpty(propertyName) || requestedNode == null)
            {
                context.Response.StatusCode = 404;
                return;
            }

            // Get the stream through our provider to let 3rd party developers serve custom data
            string         contentType;
            BinaryFileName fileName;

            using (var binaryStream = DocumentBinaryProvider.Current.GetStream(requestedNode, propertyName, out contentType, out fileName))
            {
                if (binaryStream == null)
                {
                    return;
                }

                using (var resizedStream = GetResizedOrOriginalStream(binaryStream, contentType))
                {
                    // We need to Flush the headers before we start to stream the actual binary.
                    context.Response.ContentType = contentType;
                    context.Response.AppendHeader("Content-Length", resizedStream.Length.ToString());

                    HttpHeaderTools.SetContentDispositionHeader(fileName);

                    HttpHeaderTools.SetCacheControlHeaders(lastModified: requestedNode.ModificationDate);
                    if (this.MaxAge.HasValue)
                    {
                        HttpHeaderTools.SetCacheControlHeaders(maxAge: this.MaxAge);
                    }

                    context.Response.StatusCode = 200;
                    context.Response.Flush();

                    resizedStream.Position = 0;

                    // Let ASP.NET handle sending bytes to the client.
                    resizedStream.CopyTo(context.Response.OutputStream);
                }
            }

            // Let the client code log file downloads
            var file = requestedNode as ContentRepository.File;

            if (file != null)
            {
                ContentRepository.File.Downloaded(file.Id);
            }
        }
Example #3
0
        private static void OnPreRenderComplete(object sender, EventArgs args)
        {
            // Add a link tag for every CSS bundle in the current PortalContext

            var header = ((System.Web.UI.Page)sender).Header;

            foreach (var bundle in Current.CssBundles)
            {
                // Also adding it to the bundle handler
                bundle.Close();
                BundleHandler.AddBundleIfNotThere(bundle);
                ThreadPool.QueueUserWorkItem(x => BundleHandler.AddBundleToCache(bundle));

                if (BundleHandler.IsBundleInCache(bundle))
                {
                    var cssLink = new HtmlLink();

                    cssLink.Href = "/" + BundleHandler.UrlPart + "/" + bundle.FakeFilename;
                    cssLink.Attributes["rel"]   = "stylesheet";
                    cssLink.Attributes["type"]  = bundle.MimeType;
                    cssLink.Attributes["media"] = bundle.Media;

                    header.Controls.Add(cssLink);
                }
                else
                {
                    // The bundle will be complete in a few seconds; disallow caching the page until then
                    HttpHeaderTools.SetCacheControlHeaders(httpCacheability: HttpCacheability.NoCache);

                    foreach (var path in bundle.Paths)
                    {
                        UITools.AddStyleSheetToHeader(header, path, 0, "stylesheet", bundle.MimeType, bundle.Media, string.Empty, false);
                    }
                }

                foreach (var postponedPath in bundle.PostponedPaths)
                {
                    UITools.AddStyleSheetToHeader(header, postponedPath, 0, "stylesheet", bundle.MimeType, bundle.Media, string.Empty, false);
                }
            }
        }
Example #4
0
        public void ProcessRequest(HttpContext context)
        {
            // Handling If-Modified-Since

            var modifiedSinceHeader = HttpContext.Current.Request.Headers["If-Modified-Since"];

            if (modifiedSinceHeader != null)
            {
                DateTime ifModifiedSince;

                if (DateTime.TryParse(modifiedSinceHeader, out ifModifiedSince) && ifModifiedSince != DateTime.MinValue)
                {
                    // convert the client local time to UTC
                    var utcIfModifiedSince   = ifModifiedSince.ToUniversalTime();
                    var lastModificationDate = GetLastResourceModificationDate(utcIfModifiedSince);

                    if (lastModificationDate <= utcIfModifiedSince)
                    {
                        context.Response.Clear();
                        context.Response.StatusCode = 304;
                        context.Response.Flush();
                        context.Response.End();
                    }
                }
            }

            // Handling the rest of the request

            var shouldDeny = true;

            try
            {
                var parsedUrl = ParseUrl(context.Request.RawUrl);

                if (parsedUrl != null)
                {
                    var         cultureName = parsedUrl.Item1;
                    var         className   = parsedUrl.Item2;
                    CultureInfo culture     = null;

                    if (!string.IsNullOrEmpty(cultureName))
                    {
                        culture = CultureInfo.GetCultureInfo(cultureName);
                    }

                    if (culture != null && !string.IsNullOrEmpty(className))
                    {
                        var script = ResourceScripter.RenderResourceScript(className, culture);
                        var lastModificationDate = GetLastResourceModificationDate(null);

                        HttpHeaderTools.SetCacheControlHeaders(lastModified: lastModificationDate);

                        // TODO: add an expires header when appropriate, but without clashing with the resource editor

                        context.Response.ContentType = "text/javascript";
                        context.Response.Write(script);
                        context.Response.Flush();

                        shouldDeny = false;
                    }
                }
            }
            catch
            {
                shouldDeny = true;
            }

            // If it failed for some reason, deny it

            if (shouldDeny)
            {
                DenyRequest(context);
            }
        }
Example #5
0
        /// <summary>
        /// Processes the request.
        /// </summary>
        public virtual void ProcessRequestCore(HttpContextBase context)
        {
            var propertyName  = PropertyName;
            var requestedNode = RequestedNode;

            if (string.IsNullOrEmpty(propertyName) || requestedNode == null)
            {
                context.Response.StatusCode = 404;
                return;
            }

            // Get the stream through our provider to let 3rd party developers serve custom data
            string         contentType;
            BinaryFileName fileName;
            var            binaryStream = DocumentBinaryProvider.Current.GetStream(requestedNode, propertyName, out contentType, out fileName);

            if (binaryStream == null)
            {
                return;
            }

            Stream resizedStream;

            // If this is an image and we need to resize it
            if (Width.HasValue && Height.HasValue)
            {
                resizedStream = Image.CreateResizedImageFile(binaryStream, string.Empty, Width.Value, Height.Value, 0, contentType);
            }
            else
            {
                resizedStream = binaryStream;
            }

            // We need to Flush the headers before
            // We start to stream the actual binary.

            context.Response.ContentType = contentType;
            context.Response.AppendHeader("Content-Length", resizedStream.Length.ToString());
            HttpHeaderTools.SetCacheControlHeaders(lastModified: requestedNode.ModificationDate);
            if (this.MaxAge.HasValue)
            {
                HttpHeaderTools.SetCacheControlHeaders(maxAge: this.MaxAge);
            }
            context.Response.StatusCode = 200;
            context.Response.Flush();

            resizedStream.Position = 0;

            var buffer = new byte[Math.Min(resizedStream.Length, RepositoryConfiguration.BinaryChunkSize)];
            int bytesRead;

            //while (bytesRead > 0)
            while ((bytesRead = resizedStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, bytesRead);
                context.Response.Flush();
            }

            // Let the client code log file downloads
            var file = requestedNode as ContentRepository.File;

            if (file != null)
            {
                ContentRepository.File.Downloaded(file.Id);
            }
        }