libdap  Updated for version 3.20.3
libdap4 is an implementation of OPeNDAP's DAP protocol.
getdap4.cc
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 // (c) COPYRIGHT URI/MIT 1997-1999
27 // Please read the full copyright statement in the file COPYRIGHT_URI.
28 //
29 // Authors:
30 // jhrg,jimg James Gallagher <jgallagher@gso.uri.edu>
31 
32 // This is the source to `getdap'; a simple tool to exercise the Connect
33 // class. It can be used to get naked URLs as well as the DAP2 DAS and DDS
34 // objects. jhrg.
35 
36 #include "config.h"
37 
38 #ifdef WIN32
39 #include <io.h>
40 #include <fcntl.h>
41 #endif
42 
43 #include <cstring>
44 #include <string>
45 #include <sstream>
46 
47 #include "GetOpt.h"
48 
49 #include "DMR.h"
50 #include "XMLWriter.h"
51 #include "D4BaseTypeFactory.h"
52 #include "D4Group.h"
53 #include "D4Sequence.h"
54 #include "D4Connect.h"
55 #include "StdinResponse.h"
56 #include "HTTPConnect.h"
57 #include "RCReader.h"
58 
59 using namespace std;
60 using namespace libdap ;
61 
62 const char *version = CVER " (" DVR " DAP/" DAP_PROTOCOL_VERSION ")";
63 #if 0
64 extern int libdap::dods_keep_temps; // defined in HTTPResponse.h
65 extern int libdap::www_trace;
66 #endif
67 static void usage(const string &name)
68 {
69  cerr << "Usage: " << name << endl;
70  cerr << " [dD vVikmzstM][-c <expr>][-m <num>] <url> [<url> ...] | <file> [<file> ...]" << endl;
71  cerr << endl;
72  cerr << "In the first form of the command, dereference the URL and" << endl;
73  cerr << "perform the requested operations. This includes routing" << endl;
74  cerr << "the returned information through the DAP processing" << endl;
75  cerr << "library (parsing the returned objects, et c.). If none" << endl;
76  cerr << "of a, d, or D are used with a URL, then the DAP library" << endl;
77  cerr << "routines are NOT used and the URLs contents are dumped" << endl;
78  cerr << "to standard output." << endl;
79  cerr << "Note: If the URL contains a query string the query string" << endl;
80  cerr << "will be preserved in the request. However, if the query " << endl;
81  cerr << "string contains DAP4 keys they may interfere with the" << endl;
82  cerr << "operation of " << name << ". A warning will be" << endl;
83  cerr << "written to stderr when "<< name << " identifies" << endl;
84  cerr << "the presence of a DAP4 query key in the submitted" << endl;
85  cerr << "URL's query string." << endl;
86  cerr << endl;
87  cerr << "In the second form of the command, assume the files are" << endl;
88  cerr << "DataDDS objects (stored in files or read from pipes)" << endl;
89  cerr << "and process them as if -D were given. In this case the" << endl;
90  cerr << "information *must* contain valid MIME header in order" << endl;
91  cerr << "to be processed." << endl;
92  cerr << endl;
93  cerr << "Options:" << endl;
94  cerr << " d: For each URL, get the (DAP4) DMR object. Does not get data." << endl;
95  cerr << " D: For each URL, get the DAP4 Data response." << endl;
96  cerr << endl;
97  cerr << " v: Verbose output." << endl;
98  cerr << " V: Version of this client; see 'i' for server version." << endl;
99  cerr << " i: For each URL, get the server version." << endl;
100  cerr << " k: Keep temporary files created by libdap." << endl;
101  cerr << " m: Request the same URL <num> times." << endl;
102  cerr << " z: Ask the server to compress data." << endl;
103  cerr << " s: Print Sequences using numbered rows." << endl;
104  cerr << " t: Trace www accesses." << endl;
105  cerr << " M: Assume data read from a file has no MIME headers; use only with files" << endl;
106  cerr << endl;
107  cerr << " c: <expr> is a constraint expression. Used with -d/D" << endl;
108  cerr << " NB: You can use a `?' for the CE also." << endl;
109 }
110 
111 // Used for raw http access/transfer
112 bool read_data(FILE * fp)
113 {
114  if (!fp) {
115  fprintf(stderr, "getdap4: Whoa!!! Null stream pointer.\n");
116  return false;
117  }
118  // Changed from a loop that used getc() to one that uses fread(). getc()
119  // worked fine for transfers of text information, but *not* for binary
120  // transfers. fread() will handle both.
121  char c = 0;
122  while (fp && !feof(fp) && fread(&c, 1, 1, fp))
123  printf("%c", c); // stick with stdio
124 
125  return true;
126 }
127 
128 static void read_response_from_file(D4Connect *url, DMR &dmr, Response &r, bool mime_headers, bool get_dap4_data, bool get_dmr)
129 {
130  if (mime_headers) {
131  if (get_dap4_data)
132  url->read_data(dmr, r);
133  else if (get_dmr)
134  url->read_dmr(dmr, r);
135  else
136  throw Error("Only supports Data or DMR responses");
137  }
138  else {
139  if (get_dap4_data)
140  url->read_data_no_mime(dmr, r);
141  else if (get_dmr)
142  url->read_dmr_no_mime(dmr, r);
143  else
144  throw Error("Only supports Data or DMR responses");
145  }
146 }
147 
148 static void print_group_data(D4Group *g, bool print_rows = false)
149 {
150  for (Constructor::Vars_iter i = g->var_begin(), e = g->var_end(); i != e; i++) {
151  if (print_rows && (*i)->type() == dods_sequence_c)
152  dynamic_cast<D4Sequence &>(**i).print_val_by_rows(cout);
153  else
154  (*i)->print_val(cout);
155  }
156 
157  for (D4Group::groupsIter gi = g->grp_begin(), ge = g->grp_end(); gi != ge; ++gi) {
158  print_group_data(*gi, print_rows);
159  }
160 }
161 
162 static void print_data(DMR &dmr, bool print_rows = false)
163 {
164  cout << "The data:" << endl;
165 
166  D4Group *g = dmr.root();
167 
168  print_group_data(g, print_rows);
169 
170  cout << endl << flush;
171 }
172 
173 int main(int argc, char *argv[])
174 {
175  GetOpt getopt(argc, argv, "[dDvVikrm:Mzstc:]");
176  int option_char;
177 
178  bool get_dmr = false;
179  bool get_dap4_data = false;
180  bool get_version = false;
181  bool cexpr = false;
182  bool verbose = false;
183  bool multi = false;
184  bool accept_deflate = false;
185  bool print_rows = false;
186  bool mime_headers = true;
187  bool report_errors = false;
188  int times = 1;
189  int dap_client_major = 4;
190  int dap_client_minor = 0;
191  string expr = "";
192 
193 #ifdef WIN32
194  _setmode(_fileno(stdout), _O_BINARY);
195 #endif
196 
197  while ((option_char = getopt()) != -1)
198  switch (option_char) {
199  case 'd':
200  get_dmr = true;
201  break;
202  case 'D':
203  get_dap4_data = true;
204  break;
205  case 'v':
206  verbose = true;
207  break;
208  case 'V':
209  cerr << "getdap4 version: " << version << endl;
210  exit(0);
211  case 'i':
212  get_version = true;
213  break;
214 #if 0
215  case 'k':
216  dods_keep_temps = 1;
217  break; // keep_temp is in Connect.cc
218 #endif
219  case 'r':
220  report_errors = true;
221  break;
222  case 'm':
223  multi = true;
224  times = atoi(getopt.optarg);
225  break;
226  case 'z':
227  accept_deflate = true;
228  break;
229  case 's':
230  print_rows = true;
231  break;
232  case 'M':
233  mime_headers = false;
234  break;
235 #if 0
236  case 't':
237  www_trace = 1;
238  break;
239 #endif
240  case 'c':
241  cexpr = true;
242  expr = getopt.optarg;
243  break;
244  case 'h':
245  case '?':
246  default:
247  usage(argv[0]);
248  exit(1);
249  }
250 
251  try {
252  // If after processing all the command line options there is nothing
253  // left (no URL or file) assume that we should read from stdin.
254  for (int i = getopt.optind; i < argc; ++i) {
255  if (verbose)
256  cerr << "Fetching: " << argv[i] << endl;
257 
258  string name = argv[i];
259  D4Connect *url = 0;
260  // auto_ptr? jhrg 10/19/15
261  url = new D4Connect(name);
262 
263  // This overrides the value set in the .dodsrc file.
264  if (accept_deflate)
265  url->set_accept_deflate(accept_deflate);
266 
267  if (dap_client_major > 2)
268  url->set_xdap_protocol(dap_client_major, dap_client_minor);
269 
270  if (url->is_local()) {
271  if (verbose)
272  cerr << "Assuming " << argv[i] << " is a file that contains a response object; decoding." << endl;
273 
274  try {
275  D4BaseTypeFactory factory;
276  DMR dmr(&factory);
277 
278  if (strcmp(argv[i], "-") == 0) {
279  StdinResponse r(cin);
280 
281  if (!r.get_cpp_stream())
282  throw Error("Could not open standard input.");
283 
284  read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
285  }
286  else {
287  fstream f(argv[i], std::ios_base::in);
288  if (!f.is_open() || f.bad() || f.eof())
289  throw Error((string)"Could not open: " + argv[i]);
290 
291  Response r(&f, 0);
292 
293  read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
294  }
295 
296  if (verbose)
297  cerr << "DAP version: " << url->get_protocol().c_str() << " Server version: "
298  << url->get_version().c_str() << endl;
299 
300  // Always write the DMR
301  XMLWriter xml;
302  dmr.print_dap4(xml);
303  cout << xml.get_doc() << endl;
304 
305  if (get_dap4_data)
306  print_data(dmr, print_rows);
307  }
308  catch (Error & e) {
309  cerr << "Error: " << e.get_error_message() << endl;
310  delete url; url = 0;
311  if (report_errors)
312  return EXIT_FAILURE;
313  }
314  }
315  else if (get_dmr) {
316  for (int j = 0; j < times; ++j) {
317  D4BaseTypeFactory factory;
318  DMR dmr(&factory);
319  try {
320  url->request_dmr(dmr, expr);
321 
322  if (verbose) {
323  cout << "DAP version: " << url->get_protocol() << ", Server version: " << url->get_version() << endl;
324  cout << "DMR:" << endl;
325  }
326 
327  XMLWriter xml;
328  dmr.print_dap4(xml);
329  cout << xml.get_doc() << endl;
330  }
331  catch (Error & e) {
332  cerr << e.get_error_message() << endl;
333  if (report_errors)
334  return EXIT_FAILURE;
335  continue; // Goto the next URL or exit the loop.
336  }
337  }
338  }
339  else if (get_dap4_data) {
340  for (int j = 0; j < times; ++j) {
341  D4BaseTypeFactory factory;
342  DMR dmr(&factory);
343  try {
344  url->request_dap4_data(dmr, expr);
345 
346  if (verbose) {
347  cout << "DAP version: " << url->get_protocol() << ", Server version: " << url->get_version() << endl;
348  cout << "DMR:" << endl;
349  }
350 
351  XMLWriter xml;
352  dmr.print_dap4(xml);
353  cout << xml.get_doc() << endl;
354 
355  print_data(dmr, print_rows);
356  }
357  catch (Error & e) {
358  cerr << e.get_error_message() << endl;
359  if (report_errors)
360  return EXIT_FAILURE;
361  continue; // Goto the next URL or exit the loop.
362  }
363  }
364  }
365  else {
366  HTTPConnect http(RCReader::instance());
367 
368  // This overrides the value set in the .dodsrc file.
369  if (accept_deflate)
370  http.set_accept_deflate(accept_deflate);
371 
372  if (dap_client_major > 2)
373  url->set_xdap_protocol(dap_client_major, dap_client_minor);
374 
375  string url_string = argv[i];
376  for (int j = 0; j < times; ++j) {
377  try {
378  HTTPResponse *r = http.fetch_url(url_string);
379  if (verbose) {
380  vector<string> *headers = r->get_headers();
381  copy(headers->begin(), headers->end(), ostream_iterator<string>(cout, "\n"));
382  }
383  if (!read_data(r->get_stream())) {
384  continue;
385  }
386  delete r;
387  r = 0;
388  }
389  catch (Error & e) {
390  cerr << e.get_error_message() << endl;
391  if (report_errors)
392  return EXIT_FAILURE;
393  continue;
394  }
395  }
396  }
397 
398 #if 0
399  else if (get_version) {
400  fprintf(stderr, "DAP version: %s, Server version: %s\n",
401  url->request_protocol().c_str(),
402  url->get_version().c_str());
403  }
404 #endif
405 
406  delete url; url = 0;
407  }
408  }
409  catch (Error &e) {
410 
411  if(e.get_error_code() == malformed_expr){
412  cerr << e.get_error_message() << endl;
413  usage(argv[0]);
414  }
415  else {
416  cerr << e.get_error_message() << endl;
417 
418  }
419 
420  cerr << "Exiting." << endl;
421  return 1;
422  }
423  catch (exception &e) {
424  cerr << "C++ library exception: " << e.what() << endl;
425  cerr << "Exiting." << endl;
426  return 1;
427  }
428 
429  return 0;
430 }
void print_dap4(XMLWriter &xml, bool constrained=false)
Definition: DMR.cc:437
D4Group * root()
Definition: DMR.cc:366
std::string get_version()
Definition: D4Connect.h:94
void set_xdap_protocol(int major, int minor)
Definition: D4Connect.cc:533
Definition: GetOpt.h:38
std::string get_protocol()
Definition: D4Connect.h:99
top level DAP object to house generic methods
Definition: AlarmHandler.h:35
void set_accept_deflate(bool deflate)
Definition: D4Connect.cc:523
ErrorCode get_error_code() const
Definition: Error.cc:246
groupsIter grp_end()
Get an iterator to the end of the values.
Definition: D4Group.h:114
std::string get_error_message() const
Definition: Error.cc:275
groupsIter grp_begin()
Get an iterator to the start of the values.
Definition: D4Group.h:111
Encapsulate a response read from stdin.
Definition: StdinResponse.h:44
Vars_iter var_begin()
Definition: Constructor.cc:356
Vars_iter var_end()
Definition: Constructor.cc:364
A class for error processing.
Definition: Error.h:92