<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lalit.org &#187; code</title>
	<atom:link href="http://www.lalit.org/tag/code/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lalit.org</link>
	<description>Personal page of Lalit Patel, an engineer, entrepreneur, geek from Bhubaneswar, India.</description>
	<lastBuildDate>Thu, 03 Jun 2010 05:23:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting most out of Amazon S3</title>
		<link>http://www.lalit.org/lab/setting-cache-headers-files-in-amazon-s3</link>
		<comments>http://www.lalit.org/lab/setting-cache-headers-files-in-amazon-s3#comments</comments>
		<pubDate>Sun, 02 Nov 2008 10:19:35 +0000</pubDate>
		<dc:creator>Lalit</dc:creator>
				<category><![CDATA[lab]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[s3]]></category>

		<guid isPermaLink="false">http://www.lalit.org/?p=259</guid>
		<description><![CDATA[Amazon S3 is a very useful service. S3, according to the official Amazon Web Services website is
Amazon S3 is storage for the Internet. It is designed to make web-scale computing easier for developers.
Its a no frills service and does exactly what it promises &#8212; makes it easy for developers so that they can concentrate on [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="nofollow" href="http://aws.amazon.com/s3">Amazon S3</a> is a very useful service. S3, according to the official Amazon Web Services website is</p>
<blockquote><p>Amazon S3 is storage for the Internet. It is designed to make web-scale computing easier for developers.</p></blockquote>
<p>Its a no frills service and does exactly what it promises &#8212; makes it easy for developers so that they can concentrate on features and leave the scaling to Amazon. If you are new to Amazon S3, heres a good <a href="http://www.labnol.org/internet/host-images-files-on-amazon-s3-storage/4923/">starting guide</a> for you.</p>
<p>S3 is like a sharp sword, you must know how to play with it lest you can hurt yourself. Thats exactly what happened to me. <span id="more-259"></span>One of the many applications that we are developing on MySpace, <a href="http://profile.myspace.com/Modules/Applications/Pages/Canvas.aspx?appId=104651">Sketch Me</a>, required us to store (and serve) huge amount of image data (user sketches). And due to the viral nature of the application, the load almost tripled every month. S3 was a clear choice. It saved us time, money and headache. Our current stats (with caching) are:</p>
<ul>
<li>Total files stored: <strong>205GB</strong></li>
<li>Bandwidth per month: <strong>2TB</strong></li>
<li>GET Requests per month: <strong>112m</strong></li>
</ul>
<p>Clearly I would not like to waste time setting up image serving servers that can handle such load and I am more than happy to outsource it to Amazon S3.</p>
<p>You would be surprised before caching when our total images were just 5GB, the no of requests were <strong>263m ($363.91)</strong> (almost double to what it is now with 205GB of images)</p>
<p>So if we take the total request to be directly proportional to number of images, with that rate the actual requests should be approx 4.5 billion or $15,000 :O</p>
<h3>How did I tame the beast?</h3>
<p>At first look the pricing of Amazon&#8217;s S3 services seems quite cheap. Wait until you get your first bill and you will see have cents add up to huge $$$.</p>
<blockquote><p><strong>Storage</strong></p>
<ul>
<li>$0.150 per GB – first 50 TB / month of storage used</li>
</ul>
<p><strong>Data Transfer</strong></p>
<ul>
<li>$0.100 per GB – all data transfer in</li>
<li>$0.170 per GB – first 10 TB / month data transfer out</li>
</ul>
<p><strong>Requests</strong></p>
<ul>
<li>$0.01 per 1,000 PUT, POST, or LIST requests</li>
<li class="c_red">$0.01 per 10,000 GET and all other requests</li>
</ul>
</blockquote>
<p>So after getting the first bill for a few hundred dollars, I sat down thinking how to bring that down. When I was digging the HTTP headers, I found out that by default S3 doesn’t have any cache request headers set. So even when the visitor has the requested the file from S3 before and has it in his browser cache, the browser will send a HTTP <code>GET</code> request to S3 just to verify if the file has changed. S3 returns a <code>304 Not Modified</code> header if the file has not changed and file wont be downloaded. You may think, S3 saved me a few GB of bandwidth cost. But each of this requests cost you ($0.01 per 10,000 GET) which is generally the bulk of the S3 bill.</p>
<p>Since photos our users upload almost never change. Asking S3 every time if the file has changed on S3 is certainly not required. You can stop browser sending this extra request for the same user by setting appropriate <code>Cache-Control</code> headers or <code>Expires</code> headers for the files. We can set <code>Cache-Control max-age=864000</code> which will tell browser to not request the same file until next 10 days (3600*24*10 sec)</p>
<p><img class="alignnone size-full wp-image-268" title="s3_304" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/11/s3_304.png" alt="" width="328" height="76" /></p>
<p>Fortunately S3 allows us to do that, but there is no simple and easy way to do that. So I decided write a small script (<a href="#domestication">see below</a>) to achieve this.</p>
<h3><a id="domestication" name="domestication"></a>After Domestication</h3>
<p>After setting Cache headers, my bill got down drastically. The traffic doubled and number of images  almost tripled (5GB &#8211; 15GB) within a month while the number of requests was reduced 3 times. So that is <strong>9 times reduction in cost</strong>. Ideally, your bandwidth cost should be more than your requests cost.</p>
<p><img class="alignnone size-full wp-image-266" title="s3_bill" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/11/s3_bill.png" alt="" width="497" height="403" /></p>
<p>If you own a high traffic blog or website, you can also store your javascripts or css files on S3 by using far fetched expires headers and using versioning (changing file name when contents change) so that the browser knows when the file has changed.</p>
<p><strong>For Example:</strong></p>
<p><code>&lt;link href="http://s3.amazonaws.com/lalit/style.css?<strong>v=3</strong>" ... /&gt;</code><br />
after change in stylesheet, change your code to,<br />
<code>&lt;link href="http://s3.amazonaws.com/lalit/style.css?<strong>v=4</strong>" ... /&gt;</code></p>
<h3>Domestication</h3>
<p>The popular <a rel="nofollow" href="https://addons.mozilla.org/en-US/firefox/addon/3247">Firefox extension S3Fox</a> doesn&#8217;t allow us to do that. So I decided write a small script using the <a rel="nofollow" href="http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1448&amp;categoryID=47">S3 PHP REST Library</a>.</p>
<blockquote><p>You can download the code <a href="http://www.lalit.org/wordpress/wp-content/uploads/2008/11/s3_upload.zip">here</a> (zip 6k).</p></blockquote>
<p><small><b class="red">Update:</b> Fixed a bug in code (mime type calculation was failing on some php configurations)</small></p>
<p>To use the script, you have to upload it to your server (running PHP). You need to edit the <code>upload.php</code> file to specify your AWS access key and Secret, your S3 bucket name.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* One time settings. */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$awsAccessKey</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'---'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// your AWS Key</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$awsSecretKey</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'---'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// your AWS Secret</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$bucket_name</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'---'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// S3 Bucket name</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$age</span>		<span style="color: #339933;">=</span> <span style="color: #cc66cc;">3600</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// Cache age 10 days	</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #666666; font-style: italic;">/* File Data */</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$s3_dir_name</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'dir1/dir2/'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// Directory on s3 where you want to upload file</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">				<span style="color: #666666; font-style: italic;">// example http://s3.amazonawas.com/bucket_name/dir1/dir2/filename.ext</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000088;">$upload_file</span>	<span style="color: #339933;">=</span> <span style="color: #0000ff;">'filename.ext'</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">// name of the file you want to upload.</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">				<span style="color: #666666; font-style: italic;">// keep it in the same dir as this file.</span></div></li></ol></pre></div></div>

<p>After you have saved the config info, every time you need to upload a file, you have to specify the file name and the dir name in <code>upload.php</code> and callit from the browser http://yoursite.com/s3/upload.php. (I know it sucks,  I promise will make it better soon)</p>
<p>If you want to escape all this trouble, there is a paid tool <a rel="external nofollow" href="http://www.bucketexplorer.com/">Bucket Explorer</a> which helps you to upload files with custom headers. I havn&#8217;t used it but it looks great and works on Win/Linux/Mac.</p>
<p>I hope your next S3 bill will come down <img src='http://www.lalit.org/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalit.org/lab/setting-cache-headers-files-in-amazon-s3/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Powerpoint / Slideshow Karaoke</title>
		<link>http://www.lalit.org/lab/powerpoint-slideshow-karaoke</link>
		<comments>http://www.lalit.org/lab/powerpoint-slideshow-karaoke#comments</comments>
		<pubDate>Mon, 29 Sep 2008 13:43:21 +0000</pubDate>
		<dc:creator>Lalit</dc:creator>
				<category><![CDATA[lab]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[karaoke]]></category>
		<category><![CDATA[slideshare]]></category>
		<category><![CDATA[slideshow]]></category>

		<guid isPermaLink="false">http://www.lalit.org/?p=239</guid>
		<description><![CDATA[Last year while I was at SlideShare.net, me and my friends were thinking of some cool ways to use our API. At the same time we were checking out emails from our users about how they are using sideshows in more fun way. Just then we came to know about PowerPoint Karaoke here and here.
Demo
After [...]]]></description>
			<content:encoded><![CDATA[<p>Last year while I was at <a href="http://www.slideshare.net">SlideShare.net</a>, me and my friends were thinking of some cool ways to use our <a href="http://www.slideshare.net/developers">API</a>. At the same time we were checking out emails from our users about how they are using sideshows in more fun way. Just then we came to know about PowerPoint Karaoke <a href="http://heathervescent.blogs.com/heathervescent/2007/04/powerpoint_kara.html">here </a>and <a href="http://www.boston.com/bostonglobe/ideas/articles/2008/03/02/slide_show/">here</a>.</p>
<h3>Demo</h3>
<p>After a few hours of javascript hacking, <a href="http://www.slidesharetoys.com/karaoke/">this is what</a> I came up with.<br />
<span id="more-239"></span></p>
<h3>How it works</h3>
<p>It will fetch random (Creative Commons Licensed) sideshows from slideshare.net using the API. The application uses the SlideShare.net API and Prototype/Scriptacluous to create some pretty visual effects. The <a href="http://www.slidesharetoys.com/karaoke/">SlideShare Karaoke Randomizer</a> was used for the first time on 11th July at the <a rel="nofollow" href="http://upcoming.yahoo.com/event/199762/">Creative Commons Salon</a> in San Francisco!</p>
<p style="text-align: center;"><a href="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/k1.jpg"><img class="size-full wp-image-250 aligncenter" title="Karaoke" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/k1.jpg" alt="" width="460" height="265" /></a></p>
<p style="text-align: center;"><a href="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/k2.jpg"><img class="size-full wp-image-249 aligncenter" title="Karaoke" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/k2.jpg" alt="" width="466" height="272" /></a></p>
<p>Here&#8217;s a great writeup for <a href="http://heathervescent.blogs.com/heathervescent/2007/04/powerpoint_kara.html">how to run your own PPT karaoke</a>.<br />
More info on SlideShare.net blog <a rel="nofollow" href="http://blog.slideshare.net/2008/09/05/slideshare-as-a-party-game/">here</a> and <a rel="nofollow" href="http://blog.slideshare.net/2007/07/11/slideshare-karoake-randomizer/">here</a></p>
<h3>Download</h3>
<p>The code has been released under the CC-GPL license at <a href="http://code.google.com/p/slideshare-toys/">Google Code project hosting</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalit.org/lab/powerpoint-slideshow-karaoke/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cookie Jar: Yummy JSON Cookies (using Prototype)</title>
		<link>http://www.lalit.org/lab/jsoncookies</link>
		<comments>http://www.lalit.org/lab/jsoncookies#comments</comments>
		<pubDate>Thu, 12 Apr 2007 07:09:16 +0000</pubDate>
		<dc:creator>Lalit</dc:creator>
				<category><![CDATA[lab]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.lalit.org/?p=20</guid>
		<description><![CDATA[JavaScript code to store data as JSON strings in cookies. It uses prototype.js to store and retrieve JSON data from cookies.
Now we can store and retrieve JavaScript Objects, Arrays, Boolean, String, Number values using cookies, just like storing Java Objects in session on the server side.

Works with Firefox 1.5, 2.0, IE 6.0 and Opera 9.10.
Example
(Execute [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript code to store data as JSON strings in cookies. It uses <a href="http://www.prototypejs.org/" target="_blank">prototype.js</a> to store and retrieve JSON data from cookies.</p>
<p>Now we can store and retrieve JavaScript Objects, Arrays, Boolean, String, Number values using cookies, just like storing Java Objects in session on the server side.<br />
<span id="more-20"></span><br />
Works with Firefox 1.5, 2.0, IE 6.0 and Opera 9.10.</p>
<h3>Example</h3>
<p>(Execute this example)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">jar <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> CookieJar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">expires<span style="color: #339933;">:</span><span style="color: #CC0000;">3600</span><span style="color: #339933;">,</span>   <span style="color: #006600; font-style: italic;">// seconds</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">path<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/'</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">dog <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'Jacky'</span><span style="color: #339933;">,</span> breed<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Alsatian'</span><span style="color: #339933;">,</span> age<span style="color: #339933;">:</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">jar.<span style="color: #660066;">put</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mydog'</span><span style="color: #339933;">,</span> dog<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">mydog <span style="color: #339933;">=</span> jar.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mydog'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;My dog's name is &quot;</span> <span style="color: #339933;">+</span> mydog.<span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;He is &quot;</span> <span style="color: #339933;">+</span> mydog.<span style="color: #660066;">age</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; years old&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;He is an &quot;</span> <span style="color: #339933;">+</span> mydog.<span style="color: #660066;">breed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div>

<h3>Download</h3>
<p>You can view the source code <a href="http://www.lalit.org/wordpress/wp-content/uploads/2008/06/cookiejar.js">here</a>.<br />
<del datetime="2009-01-25T06:10:56+00:00">This code is released under <a href="http://creativecommons.org/licenses/by-sa/2.5/" target="_blank">CC Attribution-ShareAlike 2.5</a></del></p>
<p>This code is released under <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache Software License</a>.</p>
<h3 class="bold">API</h3>
<p><code><strong>CookieJar</strong>(options)</code><br />
Constructor. Takes in a object as options.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">options <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">expires<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>  <span style="color: #006600; font-style: italic;">// time in seconds (defualt: 3600)</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">path<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>     <span style="color: #006600; font-style: italic;">// cookie path</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">domain<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span>   <span style="color: #006600; font-style: italic;">// cookie domain</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">secure<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span>    <span style="color: #006600; font-style: italic;">// secure ?</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #009900;">&#125;</span></div></li></ol></pre></div></div>

<p><code>boolean <strong>put</strong>(string name, mixed value)</code><br />
Puts a particular cookie in the cookie jar. The cookie is associated  with the name. Returns false if cannot add cookie (Ex: max cookie size  exceeded!). Returns true on success.</p>
<p><code>mixed <strong>get</strong>(string name)</code><br />
Gets a particular cookie from the cookie jar. Returns null if not found.</p>
<p><code>boolean <strong>remove</strong>(string name)</code><br />
Removes a particular cookie from the cookie jar. Returns true on success, false otherwise.</p>
<p><code>void <strong>empty</strong>()</code><br />
Empties the Cookie Jar.</p>
<p><code>array <strong>getKeys</strong>()</code><br />
Gets array of all the cookie names.</p>
<p><code>object <strong>getPack</strong>()</code><br />
Gets all the cookies as a single JavaScript object (Package) with name value pairs.</p>
<h3>Change Log</h3>
<p><small><strong>v 0.5</strong> (26-Jan-09)</small></p>
<ol>
<li>Changed the License to Apache Software License 2.0</li>
</ol>
<p><small><strong>v 0.4</strong> (11-Aug-07)</small></p>
<ol>
<li>Removed a extra comma in options (was breaking in IE and Opera). (Thanks Jason)</li>
<li>Removed the parameter name from the initialize function</li>
<li>Changed the way expires date was being calculated. (Thanks David)</li>
</ol>
<p><small><strong>v 0.3</strong> (22-Jun-07)</small></p>
<ol>
<li>Removed dependancy on json.js (<a href="http://web.archive.org/web/20070819092735/http://www.json.org/json.js">http://www.json.org/json.js</a>)</li>
<li><span class="code">empty()</span> function only deletes the cookies set by CookieJar. Leaves alone other cookies like <span class="code">session_id</span> etc.</li>
</ol>
<p><small><strong>v 0.2</strong> (12-Apr-07)</small></p>
<ol>
<li>Released for public use.</li>
</ol>
<h3>Related</h3>
<ul>
<li><a href="http://ajaxian.com/archives/cookiejar-json-cookies">Ajaxian review</a></li>
<li><a href="http://clientside.cnet.com/code-snippets/cookiejsonjs-a-mootools-version-of-cookiejar/" target="_blank">MooTools port</a></li>
<li><a href="http://www.jdempster.com/2007/08/11/jquery-cookiejar/" target="_blank">jQuery port</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.lalit.org/lab/jsoncookies/feed</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>JavaScript/CSS Font Detector</title>
		<link>http://www.lalit.org/lab/javascript-css-font-detect</link>
		<comments>http://www.lalit.org/lab/javascript-css-font-detect#comments</comments>
		<pubDate>Sat, 10 Mar 2007 05:49:58 +0000</pubDate>
		<dc:creator>Lalit</dc:creator>
				<category><![CDATA[lab]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[fonts]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[typography]]></category>

		<guid isPermaLink="false">http://www.orissabiz.com/?p=10</guid>
		<description><![CDATA[JavaScript code to detect available availability of a particular font in a browser using JavaScript and CSS.

I wrote a JavaScript code which can be used to guess if a particular font is present in a machine. This may be help of desktop-like web application developers when they want to provide different skins or fonts preferences [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript code to detect available availability of a particular font in a browser using JavaScript and CSS.<br />
<span id="more-10"></span><br />
I wrote a JavaScript code which can be used to guess if a particular font is present in a machine. This may be help of desktop-like web application developers when they want to provide different skins or fonts preferences to their users. This may also be help for blog skin designers which can provide different fonts for different users based on the list of fonts on their machine. Designers don&#8217;t have to rely on the most common fonts like Arial, Verdana or Times New Roman. Since increasing number of users have modern PC with new operating system / applications, they may very well have a wide array of other common fonts in their machine.</p>
<h3>How does it work?</h3>
<p>This code works on the simple principle that each character appears differently in different fonts. So different fonts will take different width and height for the same string of characters of same font-size.</p>
<p><a href="http://www.lalit.org/wordpress/wp-content/uploads/2008/05/fontwidth.gif"><img class="alignnone size-full wp-image-11" title="Widths of same text in different fonts" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/05/fontwidth.gif" alt="Widths of same text in different fonts" width="390" height="113" /></a></p>
<p>We try to create a string with a specified font-face. If the font-face is not available, it takes up the font-face of the parent element. We then compare the width of the string with the specified font-face and width of the string with the font-face of the parent element, if they are different, then the font exists, otherwise not.</p>
<p>The string which we will use to generate the widths can be anything. But I guess we use &#8216;m&#8217; or &#8216;w&#8217; because these two characters take up the maximum width. And we use a trailing &#8216;l&#8217; so that the same width fonts-faces can get separated based on the width of l character.</p>
<p>I have tested it on Firefox 2, 3, 3.5 IE 6, IE 7, Opera 9, Opera 10.</p>
<p><span style="color: red;"><sup>Update</sup></span> Now works on Opera 9.10. It required to execute the script after its completely loaded. I guess, Opera cannot calculate the offset width until all the parents are completely loaded.</p>
<p><span style="color: red;"><sup>Update (10-Jul-09)</sup></span> Fixed the Tests failing on Firefox 3 issue. changed comparison font to serif from sans-serif, as in FF3.0 font-face of child element didn&#8217;t fallback to font-face of parent element if the font-face of child element is missing.</p>
<h3>Download</h3>
<p>Get the JavaScript code <a href="http://www.lalit.org/wordpress/wp-content/uploads/2008/05/fontdetect.js?ver=1.5" target="_blank">here</a>.</p>
<p>Released under <a href="http://creativecommons.org/licenses/by-sa/2.5/" target="_blank">CC Attribution-ShareAlike 2.5</a></p>
<p>You can also see Lemmi&#8217;s version of the script as <b>Prototype plug-in</b> <a href="#comment-6776">here</a>.</p>
<h3>Demo</h3>
<p>You can test any Font here:</p>
<input id="newfont" style="padding:2px" type="text" />
<input onclick="alert(d.test($('newfont').value))" type="button" value="Test" />

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><ol><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #006600; font-style: italic;">// Usage</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #003366; font-weight: bold;">var</span> detective <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Detector<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>detective.<span style="color: #660066;">test</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'font name'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></li></ol></pre></div></div>

<h3>Fonts on your computer</h3>
<p>This table below shows which fonts are present on your system. (I have listed some of the most common and some uncommon fonts.)</p>
<p><a href="http://exhibit.ecmanaut.googlepages.com/" target="_blank">Johan Sundström</a> has presented this data in a more interesting and cool layout <a href="http://exhibit.ecmanaut.googlepages.com/installed-fonts.html" target="_blank">here</a>. Thanks Johan!</p>
<table id="table" class="_table" border="1" cellspacing="0" cellpadding="5" width="100%" bordercolor="#cccccc">
<tbody>
<tr>
<th>Font Name</th>
<th>Width</th>
<th>Height</th>
<th>Detected?</th>
</tr>
</tbody>
</table>
<p><script type="text/javascript">// <![CDATA[
	/**
	 * Actual function that does all the work. Returns an array with all the info.
	 * My Assumption is that most of the browsers will have arial set as their default sans-serif font.
	 */
	var Detector = function(){
		var h = document.getElementsByTagName("BODY")[0];
		var d = document.createElement("DIV");
		var s = document.createElement("SPAN");
		d.appendChild(s);
		d.style.fontFamily = "serif";		//font for the parent element DIV.
		s.style.fontFamily = "serif";		//have to use serif coz in FF3.0, it doesn't fall back to font of parent element.
		s.style.fontSize   = "72px";			//we test using 72px font size, we may use any size. I guess larger the better.
		s.innerHTML        = "mmmmmmmmmml";		//we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
		h.appendChild(d);
		var defaultWidth   = s.offsetWidth;		//now we have the defaultWidth
		var defaultHeight  = s.offsetHeight;	//and the defaultHeight, we compare other fonts with these.
		h.removeChild(d);
		/* test
		 * params:
		 * font - name of the font you wish to detect
		 * return:
		 * f[0] - Input font name.
		 * f[1] - Computed width.
		 * f[2] - Computed height.
		 * f[3] - Detected? (true/false).
		 */
        function debug(font) {
                h.appendChild(d);
                var f = [];
                f[0] = s.style.fontFamily = font;       // Name of the font
                f[1] = s.offsetWidth;                           // Width
                f[2] = s.offsetHeight;                          // Height
                h.removeChild(d);
                font = font.toLowerCase();
                if (font == "serif") {
                        f[3] = true;    // to set arial and sans-serif true
                } else {
                        f[3] = (f[1] != defaultWidth || f[2] != defaultHeight); // Detected?
                }
                return f;
        }
        function test(font){
                f = debug(font);
                return f[3];
        }
        this.detailedTest = debug;
      this.test = test;
	}
	var fonts = [];
	/**
	 * other stuff
	 */
	function init() {
		fonts.push("cursive");
		fonts.push("monospace");
		fonts.push("serif");
		fonts.push("sans-serif");
		fonts.push("fantasy");
		fonts.push("default");
		fonts.push("Arial");
		fonts.push("Arial Black");
		fonts.push("Arial Narrow");
		fonts.push("Arial Rounded MT Bold");
		fonts.push("Bookman Old Style");
		fonts.push("Bradley Hand ITC");
		fonts.push("Century");
		fonts.push("Century Gothic");
		fonts.push("Comic Sans MS");
		fonts.push("Courier");
		fonts.push("Courier New");
		fonts.push("Georgia");
		fonts.push("Gentium");
		fonts.push("Impact");
		fonts.push("King");
		fonts.push("Lucida Console");
		fonts.push("Lalit");
		fonts.push("Modena");
		fonts.push("Monotype Corsiva");
		fonts.push("Papyrus");
		fonts.push("Tahoma");
		fonts.push("TeX");
		fonts.push("Times");
		fonts.push("Times New Roman");
		fonts.push("Trebuchet MS");
		fonts.push("Verdana");
		fonts.push("Verona");
		d = new Detector();
		// compute height and width for all fonts
		for (i=0; i<fonts.length; i++) {
			fonts.push(d.detailedTest(fonts.shift()));
		}
		//sortResults();
		listResults();
	}
	function listResults() {
		var table = document.getElementById('table');
		for (i=0; i<fonts.length; i++) {
			row = table.insertRow(-1);
			col = row.insertCell(-1);
			col.appendChild(document.createTextNode(fonts[i][0]))
			col.style.fontFamily = fonts[i][0];
			col = row.insertCell(-1).appendChild(document.createTextNode(fonts[i][1]));
			col = row.insertCell(-1).appendChild(document.createTextNode(fonts[i][2]));
			col = row.insertCell(-1);
			fonts[i][3] ? col.className = "green" : col.className = "red";
			col.appendChild(document.createTextNode(fonts[i][3]));
		}
	}
	function sortResults() {
		for (i=0; i<fonts.length-1; i++) {
			for (j=i; j<fonts.length; j++) {
				if(fonts[i][1] < fonts[j][1]) {
					t = fonts[i];
					fonts[i] = fonts[j];
					fonts[j] = t;
				}
			}
		}
	}
Event.observe(window, 'load', init);
// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalit.org/lab/javascript-css-font-detect/feed</wfw:commentRss>
		<slash:comments>42</slash:comments>
		</item>
		<item>
		<title>Snake (Game)</title>
		<link>http://www.lalit.org/lab/snake-game</link>
		<comments>http://www.lalit.org/lab/snake-game#comments</comments>
		<pubDate>Tue, 07 Sep 2004 10:32:32 +0000</pubDate>
		<dc:creator>Lalit</dc:creator>
				<category><![CDATA[lab]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[snake]]></category>

		<guid isPermaLink="false">http://www.lalit.org/?p=211</guid>
		<description><![CDATA[Snake is a tiny game that I made in my computer lab while learning C. This is quite similar to the one found in &#8216;Nokia&#8217; mobile phones and others.
Download: Snake (122 kb, with source code)
]]></description>
			<content:encoded><![CDATA[<p>Snake is a tiny game that I made in my computer lab while learning C. This is quite similar to the one found in &#8216;Nokia&#8217; mobile phones <a href="http://en.wikipedia.org/wiki/Snake_(video_game)" rel="nofollow">and others</a>.</p>
<blockquote><p>Download: <a href='http://www.lalit.org/wordpress/wp-content/uploads/2008/09/snake_src.zip'>Snake</a> (122 kb, with source code)</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.lalit.org/lab/snake-game/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fifteen (puzzle)</title>
		<link>http://www.lalit.org/lab/fifteen-puzzle</link>
		<comments>http://www.lalit.org/lab/fifteen-puzzle#comments</comments>
		<pubDate>Sun, 08 Aug 2004 10:26:35 +0000</pubDate>
		<dc:creator>Lalit</dc:creator>
				<category><![CDATA[lab]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://www.lalit.org/?p=207</guid>
		<description><![CDATA[This is yet another puzzle game in C. It is the legacy puzzle game that is sold in fairs and is popular among children. This is 15 blocks having numbers 1 &#8211; 15 arranged in random order. The objective of the game is to arrange the 15 blocks in ascending order.
Download: Fifteen (zip 13kb)

Screenshot

]]></description>
			<content:encoded><![CDATA[<p>This is yet another puzzle game in C. It is the legacy puzzle game that is sold in fairs and is popular among children. This is 15 blocks having numbers 1 &#8211; 15 arranged in random order. The objective of the game is to arrange the 15 blocks in ascending order.</p>
<blockquote><p>Download: <a href='http://www.lalit.org/wordpress/wp-content/uploads/2008/09/puzzle151.zip'>Fifteen</a> (zip 13kb)</p></blockquote>
<p><span id="more-207"></span></p>
<h3>Screenshot</h3>
<p><a href="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/sspuzzle15.png"><img src="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/sspuzzle15-300x151.png" alt="" title="sspuzzle15" width="300" height="151" class="aligncenter size-medium wp-image-208" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.lalit.org/lab/fifteen-puzzle/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ramayana &#8211; Age of Empires Campaign</title>
		<link>http://www.lalit.org/lab/ramayana-age-of-empires-campaign</link>
		<comments>http://www.lalit.org/lab/ramayana-age-of-empires-campaign#comments</comments>
		<pubDate>Sun, 20 Jul 2003 10:12:15 +0000</pubDate>
		<dc:creator>Lalit</dc:creator>
				<category><![CDATA[lab]]></category>
		<category><![CDATA[aoe]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[ramayana]]></category>

		<guid isPermaLink="false">http://www.lalit.org/?p=196</guid>
		<description><![CDATA[Ramayana is a custom scenario map for the Age of Empires &#8211; The Conquerors. This map is based on the story of the famous Hindu epic &#8216;The Ramayana&#8216;. I made this when the AOE fever was high on me. 
If you want to know more about this epic, you must see this excellent movie or [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Ramayana</strong> is a custom scenario map for the <a rel="nofollow" href="http://www.microsoft.com/games/conquerors/">Age of Empires &#8211; The Conquerors</a>. This map is based on the story of the famous Hindu epic &#8216;<a rel="nofollow" href="http://en.wikipedia.org/wiki/Ramayana">The Ramayana</a>&#8216;. I made this when the AOE fever was high on me. </p>
<p>If you want to know more about this epic, you must see this <a rel="nofollow" href="http://en.wikipedia.org/wiki/Prince_of_Light">excellent movie</a> or this <a href="http://in.youtube.com/watch?v=DwVZeCLEWrE">YouTube video</a>.</p>
<blockquote><p>Download: <a href="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/ramayana.zip">Ramayana</a> (zip 422kb)</p></blockquote>
<p><span id="more-196"></span></p>
<h3>Screenshots</h3>

<a href='http://www.lalit.org/lab/ramayana-age-of-empires-campaign/attachment/ssramayana1' title='ssramayana1'><img width="150" height="150" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/ssramayana1-150x150.jpg" class="attachment-thumbnail" alt="" title="ssramayana1" /></a>
<a href='http://www.lalit.org/lab/ramayana-age-of-empires-campaign/attachment/ssramayana2' title='ssramayana2'><img width="150" height="150" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/ssramayana2-150x150.jpg" class="attachment-thumbnail" alt="" title="ssramayana2" /></a>
<a href='http://www.lalit.org/lab/ramayana-age-of-empires-campaign/attachment/ssramayana3' title='ssramayana3'><img width="150" height="150" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/ssramayana3-150x150.jpg" class="attachment-thumbnail" alt="" title="ssramayana3" /></a>
<a href='http://www.lalit.org/lab/ramayana-age-of-empires-campaign/attachment/ssramayana4' title='ssramayana4'><img width="150" height="150" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/ssramayana4-150x150.jpg" class="attachment-thumbnail" alt="" title="ssramayana4" /></a>
<a href='http://www.lalit.org/lab/ramayana-age-of-empires-campaign/attachment/ssramayana5' title='ssramayana5'><img width="150" height="150" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/ssramayana5-150x150.jpg" class="attachment-thumbnail" alt="" title="ssramayana5" /></a>
<a href='http://www.lalit.org/lab/ramayana-age-of-empires-campaign/attachment/ssramayana6' title='ssramayana6'><img width="150" height="150" src="http://www.lalit.org/wordpress/wp-content/uploads/2008/09/ssramayana6-150x150.jpg" class="attachment-thumbnail" alt="" title="ssramayana6" /></a>

]]></content:encoded>
			<wfw:commentRss>http://www.lalit.org/lab/ramayana-age-of-empires-campaign/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
