robbanp
I'm the co-founder of this website and the tech lead. Follow me on: twitter.com/robertpohl
Blog

The Rob blog

I'm Robert Pohl, the creator and co-founder to ThatsToday. I blog mostly about technology and internet related topics. Follow me on Twitter @robertpohl
Subscribe to RSS

I’m working on a small ASP.NET website that require for some pages to have its own meta data keywords, description and title, so that they will be better indexed by search engines. This is also know as Search Engine Optimization (SEO).  The challenge here is to have the meta data information in the Master Page, and still be able to update the data from each page, and also do this without server side code on the different aspx pages.

The solution is to add a Content Place Holder called MetaData in your Master Page, and some logic to find and parse the data on each aspx page:

Master Page aspx:

<title id="MasterTitle" runat="server">Default title</title>
<meta name="description" id="MasterDescription" runat="server" content="Default description" />
<meta name="keywords" id="MasterKeywords" runat="server" content="default, description," />

 

<asp:ContentPlaceHolder ID="MetaData" Visible="false" runat="server" />

 

Master Page cs:

protected void Page_Load(object sender, EventArgs e)

        {
            var keyWords = MetaData.FindControl("PageKeywords") as Literal;
            if( keyWords != null)
            {
                MasterKeywords.Attributes["content"] = keyWords.Text;
            }

            var description = MetaData.FindControl("PageDescription") as Literal;
            if (description != null)
            {
                MasterDescription.Attributes["content"] = description.Text;
            }

            var title = MetaData.FindControl("PageTitle") as Literal;
            if (title != null)
            {
                MasterTitle.InnerText = title.Text;
            }
        }

And in the Default aspx:

<asp:Content ID="metaData" ContentPlaceHolderID="MetaData" runat="server">
    <asp:Literal ID="PageTitle" runat="server">Title from page</asp:Literal> 
    <asp:Literal ID="PageDescription" runat="server">Description from page</asp:Literal>
    <asp:Literal ID="PageKeywords" runat="server">Keywords, from, page</asp:Literal>
</asp:Content>

 

In the Master Page you need to have the title, keywords and description run at server so you can update them from the code behind. In the Page Load there is a check to see if the Literals exist in the content place holder (which is hidden from rendering), and if so, update the tags with the Literal information.

Now my front end guy, can choose to update the meta data or title if it is needed, or just settle with the default information.

Secunia are reporting a CRITICAL bug in Firefox 3.5 (older can be affected) that enables code execution on the cliant. This means that you can surf to a website and if they are evil they can install virus, trojans or other scary programs.

The vulnerability is caused due to an error when processing JavaScript code handling e.g. "font" HTML tags and can be exploited to cause a memory corruption.

Solution:
Set "javascript.options.jit.content" to "false" by opening about:config.
Do not browse untrusted websites or follow untrusted links.

 

Original Advisory:
SBerry:
http://milw0rm.com/exploits/9137

Mozilla:
http://blog.mozilla.com/security/2009...vascript-vulnerability-in-firefox-35/

This morning I read the latest news from TechCrunch on my news page here on ThatsToday, that a hacker called ” Hacker Croll” e-mailed a bunch of internal documents from Twitter to Mike Arrington of Tech Crunch. Mike wrote that these files contain 310 confidential documents about executive meeting notes, financial projections, calendar and phone logs among other tings. He even states that these documents have information that is somewhat embarrassing to some people over at Twitter.


So what does Mike decide to do?

To publish selected information of course.
So first, what’s the reason of this security and information breach?
The documents were stolen from Google Apps, where the ”hacker” guessed some passwords and gained access to 310 secret documents. Is this Googles fault or the Twitters?
I’d say both. Google should enforce strong passwords, and Twitter should: 1. have strong passwords and 2. not having internal documents hosted in ”the cloud” If everyone is using Google then you don’t need to try very hard to find the information of a company right? The cloud is not safe!

Second, either this is a PR stunt from Twitter, or TechCrunch are publishing stolen documents, which is not cool to do. If someone steals my iPod, is it all right for him to sell it? I don’t think so. TechCrunch make money out of traffic and by publishing this information they are ”selling stolen goods”.

Writing high performance applications is a challenge since you can do the same thing in so many ways. Almost everything you code can be done in many ways that are slow but can be improved to be faster. But on the contrary to this article is my experience on building high traffic web sites, is that it is almost never the application layer that is slow but almost always the data access and text/XML parsing.
But anyway, I spend so much time on thinking how code can be faster and more slimmed ☺

