libdap Updated for version 3.21.0
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 <algorithm>
34#include <iostream>
35#include <iterator>
36#include <string>
37#include <vector>
38
39#include "Response.h"
40#include "debug.h"
41#include "util.h"
42
43namespace libdap {
44
45// defined in HTTPConnect.cc
46extern int dods_keep_temps;
47extern void close_temp(FILE *s, const string &name);
48
55class HTTPResponse : public Response {
56private:
57 std::vector<std::string> *d_headers; // Response headers
58 std::string d_file; // Temp file that holds response body
59
60protected:
63 HTTPResponse();
64 HTTPResponse(const HTTPResponse &rs);
65 HTTPResponse &operator=(const HTTPResponse &);
67
68public:
85 HTTPResponse(FILE *s, int status, std::vector<std::string> *h, const std::string &temp_file)
86 : Response(s, status), d_headers(h), d_file(temp_file) {
87 DBG(cerr << "Headers: " << endl);
88 DBGN(copy(d_headers->begin(), d_headers->end(), ostream_iterator<string>(cerr, "\n")));
89 DBGN(cerr << "end of headers." << endl);
90 }
91
101 HTTPResponse(std::fstream *s, int status, std::vector<std::string> *h, const std::string &temp_file)
102 : Response(s, status), d_headers(h), d_file(temp_file) {
103 DBG(cerr << "Headers: " << endl);
104 DBGN(copy(d_headers->begin(), d_headers->end(), ostream_iterator<string>(cerr, "\n")));
105 DBGN(cerr << "end of headers." << endl);
106 }
107
111 virtual ~HTTPResponse() {
112 DBG(cerr << "Freeing HTTPConnect resources (" + d_file + ")... ");
113
114 // This can always be done - if the cpp_stream is null, delete has no effect;
115 // if non-null in this class it was allocated in HTTPConnect::plain_fetch_url
116 // (or caching_fetch_url when that's implemented)
117 delete get_cpp_stream();
118 set_cpp_stream(0);
119
120 if (!dods_keep_temps && !d_file.empty()) {
121 if (get_stream()) {
122 close_temp(get_stream(), d_file);
123 set_stream(0);
124 } else {
125 (void)unlink(d_file.c_str());
126#if 0
127 long res = unlink(d_file.c_str());
128 if (res != 0) throw InternalErr(__FILE__, __LINE__, "!FAIL! " + long_to_string(res));
129#endif
130 }
131 }
132
133 delete d_headers;
134
135 DBGN(cerr << endl);
136 }
137
144 // ~Response() will take care of closing the FILE*. A better version of this
145 // code would not leave the FILE* open when it's not needed, but this implementation
146 // can use the existing HTTPConnect and HTTPCache software with very minimal
147 // (or no) modification. jhrg 11/8/13
148 set_cpp_stream(new std::fstream(d_file.c_str(), std::ios::in | std::ios::binary));
149 }
150
153 virtual std::vector<std::string> *get_headers() const { return d_headers; }
154 virtual std::string get_file() const { return d_file; }
156
159 virtual void set_headers(std::vector<std::string> *h) { d_headers = h; }
160 virtual void set_file(const std::string &n) { d_file = n; }
162};
163
164} // namespace libdap
165
166#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:61
top level DAP object to house generic methods
Definition AISConnect.cc:30
void close_temp(FILE *s, const string &name)