libdap Updated for version 3.21.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
HTTPResponse.h
Go to the documentation of this file.
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 "util.h"
41
42namespace libdap {
43extern int dods_keep_temps;
44
49
50class HTTPResponse : public Response {
51private:
52 std::vector<std::string> d_headers; // Response headers
53 std::string d_file; // Temp file that holds response body
54
55public:
56 HTTPResponse() = default;
57 HTTPResponse(const HTTPResponse &rs) = delete;
59
76 HTTPResponse(FILE *s, int status, const std::vector<std::string> &h, const std::string &temp_file)
77 : Response(s, status), d_headers(h), d_file(temp_file) {}
78
87 HTTPResponse(std::fstream *s, int status, const std::vector<std::string> &h, const std::string &temp_file)
88 : Response(s, status), d_headers(h), d_file(temp_file) {}
89
93 ~HTTPResponse() override {
94 if (!dods_keep_temps && !d_file.empty()) {
95 (void)unlink(d_file.c_str());
96 }
97 }
98
105 // ~Response() will take care of closing the FILE*. A better version of this
106 // code would not leave the FILE* open when it's not needed, but this implementation
107 // can use the existing HTTPConnect and HTTPCache software with very minimal
108 // (or no) modification. jhrg 11/8/13
109 set_cpp_stream(new std::fstream(d_file.c_str(), std::ios::in | std::ios::binary));
110 // This hack was added to work around a memory leak introduced here but which surfaces
111 // in the parent class. Deleting the fstream* in all cases can lead to a double free;
112 // not deleting it in some cases leads to a leak. This set of classes should be redesigned.
113 // jhrg 2/9/25
115 }
116
119 virtual std::vector<std::string> &get_headers() { return d_headers; }
120 virtual std::string get_file() const { return d_file; }
122
125 virtual void set_headers(std::vector<std::string> &h) { d_headers = h; }
126 virtual void set_file(const std::string &n) { d_file = n; }
128};
129
130} // namespace libdap
131
132#endif // http_response_h
virtual std::vector< std::string > & get_headers()
virtual std::string get_file() const
HTTPResponse & operator=(const HTTPResponse &)=delete
HTTPResponse(const HTTPResponse &rs)=delete
virtual void set_file(const std::string &n)
virtual void set_headers(std::vector< std::string > &h)
HTTPResponse(FILE *s, int status, const std::vector< std::string > &h, const std::string &temp_file)
HTTPResponse(std::fstream *s, int status, const 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() override
bool d_delete_cpp_stream_ptr
Definition Response.h:67
virtual void set_cpp_stream(std::istream *s)
Definition Response.h:116
Response()=default
top level DAP object to house generic methods
Definition AISConnect.cc:30
int dods_keep_temps