libdap  Updated for version 3.20.0
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;
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  break;
250  }
251 
252  try {
253  // If after processing all the command line options there is nothing
254  // left (no URL or file) assume that we should read from stdin.
255  for (int i = getopt.optind; i < argc; ++i) {
256  if (verbose)
257  cerr << "Fetching: " << argv[i] << endl;
258 
259  string name = argv[i];
260  D4Connect *url = 0;
261  // auto_ptr? jhrg 10/19/15
262  url = new D4Connect(name);
263 
264  // This overrides the value set in the .dodsrc file.
265  if (accept_deflate)
266  url->set_accept_deflate(accept_deflate);
267 
268  if (dap_client_major > 2)
269  url->set_xdap_protocol(dap_client_major, dap_client_minor);
270 
271  if (url->is_local()) {
272  if (verbose)
273  cerr << "Assuming " << argv[i] << " is a file that contains a response object; decoding." << endl;
274 
275  try {
276  D4BaseTypeFactory factory;
277  DMR dmr(&factory);
278 
279  if (strcmp(argv[i], "-") == 0) {
280  StdinResponse r(cin);
281 
282  if (!r.get_cpp_stream())
283  throw Error("Could not open standard input.");
284 
285  read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
286  }
287  else {
288  fstream f(argv[i], std::ios_base::in);
289  if (!f.is_open() || f.bad() || f.eof())
290  throw Error((string)"Could not open: " + argv[i]);
291 
292  Response r(&f, 0);
293 
294  read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
295  }
296 
297  if (verbose)
298  cerr << "DAP version: " << url->get_protocol().c_str() << " Server version: "
299  << url->get_version().c_str() << endl;
300 
301  // Always write the DMR
302  XMLWriter xml;
303  dmr.print_dap4(xml);
304  cout << xml.get_doc() << endl;
305 
306  if (get_dap4_data)
307  print_data(dmr, print_rows);
308  }
309  catch (Error & e) {
310  cerr << "Error: " << e.get_error_message() << endl;
311  delete url; url = 0;
312  if (report_errors)
313  return EXIT_FAILURE;
314  }
315  }
316  else if (get_dmr) {
317  for (int j = 0; j < times; ++j) {
318  D4BaseTypeFactory factory;
319  DMR dmr(&factory);
320  try {
321  url->request_dmr(dmr, expr);
322 
323  if (verbose) {
324  cout << "DAP version: " << url->get_protocol() << ", Server version: " << url->get_version() << endl;
325  cout << "DMR:" << endl;
326  }
327 
328  XMLWriter xml;
329  dmr.print_dap4(xml);
330  cout << xml.get_doc() << endl;
331  }
332  catch (Error & e) {
333  cerr << e.get_error_message() << endl;
334  if (report_errors)
335  return EXIT_FAILURE;
336  continue; // Goto the next URL or exit the loop.
337  }
338  }
339  }
340  else if (get_dap4_data) {
341  for (int j = 0; j < times; ++j) {
342  D4BaseTypeFactory factory;
343  DMR dmr(&factory);
344  try {
345  url->request_dap4_data(dmr, expr);
346 
347  if (verbose) {
348  cout << "DAP version: " << url->get_protocol() << ", Server version: " << url->get_version() << endl;
349  cout << "DMR:" << endl;
350  }
351 
352  XMLWriter xml;
353  dmr.print_dap4(xml);
354  cout << xml.get_doc() << endl;
355 
356  print_data(dmr, print_rows);
357  }
358  catch (Error & e) {
359  cerr << e.get_error_message() << endl;
360  if (report_errors)
361  return EXIT_FAILURE;
362  continue; // Goto the next URL or exit the loop.
363  }
364  }
365  }
366  else {
367  HTTPConnect http(RCReader::instance());
368 
369  // This overrides the value set in the .dodsrc file.
370  if (accept_deflate)
371  http.set_accept_deflate(accept_deflate);
372 
373  if (dap_client_major > 2)
374  url->set_xdap_protocol(dap_client_major, dap_client_minor);
375 
376  string url_string = argv[i];
377  for (int j = 0; j < times; ++j) {
378  try {
379  HTTPResponse *r = http.fetch_url(url_string);
380  if (verbose) {
381  vector<string> *headers = r->get_headers();
382  copy(headers->begin(), headers->end(), ostream_iterator<string>(cout, "\n"));
383  }
384  if (!read_data(r->get_stream())) {
385  continue;
386  }
387  delete r;
388  r = 0;
389  }
390  catch (Error & e) {
391  cerr << e.get_error_message() << endl;
392  if (report_errors)
393  return EXIT_FAILURE;
394  continue;
395  }
396  }
397  }
398 
399 #if 0
400  else if (get_version) {
401  fprintf(stderr, "DAP version: %s, Server version: %s\n",
402  url->request_protocol().c_str(),
403  url->get_version().c_str());
404  }
405 #endif
406 
407  delete url; url = 0;
408  }
409  }
410  catch (Error &e) {
411 
412  if(e.get_error_code() == malformed_expr || e.get_error_code() == malformed_expr){
413  cerr << e.get_error_message() << endl;
414  usage(argv[0]);
415  }
416  else {
417  cerr << e.get_error_message() << endl;
418 
419  }
420 
421  cerr << "Exiting." << endl;
422  return 1;
423  }
424  catch (exception &e) {
425  cerr << "C++ library exception: " << e.what() << endl;
426  cerr << "Exiting." << endl;
427  return 1;
428  }
429 
430  return 0;
431 }
void print_dap4(XMLWriter &xml, bool constrained=false)
Definition: DMR.cc:431
D4Group * root()
Definition: DMR.cc:360
std::string get_version()
Definition: D4Connect.h:94
void set_xdap_protocol(int major, int minor)
Definition: D4Connect.cc:535
STL namespace.
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:525
ErrorCode get_error_code() const
Definition: Error.cc:247
groupsIter grp_end()
Get an iterator to the end of the values.
Definition: D4Group.h:114
string get_error_message() const
Definition: Error.cc:276
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:90