Since I develop more and more abstract and generic functionality using techniques such as Generics and Interfaces, I think of how this will improve both readability and performance. What I want to show in this article, is the performance benefits of using a Interface as a input type instead of an Object.
Always when you use Object as the parameter data type, to create an abstract or dynamic method you need to Cast the object into your desired class. This cast will be negative on the execution time and also add risk for errors. I also think it is negative on readability since you can’t really see the desired input type.

Performance test:
In this performance test I have a basic class that implements an interface. I have two methods that execute the test methods doing a cast, and using the interface methods.
The test object is created 1.000.000 times for each method, which are looped 10 times to compare execution times. As you can see the fastest way is to use an Interface to execute the method. On my machine the Interface method takes 7-8ms, and the cast method takes 15-22ms.



#region
using System;
using System.Diagnostics;
#endregion
namespace InterfaceTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Stopwatch stopWatch = new Stopwatch();
            TimeSpan ts;
            for (int i = 0; i < 10; i++)
            {
                stopWatch.Start();
                TestCast();
                stopWatch.Stop();
                ts = stopWatch.Elapsed;
                string elapsedTime = String.Format("Casting {0:00}:{1:00}:{2:00}sec.{3:00}ms",
                                                   ts.Hours, ts.Minutes, ts.Seconds,
                                                   ts.Milliseconds/10);
                Console.WriteLine(elapsedTime, "RunTime Cast");
                stopWatch.Reset();
                stopWatch.Start();
                TestInterface();
                stopWatch.Stop();
                ts = stopWatch.Elapsed;
                elapsedTime = String.Format("Interface {0:00}:{1:00}:{2:00}sec.{3:00}ms",
                                            ts.Hours, ts.Minutes, ts.Seconds,
                                            ts.Milliseconds/10);
                Console.WriteLine(elapsedTime, "RunTime Interface");
            }
            Console.ReadKey();
        }
        private static void TestInterface()
        {
            for (int i = 0; i < 1000000; i++)
            {
                TestClass tc = new TestClass();
                DoInterface(tc);
            }
        }
        private static void DoInterface(ITest tc)
        {
            tc.Read("hello speed test");
            tc.DoIt("hello world");
        }
        private static void TestCast()
        {
            for (int i = 0; i < 1000000; i++)
            {
                TestClass tc = new TestClass();
                DoCast(tc);
            }
        }
        private static void DoCast(object obj)
        {
            TestClass tc = obj as TestClass;
            tc.Read("hello speed test");
            tc.DoIt("hello world");
        }
        #region Nested type: ITest
        public interface ITest
        {
            bool DoIt(string testString);
            void Read(string str);
        }
        #endregion
        #region Nested type: TestClass
        public class TestClass : ITest
        {
            private string _testString;
            public TestClass()
            {
                _testString = "empty";
            }
            #region ITest Members
            public void Read(string str)
            {
                _testString = str;
            }
            public bool DoIt(string testString) 
            {
                if (testString == _testString)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            #endregion
        }
        #endregion
    }
}

 

If you have a lot of external JavaScript files you should always have a minified version of them when you go public. All popular JS frameworks such as JQuery and Prototype have minified versions, often on a CDN. But how do you do when you have a lot of your own code that you need to minify? There are a lot of code libraries that handles the minification such as JSMin and YUI Compressor that compress your .js files to minified .js files so you can upload them to your server.
My problem is that I have a lot of JavaScript files that are often updated and I don’t want to manually minify the files that are updated. So I figured: Why not do it on the fly? Map all *.js requests on the server to a HttpHandler that minifies the file, and returns the new version. Also add a little cache so it only does this once.
So now I don’t need to worry about a thing when it comes to minify JavaScript files, the server takes care of that automatically!

Here is the code

The HttpHandler called JSMinify is invoked on all requests to *.js files. Activate the handler I web.config:

<httpHandlers>
      <add verb="*" path="*.js" type="Portal.Web.API.Handlers.JSMinify,Portal.Web"/>
</httpHandlers>

 

Here is the Handler. As you can see I use the Request.PhysicalPath as the path to the file.

 

using System.Web;
namespace Portal.Web.API.Handlers
{
    public class JSMinify : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            HttpResponse objResponse = context.Response;
            string file = context.Request.PhysicalPath;
            objResponse.Write(PCache.FileCache.GetTextFile(file,new Parsers.JSMinifyParser()));
        }
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}

 

