bes  Updated for version 3.20.6
httpd_catalog_module/RemoteHttpResource.h
1 
2 // -*- mode: c++; c-basic-offset:4 -*-
3 //
4 // This file is part of cmr_module, A C++ module that can be loaded in to
5 // the OPeNDAP Back-End Server (BES) and is able to handle remote requests.
6 //
7 // Copyright (c) 2018 OPeNDAP, Inc.
8 // Author: Nathan Potter <ndp@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 
27 #ifndef remote_http_resource_h_
28 #define remote_http_resource_h_
29 
30 #include <curl/curl.h>
31 #include <curl/easy.h>
32 
33 #include <string>
34 #include <vector>
35 
36 namespace httpd_catalog {
37 
45 private:
47  std::string d_remoteResourceUrl;
48 
50  int d_fd;
51 
53  bool d_initialized;
54 
56  CURL *d_curl;
57 
59  char d_error_buffer[CURL_ERROR_SIZE]; // A human-readable message.
60 
62  std::string d_type;
63 
65  std::string d_resourceCacheFileName;
66 
68  std::vector<std::string> *d_request_headers; // Request headers
69 
71  std::vector<std::string> *d_response_headers; // Response headers
72 
74  std::map<std::string,std::string> *d_http_response_headers; // Response headers
75 
80  void writeResourceToFile(int fd);
81 
86  void ingest_http_headers_and_type();
87 
88 protected:
90  d_fd(0), d_initialized(false), d_curl(0), d_resourceCacheFileName(""), d_request_headers(0), d_response_headers(
91  0), d_http_response_headers(0)
92  {
93  }
94 
95 public:
96  RemoteHttpResource(const std::string &url);
97  virtual ~RemoteHttpResource();
98 
99  void retrieveResource();
100 
105  std::string getType()
106  {
107  return d_type;
108  }
109 
114  std::string getCacheFileName()
115  {
116  if (!d_initialized)
117  //throw libdap::Error(
118  throw BESInternalError("RemoteHttpResource::getCacheFileName() - STATE ERROR: Remote Resource Has Not Been Retrieved.", __FILE__, __LINE__);
119  return d_resourceCacheFileName;
120  }
121 
122  std::string get_http_response_header(const std::string header_name);
123 
127  void getResponseHeaders(std::vector<std::string> &hdrs)
128  {
129  if (!d_initialized)
130  //throw libdap::Error(
131  throw BESInternalError("RemoteHttpResource::getCacheFileName() - STATE ERROR: Remote Resource Has Not Been Retrieved.", __FILE__, __LINE__);
132  hdrs.insert( hdrs.end(), d_response_headers->begin(), d_response_headers->end() );
133  }
134 };
135 
136 } /* namespace httpd_catalog */
137 
138 #endif /* remote_http_resource_h_ */
httpd_catalog::RemoteHttpResource::getType
std::string getType()
Definition: httpd_catalog_module/RemoteHttpResource.h:105
httpd_catalog::RemoteHttpResource::getCacheFileName
std::string getCacheFileName()
Definition: httpd_catalog_module/RemoteHttpResource.h:114
httpd_catalog::RemoteHttpResource::get_http_response_header
std::string get_http_response_header(const std::string header_name)
Definition: httpd_catalog_module/RemoteHttpResource.cc:406
httpd_catalog::RemoteHttpResource::~RemoteHttpResource
virtual ~RemoteHttpResource()
Definition: httpd_catalog_module/RemoteHttpResource.cc:101
httpd_catalog::RemoteHttpResource::retrieveResource
void retrieveResource()
Definition: httpd_catalog_module/RemoteHttpResource.cc:139
httpd_catalog::RemoteHttpResource
Definition: httpd_catalog_module/RemoteHttpResource.h:44
httpd_catalog::RemoteHttpResource::getResponseHeaders
void getResponseHeaders(std::vector< std::string > &hdrs)
Definition: httpd_catalog_module/RemoteHttpResource.h:127
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43