BlogRequest.cs :  » Bloggers » SubText » Subtext » Framework » Web » HttpModules » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » SubText 
SubText » Subtext » Framework » Web » HttpModules » BlogRequest.cs
#region Disclaimer/Info

///////////////////////////////////////////////////////////////////////////////////////////////////
// Subtext WebLog
// 
// Subtext is an open source weblog system that is a fork of the .TEXT
// weblog system.
//
// For updated news and information please visit http://subtextproject.com/
// Subtext is hosted at Google Code at http://code.google.com/p/subtext/
// The development mailing list is at subtext@googlegroups.com 
//
// This project is licensed under the BSD license.  See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.Web;
using Subtext.Framework.Configuration;
using Subtext.Framework.Format;
using Subtext.Framework.Text;

namespace Subtext.Framework.Web.HttpModules{
    /// <summary>
    /// Represents the state of the current blog request.
    /// </summary>
    public class BlogRequest
    {
        public const string BlogRequestKey = "Subtext__CurrentRequest";
        public const int DefaultPort = 80;

        /// <summary>
        /// Initializes a new instance of the <see cref="BlogRequest"/> class.
        /// </summary>
        public BlogRequest(string host, string subfolder, Uri url, bool isLocal, RequestLocation requestLocation,
                           string applicationPath)
        {
            Host = host;
            Subfolder = subfolder;
            RawUrl = url;
            IsLocal = isLocal;
            RequestLocation = requestLocation;
            ApplicationPath = applicationPath;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BlogRequest"/> class.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <param name="subfolder">The subfolder.</param>
        /// <param name="url">The raw requested URL</param>
        /// <param name="isLocal">True if this request is a local machine request.</param>
        public BlogRequest(string host, string subfolder, Uri url, bool isLocal)
            : this(host, subfolder, url, isLocal, RequestLocation.Blog, "/")
        {
        }

        public BlogRequest(HttpRequestBase request)
            : this(HostFromRequest(request)
                   , SubfolderFromRequest(request)
                   , request.Url
                   , request.IsLocal
                   , DetermineRequestLocation(request)
                   , request.ApplicationPath
                )
        {
        }

        /// <summary>
        /// Gets or sets the current blog request.
        /// </summary>
        /// <value>The current.</value>
        public static BlogRequest Current
        {
            get { return (BlogRequest)HttpContext.Current.Items[BlogRequestKey]; }
            set { HttpContext.Current.Items[BlogRequestKey] = value; }
        }

        public RequestLocation RequestLocation { get; private set; }

        public bool IsLocal { get; private set; }

        public Blog Blog { get; set; }

        /// <summary>
        /// Gets the host.
        /// </summary>
        /// <value>The host.</value>
        public string Host { get; private set; }

        /// <summary>
        /// Gets the host.
        /// </summary>
        /// <value>The host.</value>
        public string Subfolder { get; private set; }

        public string ApplicationPath { get; private set; }

        public Uri RawUrl { get; private set; }

        public bool IsHostAdminRequest
        {
            get { return RequestLocation == RequestLocation.HostAdmin || RequestLocation == RequestLocation.Upgrade; }
        }

        // The request is for a location in which a blog is required.
        public bool BlogNotRequired
        {
            get
            {
                return RequestLocation != RequestLocation.Blog
                       && RequestLocation != RequestLocation.LoginPage
                    /* && RequestLocation != EmbeddedResource */;
            }
        }

        private static RequestLocation DetermineRequestLocation(HttpRequestBase request)
        {
            if(IsStaticFileRequest(request))
            {
                return RequestLocation.StaticFile;
            }
            if(IsLogin(request))
            {
                return RequestLocation.LoginPage;
            }
            if(IsForgotPassword(request))
            {
                return RequestLocation.LoginPage;
            }
            if(IsSystemMessage(request))
            {
                return RequestLocation.SystemMessages;
            }
            if(IsUpgrade(request))
            {
                return RequestLocation.Upgrade;
            }
            if(IsHostAdmin(request))
            {
                return RequestLocation.HostAdmin;
            }
            if(IsInstallation(request))
            {
                return RequestLocation.Installation;
            }
            if(IsSkins(request))
            {
                return RequestLocation.Skins;
            }
            if(IsEmbeddedResource(request))
            {
                return RequestLocation.EmbeddedResource;
            }
            return RequestLocation.Blog;
        }

        private static bool IsStaticFileRequest(HttpRequestBase request)
        {
            string filePath = request.FilePath;

            return filePath.EndsWith(".css", StringComparison.OrdinalIgnoreCase)
                   || filePath.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)
                   || filePath.EndsWith(".js", StringComparison.OrdinalIgnoreCase)
                   || filePath.EndsWith(".gif", StringComparison.OrdinalIgnoreCase)
                   || filePath.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
                   || filePath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)
                   || filePath.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)
                   || filePath.EndsWith(".html", StringComparison.OrdinalIgnoreCase)
                   || filePath.EndsWith(".htm", StringComparison.OrdinalIgnoreCase)
                   || filePath.Contains("/images/", StringComparison.OrdinalIgnoreCase);
        }

        private static bool IsEmbeddedResource(HttpRequestBase request)
        {
            string filePath = request.FilePath;

            return filePath.EndsWith("ScriptResource.axd", StringComparison.OrdinalIgnoreCase)
                   || filePath.EndsWith("WebResource.axd", StringComparison.OrdinalIgnoreCase);
        }

        private static bool IsInSpecialDirectory(HttpRequestBase request, string folderName)
        {
            string appPath = request.ApplicationPath ?? string.Empty;

            if(!appPath.EndsWith("/", StringComparison.Ordinal))
            {
                appPath += "/";
            }
            string path = request.Path;
            if(!path.EndsWith("/", StringComparison.Ordinal))
            {
                path += "/";
            }
            appPath += folderName + "/";

            return path.StartsWith(appPath, StringComparison.OrdinalIgnoreCase);
        }

        private static bool IsLogin(HttpRequestBase request)
        {
            return (request.Path ?? string.Empty).EndsWith("Login.aspx", StringComparison.OrdinalIgnoreCase);
        }

        private static bool IsForgotPassword(HttpRequestBase request)
        {
            return (request.Path ?? string.Empty).EndsWith("ForgotPassword.aspx", StringComparison.OrdinalIgnoreCase);
        }

        private static bool IsSystemMessage(HttpRequestBase request)
        {
            return IsInSpecialDirectory(request, "SystemMessages");
        }

        private static bool IsSkins(HttpRequestBase request)
        {
            return IsInSpecialDirectory(request, "Skins");
        }

        private static bool IsHostAdmin(HttpRequestBase request)
        {
            return IsInSpecialDirectory(request, "HostAdmin");
        }

        private static bool IsUpgrade(HttpRequestBase request)
        {
            return IsInSpecialDirectory(request, "HostAdmin/Upgrade");
        }

        private static bool IsInstallation(HttpRequestBase request)
        {
            return IsInSpecialDirectory(request, "Install");
        }

        private static string SubfolderFromRequest(HttpRequestBase request)
        {
            string subfolder = UrlFormats.GetBlogSubfolderFromRequest(request.RawUrl, request.ApplicationPath) ?? string.Empty;
            if(!String.IsNullOrEmpty(subfolder) && !Config.IsValidSubfolderName(subfolder))
            {
                subfolder = string.Empty;
            }
            return subfolder;
        }

        private static string HostFromRequest(HttpRequestBase request)
        {
            string host = request.Url.Host;

            if(request.IsLocal && request.Url.Host == "127.0.0.1") // For testing.
            {
                return "localhost";
            }

            if(String.IsNullOrEmpty(host))
            {
                host = request.Params["HTTP_HOST"].LeftBefore(":", StringComparison.OrdinalIgnoreCase);
            }

            return host;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.