Here is my FileCache class that looks for a file on disk, and adds it to the web cache. The method GetTextFile is overloaded to handle a parser that implements IFileParser. I created IFileParser so I can have custom text parsing before I add the text into the cache. I use the file path as the cache key, and the cache settings are so that the cache will expire when the files is updated.

 

using System.IO;
using System.Web.Caching;
using Portal.PCache.Parsers;

namespace Portal.PCache
{
    public static class FileCache
    {
        /// <summary>
        /// Gets the text file.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        public static string GetTextFile(string path)
        {
            if(Exists(path))
            {
                return Get(path).ToString();
            }
            else
            {
                string data = ReadFile(path);
                Add(data,path);
                return data;
            }
        }
        /// <summary>
        /// Gets the text file using a file parser.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="parser">The parser.</param>
        /// <returns></returns>
        public static string GetTextFile(string path, IFileParser parser)
        {
            if (Exists(path))
            {
                return Get(path).ToString();
            }
            else
            {
                string data = parser.Parse(path);
                Add(data, path);
                return data;
            }
        }

        /// <summary>
        /// Reads the file form disk.
        /// </summary>
        /// <param name="path">The file path.</param>
        /// <returns></returns>
        private static string ReadFile(string path)
        {
            TextReader s = new StreamReader(path);
            string data = s.ReadToEnd();
            s.Close();
            s.Dispose();
            return data;
        }

        /// <summary>
        /// Adds the specified cache object.
        /// </summary>
        /// <param name="cacheObject">The cache object.</param>
        /// <param name="keyName">Name of the key.</param>
        private static void Add(object cacheObject, string keyName)
        {
            System.Web.HttpContext.Current.Cache.Insert(keyName, cacheObject, new CacheDependency(keyName));
        }

        /// <summary>
        /// Check if object exists in cache
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static bool Exists(string key)
        {
            if (System.Web.HttpContext.Current.Cache[key] != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// remove object from cache
        /// </summary>
        /// <param name="key"></param>
        private static void Remove(string key)
        {
            System.Web.HttpContext.Current.Cache.Remove(key);
        }

        /// <summary>
        /// get object from cache
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object Get(string key)
        {
            return System.Web.HttpContext.Current.Cache[key];
        }
    }
}


In the class JSMinifyParser I have a modified version of the popular library called JSMin. The original JSMin converts one file to another, but I want it to read a file and return the minified string version of that file. So what I did was basically to change the StreamWriter to a TextWriter, and made the method return the string.

 

//Parser interface
namespace Portal.PCache.Parsers
{
    public interface IFileParser
    {
        string Parse(string s);
    }
}
//Parser class
using Portal.PCache.Parsers;

namespace Portal.Web.API.Parsers
{
    internal class JSMinifyParser : IFileParser
    {
        #region IFileParser Members

        public string Parse(string s)
        {
            JavaScriptMinifier mini = new JavaScriptMinifier();
            string outs = mini.Minify(s);
            return outs;
        }

