<?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>Ciarpame {.com} &#187; IIS</title>
	<atom:link href="http://www.ciarpame.com/tag/iis/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ciarpame.com</link>
	<description>Useful stuffs for the masses. Useless things for few.</description>
	<lastBuildDate>Tue, 01 Dec 2009 13:03:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha</generator>
		<item>
		<title>Far Future Expires Header: come e perchè (IIS, Apache, Tomcat, PHP, ASP, JSP)</title>
		<link>http://www.ciarpame.com/2009/01/14/far-future-expires-header-come-e-perche-iis-apache-tomcat-php-asp-jsp/</link>
		<comments>http://www.ciarpame.com/2009/01/14/far-future-expires-header-come-e-perche-iis-apache-tomcat-php-asp-jsp/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 11:42:12 +0000</pubDate>
		<dc:creator>Giovanni Savastano</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[Web Servers]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://www.ciarpame.com/?p=415</guid>
		<description><![CDATA[Sempre più spesso, nel mondo dello sviluppo web, si sente parlare di aumento delle performance, dimunuzione delle richieste HTTP, caching di contenuti dinamici, compressione di immagini e altre tecniche orientate all&#8217;allegerimento delle pagine di un sito e all&#8217;aumento della visualizzazione sul computer del nostro caro visitatore. Sicuramente una condizione ottimale per ottenere prestazioni migliori della [...]]]></description>
			<content:encoded><![CDATA[<p>Sempre più spesso, nel mondo dello sviluppo web, si sente parlare di aumento delle performance, dimunuzione delle richieste HTTP, caching di contenuti dinamici, compressione di immagini e altre tecniche orientate all&#8217;allegerimento delle pagine di un sito e all&#8217;aumento della visualizzazione sul computer del nostro caro visitatore.</p>
<p>Sicuramente una condizione ottimale per ottenere prestazioni migliori della propria web app o del proprio sito web è impostare il cosidetto <span style="text-decoration: underline;">Far Future Expire Header</span> degli elementi prensenti nelle pagine e delle pagine stesse.</p>
<p>In questo post cercherò di descrivere alcune tecniche per impostare tale condizione.</p>
<p><span id="more-415"></span></p>
<p>Cito dal sito di <a title="YSlow for Firebug" href="http://developer.yahoo.com/yslow/" target="_blank">YSlow</a>:</p>
<blockquote><p>Web page designs are getting richer and richer, which means more scripts, stylesheets, images, and Flash in the page. A first-time visitor to your page may have to make several HTTP requests, but by using the Expires header you make those components cacheable. This avoids unnecessary HTTP requests on subsequent page views. Expires headers are most often used with images, but they should be used on <em>all</em> components including scripts, stylesheets, and Flash components.</p></blockquote>
<p>Quindi in pratica che fa?:-)</p>
<p>L&#8217;Expire Header definisce la scadenza dell&#8217;oggetto che il browser preleva dal server e deposita nella cache del client. Impostando la &#8220;data di scadenza&#8221; ad un periodo molto lontano (Far Future per l&#8217;appunto) diciamo al browser di non richiamare più tale componente durante le succesive visite alla pagina che lo conteneva, rendendo di fatto la navigazione più veloce. Inoltre, applicando questa tecnica ad elementi comuni a tutte le pagine (es: background o file javascript o file css) l&#8217;utente usufruirà dei benefici di questa tecnica fin dalla seconda pagina che andrà a visualizzare sul nostro sito.</p>
<p>Vediamo come fare.</p>
<p><strong>Tramite l&#8217;uso di .htaccess (Apache)</strong></p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">&lt;FilesMatch &quot;\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|php|html|htm|php4|php5)$&quot;&gt;
Header set Expires &quot;Thu, 2 Feb 2010 20:00:00 GMT&quot;
&lt;/FilesMatch&gt;</pre></div></div>

<p>con l&#8217;istruzione FileMatch indichiamo a quale tipo di file applicare le regole all&#8217;interno del blocco di codice e con &#8220;Header set Expires&#8221; andremo a dire ad Apache di <i>servire</i> il contenuto indicato con una data di scadenza molto lontana. Nell&#8217;esempio ho usato il 2 Febbraio 2010, alle 8 di sera.</p>
<p><strong>Tramite le impostazioni di IIS 6.0</strong></p>
<ol>
<li>Aprite il pannello di controllo di Internet Information Services (IIS).</li>
<li>Espandete &#8220;LocalMachine&#8221; sul pannelo di sinistra e succesivamente cliccate su Web Sites</li>
<li>Click con il tasto destro sul sito interessato e selezionate Properties.</li>
<li>Nel tab HTTP Headers, attivate la checkbox &#8220;Enable Content Expiration&#8221; e succesivamente selezionate l&#8217;opzione &#8220;Expire On&#8221;. e impostare il 2 Febbraio 2010 alle 8 di sera</li>
<li>Riavviate tutti i servizi IIS.</li>
</ol>
<p><strong>Con PHP</strong><br />
File far_future_loader.php</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: text/javascript; charset: UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/scripts.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>con il codice sopra potete, ad esempio, raggruppare tutti i vostri file javascript in un unico grande file .php, modificare l&#8217;header Content-type in modo che venga interpretato correttamente dal browser come text/javascript e poi modificarne a piaciemento la deata di scandenza.<br />
Successivamente nell&#8217;header delle vostre pagine, anzichè includere i singoli file javascript con l&#8217;istruzione:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;js/scripts.js&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>userete</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type=&quot;text/javascript&quot; src=&quot;js/far_future_loader.php&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>Potete usare il file far_future_loader.php per ogni tipo di file, semplicemente cambiano il Content-type dei file che andrete ad includere, ad esempio, se volete averne uno anche per i file .css dovrete scrivere:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type: text/css; charset: UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s&quot;</span><span style="color: #339933;">,</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">60</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">24</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/style.css'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>e successivamente nell&#8217;header delle vostre pagine, esattamente come avete fatto per il file javascript, sostituirete l&#8217;istruzione:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot; type=&quot;text/css&quot; /&gt;</pre></div></div>

<p>con</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;stylesheet&quot; href=&quot;css/far_future_loader.php&quot; type=&quot;text/css&quot; /&gt;</pre></div></div>

<p><strong>Con ASP</strong><br />
la procedura è identica a quella per PHP con la sola differenza che in ASP gli header si setttano con i seguenti comandi:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">CacheControl</span> <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;must-revalidate&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Expires</span> <span style="color: #006600; font-weight: bold;">=</span> -<span style="color: #800000;">1</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p><strong>Con JSP (Tomcat)</strong><br />
Grazie a <a href="http://www.jguru.com/guru/viewfaqs.jsp?EID=1168738" target="_blank">Byron Tymvios</a> su <a target="_blank" href="http://www.jguru.com/faq/view.jsp?EID=1311010">jGuru</a><br />
E&#8217; possibile ottenere l&#8217;identico risultato avuto con PHP o ASP tramite i filtri. Ad esempio:</p>

<div class="wp_syntax"><div class="code"><pre class="jsp" style="font-family:monospace;">package com.xyz
&nbsp;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
&nbsp;
public class CacheFilter implements javax.servlet.Filter {
  FilterConfig filterConfig = null;
&nbsp;
  public void init(FilterConfig filterConfig){
    this.filterConfig = filterConfig;
  }
&nbsp;
  public void doFilter(ServletRequest req,
     ServletResponse res,
     FilterChain chain)
     throws IOException, ServletException {
    String sCache = filterConfig.getInitParameter(&quot;cache&quot;);
&nbsp;
    if(sCache != null){       ((HttpServletResponse)res).setHeader(&quot;Cache-Control&quot;, sCache);
&nbsp;
    }
&nbsp;
    chain.doFilter(req, res);
  }
&nbsp;
  public void destroy(){
    this.filterConfig = null;
  }
}</pre></div></div>

<p>e poi impostare nel file web.xml il filtro per ogni contenuto desiderato. Ecco l&#8217;esempio per i file .jpg:</p>

<div class="wp_syntax"><div class="code"><pre class="jsp" style="font-family:monospace;">&lt;filter&gt;
  &lt;filter-name&gt;Cache&lt;/filter-name&gt;
  &lt;filter-class&gt;com.xyz.CacheFilter&lt;/filter-class&gt;
  &lt;init-param&gt;
    &lt;param-name&gt;cache&lt;/param-name&gt;
    &lt;param-value&gt;public, max-age=2592000&lt;/param-value&gt;
  &lt;/init-param&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
  &lt;filter-name&gt;Cache&lt;/filter-name&gt;
  &lt;url-pattern&gt;*.jpg&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;</pre></div></div>

<p>è tutto <img src='http://www.ciarpame.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
nella prossimo post parleremo degli ETag.<br />
Giovanni</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ciarpame.com/2009/01/14/far-future-expires-header-come-e-perche-iis-apache-tomcat-php-asp-jsp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

