libdap Updated for version 3.20.10
libdap4 is an implementation of OPeNDAP's DAP protocol.
HTTPResponse.h
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 http_response_h
27#define http_response_h
28
29#include <unistd.h>
30
31#include <cstdio>
32
33#include <string>
34#include <iostream>
35#include <algorithm>
36#include <iterator>
37#include <vector>
38
39#include "Response.h"
40#include "util.h"
41#include "debug.h"
42
43namespace libdap
44{
45
46// defined in HTTPConnect.cc
47extern int dods_keep_temps;
48extern void close_temp(FILE *s, const string &name);
49
56class HTTPResponse : public Response
57{
58private:
59 std::vector<std::string> *d_headers; // Response headers
60 std::string d_file; // Temp file that holds response body
61
62protected:
66 HTTPResponse(const HTTPResponse &rs);
67 HTTPResponse &operator=(const HTTPResponse &);
69
70public:
87 HTTPResponse(FILE *s, int status, std::vector<std::string> *h, const std::string &temp_file)
88 : Response(s, status), d_headers(h), d_file(temp_file)
89 {
90 DBG(cerr << "Headers: " << endl);
91 DBGN(copy(d_headers->begin(), d_headers->end(),
92 ostream_iterator<string>(cerr, "\n")));
93 DBGN(cerr << "end of headers." << endl);
94 }
95
105 HTTPResponse(std::fstream *s, int status, std::vector<std::string> *h, const std::string &temp_file)
106 : Response(s, status), d_headers(h), d_file(temp_file)
107 {
108 DBG(cerr << "Headers: " << endl);
109 DBGN(copy(d_headers->begin(), d_headers->end(),
110 ostream_iterator<string>(cerr, "\n")));
111 DBGN(cerr << "end of headers." << endl);
112 }
113
118 {
119 DBG(cerr << "Freeing HTTPConnect resources (" + d_file + ")... ");
120
121 // This can always be done - if the cpp_stream is null, delete has no effect;
122 // if non-null in this class it was allocated in HTTPConnect::plain_fetch_url
123 // (or caching_fetch_url when that's implemented)
124 delete get_cpp_stream();
125 set_cpp_stream(0);
126
127 if (!dods_keep_temps && !d_file.empty()) {
128 if (get_stream()) {
129 close_temp(get_stream(), d_file);
130 set_stream(0);
131 }
132 else {
133 (void) unlink(d_file.c_str());
134#if 0
135 long res = unlink(d_file.c_str());
136 if (res != 0) throw InternalErr(__FILE__, __LINE__, "!FAIL! " + long_to_string(res));
137#endif
138 }
139 }
140
141 delete d_headers;
142
143 DBGN(cerr << endl);
144 }
145
152 // ~Response() will take care of closing the FILE*. A better version of this
153 // code would not leave the FILE* open when it's not needed, but this implementation
154 // can use the existing HTTPConnect and HTTPCache software with very minimal
155 // (or no) modification. jhrg 11/8/13
156 set_cpp_stream(new std::fstream(d_file.c_str(), std::ios::in|std::ios::binary));
157 }
158
161 virtual std::vector<std::string> *get_headers() const { return d_headers; }
162 virtual std::string get_file() const { return d_file; }
164
167 virtual void set_headers(std::vector<std::string> *h) { d_headers = h; }
168 virtual void set_file(const std::string &n) { d_file = n; }
170};
171
172} // namespace libdap
173
174#endif // http_response_h
HTTPResponse(std::fstream *s, int status, std::vector< std::string > *h, const std::string &temp_file)
Build a HTTPResponse using a cpp fstream When working with DAP4 responses, use C++ streams for I/0.
HTTPResponse(FILE *s, int status, std::vector< std::string > *h, const std::string &temp_file)
A class for software fault reporting.
Definition InternalErr.h:65
top level DAP object to house generic methods
void close_temp(FILE *s, const string &name)