Sophie

Sophie

distrib > Fedora > 16 > i386 > by-pkgid > c9cfbd1355daacaf3b34517a6d7b18cc > files > 207

libdap-doc-3.11.0-2.fc15.i686.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>libdap++: HTTPCacheTable.cc Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.7.3 -->
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">libdap++&#160;<span id="projectnumber">Updated for version 3.8.2</span></div>
  </td>
 </tr>
 </tbody>
</table>
</div>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('HTTPCacheTable_8cc.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<h1>HTTPCacheTable.cc</h1>  </div>
</div>
<div class="contents">
<a href="HTTPCacheTable_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a>00002 <span class="comment">// -*- mode: c++; c-basic-offset:4 -*-</span>
<a name="l00003"></a>00003 
<a name="l00004"></a>00004 <span class="comment">// This file is part of libdap, A C++ implementation of the OPeNDAP Data</span>
<a name="l00005"></a>00005 <span class="comment">// Access Protocol.</span>
<a name="l00006"></a>00006 
<a name="l00007"></a>00007 <span class="comment">// Copyright (c) 2002,2003 OPeNDAP, Inc.</span>
<a name="l00008"></a>00008 <span class="comment">// Author: James Gallagher &lt;jgallagher@opendap.org&gt;</span>
<a name="l00009"></a>00009 <span class="comment">//</span>
<a name="l00010"></a>00010 <span class="comment">// This library is free software; you can redistribute it and/or</span>
<a name="l00011"></a>00011 <span class="comment">// modify it under the terms of the GNU Lesser General Public</span>
<a name="l00012"></a>00012 <span class="comment">// License as published by the Free Software Foundation; either</span>
<a name="l00013"></a>00013 <span class="comment">// version 2.1 of the License, or (at your option) any later version.</span>
<a name="l00014"></a>00014 <span class="comment">//</span>
<a name="l00015"></a>00015 <span class="comment">// This library is distributed in the hope that it will be useful,</span>
<a name="l00016"></a>00016 <span class="comment">// but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00017"></a>00017 <span class="comment">// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>
<a name="l00018"></a>00018 <span class="comment">// Lesser General Public License for more details.</span>
<a name="l00019"></a>00019 <span class="comment">//</span>
<a name="l00020"></a>00020 <span class="comment">// You should have received a copy of the GNU Lesser General Public</span>
<a name="l00021"></a>00021 <span class="comment">// License along with this library; if not, write to the Free Software</span>
<a name="l00022"></a>00022 <span class="comment">// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</span>
<a name="l00023"></a>00023 <span class="comment">//</span>
<a name="l00024"></a>00024 <span class="comment">// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.</span>
<a name="l00025"></a>00025 
<a name="l00026"></a>00026 <span class="preprocessor">#include &quot;<a class="code" href="config_8h.html">config.h</a>&quot;</span>
<a name="l00027"></a>00027 
<a name="l00028"></a>00028 <span class="comment">//#define DODS_DEBUG</span>
<a name="l00029"></a>00029 
<a name="l00030"></a>00030 <span class="comment">// TODO: Remove unneeded includes.</span>
<a name="l00031"></a>00031 
<a name="l00032"></a>00032 <span class="preprocessor">#include &lt;pthread.h&gt;</span>
<a name="l00033"></a>00033 <span class="preprocessor">#include &lt;limits.h&gt;</span>
<a name="l00034"></a>00034 <span class="preprocessor">#include &lt;unistd.h&gt;</span>   <span class="comment">// for stat</span>
<a name="l00035"></a>00035 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>  <span class="comment">// for stat and mkdir</span>
<a name="l00036"></a>00036 <span class="preprocessor">#include &lt;sys/stat.h&gt;</span>
<a name="l00037"></a>00037 
<a name="l00038"></a>00038 <span class="preprocessor">#include &lt;cstring&gt;</span>
<a name="l00039"></a>00039 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00040"></a>00040 <span class="preprocessor">#include &lt;sstream&gt;</span>
<a name="l00041"></a>00041 <span class="preprocessor">#include &lt;algorithm&gt;</span>
<a name="l00042"></a>00042 <span class="preprocessor">#include &lt;iterator&gt;</span>
<a name="l00043"></a>00043 <span class="preprocessor">#include &lt;set&gt;</span>
<a name="l00044"></a>00044 
<a name="l00045"></a>00045 <span class="preprocessor">#include &quot;<a class="code" href="Error_8h.html">Error.h</a>&quot;</span>
<a name="l00046"></a>00046 <span class="preprocessor">#include &quot;<a class="code" href="InternalErr_8h.html">InternalErr.h</a>&quot;</span>
<a name="l00047"></a>00047 <span class="preprocessor">#include &quot;<a class="code" href="ResponseTooBigErr_8h.html">ResponseTooBigErr.h</a>&quot;</span>
<a name="l00048"></a>00048 <span class="preprocessor">#ifndef WIN32</span>
<a name="l00049"></a>00049 <span class="preprocessor"></span><span class="preprocessor">#include &quot;<a class="code" href="SignalHandler_8h.html">SignalHandler.h</a>&quot;</span>
<a name="l00050"></a>00050 <span class="preprocessor">#endif</span>
<a name="l00051"></a>00051 <span class="preprocessor"></span><span class="preprocessor">#include &quot;<a class="code" href="HTTPCacheInterruptHandler_8h.html">HTTPCacheInterruptHandler.h</a>&quot;</span>
<a name="l00052"></a>00052 <span class="preprocessor">#include &quot;<a class="code" href="HTTPCacheTable_8h.html">HTTPCacheTable.h</a>&quot;</span>
<a name="l00053"></a>00053 
<a name="l00054"></a>00054 <span class="preprocessor">#include &quot;<a class="code" href="util__mit_8h.html">util_mit.h</a>&quot;</span>
<a name="l00055"></a>00055 <span class="preprocessor">#include &quot;<a class="code" href="debug_8h.html">debug.h</a>&quot;</span>
<a name="l00056"></a>00056 
<a name="l00057"></a>00057 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00058"></a>00058 <span class="preprocessor"></span><span class="preprocessor">#include &lt;direct.h&gt;</span>
<a name="l00059"></a>00059 <span class="preprocessor">#include &lt;time.h&gt;</span>
<a name="l00060"></a>00060 <span class="preprocessor">#include &lt;fcntl.h&gt;</span>
<a name="l00061"></a>00061 <span class="preprocessor">#define MKDIR(a,b) _mkdir((a))</span>
<a name="l00062"></a>00062 <span class="preprocessor"></span><span class="preprocessor">#define REMOVE(a) remove((a))</span>
<a name="l00063"></a>00063 <span class="preprocessor"></span><span class="preprocessor">#define MKSTEMP(a) _open(_mktemp((a)),_O_CREAT,_S_IREAD|_S_IWRITE)</span>
<a name="l00064"></a>00064 <span class="preprocessor"></span><span class="preprocessor">#define DIR_SEPARATOR_CHAR &#39;\\&#39;</span>
<a name="l00065"></a>00065 <span class="preprocessor"></span><span class="preprocessor">#define DIR_SEPARATOR_STR &quot;\\&quot;</span>
<a name="l00066"></a>00066 <span class="preprocessor"></span><span class="preprocessor">#else</span>
<a name="l00067"></a><a class="code" href="HTTPCacheTable_8cc.html#a2bf44344e285663ad0b0b7bb001ac4fd">00067</a> <span class="preprocessor"></span><span class="preprocessor">#define MKDIR(a,b) mkdir((a), (b))</span>
<a name="l00068"></a><a class="code" href="HTTPCacheTable_8cc.html#a2a6edcee170a79b132872d22066754ed">00068</a> <span class="preprocessor"></span><span class="preprocessor">#define REMOVE(a) remove((a))</span>
<a name="l00069"></a><a class="code" href="HTTPCacheTable_8cc.html#ae76603c1b2ef94cb2664dbad1b82a4c6">00069</a> <span class="preprocessor"></span><span class="preprocessor">#define MKSTEMP(a) mkstemp((a))</span>
<a name="l00070"></a><a class="code" href="HTTPCacheTable_8cc.html#a1d22fde6e7501aebedb7663f0b0aa4e2">00070</a> <span class="preprocessor"></span><span class="preprocessor">#define DIR_SEPARATOR_CHAR &#39;/&#39;</span>
<a name="l00071"></a><a class="code" href="HTTPCacheTable_8cc.html#a23e72ca2d8456a971c13441bdfc4586f">00071</a> <span class="preprocessor"></span><span class="preprocessor">#define DIR_SEPARATOR_STR &quot;/&quot;</span>
<a name="l00072"></a>00072 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00073"></a>00073 <span class="preprocessor"></span>
<a name="l00074"></a><a class="code" href="HTTPCacheTable_8cc.html#a9f34e7ee25eb923739b1ea126dce4528">00074</a> <span class="preprocessor">#define CACHE_META &quot;.meta&quot;</span>
<a name="l00075"></a><a class="code" href="HTTPCacheTable_8cc.html#a2fcbe1f1875c3c4b38a63ea99b906c49">00075</a> <span class="preprocessor"></span><span class="preprocessor">#define CACHE_INDEX &quot;.index&quot;</span>
<a name="l00076"></a><a class="code" href="HTTPCacheTable_8cc.html#a69b733628460a6923e506281cd95c43f">00076</a> <span class="preprocessor"></span><span class="preprocessor">#define CACHE_EMPTY_ETAG &quot;@cache@&quot;</span>
<a name="l00077"></a>00077 <span class="preprocessor"></span>
<a name="l00078"></a><a class="code" href="HTTPCacheTable_8cc.html#afefcb1315dce44b5c9459b8f0ad22cb7">00078</a> <span class="preprocessor">#define NO_LM_EXPIRATION 24*3600 // 24 hours</span>
<a name="l00079"></a><a class="code" href="HTTPCacheTable_8cc.html#aedf844327e904ce70cf4112a5d9fc334">00079</a> <span class="preprocessor"></span><span class="preprocessor">#define MAX_LM_EXPIRATION 48*3600 // Max expiration from LM</span>
<a name="l00080"></a>00080 <span class="preprocessor"></span>
<a name="l00081"></a>00081 <span class="comment">// If using LM to find the expiration then take 10% and no more than</span>
<a name="l00082"></a>00082 <span class="comment">// MAX_LM_EXPIRATION.</span>
<a name="l00083"></a>00083 <span class="preprocessor">#ifndef LM_EXPIRATION</span>
<a name="l00084"></a><a class="code" href="HTTPCacheTable_8cc.html#ac34ecfc4f379afa6b7cbcf41c7781d0d">00084</a> <span class="preprocessor"></span><span class="preprocessor">#define LM_EXPIRATION(t) (min((MAX_LM_EXPIRATION), static_cast&lt;int&gt;((t) / 10)))</span>
<a name="l00085"></a>00085 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00086"></a>00086 <span class="preprocessor"></span>
<a name="l00087"></a><a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">00087</a> <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">CACHE_TABLE_SIZE</a> = 1499;
<a name="l00088"></a>00088 
<a name="l00089"></a>00089 <span class="keyword">using namespace </span>std;
<a name="l00090"></a>00090 
<a name="l00091"></a>00091 <span class="keyword">namespace </span>libdap {
<a name="l00092"></a>00092 
<a name="l00096"></a>00096 <span class="keywordtype">int</span>
<a name="l00097"></a><a class="code" href="namespacelibdap.html#a68a16ef70e29c367dabd3cb5165c386a">00097</a> <a class="code" href="namespacelibdap.html#a68a16ef70e29c367dabd3cb5165c386a">get_hash</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url)
<a name="l00098"></a>00098 {
<a name="l00099"></a>00099     <span class="keywordtype">int</span> hash = 0;
<a name="l00100"></a>00100 
<a name="l00101"></a>00101     <span class="keywordflow">for</span> (<span class="keyword">const</span> <span class="keywordtype">char</span> *ptr = url.c_str(); *ptr; ptr++)
<a name="l00102"></a>00102         hash = (<span class="keywordtype">int</span>)((hash * 3 + (*(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *)ptr)) % <a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">CACHE_TABLE_SIZE</a>);
<a name="l00103"></a>00103 
<a name="l00104"></a>00104     <span class="keywordflow">return</span> hash;
<a name="l00105"></a>00105 }
<a name="l00106"></a>00106 
<a name="l00107"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a03e241526025dfaeb2662f7c15ce1d17">00107</a> HTTPCacheTable::HTTPCacheTable(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;cache_root, <span class="keywordtype">int</span> block_size) :
<a name="l00108"></a>00108         d_cache_root(cache_root),
<a name="l00109"></a>00109         d_block_size(block_size),
<a name="l00110"></a>00110         d_current_size(0),
<a name="l00111"></a>00111         d_new_entries(0)
<a name="l00112"></a>00112 {
<a name="l00113"></a>00113         d_cache_index = cache_root + <a class="code" href="HTTPCacheTable_8cc.html#a2fcbe1f1875c3c4b38a63ea99b906c49">CACHE_INDEX</a>;
<a name="l00114"></a>00114         
<a name="l00115"></a>00115         d_cache_table = <span class="keyword">new</span> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">CacheEntries</a>*[<a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">CACHE_TABLE_SIZE</a>];
<a name="l00116"></a>00116         
<a name="l00117"></a>00117         <span class="comment">// Initialize the cache table.</span>
<a name="l00118"></a>00118     <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">CACHE_TABLE_SIZE</a>; ++i)
<a name="l00119"></a>00119         d_cache_table[i] = 0;
<a name="l00120"></a>00120     
<a name="l00121"></a>00121     <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a24e70dae002ffeef9641bb6493109513">cache_index_read</a>();
<a name="l00122"></a>00122 }
<a name="l00123"></a>00123 
<a name="l00127"></a>00127 <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span>
<a name="l00128"></a>00128 delete_cache_entry(<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *e)
<a name="l00129"></a>00129 {
<a name="l00130"></a>00130     <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Deleting CacheEntry: &quot;</span> &lt;&lt; e &lt;&lt; endl);
<a name="l00131"></a>00131     <span class="keyword">delete</span> e;
<a name="l00132"></a>00132 }
<a name="l00133"></a>00133 
<a name="l00134"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a78d0655261edb7f9ff0b2d61b7f97917">00134</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a78d0655261edb7f9ff0b2d61b7f97917">HTTPCacheTable::~HTTPCacheTable</a>() {
<a name="l00135"></a>00135         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">CACHE_TABLE_SIZE</a>; ++i) {
<a name="l00136"></a>00136                 <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">HTTPCacheTable::CacheEntries</a> *cp = get_cache_table()[i];
<a name="l00137"></a>00137                 <span class="keywordflow">if</span> (cp) {
<a name="l00138"></a>00138                         <span class="comment">// delete each entry</span>
<a name="l00139"></a>00139                         for_each(cp-&gt;begin(), cp-&gt;end(), delete_cache_entry);
<a name="l00140"></a>00140                         
<a name="l00141"></a>00141                         <span class="comment">// now delete the vector that held the entries</span>
<a name="l00142"></a>00142                         <span class="keyword">delete</span> get_cache_table()[i];
<a name="l00143"></a>00143                         get_cache_table()[i] = 0;
<a name="l00144"></a>00144                 }
<a name="l00145"></a>00145         }
<a name="l00146"></a>00146         
<a name="l00147"></a>00147         <span class="keyword">delete</span>[] d_cache_table;
<a name="l00148"></a>00148 }
<a name="l00149"></a>00149 
<a name="l00157"></a>00157 <span class="keyword">class </span>DeleteExpired : <span class="keyword">public</span> unary_function&lt;HTTPCacheTable::CacheEntry *&amp;, void&gt; {
<a name="l00158"></a>00158         time_t d_time;
<a name="l00159"></a>00159         <a class="code" href="classlibdap_1_1HTTPCacheTable.html">HTTPCacheTable</a> &amp;d_table;
<a name="l00160"></a>00160 
<a name="l00161"></a>00161 <span class="keyword">public</span>:
<a name="l00162"></a>00162         DeleteExpired(<a class="code" href="classlibdap_1_1HTTPCacheTable.html">HTTPCacheTable</a> &amp;table, time_t t) :
<a name="l00163"></a>00163                 d_time(t), d_table(table) {
<a name="l00164"></a>00164                 <span class="keywordflow">if</span> (!t)
<a name="l00165"></a>00165                         d_time = time(0); <span class="comment">// 0 == now</span>
<a name="l00166"></a>00166         } 
<a name="l00167"></a>00167 
<a name="l00168"></a>00168         <span class="keywordtype">void</span> operator()(HTTPCacheTable::CacheEntry *&amp;e) {
<a name="l00169"></a>00169                 <span class="keywordflow">if</span> (e &amp;&amp; !e-&gt;readers &amp;&amp; (e-&gt;freshness_lifetime
<a name="l00170"></a>00170                                 &lt; (e-&gt;corrected_initial_age + (d_time - e-&gt;response_time)))) {
<a name="l00171"></a>00171                         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Deleting expired cache entry: &quot;</span> &lt;&lt; e-&gt;url &lt;&lt; endl);
<a name="l00172"></a>00172                         d_table.remove_cache_entry(e);
<a name="l00173"></a>00173                         <span class="keyword">delete</span> e; e = 0;
<a name="l00174"></a>00174                 }
<a name="l00175"></a>00175         }
<a name="l00176"></a>00176 };
<a name="l00177"></a>00177 
<a name="l00178"></a>00178 <span class="comment">// @param time base deletes againt this time, defaults to 0 (now)</span>
<a name="l00179"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a107d8cc87728a855eaa18210b90c09d5">00179</a> <span class="keywordtype">void</span> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a107d8cc87728a855eaa18210b90c09d5">HTTPCacheTable::delete_expired_entries</a>(time_t time) {
<a name="l00180"></a>00180         <span class="comment">// Walk through and delete all the expired entries.</span>
<a name="l00181"></a>00181         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> cnt = 0; cnt &lt; <a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">CACHE_TABLE_SIZE</a>; cnt++) {
<a name="l00182"></a>00182                 <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">HTTPCacheTable::CacheEntries</a> *slot = get_cache_table()[cnt];
<a name="l00183"></a>00183                 <span class="keywordflow">if</span> (slot) {
<a name="l00184"></a>00184                         for_each(slot-&gt;begin(), slot-&gt;end(), DeleteExpired(*<span class="keyword">this</span>, time));
<a name="l00185"></a>00185                         slot-&gt;erase(<span class="keyword">remove</span>(slot-&gt;begin(), slot-&gt;end(),
<a name="l00186"></a>00186                                         <span class="keyword">static_cast&lt;</span><a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *<span class="keyword">&gt;</span>(0)), slot-&gt;end());
<a name="l00187"></a>00187                 }
<a name="l00188"></a>00188         }
<a name="l00189"></a>00189 }
<a name="l00190"></a>00190 
<a name="l00197"></a>00197 <span class="keyword">class </span>DeleteByHits : <span class="keyword">public</span> unary_function&lt;HTTPCacheTable::CacheEntry *&amp;, void&gt; {
<a name="l00198"></a>00198         <a class="code" href="classlibdap_1_1HTTPCacheTable.html">HTTPCacheTable</a> &amp;d_table;
<a name="l00199"></a>00199         <span class="keywordtype">int</span> d_hits;
<a name="l00200"></a>00200 
<a name="l00201"></a>00201 <span class="keyword">public</span>:
<a name="l00202"></a>00202         DeleteByHits(<a class="code" href="classlibdap_1_1HTTPCacheTable.html">HTTPCacheTable</a> &amp;table, <span class="keywordtype">int</span> hits) :
<a name="l00203"></a>00203                 d_table(table), d_hits(hits) {
<a name="l00204"></a>00204         }
<a name="l00205"></a>00205 
<a name="l00206"></a>00206         <span class="keywordtype">void</span> operator()(HTTPCacheTable::CacheEntry *&amp;e) {
<a name="l00207"></a>00207                 <span class="keywordflow">if</span> (e &amp;&amp; !e-&gt;readers &amp;&amp; e-&gt;hits &lt;= d_hits) {
<a name="l00208"></a>00208                         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Deleting cache entry: &quot;</span> &lt;&lt; e-&gt;url &lt;&lt; endl);
<a name="l00209"></a>00209                         d_table.remove_cache_entry(e);
<a name="l00210"></a>00210                         <span class="keyword">delete</span> e; e = 0;
<a name="l00211"></a>00211                 }
<a name="l00212"></a>00212         }
<a name="l00213"></a>00213 };
<a name="l00214"></a>00214 
<a name="l00215"></a>00215 <span class="keywordtype">void</span> 
<a name="l00216"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a51707b1b5533c2d06e57dc52a0ea90b2">00216</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a51707b1b5533c2d06e57dc52a0ea90b2">HTTPCacheTable::delete_by_hits</a>(<span class="keywordtype">int</span> hits) {
<a name="l00217"></a>00217     <span class="keywordflow">for</span> (<span class="keywordtype">int</span> cnt = 0; cnt &lt; <a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">CACHE_TABLE_SIZE</a>; cnt++) {
<a name="l00218"></a>00218         <span class="keywordflow">if</span> (get_cache_table()[cnt]) {
<a name="l00219"></a>00219             <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">HTTPCacheTable::CacheEntries</a> *slot = get_cache_table()[cnt];
<a name="l00220"></a>00220             for_each(slot-&gt;begin(), slot-&gt;end(), DeleteByHits(*<span class="keyword">this</span>, hits));
<a name="l00221"></a>00221             slot-&gt;erase(<span class="keyword">remove</span>(slot-&gt;begin(), slot-&gt;end(),
<a name="l00222"></a>00222                                <span class="keyword">static_cast&lt;</span><a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a>*<span class="keyword">&gt;</span>(0)),
<a name="l00223"></a>00223                         slot-&gt;end());
<a name="l00224"></a>00224 
<a name="l00225"></a>00225         }
<a name="l00226"></a>00226     }
<a name="l00227"></a>00227 }
<a name="l00228"></a>00228 
<a name="l00233"></a>00233 <span class="keyword">class </span>DeleteBySize : <span class="keyword">public</span> unary_function&lt;HTTPCacheTable::CacheEntry *&amp;, void&gt; {
<a name="l00234"></a>00234         <a class="code" href="classlibdap_1_1HTTPCacheTable.html">HTTPCacheTable</a> &amp;d_table;
<a name="l00235"></a>00235         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> d_size;
<a name="l00236"></a>00236 
<a name="l00237"></a>00237 <span class="keyword">public</span>:
<a name="l00238"></a>00238         DeleteBySize(<a class="code" href="classlibdap_1_1HTTPCacheTable.html">HTTPCacheTable</a> &amp;table, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size) :
<a name="l00239"></a>00239                 d_table(table), d_size(size) {
<a name="l00240"></a>00240         }
<a name="l00241"></a>00241 
<a name="l00242"></a>00242         <span class="keywordtype">void</span> operator()(HTTPCacheTable::CacheEntry *&amp;e) {
<a name="l00243"></a>00243                 <span class="keywordflow">if</span> (e &amp;&amp; !e-&gt;readers &amp;&amp; e-&gt;size &gt; d_size) {
<a name="l00244"></a>00244                         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Deleting cache entry: &quot;</span> &lt;&lt; e-&gt;url &lt;&lt; endl);
<a name="l00245"></a>00245                         d_table.remove_cache_entry(e);
<a name="l00246"></a>00246                         <span class="keyword">delete</span> e; e = 0;
<a name="l00247"></a>00247                 }
<a name="l00248"></a>00248         }
<a name="l00249"></a>00249 };
<a name="l00250"></a>00250 
<a name="l00251"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a04655ab9af621a261420ed8af681cf80">00251</a> <span class="keywordtype">void</span> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a04655ab9af621a261420ed8af681cf80">HTTPCacheTable::delete_by_size</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> size) {
<a name="l00252"></a>00252     <span class="keywordflow">for</span> (<span class="keywordtype">int</span> cnt = 0; cnt &lt; <a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">CACHE_TABLE_SIZE</a>; cnt++) {
<a name="l00253"></a>00253         <span class="keywordflow">if</span> (get_cache_table()[cnt]) {
<a name="l00254"></a>00254             <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">HTTPCacheTable::CacheEntries</a> *slot = get_cache_table()[cnt];
<a name="l00255"></a>00255             for_each(slot-&gt;begin(), slot-&gt;end(), DeleteBySize(*<span class="keyword">this</span>, size));
<a name="l00256"></a>00256             slot-&gt;erase(<span class="keyword">remove</span>(slot-&gt;begin(), slot-&gt;end(),
<a name="l00257"></a>00257                                <span class="keyword">static_cast&lt;</span><a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a>*<span class="keyword">&gt;</span>(0)),
<a name="l00258"></a>00258                         slot-&gt;end());
<a name="l00259"></a>00259 
<a name="l00260"></a>00260         }
<a name="l00261"></a>00261     }
<a name="l00262"></a>00262 }
<a name="l00263"></a>00263 
<a name="l00270"></a>00270 
<a name="l00277"></a>00277 <span class="keywordtype">bool</span>
<a name="l00278"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a3e636690b069b3384e908135fcaebd51">00278</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a3e636690b069b3384e908135fcaebd51">HTTPCacheTable::cache_index_delete</a>()
<a name="l00279"></a>00279 {
<a name="l00280"></a>00280         d_new_entries = 0;
<a name="l00281"></a>00281         
<a name="l00282"></a>00282     <span class="keywordflow">return</span> (<a class="code" href="HTTPCacheTable_8cc.html#a2a6edcee170a79b132872d22066754ed">REMOVE</a>(d_cache_index.c_str()) == 0);
<a name="l00283"></a>00283 }
<a name="l00284"></a>00284 
<a name="l00293"></a>00293 <span class="keywordtype">bool</span>
<a name="l00294"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a24e70dae002ffeef9641bb6493109513">00294</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a24e70dae002ffeef9641bb6493109513">HTTPCacheTable::cache_index_read</a>()
<a name="l00295"></a>00295 {
<a name="l00296"></a>00296     FILE *fp = fopen(d_cache_index.c_str(), <span class="stringliteral">&quot;r&quot;</span>);
<a name="l00297"></a>00297     <span class="comment">// If the cache index can&#39;t be opened that&#39;s OK; start with an empty</span>
<a name="l00298"></a>00298     <span class="comment">// cache. 09/05/02 jhrg</span>
<a name="l00299"></a>00299     <span class="keywordflow">if</span> (!fp) {
<a name="l00300"></a>00300         <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00301"></a>00301     }
<a name="l00302"></a>00302 
<a name="l00303"></a>00303     <span class="keywordtype">char</span> line[1024];
<a name="l00304"></a>00304     <span class="keywordflow">while</span> (!feof(fp) &amp;&amp; fgets(line, 1024, fp)) {
<a name="l00305"></a>00305         <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a8f5660848048a98454fce23c28de573c">add_entry_to_cache_table</a>(<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a48569affedb01e6e0e87343bcc621c0a">cache_index_parse_line</a>(line));
<a name="l00306"></a>00306         <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; line &lt;&lt; endl);
<a name="l00307"></a>00307     }
<a name="l00308"></a>00308 
<a name="l00309"></a>00309     <span class="keywordtype">int</span> res = fclose(fp) ;
<a name="l00310"></a>00310     <span class="keywordflow">if</span> (res) {
<a name="l00311"></a>00311         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;HTTPCache::cache_index_read - Failed to close &quot;</span> &lt;&lt; (<span class="keywordtype">void</span> *)fp &lt;&lt; endl);
<a name="l00312"></a>00312     }
<a name="l00313"></a>00313 
<a name="l00314"></a>00314     d_new_entries = 0;
<a name="l00315"></a>00315     
<a name="l00316"></a>00316     <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00317"></a>00317 }
<a name="l00318"></a>00318 
<a name="l00326"></a>00326 <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *
<a name="l00327"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a48569affedb01e6e0e87343bcc621c0a">00327</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a48569affedb01e6e0e87343bcc621c0a">HTTPCacheTable::cache_index_parse_line</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> *line)
<a name="l00328"></a>00328 {
<a name="l00329"></a>00329     <span class="comment">// Read the line and create the cache object</span>
<a name="l00330"></a>00330         <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry = <span class="keyword">new</span> <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a>;
<a name="l00331"></a>00331     istringstream iss(line);
<a name="l00332"></a>00332     iss &gt;&gt; entry-&gt;url;
<a name="l00333"></a>00333     iss &gt;&gt; entry-&gt;cachename;
<a name="l00334"></a>00334 
<a name="l00335"></a>00335     iss &gt;&gt; entry-&gt;etag;
<a name="l00336"></a>00336     <span class="keywordflow">if</span> (entry-&gt;etag == <a class="code" href="HTTPCacheTable_8cc.html#a69b733628460a6923e506281cd95c43f">CACHE_EMPTY_ETAG</a>)
<a name="l00337"></a>00337         entry-&gt;etag = <span class="stringliteral">&quot;&quot;</span>;
<a name="l00338"></a>00338 
<a name="l00339"></a>00339     iss &gt;&gt; entry-&gt;lm;
<a name="l00340"></a>00340     iss &gt;&gt; entry-&gt;expires;
<a name="l00341"></a>00341     iss &gt;&gt; entry-&gt;size;
<a name="l00342"></a>00342     iss &gt;&gt; entry-&gt;range; <span class="comment">// range is not used. 10/02/02 jhrg</span>
<a name="l00343"></a>00343 
<a name="l00344"></a>00344     iss &gt;&gt; entry-&gt;hash;
<a name="l00345"></a>00345     iss &gt;&gt; entry-&gt;hits;
<a name="l00346"></a>00346     iss &gt;&gt; entry-&gt;freshness_lifetime;
<a name="l00347"></a>00347     iss &gt;&gt; entry-&gt;response_time;
<a name="l00348"></a>00348     iss &gt;&gt; entry-&gt;corrected_initial_age;
<a name="l00349"></a>00349 
<a name="l00350"></a>00350     iss &gt;&gt; entry-&gt;must_revalidate;
<a name="l00351"></a>00351 
<a name="l00352"></a>00352     <span class="keywordflow">return</span> entry;
<a name="l00353"></a>00353 }
<a name="l00354"></a>00354 
<a name="l00357"></a>00357 <span class="keyword">class </span>WriteOneCacheEntry :
<a name="l00358"></a>00358         <span class="keyword">public</span> unary_function&lt;HTTPCacheTable::CacheEntry *, void&gt;
<a name="l00359"></a>00359 {
<a name="l00360"></a>00360 
<a name="l00361"></a>00361     FILE *d_fp;
<a name="l00362"></a>00362 
<a name="l00363"></a>00363 <span class="keyword">public</span>:
<a name="l00364"></a>00364     WriteOneCacheEntry(FILE *fp) : d_fp(fp)
<a name="l00365"></a>00365     {}
<a name="l00366"></a>00366 
<a name="l00367"></a>00367     <span class="keywordtype">void</span> operator()(HTTPCacheTable::CacheEntry *e)
<a name="l00368"></a>00368     {
<a name="l00369"></a>00369         <span class="keywordflow">if</span> (e &amp;&amp; fprintf(d_fp,
<a name="l00370"></a>00370                          <span class="stringliteral">&quot;%s %s %s %ld %ld %ld %c %d %d %ld %ld %ld %c\r\n&quot;</span>,
<a name="l00371"></a>00371                          e-&gt;url.c_str(),
<a name="l00372"></a>00372                          e-&gt;cachename.c_str(),
<a name="l00373"></a>00373                          e-&gt;etag == <span class="stringliteral">&quot;&quot;</span> ? <a class="code" href="HTTPCacheTable_8cc.html#a69b733628460a6923e506281cd95c43f">CACHE_EMPTY_ETAG</a> : e-&gt;etag.c_str(),
<a name="l00374"></a>00374                          (long)(e-&gt;lm),
<a name="l00375"></a>00375                          (long)(e-&gt;expires),
<a name="l00376"></a>00376                          e-&gt;size,
<a name="l00377"></a>00377                          e-&gt;range ? <span class="charliteral">&#39;1&#39;</span> : <span class="charliteral">&#39;0&#39;</span>, <span class="comment">// not used. 10/02/02 jhrg</span>
<a name="l00378"></a>00378                          e-&gt;hash,
<a name="l00379"></a>00379                          e-&gt;hits,
<a name="l00380"></a>00380                          (long)(e-&gt;freshness_lifetime),
<a name="l00381"></a>00381                          (long)(e-&gt;response_time),
<a name="l00382"></a>00382                          (long)(e-&gt;corrected_initial_age),
<a name="l00383"></a>00383                          e-&gt;must_revalidate ? <span class="charliteral">&#39;1&#39;</span> : <span class="charliteral">&#39;0&#39;</span>) &lt; 0)
<a name="l00384"></a>00384             <span class="keywordflow">throw</span> Error(<span class="stringliteral">&quot;Cache Index. Error writing cache index\n&quot;</span>);
<a name="l00385"></a>00385     }
<a name="l00386"></a>00386 };
<a name="l00387"></a>00387 
<a name="l00397"></a>00397 <span class="keywordtype">void</span>
<a name="l00398"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a7cd5a3a24fd0cd41ac0d9a3192075e9c">00398</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a7cd5a3a24fd0cd41ac0d9a3192075e9c">HTTPCacheTable::cache_index_write</a>()
<a name="l00399"></a>00399 {
<a name="l00400"></a>00400     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache Index. Writing index &quot;</span> &lt;&lt; d_cache_index &lt;&lt; endl);
<a name="l00401"></a>00401 
<a name="l00402"></a>00402     <span class="comment">// Open the file for writing.</span>
<a name="l00403"></a>00403     FILE * fp = NULL;
<a name="l00404"></a>00404     <span class="keywordflow">if</span> ((fp = fopen(d_cache_index.c_str(), <span class="stringliteral">&quot;wb&quot;</span>)) == NULL) {
<a name="l00405"></a>00405         <span class="keywordflow">throw</span> <a class="code" href="classlibdap_1_1Error.html" title="A class for error processing.">Error</a>(<span class="keywordtype">string</span>(<span class="stringliteral">&quot;Cache Index. Can&#39;t open `&quot;</span>) + d_cache_index
<a name="l00406"></a>00406                     + <span class="keywordtype">string</span>(<span class="stringliteral">&quot;&#39; for writing&quot;</span>));
<a name="l00407"></a>00407     }
<a name="l00408"></a>00408 
<a name="l00409"></a>00409     <span class="comment">// Walk through the list and write it out. The format is really</span>
<a name="l00410"></a>00410     <span class="comment">// simple as we keep it all in ASCII.</span>
<a name="l00411"></a>00411 
<a name="l00412"></a>00412     <span class="keywordflow">for</span> (<span class="keywordtype">int</span> cnt = 0; cnt &lt; <a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">CACHE_TABLE_SIZE</a>; cnt++) {
<a name="l00413"></a>00413         <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">HTTPCacheTable::CacheEntries</a> *cp = get_cache_table()[cnt];
<a name="l00414"></a>00414         <span class="keywordflow">if</span> (cp)
<a name="l00415"></a>00415             for_each(cp-&gt;begin(), cp-&gt;end(), WriteOneCacheEntry(fp));
<a name="l00416"></a>00416     }
<a name="l00417"></a>00417 
<a name="l00418"></a>00418     <span class="comment">/* Done writing */</span>
<a name="l00419"></a>00419     <span class="keywordtype">int</span> res = fclose(fp);
<a name="l00420"></a>00420     <span class="keywordflow">if</span> (res) {
<a name="l00421"></a>00421         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;HTTPCache::cache_index_write - Failed to close &quot;</span>
<a name="l00422"></a>00422             &lt;&lt; (<span class="keywordtype">void</span> *)fp &lt;&lt; endl);
<a name="l00423"></a>00423     }
<a name="l00424"></a>00424 
<a name="l00425"></a>00425     d_new_entries = 0;
<a name="l00426"></a>00426 }
<a name="l00427"></a>00427 
<a name="l00429"></a>00429 
<a name="l00442"></a>00442 <span class="keywordtype">string</span>
<a name="l00443"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0f7f6193848fb3ba8ad17082fd95b6c6">00443</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0f7f6193848fb3ba8ad17082fd95b6c6">HTTPCacheTable::create_hash_directory</a>(<span class="keywordtype">int</span> hash)
<a name="l00444"></a>00444 {
<a name="l00445"></a>00445     <span class="keyword">struct </span>stat stat_info;
<a name="l00446"></a>00446     ostringstream path;
<a name="l00447"></a>00447 
<a name="l00448"></a>00448     path &lt;&lt; d_cache_root &lt;&lt; hash;
<a name="l00449"></a>00449     <span class="keywordtype">string</span> p = path.str();
<a name="l00450"></a>00450 
<a name="l00451"></a>00451     <span class="keywordflow">if</span> (stat(p.c_str(), &amp;stat_info) == -1) {
<a name="l00452"></a>00452         <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache....... Create dir &quot;</span> &lt;&lt; p &lt;&lt; endl);
<a name="l00453"></a>00453         <span class="keywordflow">if</span> (<a class="code" href="HTTPCacheTable_8cc.html#a2bf44344e285663ad0b0b7bb001ac4fd">MKDIR</a>(p.c_str(), 0777) &lt; 0) {
<a name="l00454"></a>00454             <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache....... Can&#39;t create...&quot;</span> &lt;&lt; endl);
<a name="l00455"></a>00455             <span class="keywordflow">throw</span> <a class="code" href="classlibdap_1_1Error.html" title="A class for error processing.">Error</a>(<span class="stringliteral">&quot;Could not create cache slot to hold response! Check the write permissions on your disk cache directory. Cache root: &quot;</span> + d_cache_root + <span class="stringliteral">&quot;.&quot;</span>);
<a name="l00456"></a>00456         }
<a name="l00457"></a>00457     }
<a name="l00458"></a>00458     <span class="keywordflow">else</span> {
<a name="l00459"></a>00459         <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache....... Directory &quot;</span> &lt;&lt; p &lt;&lt; <span class="stringliteral">&quot; already exists&quot;</span>
<a name="l00460"></a>00460              &lt;&lt; endl);
<a name="l00461"></a>00461     }
<a name="l00462"></a>00462 
<a name="l00463"></a>00463     <span class="keywordflow">return</span> p;
<a name="l00464"></a>00464 }
<a name="l00465"></a>00465 
<a name="l00480"></a>00480 <span class="keywordtype">void</span>
<a name="l00481"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0ab52cb3d5d01575977adb33a21b31d2">00481</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0ab52cb3d5d01575977adb33a21b31d2">HTTPCacheTable::create_location</a>(<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry)
<a name="l00482"></a>00482 {
<a name="l00483"></a>00483     <span class="keywordtype">string</span> hash_dir = <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0f7f6193848fb3ba8ad17082fd95b6c6">create_hash_directory</a>(entry-&gt;hash);
<a name="l00484"></a>00484 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00485"></a>00485 <span class="preprocessor"></span>    hash_dir += <span class="stringliteral">&quot;\\dodsXXXXXX&quot;</span>;
<a name="l00486"></a>00486 <span class="preprocessor">#else</span>
<a name="l00487"></a>00487 <span class="preprocessor"></span>    hash_dir += <span class="stringliteral">&quot;/dodsXXXXXX&quot;</span>; <span class="comment">// mkstemp uses six characters.</span>
<a name="l00488"></a>00488 <span class="preprocessor">#endif</span>
<a name="l00489"></a>00489 <span class="preprocessor"></span>
<a name="l00490"></a>00490     <span class="comment">// mkstemp uses the storage passed to it; must be writable and local.</span>
<a name="l00491"></a>00491     <span class="keywordtype">char</span> *templat = <span class="keyword">new</span> <span class="keywordtype">char</span>[hash_dir.size() + 1];
<a name="l00492"></a>00492     strcpy(templat, hash_dir.c_str());
<a name="l00493"></a>00493 
<a name="l00494"></a>00494     <span class="comment">// Open truncated for update. NB: mkstemp() returns a file descriptor.</span>
<a name="l00495"></a>00495     <span class="comment">// man mkstemp says &quot;... The file is opened with the O_EXCL flag,</span>
<a name="l00496"></a>00496     <span class="comment">// guaranteeing that when mkstemp returns successfully we are the only</span>
<a name="l00497"></a>00497     <span class="comment">// user.&quot; 09/19/02 jhrg</span>
<a name="l00498"></a>00498     <span class="keywordtype">int</span> fd = <a class="code" href="HTTPCacheTable_8cc.html#ae76603c1b2ef94cb2664dbad1b82a4c6">MKSTEMP</a>(templat); <span class="comment">// fd mode is 666 or 600 (Unix)</span>
<a name="l00499"></a>00499     <span class="keywordflow">if</span> (fd &lt; 0) {
<a name="l00500"></a>00500         <span class="keyword">delete</span>[] templat; templat = 0;
<a name="l00501"></a>00501         close(fd);
<a name="l00502"></a>00502         <span class="keywordflow">throw</span> <a class="code" href="classlibdap_1_1Error.html" title="A class for error processing.">Error</a>(<span class="stringliteral">&quot;The HTTP Cache could not create a file to hold the response; it will not be cached.&quot;</span>);
<a name="l00503"></a>00503     }
<a name="l00504"></a>00504 
<a name="l00505"></a>00505     entry-&gt;cachename = templat;
<a name="l00506"></a>00506     <span class="keyword">delete</span>[] templat; templat = 0;
<a name="l00507"></a>00507     close(fd);
<a name="l00508"></a>00508 }
<a name="l00509"></a>00509 
<a name="l00510"></a>00510 
<a name="l00512"></a>00512 <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">int</span>
<a name="l00513"></a>00513 entry_disk_space(<span class="keywordtype">int</span> size, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> block_size)
<a name="l00514"></a>00514 {
<a name="l00515"></a>00515     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> num_of_blocks = (size + block_size) / block_size;
<a name="l00516"></a>00516     
<a name="l00517"></a>00517     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;size: &quot;</span> &lt;&lt; size &lt;&lt; <span class="stringliteral">&quot;, block_size: &quot;</span> &lt;&lt; block_size
<a name="l00518"></a>00518         &lt;&lt; <span class="stringliteral">&quot;, num_of_blocks: &quot;</span> &lt;&lt; num_of_blocks &lt;&lt; endl);
<a name="l00519"></a>00519 
<a name="l00520"></a>00520     <span class="keywordflow">return</span> num_of_blocks * block_size;
<a name="l00521"></a>00521 }
<a name="l00522"></a>00522 
<a name="l00526"></a>00526 
<a name="l00532"></a>00532 <span class="keywordtype">void</span>
<a name="l00533"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a8f5660848048a98454fce23c28de573c">00533</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a8f5660848048a98454fce23c28de573c">HTTPCacheTable::add_entry_to_cache_table</a>(<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">CacheEntry</a> *entry)
<a name="l00534"></a>00534 {
<a name="l00535"></a>00535     <span class="keywordtype">int</span> hash = entry-&gt;hash;
<a name="l00536"></a>00536 
<a name="l00537"></a>00537     <span class="keywordflow">if</span> (!d_cache_table[hash])
<a name="l00538"></a>00538         d_cache_table[hash] = <span class="keyword">new</span> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">CacheEntries</a>;
<a name="l00539"></a>00539 
<a name="l00540"></a>00540     d_cache_table[hash]-&gt;push_back(entry);
<a name="l00541"></a>00541     
<a name="l00542"></a>00542     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;add_entry_to_cache_table, current_size: &quot;</span> &lt;&lt; d_current_size
<a name="l00543"></a>00543         &lt;&lt; <span class="stringliteral">&quot;, entry-&gt;size: &quot;</span> &lt;&lt; entry-&gt;size &lt;&lt; <span class="stringliteral">&quot;, block size: &quot;</span> &lt;&lt; d_block_size 
<a name="l00544"></a>00544         &lt;&lt; endl);
<a name="l00545"></a>00545     
<a name="l00546"></a>00546     d_current_size += entry_disk_space(entry-&gt;size, d_block_size);
<a name="l00547"></a>00547 
<a name="l00548"></a>00548     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;add_entry_to_cache_table, current_size: &quot;</span> &lt;&lt; d_current_size &lt;&lt; endl);
<a name="l00549"></a>00549     
<a name="l00550"></a>00550     <a class="code" href="classlibdap_1_1HTTPCacheTable.html#af17bd7a96186412f3ef53caed42db767">increment_new_entries</a>();
<a name="l00551"></a>00551 }
<a name="l00552"></a>00552 
<a name="l00556"></a>00556 <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *
<a name="l00557"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#ac8c8e58c208b8c542b89f500829032a0">00557</a> HTTPCacheTable::get_locked_entry_from_cache_table(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url) <span class="comment">/*const*/</span>
<a name="l00558"></a>00558 {
<a name="l00559"></a>00559     <span class="keywordflow">return</span> get_locked_entry_from_cache_table(<a class="code" href="namespacelibdap.html#a68a16ef70e29c367dabd3cb5165c386a">get_hash</a>(url), url);
<a name="l00560"></a>00560 }
<a name="l00561"></a>00561 
<a name="l00569"></a>00569 <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *
<a name="l00570"></a>00570 HTTPCacheTable::get_locked_entry_from_cache_table(<span class="keywordtype">int</span> hash, <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url) <span class="comment">/*const*/</span>
<a name="l00571"></a>00571 {
<a name="l00572"></a>00572         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;url: &quot;</span> &lt;&lt; url &lt;&lt; <span class="stringliteral">&quot;; hash: &quot;</span> &lt;&lt; hash &lt;&lt; endl);
<a name="l00573"></a>00573         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;d_cache_table: &quot;</span> &lt;&lt; hex &lt;&lt; d_cache_table &lt;&lt; dec &lt;&lt; endl);
<a name="l00574"></a>00574     <span class="keywordflow">if</span> (d_cache_table[hash]) {
<a name="l00575"></a>00575         <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">CacheEntries</a> *cp = d_cache_table[hash];
<a name="l00576"></a>00576         <span class="keywordflow">for</span> (<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a8527d6a5b1e720e2d500e1ec1eeecf9f">CacheEntriesIter</a> i = cp-&gt;begin(); i != cp-&gt;end(); ++i) {
<a name="l00577"></a>00577             <span class="comment">// Must test *i because perform_garbage_collection may have</span>
<a name="l00578"></a>00578             <span class="comment">// removed this entry; the CacheEntry will then be null.</span>
<a name="l00579"></a>00579             <span class="keywordflow">if</span> ((*i) &amp;&amp; (*i)-&gt;url == url) {
<a name="l00580"></a>00580                 (*i)-&gt;lock_read_response();     <span class="comment">// Lock the response</span>
<a name="l00581"></a>00581                 <span class="keywordflow">return</span> *i;
<a name="l00582"></a>00582             }
<a name="l00583"></a>00583         }
<a name="l00584"></a>00584     }
<a name="l00585"></a>00585 
<a name="l00586"></a>00586     <span class="keywordflow">return</span> 0;
<a name="l00587"></a>00587 }
<a name="l00588"></a>00588 
<a name="l00595"></a>00595 HTTPCacheTable::CacheEntry *
<a name="l00596"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a57c01c8b9462f0e0ea6541b7d9fcf2f5">00596</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a57c01c8b9462f0e0ea6541b7d9fcf2f5">HTTPCacheTable::get_write_locked_entry_from_cache_table</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url)
<a name="l00597"></a>00597 {
<a name="l00598"></a>00598         <span class="keywordtype">int</span> hash = <a class="code" href="namespacelibdap.html#a68a16ef70e29c367dabd3cb5165c386a">get_hash</a>(url);
<a name="l00599"></a>00599     <span class="keywordflow">if</span> (d_cache_table[hash]) {
<a name="l00600"></a>00600         <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">CacheEntries</a> *cp = d_cache_table[hash];
<a name="l00601"></a>00601         <span class="keywordflow">for</span> (<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a8527d6a5b1e720e2d500e1ec1eeecf9f">CacheEntriesIter</a> i = cp-&gt;begin(); i != cp-&gt;end(); ++i) {
<a name="l00602"></a>00602             <span class="comment">// Must test *i because perform_garbage_collection may have</span>
<a name="l00603"></a>00603             <span class="comment">// removed this entry; the CacheEntry will then be null.</span>
<a name="l00604"></a>00604             <span class="keywordflow">if</span> ((*i) &amp;&amp; (*i)-&gt;url == url) {
<a name="l00605"></a>00605                 (*i)-&gt;lock_write_response();    <span class="comment">// Lock the response</span>
<a name="l00606"></a>00606                 <span class="keywordflow">return</span> *i;
<a name="l00607"></a>00607             }
<a name="l00608"></a>00608         }
<a name="l00609"></a>00609     }
<a name="l00610"></a>00610 
<a name="l00611"></a>00611     <span class="keywordflow">return</span> 0;
<a name="l00612"></a>00612 }
<a name="l00613"></a>00613 
<a name="l00621"></a>00621 <span class="keywordtype">void</span>
<a name="l00622"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a85d4438820f0920002f5e77b51900ea9">00622</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a85d4438820f0920002f5e77b51900ea9">HTTPCacheTable::remove_cache_entry</a>(<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry)
<a name="l00623"></a>00623 {
<a name="l00624"></a>00624     <span class="comment">// This should never happen; all calls to this method are protected by</span>
<a name="l00625"></a>00625     <span class="comment">// the caller, hence the InternalErr.</span>
<a name="l00626"></a>00626     <span class="keywordflow">if</span> (entry-&gt;readers)
<a name="l00627"></a>00627         <span class="keywordflow">throw</span> <a class="code" href="classlibdap_1_1InternalErr.html" title="A class for software fault reporting.">InternalErr</a>(__FILE__, __LINE__, <span class="stringliteral">&quot;Tried to delete a cache entry that is in use.&quot;</span>);
<a name="l00628"></a>00628 
<a name="l00629"></a>00629     <a class="code" href="HTTPCacheTable_8cc.html#a2a6edcee170a79b132872d22066754ed">REMOVE</a>(entry-&gt;cachename.c_str());
<a name="l00630"></a>00630     <a class="code" href="HTTPCacheTable_8cc.html#a2a6edcee170a79b132872d22066754ed">REMOVE</a>(<span class="keywordtype">string</span>(entry-&gt;cachename + <a class="code" href="HTTPCacheTable_8cc.html#a9f34e7ee25eb923739b1ea126dce4528">CACHE_META</a>).c_str());
<a name="l00631"></a>00631 
<a name="l00632"></a>00632     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;remove_cache_entry, current_size: &quot;</span> &lt;&lt; <a class="code" href="classlibdap_1_1HTTPCacheTable.html#abb0b2349fafffdd13eea562140083485">get_current_size</a>() &lt;&lt; endl);
<a name="l00633"></a>00633 
<a name="l00634"></a>00634     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> eds = entry_disk_space(entry-&gt;size, <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0c0cd3182608b1ce1f3b930cf16e6779">get_block_size</a>());
<a name="l00635"></a>00635     <a class="code" href="classlibdap_1_1HTTPCacheTable.html#abf4e75cf192355d7c7bfcf9f0e736168">set_current_size</a>((eds &gt; <a class="code" href="classlibdap_1_1HTTPCacheTable.html#abb0b2349fafffdd13eea562140083485">get_current_size</a>()) ? 0 : <a class="code" href="classlibdap_1_1HTTPCacheTable.html#abb0b2349fafffdd13eea562140083485">get_current_size</a>() - eds);
<a name="l00636"></a>00636     
<a name="l00637"></a>00637     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;remove_cache_entry, current_size: &quot;</span> &lt;&lt; <a class="code" href="classlibdap_1_1HTTPCacheTable.html#abb0b2349fafffdd13eea562140083485">get_current_size</a>() &lt;&lt; endl);
<a name="l00638"></a>00638 }
<a name="l00639"></a>00639 
<a name="l00642"></a>00642 <span class="keyword">class </span>DeleteCacheEntry: <span class="keyword">public</span> unary_function&lt;HTTPCacheTable::CacheEntry *&amp;, void&gt;
<a name="l00643"></a>00643 {
<a name="l00644"></a>00644     <span class="keywordtype">string</span> d_url;
<a name="l00645"></a>00645     <a class="code" href="classlibdap_1_1HTTPCacheTable.html">HTTPCacheTable</a> *d_cache_table;
<a name="l00646"></a>00646 
<a name="l00647"></a>00647 <span class="keyword">public</span>:
<a name="l00648"></a>00648     DeleteCacheEntry(<a class="code" href="classlibdap_1_1HTTPCacheTable.html">HTTPCacheTable</a> *c, <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url)
<a name="l00649"></a>00649             : d_url(url), d_cache_table(c)
<a name="l00650"></a>00650     {}
<a name="l00651"></a>00651 
<a name="l00652"></a>00652     <span class="keywordtype">void</span> operator()(HTTPCacheTable::CacheEntry *&amp;e)
<a name="l00653"></a>00653     {
<a name="l00654"></a>00654         <span class="keywordflow">if</span> (e &amp;&amp; e-&gt;url == d_url) {
<a name="l00655"></a>00655                 e-&gt;lock_write_response();
<a name="l00656"></a>00656             d_cache_table-&gt;remove_cache_entry(e);
<a name="l00657"></a>00657                 e-&gt;unlock_write_response();
<a name="l00658"></a>00658             <span class="keyword">delete</span> e; e = 0;
<a name="l00659"></a>00659         }
<a name="l00660"></a>00660     }
<a name="l00661"></a>00661 };
<a name="l00662"></a>00662 
<a name="l00669"></a>00669 <span class="keywordtype">void</span>
<a name="l00670"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#ad1b8ffd257ff335b3d87b90600f5bffd">00670</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#ad1b8ffd257ff335b3d87b90600f5bffd">HTTPCacheTable::remove_entry_from_cache_table</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url)
<a name="l00671"></a>00671 {
<a name="l00672"></a>00672     <span class="keywordtype">int</span> hash = <a class="code" href="namespacelibdap.html#a68a16ef70e29c367dabd3cb5165c386a">get_hash</a>(url);
<a name="l00673"></a>00673     <span class="keywordflow">if</span> (d_cache_table[hash]) {
<a name="l00674"></a>00674         <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">CacheEntries</a> *cp = d_cache_table[hash];
<a name="l00675"></a>00675         for_each(cp-&gt;begin(), cp-&gt;end(), DeleteCacheEntry(<span class="keyword">this</span>, url));
<a name="l00676"></a>00676         cp-&gt;erase(<span class="keyword">remove</span>(cp-&gt;begin(), cp-&gt;end(), <span class="keyword">static_cast&lt;</span><a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a>*<span class="keyword">&gt;</span>(0)),
<a name="l00677"></a>00677                   cp-&gt;end());
<a name="l00678"></a>00678     }
<a name="l00679"></a>00679 }
<a name="l00680"></a>00680 
<a name="l00683"></a>00683 <span class="keyword">class </span>DeleteUnlockedCacheEntry :
<a name="l00684"></a>00684         <span class="keyword">public</span> unary_function&lt;HTTPCacheTable::CacheEntry *&amp;, void&gt; {
<a name="l00685"></a>00685         <a class="code" href="classlibdap_1_1HTTPCacheTable.html">HTTPCacheTable</a> &amp;d_table;
<a name="l00686"></a>00686 
<a name="l00687"></a>00687 <span class="keyword">public</span>:
<a name="l00688"></a>00688         DeleteUnlockedCacheEntry(<a class="code" href="classlibdap_1_1HTTPCacheTable.html">HTTPCacheTable</a> &amp;t) :
<a name="l00689"></a>00689                 d_table(t) {
<a name="l00690"></a>00690         }
<a name="l00691"></a>00691         <span class="keywordtype">void</span> operator()(HTTPCacheTable::CacheEntry *&amp;e) {
<a name="l00692"></a>00692                 <span class="keywordflow">if</span> (e) {
<a name="l00693"></a>00693                         d_table.remove_cache_entry(e);
<a name="l00694"></a>00694                         <span class="keyword">delete</span> e; e = 0;
<a name="l00695"></a>00695                 }
<a name="l00696"></a>00696         }
<a name="l00697"></a>00697 };
<a name="l00698"></a>00698 
<a name="l00699"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#ad02f9346125f4c311d6e74b6a8354f9e">00699</a> <span class="keywordtype">void</span> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#ad02f9346125f4c311d6e74b6a8354f9e">HTTPCacheTable::delete_all_entries</a>() {
<a name="l00700"></a>00700         <span class="comment">// Walk through the cache table and, for every entry in the cache, delete</span>
<a name="l00701"></a>00701         <span class="comment">// it on disk and in the cache table.</span>
<a name="l00702"></a>00702         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> cnt = 0; cnt &lt; <a class="code" href="HTTPCacheTable_8cc.html#a17929d1ac6258253df584173f0c6e279">CACHE_TABLE_SIZE</a>; cnt++) {
<a name="l00703"></a>00703                 <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a36425575bbfac384964ce2f364f15679">HTTPCacheTable::CacheEntries</a> *slot = get_cache_table()[cnt];
<a name="l00704"></a>00704                 <span class="keywordflow">if</span> (slot) {
<a name="l00705"></a>00705                         for_each(slot-&gt;begin(), slot-&gt;end(), DeleteUnlockedCacheEntry(*<span class="keyword">this</span>));
<a name="l00706"></a>00706                         slot-&gt;erase(<span class="keyword">remove</span>(slot-&gt;begin(), slot-&gt;end(), <span class="keyword">static_cast&lt;</span><a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *<span class="keyword">&gt;</span>(0)), 
<a name="l00707"></a>00707                                             slot-&gt;end());
<a name="l00708"></a>00708                 }
<a name="l00709"></a>00709         }
<a name="l00710"></a>00710         
<a name="l00711"></a>00711         <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a3e636690b069b3384e908135fcaebd51">cache_index_delete</a>();
<a name="l00712"></a>00712 }
<a name="l00713"></a>00713 
<a name="l00727"></a>00727 <span class="keywordtype">void</span>
<a name="l00728"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0ca63c76e70064549d128ff8f5951534">00728</a> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0ca63c76e70064549d128ff8f5951534">HTTPCacheTable::calculate_time</a>(<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry, <span class="keywordtype">int</span> default_expiration, time_t request_time)
<a name="l00729"></a>00729 {
<a name="l00730"></a>00730     entry-&gt;response_time = time(NULL);
<a name="l00731"></a>00731     time_t apparent_age = max(0, static_cast&lt;int&gt;(entry-&gt;response_time - entry-&gt;date));
<a name="l00732"></a>00732     time_t corrected_received_age = max(apparent_age, entry-&gt;age);
<a name="l00733"></a>00733     time_t response_delay = entry-&gt;response_time - request_time;
<a name="l00734"></a>00734     entry-&gt;corrected_initial_age = corrected_received_age + response_delay;
<a name="l00735"></a>00735 
<a name="l00736"></a>00736     <span class="comment">// Estimate an expires time using the max-age and expires time. If we</span>
<a name="l00737"></a>00737     <span class="comment">// don&#39;t have an explicit expires time then set it to 10% of the LM date</span>
<a name="l00738"></a>00738     <span class="comment">// (although max 24 h). If no LM date is available then use 24 hours.</span>
<a name="l00739"></a>00739     time_t freshness_lifetime = entry-&gt;max_age;
<a name="l00740"></a>00740     <span class="keywordflow">if</span> (freshness_lifetime &lt; 0) {
<a name="l00741"></a>00741         <span class="keywordflow">if</span> (entry-&gt;expires &lt; 0) {
<a name="l00742"></a>00742             <span class="keywordflow">if</span> (entry-&gt;lm &lt; 0) {
<a name="l00743"></a>00743                 freshness_lifetime = default_expiration;
<a name="l00744"></a>00744             }
<a name="l00745"></a>00745             <span class="keywordflow">else</span> {
<a name="l00746"></a>00746                 freshness_lifetime = <a class="code" href="HTTPCacheTable_8cc.html#ac34ecfc4f379afa6b7cbcf41c7781d0d">LM_EXPIRATION</a>(entry-&gt;date - entry-&gt;lm);
<a name="l00747"></a>00747             }
<a name="l00748"></a>00748         }
<a name="l00749"></a>00749         <span class="keywordflow">else</span>
<a name="l00750"></a>00750             freshness_lifetime = entry-&gt;expires - entry-&gt;date;
<a name="l00751"></a>00751     }
<a name="l00752"></a>00752 
<a name="l00753"></a>00753     entry-&gt;freshness_lifetime = max(0, static_cast&lt;int&gt;(freshness_lifetime));
<a name="l00754"></a>00754 
<a name="l00755"></a>00755     <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache....... Received Age &quot;</span> &lt;&lt; entry-&gt;age
<a name="l00756"></a>00756          &lt;&lt; <span class="stringliteral">&quot;, corrected &quot;</span> &lt;&lt; entry-&gt;corrected_initial_age
<a name="l00757"></a>00757          &lt;&lt; <span class="stringliteral">&quot;, freshness lifetime &quot;</span> &lt;&lt; entry-&gt;freshness_lifetime &lt;&lt; endl);
<a name="l00758"></a>00758 }
<a name="l00759"></a>00759 
<a name="l00771"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a53379f3151fe6b85e85c75c401a14963">00771</a> <span class="keywordtype">void</span> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a53379f3151fe6b85e85c75c401a14963">HTTPCacheTable::parse_headers</a>(<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry,
<a name="l00772"></a>00772                 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> max_entry_size, <span class="keyword">const</span> vector&lt;string&gt; &amp;headers) {
<a name="l00773"></a>00773         vector&lt;string&gt;::const_iterator i;
<a name="l00774"></a>00774         <span class="keywordflow">for</span> (i = headers.begin(); i != headers.end(); ++i) {
<a name="l00775"></a>00775                 <span class="comment">// skip a blank header.</span>
<a name="l00776"></a>00776                 <span class="keywordflow">if</span> ((*i).empty())
<a name="l00777"></a>00777                         <span class="keywordflow">continue</span>;
<a name="l00778"></a>00778 
<a name="l00779"></a>00779                 string::size_type colon = (*i).find(<span class="charliteral">&#39;:&#39;</span>);
<a name="l00780"></a>00780 
<a name="l00781"></a>00781                 <span class="comment">// skip a header with no colon in it.</span>
<a name="l00782"></a>00782                 <span class="keywordflow">if</span> (colon == string::npos)
<a name="l00783"></a>00783                         <span class="keywordflow">continue</span>;
<a name="l00784"></a>00784 
<a name="l00785"></a>00785                 <span class="keywordtype">string</span> header = (*i).substr(0, (*i).find(<span class="charliteral">&#39;:&#39;</span>));
<a name="l00786"></a>00786                 <span class="keywordtype">string</span> value = (*i).substr((*i).find(<span class="stringliteral">&quot;: &quot;</span>) + 2);
<a name="l00787"></a>00787                 <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Header: &quot;</span> &lt;&lt; header &lt;&lt; endl);<a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Value: &quot;</span> &lt;&lt; value &lt;&lt; endl);
<a name="l00788"></a>00788 
<a name="l00789"></a>00789                 <span class="keywordflow">if</span> (header == <span class="stringliteral">&quot;ETag&quot;</span>) {
<a name="l00790"></a>00790                         entry-&gt;etag = value;
<a name="l00791"></a>00791                 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (header == <span class="stringliteral">&quot;Last-Modified&quot;</span>) {
<a name="l00792"></a>00792                         entry-&gt;lm = <a class="code" href="namespacelibdap.html#abaf4e1a6a665dbfe9324b25ec9fa701a">parse_time</a>(value.c_str());
<a name="l00793"></a>00793                 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (header == <span class="stringliteral">&quot;Expires&quot;</span>) {
<a name="l00794"></a>00794                         entry-&gt;expires = <a class="code" href="namespacelibdap.html#abaf4e1a6a665dbfe9324b25ec9fa701a">parse_time</a>(value.c_str());
<a name="l00795"></a>00795                 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (header == <span class="stringliteral">&quot;Date&quot;</span>) {
<a name="l00796"></a>00796                         entry-&gt;date = <a class="code" href="namespacelibdap.html#abaf4e1a6a665dbfe9324b25ec9fa701a">parse_time</a>(value.c_str());
<a name="l00797"></a>00797                 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (header == <span class="stringliteral">&quot;Age&quot;</span>) {
<a name="l00798"></a>00798                         entry-&gt;age = <a class="code" href="namespacelibdap.html#abaf4e1a6a665dbfe9324b25ec9fa701a">parse_time</a>(value.c_str());
<a name="l00799"></a>00799                 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (header == <span class="stringliteral">&quot;Content-Length&quot;</span>) {
<a name="l00800"></a>00800                         <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> clength = strtoul(value.c_str(), 0, 0);
<a name="l00801"></a>00801                         <span class="keywordflow">if</span> (clength &gt; max_entry_size)
<a name="l00802"></a>00802                                 entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a112b8ac8aa541ca9a06af7de4d80fc03">set_no_cache</a>(<span class="keyword">true</span>);
<a name="l00803"></a>00803                 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (header == <span class="stringliteral">&quot;Cache-Control&quot;</span>) {
<a name="l00804"></a>00804                         <span class="comment">// Ignored Cache-Control values: public, private, no-transform,</span>
<a name="l00805"></a>00805                         <span class="comment">// proxy-revalidate, s-max-age. These are used by shared caches.</span>
<a name="l00806"></a>00806                         <span class="comment">// See section 14.9 of RFC 2612. 10/02/02 jhrg</span>
<a name="l00807"></a>00807                         <span class="keywordflow">if</span> (value == <span class="stringliteral">&quot;no-cache&quot;</span> || value == <span class="stringliteral">&quot;no-store&quot;</span>)
<a name="l00808"></a>00808                                 <span class="comment">// Note that we *can* store a &#39;no-store&#39; response in volatile</span>
<a name="l00809"></a>00809                                 <span class="comment">// memory according to RFC 2616 (section 14.9.2) but those</span>
<a name="l00810"></a>00810                                 <span class="comment">// will be rare coming from DAP servers. 10/02/02 jhrg</span>
<a name="l00811"></a>00811                                 entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a112b8ac8aa541ca9a06af7de4d80fc03">set_no_cache</a>(<span class="keyword">true</span>);
<a name="l00812"></a>00812                         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value == <span class="stringliteral">&quot;must-revalidate&quot;</span>)
<a name="l00813"></a>00813                                 entry-&gt;must_revalidate = <span class="keyword">true</span>;
<a name="l00814"></a>00814                         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value.find(<span class="stringliteral">&quot;max-age&quot;</span>) != string::npos) {
<a name="l00815"></a>00815                                 <span class="keywordtype">string</span> max_age = value.substr(value.find(<span class="stringliteral">&quot;=&quot;</span> + 1));
<a name="l00816"></a>00816                                 entry-&gt;max_age = <a class="code" href="namespacelibdap.html#abaf4e1a6a665dbfe9324b25ec9fa701a">parse_time</a>(max_age.c_str());
<a name="l00817"></a>00817                         }
<a name="l00818"></a>00818                 }
<a name="l00819"></a>00819         }
<a name="l00820"></a>00820 }
<a name="l00821"></a>00821 
<a name="l00823"></a>00823 
<a name="l00824"></a>00824 <span class="comment">// @TODO Change name to record locked response</span>
<a name="l00825"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#a24b2c86910ba623a59184ab42410b4cb">00825</a> <span class="keywordtype">void</span> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#a24b2c86910ba623a59184ab42410b4cb">HTTPCacheTable::bind_entry_to_data</a>(<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry, FILE *body) {
<a name="l00826"></a>00826         entry-&gt;hits++;  <span class="comment">// Mark hit</span>
<a name="l00827"></a>00827     d_locked_entries[body] = entry; <span class="comment">// record lock, see release_cached_r...</span>
<a name="l00828"></a>00828 }
<a name="l00829"></a>00829 
<a name="l00830"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#ad0ac242f618a804562e18546738904df">00830</a> <span class="keywordtype">void</span> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#ad0ac242f618a804562e18546738904df">HTTPCacheTable::uncouple_entry_from_data</a>(FILE *body) {
<a name="l00831"></a>00831         <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry = d_locked_entries[body];
<a name="l00832"></a>00832     <span class="keywordflow">if</span> (!entry)
<a name="l00833"></a>00833         <span class="keywordflow">throw</span> <a class="code" href="classlibdap_1_1InternalErr.html" title="A class for software fault reporting.">InternalErr</a>(<span class="stringliteral">&quot;There is no cache entry for the response given.&quot;</span>);
<a name="l00834"></a>00834 
<a name="l00835"></a>00835     d_locked_entries.erase(body);
<a name="l00836"></a>00836     entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ae2db6d25b27dd657dd0a2ef553e28246">unlock_read_response</a>();
<a name="l00837"></a>00837 
<a name="l00838"></a>00838     <span class="keywordflow">if</span> (entry-&gt;readers &lt; 0)
<a name="l00839"></a>00839         <span class="keywordflow">throw</span> <a class="code" href="classlibdap_1_1InternalErr.html" title="A class for software fault reporting.">InternalErr</a>(<span class="stringliteral">&quot;An unlocked entry was released&quot;</span>);
<a name="l00840"></a>00840 }
<a name="l00841"></a>00841 
<a name="l00842"></a><a class="code" href="classlibdap_1_1HTTPCacheTable.html#afc2ef03d687c3db809e2bfbc3bca3bd3">00842</a> <span class="keywordtype">bool</span> <a class="code" href="classlibdap_1_1HTTPCacheTable.html#afc2ef03d687c3db809e2bfbc3bca3bd3">HTTPCacheTable::is_locked_read_responses</a>() {
<a name="l00843"></a>00843         <span class="keywordflow">return</span> !d_locked_entries.empty();
<a name="l00844"></a>00844 }
<a name="l00845"></a>00845 
<a name="l00846"></a>00846 } <span class="comment">// namespace libdap</span>
</pre></div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="HTTPCacheTable_8cc.html">HTTPCacheTable.cc</a>      </li>
      <li class="footer">Generated on Tue Feb 8 2011 for libdap++ by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.3 </li>
    </ul>
  </div>

</body>
</html>