1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 
4 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
5 // Access Protocol.
6 
7 // Copyright (c) 2002,2003 OPeNDAP, Inc.
8 // Author: James Gallagher <jgallagher@opendap.org>
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Lesser General Public
12 // License as published by the Free Software Foundation; either
13 // version 2.1 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 //
24 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
25 
26 #ifndef cache_http_response_h
27 #define cache_http_response_h
28 
29 #include <cstdio>
30 
31 #ifndef response_h
32 #include "Response.h"
33 #endif
34 
35 #ifndef _debug_h
36 #include "debug.h"
37 #endif
38 
39 namespace libdap
40 {
41 
42 /** Encapsulate a response. Instead of directly returning the FILE pointer
43     from which a response is read, return an instance of this object. */
44 class HTTPCacheResponse : public HTTPResponse
45 {
46 private:
47     HTTPCache *d_cache;  // pointer to singleton instance
48 
49 protected:
50     /** @name Suppressed default methods */
51     //@{
52     HTTPCacheResponse();
53     HTTPCacheResponse(const HTTPCacheResponse &rs);
54     HTTPCacheResponse &operator=(const HTTPCacheResponse &);
55     //@}
56 
57 public:
58     /** Build a Response object. Instances of this class are used to
59     represent responses from a local HTTP/1.1 cache. The stream and
60     headers pointer are passed to the parent (HTTPResponse); there's no
61     temporary file for the parent to manage since the body is read from a
62     file managed by the cache subsystem. This class releases the lock on
63     the cache entry when the destructor is called. */
HTTPCacheResponse(FILE * s,int status_code,vector<string> * headers,HTTPCache * c)64     HTTPCacheResponse(FILE *s, int status_code, vector<string> *headers, HTTPCache *c)
65             : HTTPResponse(s, status_code, headers, ""), d_cache(c)
66     {}
67 
68     /** Build a Response object. Instances of this class are used to
69     represent responses from a local HTTP/1.1 cache. The stream and
70     headers pointer are passed to the parent (HTTPResponse); there's no
71     temporary file for the parent to manage since the body is read from a
72     file managed by the cache subsystem. This class releases the lock on
73     the cache entry when the destructor is called. */
HTTPCacheResponse(FILE * s,int status_code,vector<string> * headers,const string & file_name,HTTPCache * c)74     HTTPCacheResponse(FILE *s, int status_code, vector<string> *headers,
75 	    const string &file_name, HTTPCache *c)
76             : HTTPResponse(s, status_code, headers, file_name), d_cache(c)
77     {}
78 
79     /** Free the cache entry lock. Call the parent's destructor. */
~HTTPCacheResponse()80     virtual ~HTTPCacheResponse()
81     {
82         DBG(cerr << "Freeing HTTPCache resources... ");
83         set_file(""); // This keeps ~HTTPResponse() from removing the cache entry.
84         d_cache->release_cached_response(get_stream());
85         DBGN(cerr << endl);
86     }
87 };
88 
89 } // namespace libdap
90 
91 #endif // cache_http_response_h
92