        #endregion
    }
}
//JSMin (modified)
using System;
using System.IO;

/* Originally written in 'C', this code has been converted to the C# language.
 * The author's copyright message is reproduced below.
 * All modifications from the original to C# are placed in the public domain.
 */

/* jsmin.c
   2007-05-22

Copyright (c) 2002 Douglas Crockford  (www.crockford.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

namespace Portal.Web.API
{
    public class JavaScriptMinifier
    {
        const int EOF = -1;

        StreamReader sr;
        StringWriter sw;
        int theA;
        int theB;
        int theLookahead = EOF;


        //static void Main(string[] args)
        //{
        //    if (args.Length != 2)
        //    {
        //        Console.WriteLine("invalid arguments, 2 required, 1 in, 1 out");
        //        return;
        //    }
        //    new JavaScriptMinifier().Minify(args[0], args[1]);
        //}

        public string Minify(string src) //removed the out file path
        {
            using (sr = new StreamReader(src))
            {
                using (sw = new StringWriter())  //used to be a StreamWriter
                {
                    jsmin();
                    return sw.ToString(); // return the minified string
                }
            }
        }

        /* jsmin -- Copy the input to the output, deleting the characters which are
                insignificant to JavaScript. Comments will be removed. Tabs will be
                replaced with spaces. Carriage returns will be replaced with linefeeds.
                Most spaces and linefeeds will be removed.
        */
        void jsmin()
        {
            theA = '\n';
            action(3);
            while (theA != EOF)
            {
                switch (theA)
                {
                    case ' ':
                        {
                            if (isAlphanum(theB))
                            {
                                action(1);
                            }
                            else
                            {
                                action(2);
                            }
                            break;
                        }
                    case '\n':
                        {
                            switch (theB)
                            {
                                case '{':
                                case '[':
                                case '(':
                                case '+':
                                case '-':
                                    {
                                        action(1);
                                        break;
                                    }
                                case ' ':
                                    {
                                        action(3);
                                        break;
                                    }
                                default:
                                    {
                                        if (isAlphanum(theB))
                                        {
                                            action(1);
                                        }
                                        else
                                        {
                                            action(2);
                                        }
                                        break;
                                    }
                            }
                            break;
                        }
                    default:
                        {
                            switch (theB)
                            {
                                case ' ':
                                    {
                                        if (isAlphanum(theA))
                                        {
                                            action(1);
                                            break;
                                        }
                                        action(3);
                                        break;
                                    }
                                case '\n':
                                    {
                                        switch (theA)
                                        {
                                            case '}':
                                            case ']':
                                            case ')':
                                            case '+':
                                            case '-':
                                            case '"':
                                            case '\'':
                                                {
                                                    action(1);
                                                    break;
                                                }
                                            default:
                                                {
                                                    if (isAlphanum(theA))
                                                    {
                                                        action(1);
                                                    }
                                                    else
                                                    {
                                                        action(3);
                                                    }
                                                    break;
                                                }
                                        }
                                        break;
                                    }
                                default:
                                    {
                                        action(1);
                                        break;
                                    }
                            }
                            break;
                        }
                }
            }
        }
        /* action -- do something! What you do is determined by the argument:
                1   Output A. Copy B to A. Get the next B.
                2   Copy B to A. Get the next B. (Delete A).
                3   Get the next B. (Delete B).
           action treats a string as a single character. Wow!
           action recognizes a regular expression if it is preceded by ( or , or =.
        */
        void action(int d)
        {
            if (d <= 1)
            {
                put(theA);
            }
            if (d <= 2)
            {
                theA = theB;
                if (theA == '\'' || theA == '"')
                {
                    for (; ; )
                    {
                        put(theA);
                        theA = get();
                        if (theA == theB)
                        {
                            break;
                        }
                        if (theA <= '\n')
                        {
                            throw new Exception(string.Format("Error: JSMIN unterminated string literal: {0}\n", theA));
                        }
                        if (theA == '\\')
                        {
                            put(theA);
                            theA = get();
                        }
                    }
                }
            }
            if (d <= 3)
            {
                theB = next();
                if (theB == '/' && (theA == '(' || theA == ',' || theA == '=' ||
                                    theA == '[' || theA == '!' || theA == ':' ||
                                    theA == '&' || theA == '|' || theA == '?' ||
                                    theA == '{' || theA == '}' || theA == ';' ||
                                    theA == '\n'))
                {
                    put(theA);
                    put(theB);
                    for (; ; )
                    {
                        theA = get();
                        if (theA == '/')
                        {
                            break;
                        }
                        else if (theA == '\\')
                        {
                            put(theA);
                            theA = get();
                        }
                        else if (theA <= '\n')
                        {
                            throw new Exception(string.Format("Error: JSMIN unterminated Regular Expression literal : {0}.\n", theA));
                        }
                        put(theA);
                    }
                    theB = next();
                }
            }
        }
        /* next -- get the next character, excluding comments. peek() is used to see
                if a '/' is followed by a '/' or '*'.
        */
        int next()
        {
            int c = get();
            if (c == '/')
            {
                switch (peek())
                {
                    case '/':
                        {
                            for (; ; )
                            {
                                c = get();
                                if (c <= '\n')
                                {
                                    return c;
                                }
                            }
                        }
                    case '*':
                        {
                            get();
                            for (; ; )
                            {
                                switch (get())
                                {
                                    case '*':
                                        {
                                            if (peek() == '/')
                                            {
                                                get();
                                                return ' ';
                                            }
                                            break;
                                        }
                                    case EOF:
                                        {
                                            throw new Exception("Error: JSMIN Unterminated comment.\n");
                                        }
                                }
                            }
                        }
                    default:
                        {
                            return c;
                        }
                }
            }
            return c;
        }
        /* peek -- get the next character without getting it.
        */
        int peek()
        {
            theLookahead = get();
            return theLookahead;
        }
        /* get -- return the next character from stdin. Watch out for lookahead. If
                the character is a control character, translate it to a space or
                linefeed.
        */
        int get()
        {
            int c = theLookahead;
            theLookahead = EOF;
            if (c == EOF)
            {
                c = sr.Read();
            }
            if (c >= ' ' || c == '\n' || c == EOF)
            {
                return c;
            }
            if (c == '\r')
            {
                return '\n';
            }
            return ' ';
        }
        void put(int c)
        {
            sw.Write((char)c);
        }
        /* isAlphanum -- return true if the character is a letter, digit, underscore,
                dollar sign, or non-ASCII character.
        */
        bool isAlphanum(int c)
        {
            return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
                (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' ||
                c > 126);
        }
    }
}

