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