Exemplo n.º 1
0
    private static MemoryStream CompressResponse(Stream responseStream, HttpApplication app, string key)
    {
        MemoryStream memoryStream = new MemoryStream();

        CompressionModule.StreamCopy(responseStream, memoryStream);
        responseStream.Dispose();
        byte[] array = memoryStream.ToArray();
        memoryStream.Dispose();
        MemoryStream memoryStream2 = new MemoryStream();
        Stream       stream        = null;

        if (CompressionModule.IsEncodingAccepted("deflate"))
        {
            stream = new DeflateStream(memoryStream2, CompressionMode.Compress);
            app.Application.Add(key + "enc", "deflate");
        }
        else if (CompressionModule.IsEncodingAccepted("gzip"))
        {
            stream = new GZipStream(memoryStream2, CompressionMode.Compress);
            app.Application.Add(key + "enc", "deflate");
        }
        stream.Write(array, 0, array.Length);
        stream.Dispose();
        return(memoryStream2);
    }
Exemplo n.º 2
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements
        ///     the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">
        /// An <see cref="T:System.Web.HttpContext"></see> object that provides references
        ///     to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.
        /// </param>
        public void ProcessRequest(HttpContext context)
        {
            if (!Security.IsAuthorizedTo(Rights.ViewPublicPosts))
            {
                context.Response.StatusCode = 401;
                return;
            }

            var title  = RetrieveTitle(context);
            var format = RetrieveFormat(context);
            var list   = GenerateItemList(context);

            list = CleanList(list);

            if (string.IsNullOrEmpty(context.Request.QueryString["post"]))
            {
                // Shorten the list to the number of posts stated in the settings, except for the comment feed.
                var max = Math.Min(BlogSettings.Instance.PostsPerFeed, list.Count);
                list = list.FindAll(item => item.IsVisible);

                list = list.GetRange(0, max);
            }

            SetHeaderInformation(context, list, format);

            if (BlogSettings.Instance.EnableHttpCompression)
            {
                CompressionModule.CompressResponse(context);
            }

            var generator = new SyndicationGenerator(BlogSettings.Instance, Category.Categories);

            generator.WriteFeed(format, context.Response.OutputStream, list, title);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom
        ///     HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">
        /// An <see cref="T:System.Web.HttpContext"></see> object that provides
        ///     references to the intrinsic server objects
        ///     (for example, Request, Response, Session, and Server) used to service HTTP requests.
        /// </param>
        public void ProcessRequest(HttpContext context)
        {
            var    request  = context.Request;
            string fileName = (string)request.QueryString["name"];

            if (!string.IsNullOrEmpty(fileName))
            {
                fileName = fileName.Replace(BlogSettings.Instance.Version(), string.Empty);

                OnServing(fileName);

                if (StringComparer.InvariantCultureIgnoreCase.Compare(Path.GetExtension(fileName), ".css") != 0)
                {
                    throw new SecurityException("Invalid CSS file extension");
                }

                string cacheKey = request.RawUrl.Trim();
                string css      = (string)context.Cache[cacheKey];

                if (String.IsNullOrEmpty(css))
                {
                    if (fileName.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                    {
                        css = RetrieveRemoteCss(fileName, cacheKey);
                    }
                    else
                    {
                        css = RetrieveLocalCss(fileName, cacheKey);
                    }
                }

                // Make sure css isn't empty
                if (!string.IsNullOrEmpty(css))
                {
                    // Configure response headers
                    SetHeaders(css.GetHashCode(), context);

                    context.Response.Write(css);

                    // Check if we should compress content
                    if (BlogSettings.Instance.EnableHttpCompression)
                    {
                        CompressionModule.CompressResponse(context);
                    }

                    OnServed(fileName);
                }
                else
                {
                    OnBadRequest(fileName);
                    context.Response.Status = "404 Bad Request";
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom
        ///     HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">
        /// An <see cref="T:System.Web.HttpContext"></see> object that provides
        ///     references to the intrinsic server objects
        ///     (for example, Request, Response, Session, and Server) used to service HTTP requests.
        /// </param>
        public void ProcessRequest(HttpContext context)
        {
            var request = context.Request;
            var lang    = request.QueryString["lang"];

            if (string.IsNullOrEmpty(lang))
            {
                // Use the current Language if the lang query isn't set.
                lang = BlogSettings.Instance.Language;
            }

            lang = lang.ToLowerInvariant();

            string cacheKey = "resource.axd - " + lang;
            string script   = (string)Blog.CurrentInstance.Cache[cacheKey];

            if (String.IsNullOrEmpty(script))
            {
                System.Globalization.CultureInfo culture = null;
                try
                {
                    // This needs to be in a try-catch because there's no other
                    // way to find an invalid culture/language string.
                    culture = new System.Globalization.CultureInfo(lang);
                }
                catch (Exception)
                {
                    // set to default language otherwise.
                    culture = Utils.GetDefaultCulture();
                }

                Json.JsonCulture jc = new Json.JsonCulture(culture);

                // Although this handler is intended to output resource strings,
                // also outputting other non-resource variables.

                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("BlogEngine.webRoot='{0}';", Utils.RelativeWebRoot);
                sb.AppendFormat("BlogEngine.applicationWebRoot='{0}';", Utils.ApplicationRelativeWebRoot);
                sb.AppendFormat("BlogEngine.blogInstanceId='{0}';", Blog.CurrentInstance.Id);
                sb.AppendFormat("BlogEngine.i18n = {0};", jc.ToJsonString());

                script = sb.ToString();
                Blog.CurrentInstance.Cache.Insert(cacheKey, script, null, Cache.NoAbsoluteExpiration, new TimeSpan(3, 0, 0, 0));
            }

            SetHeaders(script.GetHashCode(), context);
            context.Response.Write(script);

            if (BlogSettings.Instance.EnableHttpCompression)
            {
                CompressionModule.CompressResponse(context); // Compress(context);
            }
        }
Exemplo n.º 5
0
    private void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication httpApplication = (HttpApplication)sender;

        if (httpApplication.Request.Path.Contains("WebResource.axd"))
        {
            CompressionModule.SetCachingHeaders(httpApplication);
            if (CompressionModule.IsBrowserSupported() && httpApplication.Context.Request.QueryString["c"] == null && (CompressionModule.IsEncodingAccepted("deflate") || CompressionModule.IsEncodingAccepted("gzip")))
            {
                httpApplication.CompleteRequest();
            }
        }
    }
Exemplo n.º 6
0
    private static void AddCompressedBytesToCache(HttpApplication app, string key)
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(app.Context.Request.Url.OriginalString + "&c=1");

        using (HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse)
        {
            Stream responseStream = httpWebResponse.GetResponseStream();
            using (MemoryStream memoryStream = CompressionModule.CompressResponse(responseStream, app, key))
            {
                app.Application.Add(key, memoryStream.ToArray());
            }
        }
    }
Exemplo n.º 7
0
 private void context_EndRequest(object sender, EventArgs e)
 {
     if (CompressionModule.IsBrowserSupported() && (CompressionModule.IsEncodingAccepted("deflate") || CompressionModule.IsEncodingAccepted("gzip")))
     {
         HttpApplication httpApplication = (HttpApplication)sender;
         string          text            = httpApplication.Request.QueryString.ToString();
         if (httpApplication.Request.Path.Contains("WebResource.axd") && httpApplication.Context.Request.QueryString["c"] == null)
         {
             if (httpApplication.Application[text] == null)
             {
                 CompressionModule.AddCompressedBytesToCache(httpApplication, text);
             }
             CompressionModule.SetEncoding((string)httpApplication.Application[text + "enc"]);
             httpApplication.Context.Response.ContentType = "text/javascript";
             httpApplication.Context.Response.BinaryWrite((byte[])httpApplication.Application[text]);
         }
     }
 }
Exemplo n.º 8
0
    private void context_PostReleaseRequestState(object sender, EventArgs e)
    {
        HttpApplication httpApplication = (HttpApplication)sender;

        if (httpApplication.Context is Page && httpApplication.Request["HTTP_X_MICROSOFTAJAX"] == null)
        {
            if (CompressionModule.IsEncodingAccepted("deflate"))
            {
                httpApplication.Response.Filter = new DeflateStream(httpApplication.Response.Filter, CompressionMode.Compress);
                CompressionModule.SetEncoding("deflate");
            }
            else if (CompressionModule.IsEncodingAccepted("gzip"))
            {
                httpApplication.Response.Filter = new GZipStream(httpApplication.Response.Filter, CompressionMode.Compress);
                CompressionModule.SetEncoding("gzip");
            }
        }
    }
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements
        ///     the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">
        /// An <see cref="T:System.Web.HttpContext"></see> object that provides references
        ///     to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.
        /// </param>
        public void ProcessRequest(HttpContext context)
        {
            if (!Security.IsAuthorizedTo(Rights.ViewPublicPosts))
            {
                context.Response.StatusCode = 401;
                return;
            }

            var title  = RetrieveTitle(context);
            var format = RetrieveFormat(context);
            var list   = GenerateItemList(context);

            list = CleanList(list);

            if (string.IsNullOrEmpty(context.Request.QueryString["post"]))
            {
                // Shorten the list to the number of posts stated in the settings, except for the comment feed.
                var max = BlogSettings.Instance.PostsPerFeed;

                // usually we want to restrict number of posts for subscribers to latest
                // but it can be overriden in query string to bring any number of items
                if (!string.IsNullOrEmpty(context.Request.QueryString["maxitems"]))
                {
                    int maxItems;
                    if (int.TryParse(context.Request.QueryString["maxitems"], out maxItems))
                    {
                        max = maxItems;
                    }
                }

                list = list.FindAll(item => item.IsVisible).Take(Math.Min(max, list.Count)).ToList();
            }

            SetHeaderInformation(context, list, format);

            if (BlogSettings.Instance.EnableHttpCompression)
            {
                CompressionModule.CompressResponse(context);
            }

            var generator = new SyndicationGenerator(BlogSettings.Instance, Category.Categories);

            generator.WriteFeed(format, context.Response.OutputStream, list, title);
        }
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom
        ///     HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">
        /// An <see cref="T:System.Web.HttpContext"></see> object that provides
        ///     references to the intrinsic server objects
        ///     (for example, Request, Response, Session, and Server) used to service HTTP requests.
        /// </param>
        public void ProcessRequest(HttpContext context)
        {
            var request = context.Request;
            var path    = request.QueryString["path"];

            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            string rawUrl   = request.RawUrl.Trim();
            string cacheKey = context.Server.HtmlDecode(rawUrl);
            string script   = (string)context.Cache[cacheKey];
            bool   minify   = ((request.QueryString["minify"] != null) || (BlogSettings.Instance.CompressWebResource && cacheKey.Contains("WebResource.axd")));


            if (String.IsNullOrEmpty(script))
            {
                if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    script = RetrieveRemoteScript(path, cacheKey, minify);
                }
                else
                {
                    script = RetrieveLocalScript(path, cacheKey, minify);
                }
            }


            if (string.IsNullOrEmpty(script))
            {
                return;
            }

            SetHeaders(script.GetHashCode(), context);
            context.Response.Write(script);

            if (BlogSettings.Instance.EnableHttpCompression)
            {
                CompressionModule.CompressResponse(context); // Compress(context);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements
        ///     the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">
        /// An <see cref="T:System.Web.HttpContext"></see> object that provides references
        ///     to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.
        /// </param>
        public void ProcessRequest(HttpContext context)
        {
            var title  = RetrieveTitle(context);
            var format = RetrieveFormat(context);
            var list   = GenerateItemList(context);

            list = CleanList(list);

            // Sueetie Modified - Enter Rss Retrieval
            BlogCommon.FireAndForgetRecordBlogRss(new
                                                  SueetieBlogRss
            {
                ApplicationID = SueetieApplications.Current.ApplicationID,
                RemoteIP      = context.Request.UserHostAddress,
                UserAgent     = context.Request.UserAgent
            });

            if (string.IsNullOrEmpty(context.Request.QueryString["post"]))
            {
                // Shorten the list to the number of posts stated in the settings, except for the comment feed.
                var max = Math.Min(BlogSettings.Instance.PostsPerFeed, list.Count);
                list = list.FindAll(item => item.IsVisible);

                list = list.GetRange(0, max);
            }

            SetHeaderInformation(context, list, format);

            if (BlogSettings.Instance.EnableHttpCompression)
            {
                CompressionModule.CompressResponse(context);
            }

            var generator = new SyndicationGenerator(BlogSettings.Instance, Category.Categories);

            generator.WriteFeed(format, context.Response.OutputStream, list, title);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Enables processing of HTTP Web requests by a custom
        ///     HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">
        /// An <see cref="T:System.Web.HttpContext"></see> object that provides
        ///     references to the intrinsic server objects
        ///     (for example, Request, Response, Session, and Server) used to service HTTP requests.
        /// </param>
        public void ProcessRequest(HttpContext context)
        {
            var request = context.Request;
            var lang    = request.FilePath;

            lang = lang.Replace(".res.axd", "");

            if (lang.IndexOf("/") >= 0)
            {
                lang = lang.Substring(lang.LastIndexOf("/") + 1);
            }

            if (string.IsNullOrEmpty(lang))
            {
                // Use the current Language if the lang query isn't set.
                lang = BlogSettings.Instance.Language;
            }

            lang = lang.ToLowerInvariant();
            var    sb = new StringBuilder();
            string cacheKey;
            string script;

            if (request.FilePath.Contains("admin.res.axd"))
            {
                lang = BlogSettings.Instance.Culture;

                cacheKey = "admin.resource.axd - " + lang;
                script   = (string)Blog.CurrentInstance.Cache[cacheKey];

                if (String.IsNullOrEmpty(script))
                {
                    System.Globalization.CultureInfo culture;
                    try
                    {
                        culture = new System.Globalization.CultureInfo(lang);
                    }
                    catch (Exception)
                    {
                        culture = Utils.GetDefaultCulture();
                    }

                    var jc = new JsonCulture(culture, JsonCulture.ResourceType.Admin);

                    sb.Append("BlogAdmin = { i18n: " + jc.ToJsonString() + "};");
                    script = sb.ToString();
                }
            }
            else
            {
                cacheKey = "resource.axd - " + lang;
                script   = (string)Blog.CurrentInstance.Cache[cacheKey];

                if (String.IsNullOrEmpty(script))
                {
                    System.Globalization.CultureInfo culture;
                    try
                    {
                        culture = new System.Globalization.CultureInfo(lang);
                    }
                    catch (Exception)
                    {
                        culture = Utils.GetDefaultCulture();
                    }

                    var jc = new JsonCulture(culture, JsonCulture.ResourceType.Blog);

                    // Although this handler is intended to output resource strings,
                    // also outputting other non-resource variables.
                    sb.AppendFormat("webRoot: '{0}',", Utils.RelativeWebRoot);
                    sb.AppendFormat("applicationWebRoot: '{0}',", Utils.ApplicationRelativeWebRoot);
                    sb.AppendFormat("blogInstanceId: '{0}',", Blog.CurrentInstance.Id);
                    sb.AppendFormat("fileExtension: '{0}',", BlogConfig.FileExtension);
                    sb.AppendFormat("i18n: {0}", jc.ToJsonString());
                    script = "BlogEngineRes = {" + sb + "};";
                }
            }

            Blog.CurrentInstance.Cache.Insert(cacheKey, script, null, Cache.NoAbsoluteExpiration, new TimeSpan(3, 0, 0, 0));

            SetHeaders(script.GetHashCode(), context);
            context.Response.Write(script);

            if (BlogSettings.Instance.EnableHttpCompression)
            {
                CompressionModule.CompressResponse(context); // Compress(context);
            }
        }