Sophie

Sophie

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

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++: HTTPCache.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('HTTPCache_8cc.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<h1>HTTPCache.cc</h1>  </div>
</div>
<div class="contents">
<a href="HTTPCache_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 <span class="comment">//#define DODS_DEBUG2</span>
<a name="l00030"></a>00030 
<a name="l00031"></a>00031 <span class="preprocessor">#include &lt;pthread.h&gt;</span>
<a name="l00032"></a>00032 <span class="preprocessor">#include &lt;limits.h&gt;</span>
<a name="l00033"></a>00033 <span class="preprocessor">#include &lt;unistd.h&gt;</span>   <span class="comment">// for stat</span>
<a name="l00034"></a>00034 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>  <span class="comment">// for stat and mkdir</span>
<a name="l00035"></a>00035 <span class="preprocessor">#include &lt;sys/stat.h&gt;</span>
<a name="l00036"></a>00036 
<a name="l00037"></a>00037 <span class="preprocessor">#include &lt;cstring&gt;</span>
<a name="l00038"></a>00038 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00039"></a>00039 <span class="preprocessor">#include &lt;sstream&gt;</span>
<a name="l00040"></a>00040 <span class="preprocessor">#include &lt;algorithm&gt;</span>
<a name="l00041"></a>00041 <span class="preprocessor">#include &lt;iterator&gt;</span>
<a name="l00042"></a>00042 <span class="preprocessor">#include &lt;set&gt;</span>
<a name="l00043"></a>00043 
<a name="l00044"></a>00044 <span class="preprocessor">#include &quot;<a class="code" href="Error_8h.html">Error.h</a>&quot;</span>
<a name="l00045"></a>00045 <span class="preprocessor">#include &quot;<a class="code" href="InternalErr_8h.html">InternalErr.h</a>&quot;</span>
<a name="l00046"></a>00046 <span class="preprocessor">#include &quot;<a class="code" href="ResponseTooBigErr_8h.html">ResponseTooBigErr.h</a>&quot;</span>
<a name="l00047"></a>00047 <span class="preprocessor">#ifndef WIN32</span>
<a name="l00048"></a>00048 <span class="preprocessor"></span><span class="preprocessor">#include &quot;<a class="code" href="SignalHandler_8h.html">SignalHandler.h</a>&quot;</span>
<a name="l00049"></a>00049 <span class="preprocessor">#endif</span>
<a name="l00050"></a>00050 <span class="preprocessor"></span><span class="preprocessor">#include &quot;<a class="code" href="HTTPCacheInterruptHandler_8h.html">HTTPCacheInterruptHandler.h</a>&quot;</span>
<a name="l00051"></a>00051 <span class="preprocessor">#include &quot;<a class="code" href="HTTPCacheTable_8h.html">HTTPCacheTable.h</a>&quot;</span>
<a name="l00052"></a>00052 <span class="preprocessor">#include &quot;<a class="code" href="HTTPCache_8h.html">HTTPCache.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="keyword">using namespace </span>std;
<a name="l00058"></a>00058 
<a name="l00059"></a>00059 <span class="keyword">namespace </span>libdap {
<a name="l00060"></a>00060 
<a name="l00061"></a>00061 HTTPCache *HTTPCache::_instance = 0;
<a name="l00062"></a>00062 
<a name="l00063"></a>00063 <span class="comment">// instance_mutex is used to ensure that only one instance is created.</span>
<a name="l00064"></a>00064 <span class="comment">// That is, it protects the body of the HTTPCache::instance() method. This</span>
<a name="l00065"></a>00065 <span class="comment">// mutex is initialized from within the static function once_init_routine()</span>
<a name="l00066"></a>00066 <span class="comment">// and the call to that takes place using pthread_once_init() where the mutex</span>
<a name="l00067"></a>00067 <span class="comment">// once_block is used to protect that call. All of this ensures that no matter</span>
<a name="l00068"></a>00068 <span class="comment">// how many threads call the instance() method, only one instance is ever</span>
<a name="l00069"></a>00069 <span class="comment">// made.</span>
<a name="l00070"></a>00070 <span class="keyword">static</span> pthread_mutex_t instance_mutex;
<a name="l00071"></a>00071 <span class="keyword">static</span> pthread_once_t once_block = PTHREAD_ONCE_INIT;
<a name="l00072"></a>00072 
<a name="l00073"></a>00073 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00074"></a>00074 <span class="preprocessor"></span><span class="preprocessor">#include &lt;direct.h&gt;</span>
<a name="l00075"></a>00075 <span class="preprocessor">#include &lt;time.h&gt;</span>
<a name="l00076"></a>00076 <span class="preprocessor">#include &lt;fcntl.h&gt;</span>
<a name="l00077"></a>00077 <span class="preprocessor">#define MKDIR(a,b) _mkdir((a))</span>
<a name="l00078"></a>00078 <span class="preprocessor"></span><span class="preprocessor">#define UMASK(a) _umask((a))</span>
<a name="l00079"></a>00079 <span class="preprocessor"></span><span class="preprocessor">#define REMOVE(a) remove((a))</span>
<a name="l00080"></a>00080 <span class="preprocessor"></span><span class="preprocessor">#define MKSTEMP(a) _open(_mktemp((a)),_O_CREAT,_S_IREAD|_S_IWRITE)</span>
<a name="l00081"></a>00081 <span class="preprocessor"></span><span class="preprocessor">#define DIR_SEPARATOR_CHAR &#39;\\&#39;</span>
<a name="l00082"></a>00082 <span class="preprocessor"></span><span class="preprocessor">#define DIR_SEPARATOR_STR &quot;\\&quot;</span>
<a name="l00083"></a>00083 <span class="preprocessor"></span><span class="preprocessor">#else</span>
<a name="l00084"></a><a class="code" href="HTTPCache_8cc.html#a2bf44344e285663ad0b0b7bb001ac4fd">00084</a> <span class="preprocessor"></span><span class="preprocessor">#define MKDIR(a,b) mkdir((a), (b))</span>
<a name="l00085"></a><a class="code" href="HTTPCache_8cc.html#ab51d9d52fad4f0a5361d2a3ab9d0edea">00085</a> <span class="preprocessor"></span><span class="preprocessor">#define UMASK(a) umask((a))</span>
<a name="l00086"></a><a class="code" href="HTTPCache_8cc.html#a2a6edcee170a79b132872d22066754ed">00086</a> <span class="preprocessor"></span><span class="preprocessor">#define REMOVE(a) remove((a))</span>
<a name="l00087"></a><a class="code" href="HTTPCache_8cc.html#ae76603c1b2ef94cb2664dbad1b82a4c6">00087</a> <span class="preprocessor"></span><span class="preprocessor">#define MKSTEMP(a) mkstemp((a))</span>
<a name="l00088"></a><a class="code" href="HTTPCache_8cc.html#a1d22fde6e7501aebedb7663f0b0aa4e2">00088</a> <span class="preprocessor"></span><span class="preprocessor">#define DIR_SEPARATOR_CHAR &#39;/&#39;</span>
<a name="l00089"></a><a class="code" href="HTTPCache_8cc.html#a23e72ca2d8456a971c13441bdfc4586f">00089</a> <span class="preprocessor"></span><span class="preprocessor">#define DIR_SEPARATOR_STR &quot;/&quot;</span>
<a name="l00090"></a>00090 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00091"></a>00091 <span class="preprocessor"></span>
<a name="l00092"></a>00092 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00093"></a>00093 <span class="preprocessor"></span><span class="preprocessor">#define CACHE_LOC &quot;\\tmp\\&quot;</span>
<a name="l00094"></a>00094 <span class="preprocessor"></span><span class="preprocessor">#define CACHE_ROOT &quot;dods-cache\\&quot;</span>
<a name="l00095"></a>00095 <span class="preprocessor"></span><span class="preprocessor">#else</span>
<a name="l00096"></a><a class="code" href="HTTPCache_8cc.html#ac3dd016cd86fde4c7d4380ed0a9151c8">00096</a> <span class="preprocessor"></span><span class="preprocessor">#define CACHE_LOC &quot;/tmp/&quot;</span>
<a name="l00097"></a><a class="code" href="HTTPCache_8cc.html#aeb598ae6ad780a09fe6ae3ee0db679f3">00097</a> <span class="preprocessor"></span><span class="preprocessor">#define CACHE_ROOT &quot;dods-cache/&quot;</span>
<a name="l00098"></a>00098 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00099"></a><a class="code" href="HTTPCache_8cc.html#a2fcbe1f1875c3c4b38a63ea99b906c49">00099</a> <span class="preprocessor"></span><span class="preprocessor">#define CACHE_INDEX &quot;.index&quot;</span>
<a name="l00100"></a><a class="code" href="HTTPCache_8cc.html#a74ca3ee931733bde029a605ec0eab397">00100</a> <span class="preprocessor"></span><span class="preprocessor">#define CACHE_LOCK &quot;.lock&quot;</span>
<a name="l00101"></a><a class="code" href="HTTPCache_8cc.html#a9f34e7ee25eb923739b1ea126dce4528">00101</a> <span class="preprocessor"></span><span class="preprocessor">#define CACHE_META &quot;.meta&quot;</span>
<a name="l00102"></a>00102 <span class="preprocessor"></span><span class="comment">//#define CACHE_EMPTY_ETAG &quot;@cache@&quot;</span>
<a name="l00103"></a>00103 
<a name="l00104"></a><a class="code" href="HTTPCache_8cc.html#afefcb1315dce44b5c9459b8f0ad22cb7">00104</a> <span class="preprocessor">#define NO_LM_EXPIRATION 24*3600 // 24 hours</span>
<a name="l00105"></a>00105 <span class="preprocessor"></span>
<a name="l00106"></a><a class="code" href="HTTPCache_8cc.html#a38b298d045bf3f59133a1852187d9a1c">00106</a> <span class="preprocessor">#define DUMP_FREQUENCY 10 // Dump index every x loads</span>
<a name="l00107"></a>00107 <span class="preprocessor"></span>
<a name="l00108"></a><a class="code" href="HTTPCache_8cc.html#a78a6115b485de47c7cc56b224c558ea2">00108</a> <span class="preprocessor">#define MEGA 0x100000L</span>
<a name="l00109"></a><a class="code" href="HTTPCache_8cc.html#af51756767c4eeb33c45209a62b205fcf">00109</a> <span class="preprocessor"></span><span class="preprocessor">#define CACHE_TOTAL_SIZE 20 // Default cache size is 20M</span>
<a name="l00110"></a><a class="code" href="HTTPCache_8cc.html#ac3215fe6da2907a9efc7f488fb7c5ec5">00110</a> <span class="preprocessor"></span><span class="preprocessor">#define CACHE_FOLDER_PCT 10 // 10% of cache size for metainfo etc.</span>
<a name="l00111"></a><a class="code" href="HTTPCache_8cc.html#a2065c49052d2d2adeefc9d66fa136b46">00111</a> <span class="preprocessor"></span><span class="preprocessor">#define CACHE_GC_PCT 10  // 10% of cache size free after GC</span>
<a name="l00112"></a><a class="code" href="HTTPCache_8cc.html#a33a98a1c97431e86ac8ae32ea5e08cbc">00112</a> <span class="preprocessor"></span><span class="preprocessor">#define MIN_CACHE_TOTAL_SIZE 5 // 5M Min cache size</span>
<a name="l00113"></a><a class="code" href="HTTPCache_8cc.html#a9b0cd750616422a4c04187b15666628a">00113</a> <span class="preprocessor"></span><span class="preprocessor">#define MAX_CACHE_ENTRY_SIZE 3 // 3M Max size of single cached entry</span>
<a name="l00114"></a>00114 <span class="preprocessor"></span>
<a name="l00115"></a>00115 <span class="keyword">static</span> <span class="keywordtype">void</span>
<a name="l00116"></a>00116 once_init_routine()
<a name="l00117"></a>00117 {
<a name="l00118"></a>00118     <span class="keywordtype">int</span> status;
<a name="l00119"></a>00119     status = <a class="code" href="HTTPCache_8h.html#a80412ba526b7b04f6784d1f992be57e9">INIT</a>(&amp;instance_mutex);
<a name="l00120"></a>00120 
<a name="l00121"></a>00121     <span class="keywordflow">if</span> (status != 0)
<a name="l00122"></a>00122         <span class="keywordflow">throw</span> InternalErr(__FILE__, __LINE__, <span class="stringliteral">&quot;Could not initialize the HTTP Cache mutex. Exiting.&quot;</span>);
<a name="l00123"></a>00123 }
<a name="l00124"></a>00124 
<a name="l00153"></a>00153 HTTPCache *
<a name="l00154"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a152bdc19fdec15758d9c1a3a029d6900">00154</a> HTTPCache::instance(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;cache_root, <span class="keywordtype">bool</span> force)
<a name="l00155"></a>00155 {
<a name="l00156"></a>00156     <a class="code" href="HTTPCache_8h.html#a16e4e2643dc8f8b07b8be8bebd189ad5">LOCK</a>(&amp;instance_mutex);
<a name="l00157"></a>00157     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Entering instance(); (&quot;</span> &lt;&lt; hex &lt;&lt; _instance &lt;&lt; dec &lt;&lt; <span class="stringliteral">&quot;)&quot;</span>
<a name="l00158"></a>00158         &lt;&lt; <span class="stringliteral">&quot;... &quot;</span>);
<a name="l00159"></a>00159 
<a name="l00160"></a>00160     <span class="keywordflow">try</span> {
<a name="l00161"></a>00161         <span class="keywordflow">if</span> (!_instance) {
<a name="l00162"></a>00162             _instance = <span class="keyword">new</span> <a class="code" href="classlibdap_1_1HTTPCache.html">HTTPCache</a>(cache_root, force);
<a name="l00163"></a>00163 
<a name="l00164"></a>00164             <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;New instance: &quot;</span> &lt;&lt; _instance &lt;&lt; <span class="stringliteral">&quot;, cache root: &quot;</span>
<a name="l00165"></a>00165                 &lt;&lt; _instance-&gt;d_cache_root &lt;&lt; endl);
<a name="l00166"></a>00166 
<a name="l00167"></a>00167             atexit(delete_instance);
<a name="l00168"></a>00168 
<a name="l00169"></a>00169 <span class="preprocessor">#ifndef WIN32</span>
<a name="l00170"></a>00170 <span class="preprocessor"></span>            <span class="comment">// Register the interrupt handler. If we&#39;ve already registered</span>
<a name="l00171"></a>00171             <span class="comment">// one, barf. If this becomes a problem, hack SignalHandler so</span>
<a name="l00172"></a>00172             <span class="comment">// that we can chain these handlers... 02/10/04 jhrg</span>
<a name="l00173"></a>00173             <span class="comment">//</span>
<a name="l00174"></a>00174             <span class="comment">// Technically we&#39;re leaking memory here. However, since this</span>
<a name="l00175"></a>00175             <span class="comment">// class is a singleton, we know that only three objects will</span>
<a name="l00176"></a>00176             <span class="comment">// ever be created and they will all exist until the process</span>
<a name="l00177"></a>00177             <span class="comment">// exits. We can let this slide... 02/12/04 jhrg</span>
<a name="l00178"></a>00178             <a class="code" href="classlibdap_1_1EventHandler.html">EventHandler</a> *old_eh = SignalHandler::instance()-&gt;register_handler
<a name="l00179"></a>00179                                    (SIGINT, <span class="keyword">new</span> <a class="code" href="classlibdap_1_1HTTPCacheInterruptHandler.html">HTTPCacheInterruptHandler</a>);
<a name="l00180"></a>00180             <span class="keywordflow">if</span> (old_eh) {
<a name="l00181"></a>00181                 SignalHandler::instance()-&gt;register_handler(SIGINT, old_eh);
<a name="l00182"></a>00182                 <span class="keywordflow">throw</span> <a class="code" href="classlibdap_1_1SignalHandlerRegisteredErr.html">SignalHandlerRegisteredErr</a>(
<a name="l00183"></a>00183                     <span class="stringliteral">&quot;Could not register event handler for SIGINT without superseding an existing one.&quot;</span>);
<a name="l00184"></a>00184             }
<a name="l00185"></a>00185 
<a name="l00186"></a>00186             old_eh = SignalHandler::instance()-&gt;register_handler
<a name="l00187"></a>00187                      (SIGPIPE, <span class="keyword">new</span> <a class="code" href="classlibdap_1_1HTTPCacheInterruptHandler.html">HTTPCacheInterruptHandler</a>);
<a name="l00188"></a>00188             <span class="keywordflow">if</span> (old_eh) {
<a name="l00189"></a>00189                 SignalHandler::instance()-&gt;register_handler(SIGPIPE, old_eh);
<a name="l00190"></a>00190                 <span class="keywordflow">throw</span> <a class="code" href="classlibdap_1_1SignalHandlerRegisteredErr.html">SignalHandlerRegisteredErr</a>(
<a name="l00191"></a>00191                     <span class="stringliteral">&quot;Could not register event handler for SIGPIPE without superseding an existing one.&quot;</span>);
<a name="l00192"></a>00192             }
<a name="l00193"></a>00193 
<a name="l00194"></a>00194             old_eh = SignalHandler::instance()-&gt;register_handler
<a name="l00195"></a>00195                      (SIGTERM, <span class="keyword">new</span> <a class="code" href="classlibdap_1_1HTTPCacheInterruptHandler.html">HTTPCacheInterruptHandler</a>);
<a name="l00196"></a>00196             <span class="keywordflow">if</span> (old_eh) {
<a name="l00197"></a>00197                 SignalHandler::instance()-&gt;register_handler(SIGTERM, old_eh);
<a name="l00198"></a>00198                 <span class="keywordflow">throw</span> <a class="code" href="classlibdap_1_1SignalHandlerRegisteredErr.html">SignalHandlerRegisteredErr</a>(
<a name="l00199"></a>00199                     <span class="stringliteral">&quot;Could not register event handler for SIGTERM without superseding an existing one.&quot;</span>);
<a name="l00200"></a>00200             }
<a name="l00201"></a>00201 <span class="preprocessor">#endif</span>
<a name="l00202"></a>00202 <span class="preprocessor"></span>        }
<a name="l00203"></a>00203     }
<a name="l00204"></a>00204     <span class="keywordflow">catch</span> (...) {
<a name="l00205"></a>00205         <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;The constructor threw an Error!&quot;</span> &lt;&lt; endl);
<a name="l00206"></a>00206         <a class="code" href="HTTPCache_8h.html#af2aa2fe13863361670b63de7e6df0fdd">UNLOCK</a>(&amp;instance_mutex);
<a name="l00207"></a>00207         <span class="keywordflow">throw</span>;
<a name="l00208"></a>00208     }
<a name="l00209"></a>00209 
<a name="l00210"></a>00210     <a class="code" href="HTTPCache_8h.html#af2aa2fe13863361670b63de7e6df0fdd">UNLOCK</a>(&amp;instance_mutex);
<a name="l00211"></a>00211     <a class="code" href="debug_8h.html#a0d28d57d1228cf13a4c1288cc4758344">DBGN</a>(cerr &lt;&lt; <span class="stringliteral">&quot;returning &quot;</span> &lt;&lt; hex &lt;&lt; _instance &lt;&lt; dec &lt;&lt; endl);
<a name="l00212"></a>00212 
<a name="l00213"></a>00213     <span class="keywordflow">return</span> _instance;
<a name="l00214"></a>00214 }
<a name="l00215"></a>00215 
<a name="l00219"></a>00219 <span class="keywordtype">void</span>
<a name="l00220"></a>00220 HTTPCache::delete_instance()
<a name="l00221"></a>00221 {
<a name="l00222"></a>00222     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Entering delete_instance()...&quot;</span> &lt;&lt; endl);
<a name="l00223"></a>00223     <span class="keywordflow">if</span> (HTTPCache::_instance) {
<a name="l00224"></a>00224         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Deleting the cache: &quot;</span> &lt;&lt; HTTPCache::_instance &lt;&lt; endl);
<a name="l00225"></a>00225         <span class="keyword">delete</span> HTTPCache::_instance;
<a name="l00226"></a>00226         HTTPCache::_instance = 0;
<a name="l00227"></a>00227     }
<a name="l00228"></a>00228 
<a name="l00229"></a>00229     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Exiting delete_instance()&quot;</span> &lt;&lt; endl);
<a name="l00230"></a>00230 }
<a name="l00231"></a>00231 
<a name="l00246"></a>00246 HTTPCache::HTTPCache(<span class="keywordtype">string</span> cache_root, <span class="keywordtype">bool</span> force) :
<a name="l00247"></a>00247         d_locked_open_file(0),
<a name="l00248"></a>00248         d_cache_enabled(false),
<a name="l00249"></a>00249         d_cache_protected(false),
<a name="l00250"></a>00250         d_expire_ignored(false),
<a name="l00251"></a>00251         d_always_validate(false),
<a name="l00252"></a>00252         d_total_size(<a class="code" href="HTTPCache_8cc.html#af51756767c4eeb33c45209a62b205fcf">CACHE_TOTAL_SIZE</a> * <a class="code" href="HTTPCache_8cc.html#a78a6115b485de47c7cc56b224c558ea2">MEGA</a>),
<a name="l00253"></a>00253         d_folder_size(<a class="code" href="HTTPCache_8cc.html#af51756767c4eeb33c45209a62b205fcf">CACHE_TOTAL_SIZE</a> / <a class="code" href="HTTPCache_8cc.html#ac3215fe6da2907a9efc7f488fb7c5ec5">CACHE_FOLDER_PCT</a>),
<a name="l00254"></a>00254         d_gc_buffer(<a class="code" href="HTTPCache_8cc.html#af51756767c4eeb33c45209a62b205fcf">CACHE_TOTAL_SIZE</a> / <a class="code" href="HTTPCache_8cc.html#a2065c49052d2d2adeefc9d66fa136b46">CACHE_GC_PCT</a>),
<a name="l00255"></a>00255         d_max_entry_size(<a class="code" href="HTTPCache_8cc.html#a9b0cd750616422a4c04187b15666628a">MAX_CACHE_ENTRY_SIZE</a> * <a class="code" href="HTTPCache_8cc.html#a78a6115b485de47c7cc56b224c558ea2">MEGA</a>),
<a name="l00256"></a>00256         d_default_expiration(<a class="code" href="HTTPCache_8cc.html#afefcb1315dce44b5c9459b8f0ad22cb7">NO_LM_EXPIRATION</a>),
<a name="l00257"></a>00257         d_max_age(-1),
<a name="l00258"></a>00258         d_max_stale(-1),
<a name="l00259"></a>00259         d_min_fresh(-1),
<a name="l00260"></a>00260         d_http_cache_table(0)
<a name="l00261"></a>00261 {
<a name="l00262"></a>00262     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Entering the constructor for &quot;</span> &lt;&lt; <span class="keyword">this</span> &lt;&lt; <span class="stringliteral">&quot;... &quot;</span>);
<a name="l00263"></a>00263 
<a name="l00264"></a>00264         <span class="keywordtype">int</span> status = pthread_once(&amp;once_block, once_init_routine);
<a name="l00265"></a>00265         <span class="keywordflow">if</span> (status != 0)
<a name="l00266"></a>00266                 <span class="keywordflow">throw</span> InternalErr(__FILE__, __LINE__, <span class="stringliteral">&quot;Could not initialize the HTTP Cache mutex. Exiting.&quot;</span>);
<a name="l00267"></a>00267 
<a name="l00268"></a>00268         <a class="code" href="HTTPCache_8h.html#a80412ba526b7b04f6784d1f992be57e9">INIT</a>(&amp;d_cache_mutex);
<a name="l00269"></a>00269 
<a name="l00270"></a>00270         <span class="comment">// This used to throw an Error object if we could not get the</span>
<a name="l00271"></a>00271         <span class="comment">// single user lock. However, that results in an invalid object. It&#39;s</span>
<a name="l00272"></a>00272         <span class="comment">// better to have an instance that has default values. If we cannot get</span>
<a name="l00273"></a>00273         <span class="comment">// the lock, make sure to set the cache as *disabled*. 03/12/03 jhrg</span>
<a name="l00274"></a>00274         <span class="comment">//</span>
<a name="l00275"></a>00275         <span class="comment">// I fixed this block so that the cache root is set before we try to get</span>
<a name="l00276"></a>00276         <span class="comment">// the single user lock. That was the fix for bug #661. To make that</span>
<a name="l00277"></a>00277         <span class="comment">// work, I had to move the call to create_cache_root out of</span>
<a name="l00278"></a>00278         <span class="comment">// set_cache_root(). 09/08/03 jhrg</span>
<a name="l00279"></a>00279 
<a name="l00280"></a>00280         set_cache_root(cache_root);
<a name="l00281"></a>00281         <span class="keywordtype">int</span> block_size;
<a name="l00282"></a>00282 
<a name="l00283"></a>00283         <span class="keywordflow">if</span> (!get_single_user_lock(force))
<a name="l00284"></a>00284                 <span class="keywordflow">throw</span> Error(<span class="stringliteral">&quot;Could not get single user lock for the cache&quot;</span>);
<a name="l00285"></a>00285 
<a name="l00286"></a>00286 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00287"></a>00287 <span class="preprocessor"></span>        <span class="comment">//  Windows is unable to provide us this information.  4096 appears</span>
<a name="l00288"></a>00288         <span class="comment">//  a best guess.  It is likely to be in the range [2048, 8192] on</span>
<a name="l00289"></a>00289         <span class="comment">//  windows, but will the level of truth of that statement vary over</span>
<a name="l00290"></a>00290         <span class="comment">//  time ?</span>
<a name="l00291"></a>00291         block_size = 4096;
<a name="l00292"></a>00292 <span class="preprocessor">#else</span>
<a name="l00293"></a>00293 <span class="preprocessor"></span>        <span class="keyword">struct </span>stat s;
<a name="l00294"></a>00294         <span class="keywordflow">if</span> (stat(cache_root.c_str(), &amp;s) == 0)
<a name="l00295"></a>00295                 block_size = s.st_blksize;
<a name="l00296"></a>00296         <span class="keywordflow">else</span>
<a name="l00297"></a>00297                 <span class="keywordflow">throw</span> Error(<span class="stringliteral">&quot;Could not set file system block size.&quot;</span>);
<a name="l00298"></a>00298 <span class="preprocessor">#endif</span>
<a name="l00299"></a>00299 <span class="preprocessor"></span>        d_http_cache_table = <span class="keyword">new</span> HTTPCacheTable(d_cache_root, block_size);
<a name="l00300"></a>00300         d_cache_enabled = <span class="keyword">true</span>;
<a name="l00301"></a>00301 
<a name="l00302"></a>00302         <a class="code" href="debug_8h.html#a0d28d57d1228cf13a4c1288cc4758344">DBGN</a>(cerr &lt;&lt; <span class="stringliteral">&quot;exiting&quot;</span> &lt;&lt; endl);
<a name="l00303"></a>00303 }
<a name="l00304"></a>00304 
<a name="l00317"></a><a class="code" href="classlibdap_1_1HTTPCache.html#ac8fcde4b345c50bad6c3507dcbd2f447">00317</a> <a class="code" href="classlibdap_1_1HTTPCache.html#ac8fcde4b345c50bad6c3507dcbd2f447">HTTPCache::~HTTPCache</a>()
<a name="l00318"></a>00318 {
<a name="l00319"></a>00319     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Entering the destructor for &quot;</span> &lt;&lt; <span class="keyword">this</span> &lt;&lt; <span class="stringliteral">&quot;... &quot;</span>);
<a name="l00320"></a>00320 
<a name="l00321"></a>00321     <span class="keywordflow">try</span> {
<a name="l00322"></a>00322         <span class="keywordflow">if</span> (startGC())
<a name="l00323"></a>00323             perform_garbage_collection();
<a name="l00324"></a>00324 
<a name="l00325"></a>00325         d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a7cd5a3a24fd0cd41ac0d9a3192075e9c">cache_index_write</a>();
<a name="l00326"></a>00326     }
<a name="l00327"></a>00327     <span class="keywordflow">catch</span> (<a class="code" href="classlibdap_1_1Error.html" title="A class for error processing.">Error</a> &amp;e) {
<a name="l00328"></a>00328         <span class="comment">// If the cache index cannot be written, we&#39;ve got problems. However,</span>
<a name="l00329"></a>00329         <span class="comment">// unless we&#39;re debugging, still free up the cache table in memory.</span>
<a name="l00330"></a>00330         <span class="comment">// How should we let users know they cache index is not being</span>
<a name="l00331"></a>00331         <span class="comment">// written?? 10/03/02 jhrg</span>
<a name="l00332"></a>00332         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; e.<a class="code" href="classlibdap_1_1Error.html#a917fdf537b7dd7e587e9cf6dc7e4c687">get_error_message</a>() &lt;&lt; endl);
<a name="l00333"></a>00333     }
<a name="l00334"></a>00334 
<a name="l00335"></a>00335     <span class="keyword">delete</span> d_http_cache_table;
<a name="l00336"></a>00336 
<a name="l00337"></a>00337     release_single_user_lock();
<a name="l00338"></a>00338 
<a name="l00339"></a>00339     <a class="code" href="debug_8h.html#a0d28d57d1228cf13a4c1288cc4758344">DBGN</a>(cerr &lt;&lt; <span class="stringliteral">&quot;exiting destructor.&quot;</span> &lt;&lt; endl);
<a name="l00340"></a>00340     <a class="code" href="HTTPCache_8h.html#aa713a380e0f57370ec8380acf82b951c">DESTROY</a>(&amp;d_cache_mutex);
<a name="l00341"></a>00341 }
<a name="l00342"></a>00342 
<a name="l00343"></a>00343 
<a name="l00347"></a>00347 
<a name="l00351"></a>00351 <span class="keywordtype">bool</span>
<a name="l00352"></a>00352 HTTPCache::stopGC()<span class="keyword"> const</span>
<a name="l00353"></a>00353 <span class="keyword"></span>{
<a name="l00354"></a>00354     <span class="keywordflow">return</span> (d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#abb0b2349fafffdd13eea562140083485">get_current_size</a>() + d_folder_size &lt; d_total_size - d_gc_buffer);
<a name="l00355"></a>00355 }
<a name="l00356"></a>00356 
<a name="l00363"></a>00363 <span class="keywordtype">bool</span>
<a name="l00364"></a>00364 HTTPCache::startGC()<span class="keyword"> const</span>
<a name="l00365"></a>00365 <span class="keyword"></span>{
<a name="l00366"></a>00366     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;startGC, current_size: &quot;</span> &lt;&lt; d_http_cache_table-&gt;get_current_size() &lt;&lt; endl);
<a name="l00367"></a>00367     <span class="keywordflow">return</span> (d_http_cache_table-&gt;get_current_size() + d_folder_size &gt; d_total_size);
<a name="l00368"></a>00368 }
<a name="l00369"></a>00369 
<a name="l00384"></a>00384 <span class="keywordtype">void</span>
<a name="l00385"></a>00385 HTTPCache::perform_garbage_collection()
<a name="l00386"></a>00386 {
<a name="l00387"></a>00387     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Performing garbage collection&quot;</span> &lt;&lt; endl);
<a name="l00388"></a>00388 
<a name="l00389"></a>00389     <span class="comment">// Remove all the expired responses.</span>
<a name="l00390"></a>00390     expired_gc();
<a name="l00391"></a>00391 
<a name="l00392"></a>00392     <span class="comment">// Remove entries larger than max_entry_size.</span>
<a name="l00393"></a>00393     too_big_gc();
<a name="l00394"></a>00394 
<a name="l00395"></a>00395     <span class="comment">// Remove entries starting with zero hits, 1, ..., until stopGC()</span>
<a name="l00396"></a>00396     <span class="comment">// returns true.</span>
<a name="l00397"></a>00397     hits_gc();
<a name="l00398"></a>00398 }
<a name="l00399"></a>00399 
<a name="l00405"></a>00405 <span class="keywordtype">void</span>
<a name="l00406"></a>00406 HTTPCache::expired_gc()
<a name="l00407"></a>00407 {
<a name="l00408"></a>00408     <span class="keywordflow">if</span> (!d_expire_ignored) {
<a name="l00409"></a>00409         d_http_cache_table-&gt;delete_expired_entries();
<a name="l00410"></a>00410     }
<a name="l00411"></a>00411 }
<a name="l00412"></a>00412 
<a name="l00429"></a>00429 <span class="keywordtype">void</span>
<a name="l00430"></a>00430 HTTPCache::hits_gc()
<a name="l00431"></a>00431 {
<a name="l00432"></a>00432     <span class="keywordtype">int</span> hits = 0;
<a name="l00433"></a>00433 
<a name="l00434"></a>00434     <span class="keywordflow">if</span> (startGC()) {
<a name="l00435"></a>00435                 <span class="keywordflow">while</span> (!stopGC()) {
<a name="l00436"></a>00436                         d_http_cache_table-&gt;delete_by_hits(hits);
<a name="l00437"></a>00437                         hits++;
<a name="l00438"></a>00438                 }
<a name="l00439"></a>00439         }
<a name="l00440"></a>00440 }
<a name="l00441"></a>00441 
<a name="l00446"></a>00446 <span class="keywordtype">void</span> HTTPCache::too_big_gc() {
<a name="l00447"></a>00447         <span class="keywordflow">if</span> (startGC())
<a name="l00448"></a>00448                 d_http_cache_table-&gt;delete_by_size(d_max_entry_size);
<a name="l00449"></a>00449 }
<a name="l00450"></a>00450 
<a name="l00452"></a>00452 
<a name="l00463"></a>00463 <span class="keywordtype">bool</span> HTTPCache::get_single_user_lock(<span class="keywordtype">bool</span> force) {
<a name="l00464"></a>00464         <span class="keywordflow">if</span> (!d_locked_open_file) {
<a name="l00465"></a>00465                 FILE * fp = NULL;
<a name="l00466"></a>00466 
<a name="l00467"></a>00467                 <span class="keywordflow">try</span> {
<a name="l00468"></a>00468                         <span class="comment">// It&#39;s OK to call create_cache_root if the directory already</span>
<a name="l00469"></a>00469                         <span class="comment">// exists.</span>
<a name="l00470"></a>00470                         create_cache_root(d_cache_root);
<a name="l00471"></a>00471                 }
<a name="l00472"></a>00472                 <span class="keywordflow">catch</span> (Error &amp;e) {
<a name="l00473"></a>00473                         <span class="comment">// We need to catch and return false because this method is</span>
<a name="l00474"></a>00474                         <span class="comment">// called from a ctor and throwing at this point will result in a</span>
<a name="l00475"></a>00475                         <span class="comment">// partially constructed object. 01/22/04 jhrg</span>
<a name="l00476"></a>00476                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00477"></a>00477                 }
<a name="l00478"></a>00478 
<a name="l00479"></a>00479                 <span class="comment">// Try to read the lock file. If we can open for reading, it exists.</span>
<a name="l00480"></a>00480                 <span class="keywordtype">string</span> lock = d_cache_root + <a class="code" href="HTTPCache_8cc.html#a74ca3ee931733bde029a605ec0eab397">CACHE_LOCK</a>;
<a name="l00481"></a>00481                 <span class="keywordflow">if</span> ((fp = fopen(lock.c_str(), <span class="stringliteral">&quot;r&quot;</span>)) != NULL) {
<a name="l00482"></a>00482                         <span class="keywordtype">int</span> res = fclose(fp);
<a name="l00483"></a>00483                         <span class="keywordflow">if</span> (res) {
<a name="l00484"></a>00484                                 <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Failed to close &quot;</span> &lt;&lt; (<span class="keywordtype">void</span> *)fp &lt;&lt; endl);
<a name="l00485"></a>00485                         }
<a name="l00486"></a>00486                         <span class="keywordflow">if</span> (force)
<a name="l00487"></a>00487                                 <a class="code" href="HTTPCache_8cc.html#a2a6edcee170a79b132872d22066754ed">REMOVE</a>(lock.c_str());
<a name="l00488"></a>00488                         <span class="keywordflow">else</span>
<a name="l00489"></a>00489                                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00490"></a>00490                 }
<a name="l00491"></a>00491 
<a name="l00492"></a>00492                 <span class="keywordflow">if</span> ((fp = fopen(lock.c_str(), <span class="stringliteral">&quot;w&quot;</span>)) == NULL)
<a name="l00493"></a>00493                         <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00494"></a>00494 
<a name="l00495"></a>00495                 d_locked_open_file = fp;
<a name="l00496"></a>00496                 <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00497"></a>00497         }
<a name="l00498"></a>00498 
<a name="l00499"></a>00499         <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00500"></a>00500 }
<a name="l00501"></a>00501 
<a name="l00504"></a>00504 <span class="keywordtype">void</span>
<a name="l00505"></a>00505 HTTPCache::release_single_user_lock()
<a name="l00506"></a>00506 {
<a name="l00507"></a>00507     <span class="keywordflow">if</span> (d_locked_open_file) {
<a name="l00508"></a>00508         <span class="keywordtype">int</span> res = fclose(d_locked_open_file);
<a name="l00509"></a>00509         <span class="keywordflow">if</span> (res) {
<a name="l00510"></a>00510             <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Failed to close &quot;</span> &lt;&lt; (<span class="keywordtype">void</span> *)d_locked_open_file &lt;&lt; endl) ;
<a name="l00511"></a>00511         }
<a name="l00512"></a>00512         d_locked_open_file = 0;
<a name="l00513"></a>00513     }
<a name="l00514"></a>00514 
<a name="l00515"></a>00515     <span class="keywordtype">string</span> lock = d_cache_root + <a class="code" href="HTTPCache_8cc.html#a74ca3ee931733bde029a605ec0eab397">CACHE_LOCK</a>;
<a name="l00516"></a>00516     <a class="code" href="HTTPCache_8cc.html#a2a6edcee170a79b132872d22066754ed">REMOVE</a>(lock.c_str());
<a name="l00517"></a>00517 }
<a name="l00518"></a>00518 
<a name="l00521"></a>00521 
<a name="l00525"></a>00525 <span class="keywordtype">string</span>
<a name="l00526"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a7cbd2f101aa03496fa7b7a3223327729">00526</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a7cbd2f101aa03496fa7b7a3223327729">HTTPCache::get_cache_root</a>()<span class="keyword"> const</span>
<a name="l00527"></a>00527 <span class="keyword"></span>{
<a name="l00528"></a>00528     <span class="keywordflow">return</span> d_cache_root;
<a name="l00529"></a>00529 }
<a name="l00530"></a>00530 
<a name="l00531"></a>00531 
<a name="l00540"></a>00540 <span class="keywordtype">void</span>
<a name="l00541"></a>00541 HTTPCache::create_cache_root(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;cache_root)
<a name="l00542"></a>00542 {
<a name="l00543"></a>00543     <span class="keyword">struct </span>stat stat_info;
<a name="l00544"></a>00544     string::size_type cur = 0;
<a name="l00545"></a>00545 
<a name="l00546"></a>00546 <span class="preprocessor">#ifdef WIN32</span>
<a name="l00547"></a>00547 <span class="preprocessor"></span>    cur = cache_root[1] == <span class="charliteral">&#39;:&#39;</span> ? 3 : 1;
<a name="l00548"></a>00548     <span class="keyword">typedef</span> <span class="keywordtype">int</span> mode_t;
<a name="l00549"></a>00549 <span class="preprocessor">#else</span>
<a name="l00550"></a>00550 <span class="preprocessor"></span>    cur = 1;
<a name="l00551"></a>00551 <span class="preprocessor">#endif</span>
<a name="l00552"></a>00552 <span class="preprocessor"></span>    <span class="keywordflow">while</span> ((cur = cache_root.find(<a class="code" href="HTTPCache_8cc.html#a1d22fde6e7501aebedb7663f0b0aa4e2">DIR_SEPARATOR_CHAR</a>, cur)) != string::npos) {
<a name="l00553"></a>00553         <span class="keywordtype">string</span> dir = cache_root.substr(0, cur);
<a name="l00554"></a>00554         <span class="keywordflow">if</span> (stat(dir.c_str(), &amp;stat_info) == -1) {
<a name="l00555"></a>00555             <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache....... Creating &quot;</span> &lt;&lt; dir &lt;&lt; endl);
<a name="l00556"></a>00556             mode_t mask = <a class="code" href="HTTPCache_8cc.html#ab51d9d52fad4f0a5361d2a3ab9d0edea">UMASK</a>(0);
<a name="l00557"></a>00557             <span class="keywordflow">if</span> (<a class="code" href="HTTPCache_8cc.html#a2bf44344e285663ad0b0b7bb001ac4fd">MKDIR</a>(dir.c_str(), 0777) &lt; 0) {
<a name="l00558"></a>00558                 <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Error: can&#39;t create.&quot;</span> &lt;&lt; endl);
<a name="l00559"></a>00559                 <a class="code" href="HTTPCache_8cc.html#ab51d9d52fad4f0a5361d2a3ab9d0edea">UMASK</a>(mask);
<a name="l00560"></a>00560                 <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;Could not create the directory for the cache. Failed when building path at &quot;</span>) + dir + <span class="keywordtype">string</span>(<span class="stringliteral">&quot;.&quot;</span>));
<a name="l00561"></a>00561             }
<a name="l00562"></a>00562             <a class="code" href="HTTPCache_8cc.html#ab51d9d52fad4f0a5361d2a3ab9d0edea">UMASK</a>(mask);
<a name="l00563"></a>00563         }
<a name="l00564"></a>00564         <span class="keywordflow">else</span> {
<a name="l00565"></a>00565             <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache....... Found &quot;</span> &lt;&lt; dir &lt;&lt; endl);
<a name="l00566"></a>00566         }
<a name="l00567"></a>00567         cur++;
<a name="l00568"></a>00568     }
<a name="l00569"></a>00569 }
<a name="l00570"></a>00570 
<a name="l00585"></a>00585 <span class="keywordtype">void</span>
<a name="l00586"></a>00586 HTTPCache::set_cache_root(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;root)
<a name="l00587"></a>00587 {
<a name="l00588"></a>00588     <span class="keywordflow">if</span> (root != <span class="stringliteral">&quot;&quot;</span>) {
<a name="l00589"></a>00589         d_cache_root = root;
<a name="l00590"></a>00590         <span class="comment">// cache root should end in /.</span>
<a name="l00591"></a>00591         <span class="keywordflow">if</span> (d_cache_root[d_cache_root.size()-1] != <a class="code" href="HTTPCache_8cc.html#a1d22fde6e7501aebedb7663f0b0aa4e2">DIR_SEPARATOR_CHAR</a>)
<a name="l00592"></a>00592             d_cache_root += <a class="code" href="HTTPCache_8cc.html#a1d22fde6e7501aebedb7663f0b0aa4e2">DIR_SEPARATOR_CHAR</a>;
<a name="l00593"></a>00593     }
<a name="l00594"></a>00594     <span class="keywordflow">else</span> {
<a name="l00595"></a>00595         <span class="comment">// If no cache root has been indicated then look for a suitable</span>
<a name="l00596"></a>00596         <span class="comment">// location.</span>
<a name="l00597"></a>00597         <span class="keywordtype">char</span> * cr = (<span class="keywordtype">char</span> *) getenv(<span class="stringliteral">&quot;DODS_CACHE&quot;</span>);
<a name="l00598"></a>00598         <span class="keywordflow">if</span> (!cr) cr = (<span class="keywordtype">char</span> *) getenv(<span class="stringliteral">&quot;TMP&quot;</span>);
<a name="l00599"></a>00599         <span class="keywordflow">if</span> (!cr) cr = (<span class="keywordtype">char</span> *) getenv(<span class="stringliteral">&quot;TEMP&quot;</span>);
<a name="l00600"></a>00600         <span class="keywordflow">if</span> (!cr) cr = <a class="code" href="HTTPCache_8cc.html#ac3dd016cd86fde4c7d4380ed0a9151c8">CACHE_LOC</a>;
<a name="l00601"></a>00601 
<a name="l00602"></a>00602         d_cache_root = cr;
<a name="l00603"></a>00603         <span class="keywordflow">if</span> (d_cache_root[d_cache_root.size()-1] != <a class="code" href="HTTPCache_8cc.html#a1d22fde6e7501aebedb7663f0b0aa4e2">DIR_SEPARATOR_CHAR</a>)
<a name="l00604"></a>00604             d_cache_root += <a class="code" href="HTTPCache_8cc.html#a1d22fde6e7501aebedb7663f0b0aa4e2">DIR_SEPARATOR_CHAR</a>;
<a name="l00605"></a>00605 
<a name="l00606"></a>00606         d_cache_root += <a class="code" href="HTTPCache_8cc.html#aeb598ae6ad780a09fe6ae3ee0db679f3">CACHE_ROOT</a>;
<a name="l00607"></a>00607     }
<a name="l00608"></a>00608 
<a name="l00609"></a>00609     <span class="comment">// Test d_hhtp_cache_table because this method can be called before that</span>
<a name="l00610"></a>00610     <span class="comment">// instance is created and also can be called later to cahnge the cache</span>
<a name="l00611"></a>00611     <span class="comment">// root. jhrg 05.14.08</span>
<a name="l00612"></a>00612     <span class="keywordflow">if</span> (d_http_cache_table)
<a name="l00613"></a>00613         d_http_cache_table-&gt;set_cache_root(d_cache_root);
<a name="l00614"></a>00614 }
<a name="l00615"></a>00615 
<a name="l00627"></a>00627 <span class="keywordtype">void</span>
<a name="l00628"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a71aa835c2b38bc0e891f40b5a77c53ce">00628</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a71aa835c2b38bc0e891f40b5a77c53ce">HTTPCache::set_cache_enabled</a>(<span class="keywordtype">bool</span> mode)
<a name="l00629"></a>00629 {
<a name="l00630"></a>00630     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l00631"></a>00631 
<a name="l00632"></a>00632     d_cache_enabled = mode;
<a name="l00633"></a>00633 
<a name="l00634"></a>00634     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l00635"></a>00635 }
<a name="l00636"></a>00636 
<a name="l00639"></a>00639 <span class="keywordtype">bool</span>
<a name="l00640"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a4492276bc732605cef02a146a657811f">00640</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a4492276bc732605cef02a146a657811f">HTTPCache::is_cache_enabled</a>()<span class="keyword"> const</span>
<a name="l00641"></a>00641 <span class="keyword"></span>{
<a name="l00642"></a>00642     <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;In HTTPCache::is_cache_enabled: (&quot;</span> &lt;&lt; d_cache_enabled &lt;&lt; <span class="stringliteral">&quot;)&quot;</span>
<a name="l00643"></a>00643          &lt;&lt; endl);
<a name="l00644"></a>00644     <span class="keywordflow">return</span> d_cache_enabled;
<a name="l00645"></a>00645 }
<a name="l00646"></a>00646 
<a name="l00657"></a>00657 <span class="keywordtype">void</span>
<a name="l00658"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a44857ae03334d09e34cc5b2275669a71">00658</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a44857ae03334d09e34cc5b2275669a71">HTTPCache::set_cache_disconnected</a>(<a class="code" href="namespacelibdap.html#a49a7ae9e704d21900a5bebcb92ebae94">CacheDisconnectedMode</a> mode)
<a name="l00659"></a>00659 {
<a name="l00660"></a>00660     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l00661"></a>00661 
<a name="l00662"></a>00662     d_cache_disconnected = mode;
<a name="l00663"></a>00663 
<a name="l00664"></a>00664     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l00665"></a>00665 }
<a name="l00666"></a>00666 
<a name="l00669"></a>00669 <a class="code" href="namespacelibdap.html#a49a7ae9e704d21900a5bebcb92ebae94">CacheDisconnectedMode</a>
<a name="l00670"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a857226db32577ea790fe2a283d751ce8">00670</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a857226db32577ea790fe2a283d751ce8">HTTPCache::get_cache_disconnected</a>()<span class="keyword"> const</span>
<a name="l00671"></a>00671 <span class="keyword"></span>{
<a name="l00672"></a>00672     <span class="keywordflow">return</span> d_cache_disconnected;
<a name="l00673"></a>00673 }
<a name="l00674"></a>00674 
<a name="l00683"></a>00683 <span class="keywordtype">void</span>
<a name="l00684"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a1a8c5f13e82c3038e97eca9979ebd39c">00684</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a1a8c5f13e82c3038e97eca9979ebd39c">HTTPCache::set_expire_ignored</a>(<span class="keywordtype">bool</span> mode)
<a name="l00685"></a>00685 {
<a name="l00686"></a>00686     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l00687"></a>00687 
<a name="l00688"></a>00688     d_expire_ignored = mode;
<a name="l00689"></a>00689 
<a name="l00690"></a>00690     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l00691"></a>00691 }
<a name="l00692"></a>00692 
<a name="l00693"></a>00693 <span class="comment">/* Is the cache ignoring Expires headers returned with responses that have</span>
<a name="l00694"></a>00694 <span class="comment">   been cached? */</span>
<a name="l00695"></a>00695 
<a name="l00696"></a>00696 <span class="keywordtype">bool</span>
<a name="l00697"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a0832a441ed8e037653d4e57b5f85813f">00697</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a0832a441ed8e037653d4e57b5f85813f">HTTPCache::is_expire_ignored</a>()<span class="keyword"> const</span>
<a name="l00698"></a>00698 <span class="keyword"></span>{
<a name="l00699"></a>00699     <span class="keywordflow">return</span> d_expire_ignored;
<a name="l00700"></a>00700 }
<a name="l00701"></a>00701 
<a name="l00717"></a>00717 <span class="keywordtype">void</span>
<a name="l00718"></a><a class="code" href="classlibdap_1_1HTTPCache.html#ab2447c27c88ebb29511379ede8ac5b6f">00718</a> <a class="code" href="classlibdap_1_1HTTPCache.html#ab2447c27c88ebb29511379ede8ac5b6f">HTTPCache::set_max_size</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> size)
<a name="l00719"></a>00719 {
<a name="l00720"></a>00720     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l00721"></a>00721 
<a name="l00722"></a>00722     <span class="keywordflow">try</span> {
<a name="l00723"></a>00723         <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> new_size = size &lt; <a class="code" href="HTTPCache_8cc.html#a33a98a1c97431e86ac8ae32ea5e08cbc">MIN_CACHE_TOTAL_SIZE</a> ?
<a name="l00724"></a>00724                                  <a class="code" href="HTTPCache_8cc.html#a33a98a1c97431e86ac8ae32ea5e08cbc">MIN_CACHE_TOTAL_SIZE</a> * <a class="code" href="HTTPCache_8cc.html#a78a6115b485de47c7cc56b224c558ea2">MEGA</a> :
<a name="l00725"></a>00725                                  (size &gt; ULONG_MAX ? ULONG_MAX : size * <a class="code" href="HTTPCache_8cc.html#a78a6115b485de47c7cc56b224c558ea2">MEGA</a>);
<a name="l00726"></a>00726         <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> old_size = d_total_size;
<a name="l00727"></a>00727         d_total_size = new_size;
<a name="l00728"></a>00728         d_folder_size = d_total_size / <a class="code" href="HTTPCache_8cc.html#ac3215fe6da2907a9efc7f488fb7c5ec5">CACHE_FOLDER_PCT</a>;
<a name="l00729"></a>00729         d_gc_buffer = d_total_size / <a class="code" href="HTTPCache_8cc.html#a2065c49052d2d2adeefc9d66fa136b46">CACHE_GC_PCT</a>;
<a name="l00730"></a>00730 
<a name="l00731"></a>00731         <span class="keywordflow">if</span> (new_size &lt; old_size &amp;&amp; startGC()) {
<a name="l00732"></a>00732             perform_garbage_collection();
<a name="l00733"></a>00733             d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a7cd5a3a24fd0cd41ac0d9a3192075e9c">cache_index_write</a>();
<a name="l00734"></a>00734         }
<a name="l00735"></a>00735     }
<a name="l00736"></a>00736     <span class="keywordflow">catch</span> (...) {
<a name="l00737"></a>00737         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l00738"></a>00738         <a class="code" href="debug_8h.html#a0d28d57d1228cf13a4c1288cc4758344">DBGN</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Unlocking interface.&quot;</span> &lt;&lt; endl);
<a name="l00739"></a>00739         <span class="keywordflow">throw</span>;
<a name="l00740"></a>00740     }
<a name="l00741"></a>00741 
<a name="l00742"></a>00742     <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache....... Total cache size: &quot;</span> &lt;&lt; d_total_size
<a name="l00743"></a>00743          &lt;&lt; <span class="stringliteral">&quot; with &quot;</span> &lt;&lt; d_folder_size
<a name="l00744"></a>00744          &lt;&lt; <span class="stringliteral">&quot; bytes for meta information and folders and at least &quot;</span>
<a name="l00745"></a>00745          &lt;&lt; d_gc_buffer &lt;&lt; <span class="stringliteral">&quot; bytes free after every gc&quot;</span> &lt;&lt; endl);
<a name="l00746"></a>00746 
<a name="l00747"></a>00747     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l00748"></a>00748 }
<a name="l00749"></a>00749 
<a name="l00752"></a>00752 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>
<a name="l00753"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a82e0235b7df1edbdb284b4f790d83cca">00753</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a82e0235b7df1edbdb284b4f790d83cca">HTTPCache::get_max_size</a>()<span class="keyword"> const</span>
<a name="l00754"></a>00754 <span class="keyword"></span>{
<a name="l00755"></a>00755     <span class="keywordflow">return</span> d_total_size / <a class="code" href="HTTPCache_8cc.html#a78a6115b485de47c7cc56b224c558ea2">MEGA</a>;
<a name="l00756"></a>00756 }
<a name="l00757"></a>00757 
<a name="l00766"></a>00766 <span class="keywordtype">void</span>
<a name="l00767"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a92cc5b035dc08ae2704924002144fa29">00767</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a92cc5b035dc08ae2704924002144fa29">HTTPCache::set_max_entry_size</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> size)
<a name="l00768"></a>00768 {
<a name="l00769"></a>00769     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l00770"></a>00770 
<a name="l00771"></a>00771     <span class="keywordflow">try</span> {
<a name="l00772"></a>00772         <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> new_size = size * <a class="code" href="HTTPCache_8cc.html#a78a6115b485de47c7cc56b224c558ea2">MEGA</a>;
<a name="l00773"></a>00773         <span class="keywordflow">if</span> (new_size &gt; 0 &amp;&amp; new_size &lt; d_total_size - d_folder_size) {
<a name="l00774"></a>00774             <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> old_size = d_max_entry_size;
<a name="l00775"></a>00775             d_max_entry_size = new_size;
<a name="l00776"></a>00776             <span class="keywordflow">if</span> (new_size &lt; old_size &amp;&amp; startGC()) {
<a name="l00777"></a>00777                 perform_garbage_collection();
<a name="l00778"></a>00778                 d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a7cd5a3a24fd0cd41ac0d9a3192075e9c">cache_index_write</a>();
<a name="l00779"></a>00779             }
<a name="l00780"></a>00780         }
<a name="l00781"></a>00781     }
<a name="l00782"></a>00782     <span class="keywordflow">catch</span> (...) {
<a name="l00783"></a>00783         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l00784"></a>00784         <span class="keywordflow">throw</span>;
<a name="l00785"></a>00785     }
<a name="l00786"></a>00786 
<a name="l00787"></a>00787     <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache...... Max entry cache size is &quot;</span>
<a name="l00788"></a>00788          &lt;&lt; d_max_entry_size &lt;&lt; endl);
<a name="l00789"></a>00789 
<a name="l00790"></a>00790     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l00791"></a>00791 }
<a name="l00792"></a>00792 
<a name="l00797"></a>00797 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span>
<a name="l00798"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a1adefd265732d5cd60d0a2a44bfd54a0">00798</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a1adefd265732d5cd60d0a2a44bfd54a0">HTTPCache::get_max_entry_size</a>()<span class="keyword"> const</span>
<a name="l00799"></a>00799 <span class="keyword"></span>{
<a name="l00800"></a>00800     <span class="keywordflow">return</span> d_max_entry_size / <a class="code" href="HTTPCache_8cc.html#a78a6115b485de47c7cc56b224c558ea2">MEGA</a>;
<a name="l00801"></a>00801 }
<a name="l00802"></a>00802 
<a name="l00813"></a>00813 <span class="keywordtype">void</span>
<a name="l00814"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a1ca5c8c205241f62596e283943906386">00814</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a1ca5c8c205241f62596e283943906386">HTTPCache::set_default_expiration</a>(<span class="keyword">const</span> <span class="keywordtype">int</span> exp_time)
<a name="l00815"></a>00815 {
<a name="l00816"></a>00816     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l00817"></a>00817 
<a name="l00818"></a>00818     d_default_expiration = exp_time;
<a name="l00819"></a>00819 
<a name="l00820"></a>00820     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l00821"></a>00821 }
<a name="l00822"></a>00822 
<a name="l00825"></a>00825 <span class="keywordtype">int</span>
<a name="l00826"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a391c78fac4f21bae526d363cd6f66ee7">00826</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a391c78fac4f21bae526d363cd6f66ee7">HTTPCache::get_default_expiration</a>()<span class="keyword"> const</span>
<a name="l00827"></a>00827 <span class="keyword"></span>{
<a name="l00828"></a>00828     <span class="keywordflow">return</span> d_default_expiration;
<a name="l00829"></a>00829 }
<a name="l00830"></a>00830 
<a name="l00835"></a>00835 <span class="keywordtype">void</span>
<a name="l00836"></a><a class="code" href="classlibdap_1_1HTTPCache.html#aa9a13a18ec837e891e81823a4d30da47">00836</a> <a class="code" href="classlibdap_1_1HTTPCache.html#aa9a13a18ec837e891e81823a4d30da47">HTTPCache::set_always_validate</a>(<span class="keywordtype">bool</span> validate)
<a name="l00837"></a>00837 {
<a name="l00838"></a>00838     d_always_validate = validate;
<a name="l00839"></a>00839 }
<a name="l00840"></a>00840 
<a name="l00844"></a>00844 <span class="keywordtype">bool</span>
<a name="l00845"></a><a class="code" href="classlibdap_1_1HTTPCache.html#ac0cb038b8b7aa0680b2058267e285e41">00845</a> <a class="code" href="classlibdap_1_1HTTPCache.html#ac0cb038b8b7aa0680b2058267e285e41">HTTPCache::get_always_validate</a>()<span class="keyword"> const</span>
<a name="l00846"></a>00846 <span class="keyword"></span>{
<a name="l00847"></a>00847     <span class="keywordflow">return</span> d_always_validate;
<a name="l00848"></a>00848 }
<a name="l00849"></a>00849 
<a name="l00866"></a>00866 <span class="keywordtype">void</span>
<a name="l00867"></a><a class="code" href="classlibdap_1_1HTTPCache.html#ac6823b2de27745c20c7e8d41af5a2088">00867</a> <a class="code" href="classlibdap_1_1HTTPCache.html#ac6823b2de27745c20c7e8d41af5a2088">HTTPCache::set_cache_control</a>(<span class="keyword">const</span> vector&lt;string&gt; &amp;cc)
<a name="l00868"></a>00868 {
<a name="l00869"></a>00869     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l00870"></a>00870 
<a name="l00871"></a>00871     <span class="keywordflow">try</span> {
<a name="l00872"></a>00872         d_cache_control = cc;
<a name="l00873"></a>00873 
<a name="l00874"></a>00874         vector&lt;string&gt;::const_iterator i;
<a name="l00875"></a>00875         <span class="keywordflow">for</span> (i = cc.begin(); i != cc.end(); ++i) {
<a name="l00876"></a>00876             <span class="keywordtype">string</span> header = (*i).substr(0, (*i).find(<span class="charliteral">&#39;:&#39;</span>));
<a name="l00877"></a>00877             <span class="keywordtype">string</span> value = (*i).substr((*i).find(<span class="stringliteral">&quot;: &quot;</span>) + 2);
<a name="l00878"></a>00878             <span class="keywordflow">if</span> (header != <span class="stringliteral">&quot;Cache-Control&quot;</span>) {
<a name="l00879"></a>00879                 <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;Expected cache control header not found.&quot;</span>);
<a name="l00880"></a>00880             }
<a name="l00881"></a>00881             <span class="keywordflow">else</span> {
<a name="l00882"></a>00882                 <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="l00883"></a>00883                     d_cache_enabled = <span class="keyword">false</span>;
<a name="l00884"></a>00884                 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value.find(<span class="stringliteral">&quot;max-age&quot;</span>) != string::npos) {
<a name="l00885"></a>00885                     <span class="keywordtype">string</span> max_age = value.substr(value.find(<span class="stringliteral">&quot;=&quot;</span> + 1));
<a name="l00886"></a>00886                     d_max_age = <a class="code" href="namespacelibdap.html#abaf4e1a6a665dbfe9324b25ec9fa701a">parse_time</a>(max_age.c_str());
<a name="l00887"></a>00887                 }
<a name="l00888"></a>00888                 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value == <span class="stringliteral">&quot;max-stale&quot;</span>)
<a name="l00889"></a>00889                     d_max_stale = 0; <span class="comment">// indicates will take anything;</span>
<a name="l00890"></a>00890                 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value.find(<span class="stringliteral">&quot;max-stale&quot;</span>) != string::npos) {
<a name="l00891"></a>00891                     <span class="keywordtype">string</span> max_stale = value.substr(value.find(<span class="stringliteral">&quot;=&quot;</span> + 1));
<a name="l00892"></a>00892                     d_max_stale = <a class="code" href="namespacelibdap.html#abaf4e1a6a665dbfe9324b25ec9fa701a">parse_time</a>(max_stale.c_str());
<a name="l00893"></a>00893                 }
<a name="l00894"></a>00894                 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (value.find(<span class="stringliteral">&quot;min-fresh&quot;</span>) != string::npos) {
<a name="l00895"></a>00895                     <span class="keywordtype">string</span> min_fresh = value.substr(value.find(<span class="stringliteral">&quot;=&quot;</span> + 1));
<a name="l00896"></a>00896                     d_min_fresh = <a class="code" href="namespacelibdap.html#abaf4e1a6a665dbfe9324b25ec9fa701a">parse_time</a>(min_fresh.c_str());
<a name="l00897"></a>00897                 }
<a name="l00898"></a>00898             }
<a name="l00899"></a>00899         }
<a name="l00900"></a>00900     }
<a name="l00901"></a>00901     <span class="keywordflow">catch</span> (...) {
<a name="l00902"></a>00902         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l00903"></a>00903         <span class="keywordflow">throw</span>;
<a name="l00904"></a>00904     }
<a name="l00905"></a>00905 
<a name="l00906"></a>00906     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l00907"></a>00907 }
<a name="l00908"></a>00908 
<a name="l00909"></a>00909 
<a name="l00914"></a>00914 vector&lt;string&gt;
<a name="l00915"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a15d15a90105d60b0d743fc90456d223e">00915</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a15d15a90105d60b0d743fc90456d223e">HTTPCache::get_cache_control</a>()
<a name="l00916"></a>00916 {
<a name="l00917"></a>00917     <span class="keywordflow">return</span> d_cache_control;
<a name="l00918"></a>00918 }
<a name="l00919"></a>00919 
<a name="l00921"></a>00921 
<a name="l00930"></a>00930 <span class="keywordtype">bool</span>
<a name="l00931"></a>00931 HTTPCache::is_url_in_cache(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url)
<a name="l00932"></a>00932 {
<a name="l00933"></a>00933     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Is this url in the cache? (&quot;</span> &lt;&lt; url &lt;&lt; <span class="stringliteral">&quot;)&quot;</span> &lt;&lt; endl);
<a name="l00934"></a>00934 
<a name="l00935"></a>00935     <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry = d_http_cache_table-&gt;get_locked_entry_from_cache_table(url);
<a name="l00936"></a>00936     <span class="keywordtype">bool</span> status = entry != 0;
<a name="l00937"></a>00937     <span class="keywordflow">if</span> (entry) {
<a name="l00938"></a>00938         entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ae2db6d25b27dd657dd0a2ef553e28246">unlock_read_response</a>();
<a name="l00939"></a>00939     }
<a name="l00940"></a>00940     <span class="keywordflow">return</span>  status;
<a name="l00941"></a>00941 }
<a name="l00942"></a>00942 
<a name="l00948"></a>00948 <span class="keywordtype">bool</span>
<a name="l00949"></a><a class="code" href="namespacelibdap.html#a1987f9d225c2778a297eab61a6a612b3">00949</a> <a class="code" href="namespacelibdap.html#a1987f9d225c2778a297eab61a6a612b3">is_hop_by_hop_header</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;header)
<a name="l00950"></a>00950 {
<a name="l00951"></a>00951     <span class="keywordflow">return</span> header.find(<span class="stringliteral">&quot;Connection&quot;</span>) != string::npos
<a name="l00952"></a>00952            || header.find(<span class="stringliteral">&quot;Keep-Alive&quot;</span>) != string::npos
<a name="l00953"></a>00953            || header.find(<span class="stringliteral">&quot;Proxy-Authenticate&quot;</span>) != string::npos
<a name="l00954"></a>00954            || header.find(<span class="stringliteral">&quot;Proxy-Authorization&quot;</span>) != string::npos
<a name="l00955"></a>00955            || header.find(<span class="stringliteral">&quot;Transfer-Encoding&quot;</span>) != string::npos
<a name="l00956"></a>00956            || header.find(<span class="stringliteral">&quot;Upgrade&quot;</span>) != string::npos;
<a name="l00957"></a>00957 }
<a name="l00958"></a>00958 
<a name="l00970"></a>00970 <span class="keywordtype">void</span>
<a name="l00971"></a>00971 HTTPCache::write_metadata(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;cachename, <span class="keyword">const</span> vector&lt;string&gt; &amp;headers)
<a name="l00972"></a>00972 {
<a name="l00973"></a>00973     <span class="keywordtype">string</span> fname = cachename + <a class="code" href="HTTPCache_8cc.html#a9f34e7ee25eb923739b1ea126dce4528">CACHE_META</a>;
<a name="l00974"></a>00974     d_open_files.push_back(fname);
<a name="l00975"></a>00975 
<a name="l00976"></a>00976     FILE *dest = fopen(fname.c_str(), <span class="stringliteral">&quot;w&quot;</span>);
<a name="l00977"></a>00977     <span class="keywordflow">if</span> (!dest) {
<a name="l00978"></a>00978         <span class="keywordflow">throw</span> InternalErr(__FILE__, __LINE__,
<a name="l00979"></a>00979                           <span class="stringliteral">&quot;Could not open named cache entry file.&quot;</span>);
<a name="l00980"></a>00980     }
<a name="l00981"></a>00981 
<a name="l00982"></a>00982     vector&lt;string&gt;::const_iterator i;
<a name="l00983"></a>00983     <span class="keywordflow">for</span> (i = headers.begin(); i != headers.end(); ++i) {
<a name="l00984"></a>00984         <span class="keywordflow">if</span> (!<a class="code" href="namespacelibdap.html#a1987f9d225c2778a297eab61a6a612b3">is_hop_by_hop_header</a>(*i)) {
<a name="l00985"></a>00985             fwrite((*i).c_str(), (*i).size(), 1, dest);
<a name="l00986"></a>00986             fwrite(<span class="stringliteral">&quot;\n&quot;</span>, 1, 1, dest);
<a name="l00987"></a>00987         }
<a name="l00988"></a>00988     }
<a name="l00989"></a>00989 
<a name="l00990"></a>00990     <span class="keywordtype">int</span> res = fclose(dest);
<a name="l00991"></a>00991     <span class="keywordflow">if</span> (res) {
<a name="l00992"></a>00992         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;HTTPCache::write_metadata - Failed to close &quot;</span>
<a name="l00993"></a>00993             &lt;&lt; dest &lt;&lt; endl);
<a name="l00994"></a>00994     }
<a name="l00995"></a>00995 
<a name="l00996"></a>00996     d_open_files.pop_back();
<a name="l00997"></a>00997 }
<a name="l00998"></a>00998 
<a name="l01009"></a>01009 <span class="keywordtype">void</span>
<a name="l01010"></a>01010 HTTPCache::read_metadata(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;cachename, vector&lt;string&gt; &amp;headers)
<a name="l01011"></a>01011 {
<a name="l01012"></a>01012     FILE *md = fopen(<span class="keywordtype">string</span>(cachename + CACHE_META).c_str(), <span class="stringliteral">&quot;r&quot;</span>);
<a name="l01013"></a>01013     <span class="keywordflow">if</span> (!md) {
<a name="l01014"></a>01014         <span class="keywordflow">throw</span> InternalErr(__FILE__, __LINE__,
<a name="l01015"></a>01015                           <span class="stringliteral">&quot;Could not open named cache entry meta data file.&quot;</span>);
<a name="l01016"></a>01016     }
<a name="l01017"></a>01017 
<a name="l01018"></a>01018     <span class="keywordtype">char</span> line[1024];
<a name="l01019"></a>01019     <span class="keywordflow">while</span> (!feof(md) &amp;&amp; fgets(line, 1024, md)) {
<a name="l01020"></a>01020         line[min(1024, static_cast&lt;int&gt;(strlen(line)))-1] = <span class="charliteral">&#39;\0&#39;</span>; <span class="comment">// erase newline</span>
<a name="l01021"></a>01021         headers.push_back(<span class="keywordtype">string</span>(line));
<a name="l01022"></a>01022     }
<a name="l01023"></a>01023 
<a name="l01024"></a>01024     <span class="keywordtype">int</span> res = fclose(md);
<a name="l01025"></a>01025     <span class="keywordflow">if</span> (res) {
<a name="l01026"></a>01026         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;HTTPCache::read_metadata - Failed to close &quot;</span>
<a name="l01027"></a>01027             &lt;&lt; md &lt;&lt; endl);
<a name="l01028"></a>01028     }
<a name="l01029"></a>01029 }
<a name="l01030"></a>01030 
<a name="l01052"></a>01052 <span class="keywordtype">int</span>
<a name="l01053"></a>01053 HTTPCache::write_body(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;cachename, <span class="keyword">const</span> FILE *src)
<a name="l01054"></a>01054 {
<a name="l01055"></a>01055     d_open_files.push_back(cachename);
<a name="l01056"></a>01056 
<a name="l01057"></a>01057     FILE *dest = fopen(cachename.c_str(), <span class="stringliteral">&quot;wb&quot;</span>);
<a name="l01058"></a>01058     <span class="keywordflow">if</span> (!dest) {
<a name="l01059"></a>01059         <span class="keywordflow">throw</span> InternalErr(__FILE__, __LINE__,
<a name="l01060"></a>01060                           <span class="stringliteral">&quot;Could not open named cache entry file.&quot;</span>);
<a name="l01061"></a>01061     }
<a name="l01062"></a>01062 
<a name="l01063"></a>01063     <span class="comment">// Read and write in 1k blocks; an attempt at doing this efficiently.</span>
<a name="l01064"></a>01064     <span class="comment">// 09/30/02 jhrg</span>
<a name="l01065"></a>01065     <span class="keywordtype">char</span> line[1024];
<a name="l01066"></a>01066     <span class="keywordtype">size_t</span> n;
<a name="l01067"></a>01067     <span class="keywordtype">int</span> total = 0;
<a name="l01068"></a>01068     <span class="keywordflow">while</span> ((n = fread(line, 1, 1024, const_cast&lt;FILE *&gt;(src))) &gt; 0) {
<a name="l01069"></a>01069         total += fwrite(line, 1, n, dest);
<a name="l01070"></a>01070         <a class="code" href="debug_8h.html#ac1f764c266c50f0050e33557004c914c">DBG2</a>(sleep(3));
<a name="l01071"></a>01071     }
<a name="l01072"></a>01072 
<a name="l01073"></a>01073     <span class="keywordflow">if</span> (ferror(const_cast&lt;FILE *&gt;(src)) || ferror(dest)) {
<a name="l01074"></a>01074         <span class="keywordtype">int</span> res = fclose(dest);
<a name="l01075"></a>01075         res = res &amp; unlink(cachename.c_str());
<a name="l01076"></a>01076         <span class="keywordflow">if</span> (res) {
<a name="l01077"></a>01077             <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;HTTPCache::write_body - Failed to close/unlink &quot;</span>
<a name="l01078"></a>01078                 &lt;&lt; dest &lt;&lt; endl);
<a name="l01079"></a>01079         }
<a name="l01080"></a>01080         <span class="keywordflow">throw</span> InternalErr(__FILE__, __LINE__,
<a name="l01081"></a>01081                           <span class="stringliteral">&quot;I/O error transferring data to the cache.&quot;</span>);
<a name="l01082"></a>01082     }
<a name="l01083"></a>01083 
<a name="l01084"></a>01084     rewind(const_cast&lt;FILE *&gt;(src));
<a name="l01085"></a>01085 
<a name="l01086"></a>01086     <span class="keywordtype">int</span> res = fclose(dest);
<a name="l01087"></a>01087     <span class="keywordflow">if</span> (res) {
<a name="l01088"></a>01088         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;HTTPCache::write_body - Failed to close &quot;</span>
<a name="l01089"></a>01089             &lt;&lt; dest &lt;&lt; endl);
<a name="l01090"></a>01090     }
<a name="l01091"></a>01091 
<a name="l01092"></a>01092     d_open_files.pop_back();
<a name="l01093"></a>01093 
<a name="l01094"></a>01094     <span class="keywordflow">return</span> total;
<a name="l01095"></a>01095 }
<a name="l01096"></a>01096 
<a name="l01105"></a>01105 FILE *
<a name="l01106"></a>01106 HTTPCache::open_body(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;cachename)
<a name="l01107"></a>01107 {
<a name="l01108"></a>01108         FILE *src = fopen(cachename.c_str(), <span class="stringliteral">&quot;rb&quot;</span>);             <span class="comment">// Read only</span>
<a name="l01109"></a>01109         <span class="keywordflow">if</span> (!src)
<a name="l01110"></a>01110         <span class="keywordflow">throw</span> InternalErr(__FILE__, __LINE__, <span class="stringliteral">&quot;Could not open cache file.&quot;</span>);
<a name="l01111"></a>01111 
<a name="l01112"></a>01112     <span class="keywordflow">return</span> src;
<a name="l01113"></a>01113 }
<a name="l01114"></a>01114 
<a name="l01140"></a>01140 <span class="keywordtype">bool</span>
<a name="l01141"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a0cbaded239e55f3e328448adcc180365">01141</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a0cbaded239e55f3e328448adcc180365">HTTPCache::cache_response</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url, time_t request_time,
<a name="l01142"></a>01142                           <span class="keyword">const</span> vector&lt;string&gt; &amp;headers, <span class="keyword">const</span> FILE *body)
<a name="l01143"></a>01143 {
<a name="l01144"></a>01144     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l01145"></a>01145 
<a name="l01146"></a>01146     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Caching url: &quot;</span> &lt;&lt; url &lt;&lt; <span class="stringliteral">&quot;.&quot;</span> &lt;&lt; endl);
<a name="l01147"></a>01147 
<a name="l01148"></a>01148     <span class="keywordflow">try</span> {
<a name="l01149"></a>01149         <span class="comment">// If this is not an http or https URL, don&#39;t cache.</span>
<a name="l01150"></a>01150         <span class="keywordflow">if</span> (url.find(<span class="stringliteral">&quot;http:&quot;</span>) == string::npos &amp;&amp;
<a name="l01151"></a>01151             url.find(<span class="stringliteral">&quot;https:&quot;</span>) == string::npos) {
<a name="l01152"></a>01152             <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01153"></a>01153             <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l01154"></a>01154         }
<a name="l01155"></a>01155 
<a name="l01156"></a>01156         <span class="comment">// This does nothing if url is not already in the cache. It&#39;s</span>
<a name="l01157"></a>01157         <span class="comment">// more efficient to do this than to first check and see if the entry</span>
<a name="l01158"></a>01158         <span class="comment">// exists. 10/10/02 jhrg</span>
<a name="l01159"></a>01159         d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#ad1b8ffd257ff335b3d87b90600f5bffd">remove_entry_from_cache_table</a>(url);
<a name="l01160"></a>01160 
<a name="l01161"></a>01161         <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>(url);
<a name="l01162"></a>01162         entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a127f19c06a5f0d2da8b55e4d0d175ae2">lock_write_response</a>();
<a name="l01163"></a>01163 
<a name="l01164"></a>01164         <span class="keywordflow">try</span> {
<a name="l01165"></a>01165             d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a53379f3151fe6b85e85c75c401a14963">parse_headers</a>(entry, d_max_entry_size, headers); <span class="comment">// etag, lm, date, age, expires, max_age.</span>
<a name="l01166"></a>01166             <span class="keywordflow">if</span> (entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ac58af16f8cfb31f08020d64ca1914407">is_no_cache</a>()) {
<a name="l01167"></a>01167                 <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Not cache-able; deleting HTTPCacheTable::CacheEntry: &quot;</span> &lt;&lt; entry
<a name="l01168"></a>01168                     &lt;&lt; <span class="stringliteral">&quot;(&quot;</span> &lt;&lt; url &lt;&lt; <span class="stringliteral">&quot;)&quot;</span> &lt;&lt; endl);
<a name="l01169"></a>01169                 entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a0cddc0fbf4569c5e323e79c5ec098c22">unlock_write_response</a>();
<a name="l01170"></a>01170                 <span class="keyword">delete</span> entry; entry = 0;
<a name="l01171"></a>01171                 <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01172"></a>01172                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l01173"></a>01173             }
<a name="l01174"></a>01174 
<a name="l01175"></a>01175             <span class="comment">// corrected_initial_age, freshness_lifetime, response_time.</span>
<a name="l01176"></a>01176             d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0ca63c76e70064549d128ff8f5951534">calculate_time</a>(entry, d_default_expiration, request_time);
<a name="l01177"></a>01177 
<a name="l01178"></a>01178             d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0ab52cb3d5d01575977adb33a21b31d2">create_location</a>(entry); <span class="comment">// cachename, cache_body_fd</span>
<a name="l01179"></a>01179             <span class="comment">// move these write function to cache table</span>
<a name="l01180"></a>01180             entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a6711b2f4b3ebc87f2f6b87da87d0f44b">set_size</a>(write_body(entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a78b388e7b644a3548a7d548550603fd9">get_cachename</a>(), body));
<a name="l01181"></a>01181             write_metadata(entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a78b388e7b644a3548a7d548550603fd9">get_cachename</a>(), headers);
<a name="l01182"></a>01182             d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a8f5660848048a98454fce23c28de573c">add_entry_to_cache_table</a>(entry);
<a name="l01183"></a>01183             entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a0cddc0fbf4569c5e323e79c5ec098c22">unlock_write_response</a>();
<a name="l01184"></a>01184         }
<a name="l01185"></a>01185         <span class="keywordflow">catch</span> (<a class="code" href="classlibdap_1_1ResponseTooBigErr.html">ResponseTooBigErr</a> &amp;e) {
<a name="l01186"></a>01186             <span class="comment">// Oops. Bummer. Clean up and exit.</span>
<a name="l01187"></a>01187             <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; e.<a class="code" href="classlibdap_1_1Error.html#a917fdf537b7dd7e587e9cf6dc7e4c687">get_error_message</a>() &lt;&lt; endl);
<a name="l01188"></a>01188             <a class="code" href="HTTPCache_8cc.html#a2a6edcee170a79b132872d22066754ed">REMOVE</a>(entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a78b388e7b644a3548a7d548550603fd9">get_cachename</a>().c_str());
<a name="l01189"></a>01189             <a class="code" href="HTTPCache_8cc.html#a2a6edcee170a79b132872d22066754ed">REMOVE</a>(<span class="keywordtype">string</span>(entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a78b388e7b644a3548a7d548550603fd9">get_cachename</a>() + <a class="code" href="HTTPCache_8cc.html#a9f34e7ee25eb923739b1ea126dce4528">CACHE_META</a>).c_str());
<a name="l01190"></a>01190             <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Too big; deleting HTTPCacheTable::CacheEntry: &quot;</span> &lt;&lt; entry &lt;&lt; <span class="stringliteral">&quot;(&quot;</span> &lt;&lt; url
<a name="l01191"></a>01191                 &lt;&lt; <span class="stringliteral">&quot;)&quot;</span> &lt;&lt; endl);
<a name="l01192"></a>01192             entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a0cddc0fbf4569c5e323e79c5ec098c22">unlock_write_response</a>();
<a name="l01193"></a>01193             <span class="keyword">delete</span> entry; entry = 0;
<a name="l01194"></a>01194             <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01195"></a>01195             <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l01196"></a>01196         }
<a name="l01197"></a>01197 
<a name="l01198"></a>01198         <span class="keywordflow">if</span> (d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a7a412fb2155e6bcaab3ce15a2d8121a5">get_new_entries</a>() &gt; <a class="code" href="HTTPCache_8cc.html#a38b298d045bf3f59133a1852187d9a1c">DUMP_FREQUENCY</a>) {
<a name="l01199"></a>01199             <span class="keywordflow">if</span> (startGC())
<a name="l01200"></a>01200                 perform_garbage_collection();
<a name="l01201"></a>01201 
<a name="l01202"></a>01202             d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a7cd5a3a24fd0cd41ac0d9a3192075e9c">cache_index_write</a>(); <span class="comment">// resets new_entries</span>
<a name="l01203"></a>01203         }
<a name="l01204"></a>01204     }
<a name="l01205"></a>01205     <span class="keywordflow">catch</span> (...) {
<a name="l01206"></a>01206         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01207"></a>01207         <span class="keywordflow">throw</span>;
<a name="l01208"></a>01208     }
<a name="l01209"></a>01209 
<a name="l01210"></a>01210     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01211"></a>01211 
<a name="l01212"></a>01212     <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l01213"></a>01213 }
<a name="l01214"></a>01214 
<a name="l01233"></a>01233 vector&lt;string&gt;
<a name="l01234"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a635a277480a95d7d37d0e9fddd480362">01234</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a635a277480a95d7d37d0e9fddd480362">HTTPCache::get_conditional_request_headers</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url)
<a name="l01235"></a>01235 {
<a name="l01236"></a>01236     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l01237"></a>01237 
<a name="l01238"></a>01238     <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry = 0;
<a name="l01239"></a>01239     vector&lt;string&gt; headers;
<a name="l01240"></a>01240 
<a name="l01241"></a>01241     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Getting conditional request headers for &quot;</span> &lt;&lt; url &lt;&lt; endl);
<a name="l01242"></a>01242 
<a name="l01243"></a>01243     <span class="keywordflow">try</span> {
<a name="l01244"></a>01244         entry = d_http_cache_table-&gt;get_locked_entry_from_cache_table(url);
<a name="l01245"></a>01245         <span class="keywordflow">if</span> (!entry)
<a name="l01246"></a>01246             <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;There is no cache entry for the URL: &quot;</span> + url);
<a name="l01247"></a>01247 
<a name="l01248"></a>01248         <span class="keywordflow">if</span> (entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a6c442bb2e75458b0221fdc0e33a1b987">get_etag</a>() != <span class="stringliteral">&quot;&quot;</span>)
<a name="l01249"></a>01249             headers.push_back(<span class="keywordtype">string</span>(<span class="stringliteral">&quot;If-None-Match: &quot;</span>) + entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a6c442bb2e75458b0221fdc0e33a1b987">get_etag</a>());
<a name="l01250"></a>01250 
<a name="l01251"></a>01251         <span class="keywordflow">if</span> (entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a45a937fc7a01fe7b9eff6ed71234ae90">get_lm</a>() &gt; 0) {
<a name="l01252"></a>01252                 time_t lm = entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a45a937fc7a01fe7b9eff6ed71234ae90">get_lm</a>();
<a name="l01253"></a>01253             headers.push_back(<span class="keywordtype">string</span>(<span class="stringliteral">&quot;If-Modified-Since: &quot;</span>)
<a name="l01254"></a>01254                               + <a class="code" href="namespacelibdap.html#ab9983dcf36d072b24f4ecee5558d790a">date_time_str</a>(&amp;lm));
<a name="l01255"></a>01255         }
<a name="l01256"></a>01256         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a15c27c48f367f79bded48569b4d12351">get_max_age</a>() &gt; 0) {
<a name="l01257"></a>01257                 time_t max_age = entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a15c27c48f367f79bded48569b4d12351">get_max_age</a>();
<a name="l01258"></a>01258             headers.push_back(<span class="keywordtype">string</span>(<span class="stringliteral">&quot;If-Modified-Since: &quot;</span>)
<a name="l01259"></a>01259                               + <a class="code" href="namespacelibdap.html#ab9983dcf36d072b24f4ecee5558d790a">date_time_str</a>(&amp;max_age));
<a name="l01260"></a>01260         }
<a name="l01261"></a>01261         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ad6c75b405f431deafaba0ce949e514bc">get_expires</a>() &gt; 0) {
<a name="l01262"></a>01262                 time_t expires = entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ad6c75b405f431deafaba0ce949e514bc">get_expires</a>();
<a name="l01263"></a>01263             headers.push_back(<span class="keywordtype">string</span>(<span class="stringliteral">&quot;If-Modified-Since: &quot;</span>)
<a name="l01264"></a>01264                               + <a class="code" href="namespacelibdap.html#ab9983dcf36d072b24f4ecee5558d790a">date_time_str</a>(&amp;expires));
<a name="l01265"></a>01265         }
<a name="l01266"></a>01266         entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ae2db6d25b27dd657dd0a2ef553e28246">unlock_read_response</a>();
<a name="l01267"></a>01267         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01268"></a>01268     }
<a name="l01269"></a>01269     <span class="keywordflow">catch</span> (...) {
<a name="l01270"></a>01270         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01271"></a>01271         <span class="keywordflow">if</span> (entry) {
<a name="l01272"></a>01272             entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ae2db6d25b27dd657dd0a2ef553e28246">unlock_read_response</a>();
<a name="l01273"></a>01273         }
<a name="l01274"></a>01274         <span class="keywordflow">throw</span>;
<a name="l01275"></a>01275     }
<a name="l01276"></a>01276 
<a name="l01277"></a>01277     <span class="keywordflow">return</span> headers;
<a name="l01278"></a>01278 }
<a name="l01279"></a>01279 
<a name="l01283"></a>01283 <span class="keyword">struct </span>HeaderLess: binary_function&lt;const string&amp;, const string&amp;, bool&gt;
<a name="l01284"></a>01284 {
<a name="l01285"></a>01285     <span class="keywordtype">bool</span> operator()(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;s1, <span class="keyword">const</span> <span class="keywordtype">string</span> &amp;s2)<span class="keyword"> const </span>{
<a name="l01286"></a>01286         <span class="keywordflow">return</span> s1.substr(0, s1.find(<span class="charliteral">&#39;:&#39;</span>)) &lt; s2.substr(0, s2.find(<span class="charliteral">&#39;:&#39;</span>));
<a name="l01287"></a>01287     }
<a name="l01288"></a>01288 };
<a name="l01289"></a>01289 
<a name="l01303"></a>01303 <span class="keywordtype">void</span>
<a name="l01304"></a><a class="code" href="classlibdap_1_1HTTPCache.html#ab0b1e3fb63084b93e277c53d189ad6c2">01304</a> <a class="code" href="classlibdap_1_1HTTPCache.html#ab0b1e3fb63084b93e277c53d189ad6c2">HTTPCache::update_response</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url, time_t request_time,
<a name="l01305"></a>01305                            <span class="keyword">const</span> vector&lt;string&gt; &amp;headers)
<a name="l01306"></a>01306 {
<a name="l01307"></a>01307     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l01308"></a>01308 
<a name="l01309"></a>01309     <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry = 0;
<a name="l01310"></a>01310     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Updating the response headers for: &quot;</span> &lt;&lt; url &lt;&lt; endl);
<a name="l01311"></a>01311 
<a name="l01312"></a>01312     <span class="keywordflow">try</span> {
<a name="l01313"></a>01313         entry = d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a57c01c8b9462f0e0ea6541b7d9fcf2f5">get_write_locked_entry_from_cache_table</a>(url);
<a name="l01314"></a>01314         <span class="keywordflow">if</span> (!entry)
<a name="l01315"></a>01315             <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;There is no cache entry for the URL: &quot;</span> + url);
<a name="l01316"></a>01316 
<a name="l01317"></a>01317         <span class="comment">// Merge the new headers with the exiting HTTPCacheTable::CacheEntry object.</span>
<a name="l01318"></a>01318         d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a53379f3151fe6b85e85c75c401a14963">parse_headers</a>(entry, d_max_entry_size, headers);
<a name="l01319"></a>01319 
<a name="l01320"></a>01320         <span class="comment">// Update corrected_initial_age, freshness_lifetime, response_time.</span>
<a name="l01321"></a>01321         d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a0ca63c76e70064549d128ff8f5951534">calculate_time</a>(entry, d_default_expiration, request_time);
<a name="l01322"></a>01322 
<a name="l01323"></a>01323         <span class="comment">// Merge the new headers with those in the persistent store. How:</span>
<a name="l01324"></a>01324         <span class="comment">// Load the new headers into a set, then merge the old headers. Since</span>
<a name="l01325"></a>01325         <span class="comment">// set&lt;&gt; ignores duplicates, old headers with the same name as a new</span>
<a name="l01326"></a>01326         <span class="comment">// header will got into the bit bucket. Define a special compare</span>
<a name="l01327"></a>01327         <span class="comment">// functor to make sure that headers are compared using only their</span>
<a name="l01328"></a>01328         <span class="comment">// name and not their value too.</span>
<a name="l01329"></a>01329         set&lt;string, HeaderLess&gt; merged_headers;
<a name="l01330"></a>01330 
<a name="l01331"></a>01331         <span class="comment">// Load in the new headers</span>
<a name="l01332"></a>01332         copy(headers.begin(), headers.end(),
<a name="l01333"></a>01333              inserter(merged_headers, merged_headers.begin()));
<a name="l01334"></a>01334 
<a name="l01335"></a>01335         <span class="comment">// Get the old headers and load them in.</span>
<a name="l01336"></a>01336         vector&lt;string&gt; old_headers;
<a name="l01337"></a>01337         read_metadata(entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a78b388e7b644a3548a7d548550603fd9">get_cachename</a>(), old_headers);
<a name="l01338"></a>01338         copy(old_headers.begin(), old_headers.end(),
<a name="l01339"></a>01339              inserter(merged_headers, merged_headers.begin()));
<a name="l01340"></a>01340 
<a name="l01341"></a>01341         <span class="comment">// Read the values back out. Use reverse iterators with back_inserter</span>
<a name="l01342"></a>01342         <span class="comment">// to preserve header order. NB: vector&lt;&gt; does not support push_front</span>
<a name="l01343"></a>01343         <span class="comment">// so we can&#39;t use front_inserter(). 01/09/03 jhrg</span>
<a name="l01344"></a>01344         vector&lt;string&gt; result;
<a name="l01345"></a>01345         copy(merged_headers.rbegin(), merged_headers.rend(),
<a name="l01346"></a>01346              back_inserter(result));
<a name="l01347"></a>01347 
<a name="l01348"></a>01348         write_metadata(entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a78b388e7b644a3548a7d548550603fd9">get_cachename</a>(), result);
<a name="l01349"></a>01349         entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a0cddc0fbf4569c5e323e79c5ec098c22">unlock_write_response</a>();
<a name="l01350"></a>01350         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01351"></a>01351     }
<a name="l01352"></a>01352     <span class="keywordflow">catch</span> (...) {
<a name="l01353"></a>01353         <span class="keywordflow">if</span> (entry) {
<a name="l01354"></a>01354             entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ae2db6d25b27dd657dd0a2ef553e28246">unlock_read_response</a>();
<a name="l01355"></a>01355         }
<a name="l01356"></a>01356         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01357"></a>01357         <span class="keywordflow">throw</span>;
<a name="l01358"></a>01358     }
<a name="l01359"></a>01359 }
<a name="l01360"></a>01360 
<a name="l01372"></a>01372 <span class="keywordtype">bool</span>
<a name="l01373"></a><a class="code" href="classlibdap_1_1HTTPCache.html#aa695d43815cc2588458232f37095b780">01373</a> <a class="code" href="classlibdap_1_1HTTPCache.html#aa695d43815cc2588458232f37095b780">HTTPCache::is_url_valid</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url)
<a name="l01374"></a>01374 {
<a name="l01375"></a>01375     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l01376"></a>01376 
<a name="l01377"></a>01377     <span class="keywordtype">bool</span> freshness;
<a name="l01378"></a>01378     <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry = 0;
<a name="l01379"></a>01379 
<a name="l01380"></a>01380     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Is this URL valid? (&quot;</span> &lt;&lt; url &lt;&lt; <span class="stringliteral">&quot;)&quot;</span> &lt;&lt; endl);
<a name="l01381"></a>01381 
<a name="l01382"></a>01382     <span class="keywordflow">try</span> {
<a name="l01383"></a>01383         <span class="keywordflow">if</span> (d_always_validate) {
<a name="l01384"></a>01384             <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01385"></a>01385             <span class="keywordflow">return</span> <span class="keyword">false</span>;  <span class="comment">// force re-validation.</span>
<a name="l01386"></a>01386         }
<a name="l01387"></a>01387 
<a name="l01388"></a>01388         entry = d_http_cache_table-&gt;get_locked_entry_from_cache_table(url);
<a name="l01389"></a>01389         <span class="keywordflow">if</span> (!entry)
<a name="l01390"></a>01390             <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;There is no cache entry for the URL: &quot;</span> + url);
<a name="l01391"></a>01391 
<a name="l01392"></a>01392         <span class="comment">// If we supported range requests, we&#39;d need code here to check if</span>
<a name="l01393"></a>01393         <span class="comment">// there was only a partial response in the cache. 10/02/02 jhrg</span>
<a name="l01394"></a>01394 
<a name="l01395"></a>01395         <span class="comment">// In case this entry is of type &quot;must-revalidate&quot; then we consider it</span>
<a name="l01396"></a>01396         <span class="comment">// invalid.</span>
<a name="l01397"></a>01397         <span class="keywordflow">if</span> (entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ad654840f637bcaf7232ccf851469daba">get_must_revalidate</a>()) {
<a name="l01398"></a>01398             entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ae2db6d25b27dd657dd0a2ef553e28246">unlock_read_response</a>();
<a name="l01399"></a>01399             <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01400"></a>01400             <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l01401"></a>01401         }
<a name="l01402"></a>01402 
<a name="l01403"></a>01403         time_t resident_time = time(NULL) - entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a7002294360aa52d1dd86dedca57c7e6d">get_response_time</a>();
<a name="l01404"></a>01404         time_t current_age = entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a83412eeb15ec59d73b2407943d0a4a04">get_corrected_initial_age</a>() + resident_time;
<a name="l01405"></a>01405 
<a name="l01406"></a>01406         <span class="comment">// Check that the max-age, max-stale, and min-fresh directives</span>
<a name="l01407"></a>01407         <span class="comment">// given in the request cache control header is followed.</span>
<a name="l01408"></a>01408         <span class="keywordflow">if</span> (d_max_age &gt;= 0 &amp;&amp; current_age &gt; d_max_age) {
<a name="l01409"></a>01409             <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache....... Max-age validation&quot;</span> &lt;&lt; endl);
<a name="l01410"></a>01410             entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ae2db6d25b27dd657dd0a2ef553e28246">unlock_read_response</a>();
<a name="l01411"></a>01411             <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01412"></a>01412             <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l01413"></a>01413         }
<a name="l01414"></a>01414         <span class="keywordflow">if</span> (d_min_fresh &gt;= 0
<a name="l01415"></a>01415             &amp;&amp; entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a59da6c588993d40d2829a90cc0c67f1a">get_freshness_lifetime</a>() &lt; current_age + d_min_fresh) {
<a name="l01416"></a>01416             <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Cache....... Min-fresh validation&quot;</span> &lt;&lt; endl);
<a name="l01417"></a>01417             entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ae2db6d25b27dd657dd0a2ef553e28246">unlock_read_response</a>();
<a name="l01418"></a>01418             <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01419"></a>01419             <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l01420"></a>01420         }
<a name="l01421"></a>01421 
<a name="l01422"></a>01422         freshness = (entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a59da6c588993d40d2829a90cc0c67f1a">get_freshness_lifetime</a>()
<a name="l01423"></a>01423                      + (d_max_stale &gt;= 0 ? d_max_stale : 0) &gt; current_age);
<a name="l01424"></a>01424         entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ae2db6d25b27dd657dd0a2ef553e28246">unlock_read_response</a>();
<a name="l01425"></a>01425         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01426"></a>01426     }
<a name="l01427"></a>01427     <span class="keywordflow">catch</span> (...) {
<a name="l01428"></a>01428         <span class="keywordflow">if</span> (entry) {
<a name="l01429"></a>01429             entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#ae2db6d25b27dd657dd0a2ef553e28246">unlock_read_response</a>();
<a name="l01430"></a>01430         }
<a name="l01431"></a>01431         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01432"></a>01432         <span class="keywordflow">throw</span>;
<a name="l01433"></a>01433     }
<a name="l01434"></a>01434 
<a name="l01435"></a>01435     <span class="keywordflow">return</span> freshness;
<a name="l01436"></a>01436 }
<a name="l01437"></a>01437 
<a name="l01465"></a><a class="code" href="classlibdap_1_1HTTPCache.html#aefcbe732444c6160c839b9e2c3468b1d">01465</a> FILE * <a class="code" href="classlibdap_1_1HTTPCache.html#aefcbe732444c6160c839b9e2c3468b1d">HTTPCache::get_cached_response</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url,
<a name="l01466"></a>01466                 vector&lt;string&gt; &amp;headers, <span class="keywordtype">string</span> &amp;cacheName) {
<a name="l01467"></a>01467     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l01468"></a>01468 
<a name="l01469"></a>01469     FILE *body;
<a name="l01470"></a>01470     <a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html">HTTPCacheTable::CacheEntry</a> *entry = 0;
<a name="l01471"></a>01471 
<a name="l01472"></a>01472     <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Getting the cached response for &quot;</span> &lt;&lt; url &lt;&lt; endl);
<a name="l01473"></a>01473 
<a name="l01474"></a>01474     <span class="keywordflow">try</span> {
<a name="l01475"></a>01475         entry = d_http_cache_table-&gt;get_locked_entry_from_cache_table(url);
<a name="l01476"></a>01476         <span class="keywordflow">if</span> (!entry) {
<a name="l01477"></a>01477                 <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01478"></a>01478                 <span class="keywordflow">return</span> 0;
<a name="l01479"></a>01479         }
<a name="l01480"></a>01480 
<a name="l01481"></a>01481         cacheName = entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a78b388e7b644a3548a7d548550603fd9">get_cachename</a>();
<a name="l01482"></a>01482         read_metadata(entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a78b388e7b644a3548a7d548550603fd9">get_cachename</a>(), headers);
<a name="l01483"></a>01483 
<a name="l01484"></a>01484         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Headers just read from cache: &quot;</span> &lt;&lt; endl);
<a name="l01485"></a>01485         <a class="code" href="debug_8h.html#a0d28d57d1228cf13a4c1288cc4758344">DBGN</a>(copy(headers.begin(), headers.end(), ostream_iterator&lt;string&gt;(cerr, <span class="stringliteral">&quot;\n&quot;</span>)));
<a name="l01486"></a>01486 
<a name="l01487"></a>01487         body = open_body(entry-&gt;<a class="code" href="structlibdap_1_1HTTPCacheTable_1_1CacheEntry.html#a78b388e7b644a3548a7d548550603fd9">get_cachename</a>());
<a name="l01488"></a>01488 
<a name="l01489"></a>01489         <a class="code" href="debug_8h.html#a32adf79142f0a426b5e782fb7cd4cad3">DBG</a>(cerr &lt;&lt; <span class="stringliteral">&quot;Returning: &quot;</span> &lt;&lt; url &lt;&lt; <span class="stringliteral">&quot; from the cache.&quot;</span> &lt;&lt; endl);
<a name="l01490"></a>01490 
<a name="l01491"></a>01491         d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#a24b2c86910ba623a59184ab42410b4cb">bind_entry_to_data</a>(entry, body);
<a name="l01492"></a>01492     }
<a name="l01493"></a>01493     <span class="keywordflow">catch</span> (...) {
<a name="l01494"></a>01494         <span class="keywordflow">if</span> (entry)
<a name="l01495"></a>01495         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01496"></a>01496         <span class="keywordflow">throw</span>;
<a name="l01497"></a>01497     }
<a name="l01498"></a>01498 
<a name="l01499"></a>01499     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01500"></a>01500 
<a name="l01501"></a>01501     <span class="keywordflow">return</span> body;
<a name="l01502"></a>01502 }
<a name="l01514"></a>01514 FILE *
<a name="l01515"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a151026836e9f397857dd76aa080ddfef">01515</a> <a class="code" href="classlibdap_1_1HTTPCache.html#aefcbe732444c6160c839b9e2c3468b1d">HTTPCache::get_cached_response</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url, vector&lt;string&gt; &amp;headers)
<a name="l01516"></a>01516 {
<a name="l01517"></a>01517         <span class="keywordtype">string</span> discard_name;
<a name="l01518"></a>01518         <span class="keywordflow">return</span> <a class="code" href="classlibdap_1_1HTTPCache.html#aefcbe732444c6160c839b9e2c3468b1d">get_cached_response</a>(url, headers, discard_name);
<a name="l01519"></a>01519 }
<a name="l01520"></a>01520 
<a name="l01531"></a>01531 FILE *
<a name="l01532"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a6f3bbb3dc0b1e85f32e2291006ff858e">01532</a> <a class="code" href="classlibdap_1_1HTTPCache.html#aefcbe732444c6160c839b9e2c3468b1d">HTTPCache::get_cached_response</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;url)
<a name="l01533"></a>01533 {
<a name="l01534"></a>01534         <span class="keywordtype">string</span> discard_name;
<a name="l01535"></a>01535         vector&lt;string&gt; discard_headers;
<a name="l01536"></a>01536         <span class="keywordflow">return</span> <a class="code" href="classlibdap_1_1HTTPCache.html#aefcbe732444c6160c839b9e2c3468b1d">get_cached_response</a>(url, discard_headers, discard_name);
<a name="l01537"></a>01537 }
<a name="l01538"></a>01538 
<a name="l01551"></a>01551 <span class="keywordtype">void</span>
<a name="l01552"></a><a class="code" href="classlibdap_1_1HTTPCache.html#a4de0458e4ab0e89049a0f0fa7b819b91">01552</a> <a class="code" href="classlibdap_1_1HTTPCache.html#a4de0458e4ab0e89049a0f0fa7b819b91">HTTPCache::release_cached_response</a>(FILE *body)
<a name="l01553"></a>01553 {
<a name="l01554"></a>01554     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l01555"></a>01555 
<a name="l01556"></a>01556     <span class="keywordflow">try</span> {
<a name="l01557"></a>01557         d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#ad0ac242f618a804562e18546738904df">uncouple_entry_from_data</a>(body);
<a name="l01558"></a>01558     }
<a name="l01559"></a>01559     <span class="keywordflow">catch</span> (...) {
<a name="l01560"></a>01560         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01561"></a>01561         <span class="keywordflow">throw</span>;
<a name="l01562"></a>01562     }
<a name="l01563"></a>01563 
<a name="l01564"></a>01564     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01565"></a>01565 }
<a name="l01566"></a>01566 
<a name="l01579"></a>01579 <span class="keywordtype">void</span>
<a name="l01580"></a><a class="code" href="classlibdap_1_1HTTPCache.html#afee7fca59d1a37f495ea74c95fad600a">01580</a> <a class="code" href="classlibdap_1_1HTTPCache.html#afee7fca59d1a37f495ea74c95fad600a">HTTPCache::purge_cache</a>()
<a name="l01581"></a>01581 {
<a name="l01582"></a>01582     <a class="code" href="classlibdap_1_1HTTPCache.html#a71ed87540113c4403a33bb436d3f8b9c">lock_cache_interface</a>();
<a name="l01583"></a>01583 
<a name="l01584"></a>01584     <span class="keywordflow">try</span> {
<a name="l01585"></a>01585         <span class="keywordflow">if</span> (d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#afc2ef03d687c3db809e2bfbc3bca3bd3">is_locked_read_responses</a>())
<a name="l01586"></a>01586             <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;Attempt to purge the cache with entries in use.&quot;</span>);
<a name="l01587"></a>01587 
<a name="l01588"></a>01588         d_http_cache_table-&gt;<a class="code" href="classlibdap_1_1HTTPCacheTable.html#ad02f9346125f4c311d6e74b6a8354f9e">delete_all_entries</a>();
<a name="l01589"></a>01589     }
<a name="l01590"></a>01590     <span class="keywordflow">catch</span> (...) {
<a name="l01591"></a>01591         <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01592"></a>01592         <span class="keywordflow">throw</span>;
<a name="l01593"></a>01593     }
<a name="l01594"></a>01594 
<a name="l01595"></a>01595     <a class="code" href="classlibdap_1_1HTTPCache.html#a5b6a28e17912e87389260f7695fdf876">unlock_cache_interface</a>();
<a name="l01596"></a>01596 }
<a name="l01597"></a>01597 
<a name="l01598"></a>01598 } <span class="comment">// namespace libdap</span>
</pre></div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="HTTPCache_8cc.html">HTTPCache.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>