Example #1
0
        /// <summary>
        /// This method prepares the HTTP body based on the browser content encoding settings.
        /// </summary>
        /// <param name="context">The current compile context.</param>
        /// <param name="blob">The UTF-8 formatted byte array.</param>
        /// <returns>Returns a ContentCompilerMessageFragmentBody object containing the prepared output binary data.</returns>
        protected virtual ResourceManagerMessageFragmentBody PrepareBody(
            ResourceManagerContext context, BinaryContent content, out string status)
        {
            DateTime? expiry = null;

            string cacheControl = context.Request.Data.RequestHeaderGet("Cache-Control");
            bool allowNotModified = cacheControl == null || cacheControl.ToLowerInvariant() != "no-cache";

            
            int? expiryIns = content.CacheExpiry;
            ResourceManagerMessageFragmentBody body = null;

            string eTag = content.IDVersion.ToString("N").ToUpperInvariant();
            string eTagValidate = context.Request.Data.RequestIfNoneMatch.Trim(new char[]{'"',' '});

            if ((content.CacheOptions & ContentCacheOptions.Cacheable)>0)
                expiry = DateTime.Now.AddSeconds(expiryIns.Value);

            context.ContextSettings.ETagAdd(content.IDVersion, content.IDContent, expiry, content.GetType(), expiryIns);

            //Next check whether we have an etag in the request, and whether it matches the content.
            if (!allowNotModified || eTagValidate == "" || eTag != eTagValidate)
            {
                //Ok, get the body.
                body = PrepareBodyOutput(context, content);

                body.ETag = string.Format(@"""{0}""", eTag);
                if (expiry.HasValue)
                    body.Expires = CH.ConvertToRFC1123DateString(expiry.Value);

                status = CH.HTTPCodes.OK_200;
            }
            else
            {
                if (expiry.HasValue)
                    context.Request.Data.ResponseHeaderAdd("Expires", CH.ConvertToRFC1123DateString(expiry.Value));
                context.Request.Data.ResponseHeaderAdd("ETag", string.Format(@"""{0}""", eTag));

                status = CH.HTTPCodes.NotModified_304;
            }

            if (expiry.HasValue)
                context.Request.Data.ResponseHeaderAdd("Cache-control", "public");
            else if ((content.CacheOptions & ContentCacheOptions.VersionCheck) > 0)
                context.Request.Data.ResponseHeaderAdd("Cache-control", "must-revalidate");

            return body;
        }