So that is basically it! What you need to think about is to map *.js file types to be handled ASP.NET in the IIS settings, otherwise the HttpHandler won’t be triggered. Also there are some differences how you add the handler in web.config for IIS6 and IIS7. Here is a lot of usefull info on IIS7 http://learn.iis.net/page.aspx/26/installing-and-configuring-iis-70/

 

While building a URL mapping routine for my portal, I needed to optimize the loading of member specific URL keys.
For this I use the HttpContext.Current.Cache object that is easily used in the ASP.NET environment. Whats "ugly" with the Cache object is that it takes a object as a input parameter and of course return the cached object as the type of object.

So instead of do alot of type casting, I created a small wrapper class that use generics to handle the object types.

Take a look:

#region
using System;
using System.Web;
using System.Web.Caching;
#endregion
namespace Portal.PCache
{
    /// <summary>
    /// Type safe object cache
    /// </summary>
    public class ObjectCache
    {
        private const int TIMEOUT = 60;
        /// <summary>
        /// Adds the specified cache object. it will last for max 1hr from last access
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheObject">The cache object.</param>
        /// <param name="keyName">Name of the key.</param>
        public static void Add<T>(T cacheObject, string keyName)
        {
            HttpContext.Current.Cache.Insert(keyName, cacheObject, null, Cache.NoAbsoluteExpiration,
                                             TimeSpan.FromMinutes(TIMEOUT));

        }


        /// <summary>
        /// Removes object with the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        public static void Remove(string key)
        {
            HttpContext.Current.Cache.Remove(key);
        }

        /// <summary>
        /// Check if object with the specified key exists.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>

        public static bool Exist(string key)
        {
            return HttpContext.Current.Cache[key] != null;
        }

        /// <summary>
        /// Gets the object with the specified key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static T Get<T>(string key) where T : class
        {
            return HttpContext.Current.Cache[key] as T;
        }
    }
}

 

And to use the class:

 


MyObject obj = new MyObject();
string key = "myobject1";

bool exists = ObjectCache.Exist(key);
if(!exists)
{
  ObjectCache.Add<MyObject>(obj, key);
}

MyObject another = ObjectCache.Get<MyObject>(key);

 

When I implemented an anti-cache function for my website i discovered that ASP.NET somehow parses <link> tags and makes it impossible to add parameters to the url.

I pass an version number to the url so that I can force new loads of the js and CSS, like this:

 

    <script src="/script/portal.js?v=<%=ScriptVersion%>" type="text/javascript"></script>
    <link href="/css/portal.css?v=<%=ScriptVersion%>" rel="stylesheet" type="text/css" media="screen" /> <!-- does not work -->

 

Somehow ASP.NET parse the <link> tag and create this ugly code:

 

    <link href="/css/portal.css?v=&lt;%=ScriptVersion%&gt;" rel="stylesheet" type="text/css" media="screen" /> <!-- does not work -->

 

The only solution for this was to include CSS like this;

 

    <style type="text/css" media="screen">
         @import "/css/portal.css?v=<%=ScriptVersion%>";
    </style>

 

JavaScript includes works perfect, it's just the css links that is parsed :/

 

 

Back again after a nice xmas and a relaxed new years celebration.
 

So, what's the plan for 2009 then?

 

1. Get a proper alpha version out there.

2. Attract users

3. Launch Beta

4. Attract more users

5. Release cool and usefull functions

6. GOTO 4.

After working long hours yesturday we have now released the alpha version of the site. Testers are reporing bugs in a constant flow :P

 

 

//Rob

Sign In

Not a member yet?

Signing up is FREE and will only take 15 seconds!

Facebook Login

Sign In

E-mail address:
Password:
Remember me
Sponsored links
AdUniver.se - ad platform Create wish-list e-böcker ljudböcker till din ipad android läsplatta dator

Close