libdap Updated for version 3.21.0
libdap4 is an implementation of OPeNDAP's DAP protocol.
getdap.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 "Sequence.h"
52#include "Connect.h"
53#include "Response.h"
54#include "StdinResponse.h"
55
56using std::cerr;
57using std::endl;
58using std::flush;
59
60using namespace libdap ;
61
62const char *version = CVER " (" DVR " DAP/" DAP_PROTOCOL_VERSION ")";
63
64extern int libdap::dods_keep_temps; // defined in HTTPResponse.h
65extern int libdap::www_trace;
66extern int libdap::www_trace_extensive;
67
68void usage(string name)
69{
70 cerr << "Usage: " << name << endl;
71 cerr << " [idaDxBzp vVkms][-c <expr>][-m <num>] <url> [<url> ...]" << endl;
72 cerr << " [M vVkms] <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 << endl;
82 cerr << "In the second form of the command, assume the files are" << endl;
83 cerr << "DataDDS objects (stored in files or read from pipes)" << endl;
84 cerr << "and process them as if -D were given. In this case the" << endl;
85 cerr << "information *must* contain valid MIME header in order" << endl;
86 cerr << "to be processed." << endl;
87 cerr << endl;
88 cerr << "Options:" << endl;
89 cerr << " i: For each URL, get the server version." << endl;
90 cerr << " d: For each URL, get the the DDS." << endl;
91 cerr << " a: For each URL, get the the DAS." << endl;
92 cerr << " D: For each URL, get the the DataDDS." << endl;
93 cerr << " x: For each URL, get the (DAP2) DDX object. Does not get data." << endl;
94 cerr << " B: Build a DDX in getdap using the DDS and DAS." << endl;
95 cerr << " v: Verbose output." << endl;
96 cerr << " e: Extensive (or very) verbose." << endl;
97 cerr << " V: Version of this client; see 'i' for server version." << endl;
98 cerr << " c: <expr> is a constraint expression. Used with -D/X and -d/r" << endl;
99 cerr << " NB: You can use a `?' for the CE also." << 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 << " M: Assume data read from a file has no MIME headers" << endl;
105 cerr << " (the default is to assume the headers are present)." << endl;
106 cerr << " p: Set DAP protocol to x.y" << endl;
107}
108
109bool read_data(FILE * fp)
110{
111 if (!fp) {
112 fprintf(stderr, "getdap: Whoa!!! Null stream pointer.\n");
113 return false;
114 }
115 // Changed from a loop that used getc() to one that uses fread(). getc()
116 // worked fine for transfers of text information, but *not* for binary
117 // transfers. fread() will handle both.
118 char c = 0;
119 while (fp && !feof(fp) && fread(&c, 1, 1, fp))
120 printf("%c", c); // stick with stdio
121
122 return true;
123}
124
125static void print_data(DDS & dds, bool print_rows = false)
126{
127 cout << "The data:" << endl;
128
129 for (DDS::Vars_iter i = dds.var_begin(); i != dds.var_end(); i++) {
130 BaseType *v = *i;
131 if (print_rows && (*i)->type() == dods_sequence_c)
132 dynamic_cast < Sequence * >(*i)->print_val_by_rows(cout);
133 else
134 v->print_val(cout);
135 }
136
137 cout << endl << flush;
138}
139
140int main(int argc, char *argv[])
141{
142 GetOpt getopt(argc, argv, "idaDxrXBVvekc:m:zshM?Hp:t");
143 int option_char;
144
145 bool get_das = false;
146 bool get_dds = false;
147 bool get_data = false;
148 bool get_ddx = false;
149 bool build_ddx = false;
150 bool get_version = false;
151 bool verbose = false;
152 bool accept_deflate = false;
153 bool print_rows = false;
154 bool mime_headers = true;
155 int times = 1;
156 int dap_client_major = 2;
157 int dap_client_minor = 0;
158 string expr;
159
160#ifdef WIN32
161 _setmode(_fileno(stdout), _O_BINARY);
162#endif
163
164 while ((option_char = getopt()) != -1)
165 switch (option_char) {
166 case 'd':
167 get_dds = true;
168 break;
169 case 'a':
170 get_das = true;
171 break;
172 case 'D':
173 get_data = true;
174 break;
175 case 'x':
176 get_ddx = true;
177 break;
178 case 'V':
179 fprintf(stderr, "getdap version: %s\n", version);
180 exit(0);
181 case 'i':
182 get_version = true;
183 break;
184 case 'v':
185 verbose = true;
186 www_trace = 1;
187 break;
188 case 'e':
189 verbose = true;
190 www_trace = 1;
191 www_trace_extensive = 1;
192 break;
193 case 'k':
194 dods_keep_temps = 1;
195 break; // keep_temp is in Connect.cc
196 case 'c':
197 expr = getopt.optarg;
198 break;
199 case 'm':
200 times = atoi(getopt.optarg);
201 break;
202 case 'B':
203 build_ddx = true;
204 break;
205 case 'z':
206 accept_deflate = true;
207 break;
208 case 's':
209 print_rows = true;
210 break;
211 case 'M':
212 mime_headers = false;
213 break;
214 case 'p': {
215 istringstream iss(getopt.optarg);
216 char dot;
217 iss >> dap_client_major;
218 iss >> dot;
219 iss >> dap_client_minor;
220 break;
221 }
222 case 't':
223 www_trace = 1;
224 break;
225 case 'h':
226 case '?':
227 default:
228 usage(argv[0]);
229 exit(1);
230 }
231
232 try {
233 // If after processing all the command line options there is nothing
234 // left (no URL or file) assume that we should read from stdin.
235 for (int i = getopt.optind; i < argc; ++i) {
236 if (verbose)
237 fprintf(stderr, "Fetching: %s\n", argv[i]);
238
239 string name = argv[i];
240 Connect *url = 0;
241
242 url = new Connect(name);
243
244 // This overrides the value set in the .dodsrc file.
245 if (accept_deflate)
246 url->set_accept_deflate(accept_deflate);
247
248 if (dap_client_major > 2)
249 url->set_xdap_protocol(dap_client_major, dap_client_minor);
250
251 if (url->is_local()) {
252 if (verbose) {
253 fprintf(stderr, "Assuming that the argument %s is a file that contains a response object; decoding.\n", argv[i]);
254 }
255
256 Response *r = 0;
257
258 BaseTypeFactory factory;
259 DataDDS dds(&factory);
260
261 try {
262 if (strcmp(argv[i], "-") == 0) {
263 r = new StdinResponse(stdin);
264
265 if (!r->get_stream())
266 throw Error("Could not open standard input.");
267
268 if (mime_headers)
269 url->read_data(dds, r); // The default case
270 else
271 url->read_data_no_mime(dds, r);
272 }
273 else {
274 r = new Response(fopen(argv[i], "r"), 0);
275
276 if (!r->get_stream())
277 throw Error(string("The input source: ")
278 + string(argv[i])
279 + string(" could not be opened"));
280
281 url->read_data_no_mime(dds, r);
282 }
283 }
284 catch (Error & e) {
285 cerr << e.get_error_message() << endl;
286 delete r;
287 r = 0;
288 delete url;
289 url = 0;
290 break;
291 }
292
293 if (verbose)
294 fprintf(stderr, "DAP version: %s, Server version: %s\n",
295 url->get_protocol().c_str(),
296 url->get_version().c_str());
297
298 print_data(dds, print_rows);
299
300 }
301
302 else if (get_version) {
303 fprintf(stderr, "DAP version: %s, Server version: %s\n",
304 url->request_protocol().c_str(),
305 url->get_version().c_str());
306 }
307
308 else if (get_das) {
309 for (int j = 0; j < times; ++j) {
310 DAS das;
311 try {
312 url->request_das(das);
313 }
314 catch (Error & e) {
315 cerr << e.get_error_message() << endl;
316 delete url;
317 url = 0;
318 continue;
319 }
320
321 if (verbose) {
322 fprintf(stderr, "DAP version: %s, Server version: %s\n",
323 url->get_protocol().c_str(),
324 url->get_version().c_str());
325
326 fprintf(stderr, "DAS:\n");
327 }
328
329 das.print(stdout);
330 }
331 }
332
333 else if (get_dds) {
334 for (int j = 0; j < times; ++j) {
335 BaseTypeFactory factory;
336 DDS dds(&factory);
337 try {
338 url->request_dds(dds, expr);
339 }
340 catch (Error & e) {
341 cerr << e.get_error_message() << endl;
342 delete url;
343 url = 0;
344 continue; // Goto the next URL or exit the loop.
345 }
346
347 if (verbose) {
348 fprintf(stderr, "DAP version: %s, Server version: %s\n",
349 url->get_protocol().c_str(),
350 url->get_version().c_str());
351
352 fprintf(stderr, "DDS:\n");
353 }
354
355 dds.print(cout);
356 }
357 }
358
359 else if (get_ddx) {
360 for (int j = 0; j < times; ++j) {
361 BaseTypeFactory factory;
362 DDS dds(&factory);
363 try {
364 url->request_ddx(dds, expr);
365 }
366 catch (Error & e) {
367 cerr << e.get_error_message() << endl;
368 continue; // Goto the next URL or exit the loop.
369 }
370
371 if (verbose) {
372 fprintf(stderr, "DAP version: %s, Server version: %s\n",
373 url->get_protocol().c_str(),
374 url->get_version().c_str());
375
376 fprintf(stderr, "DDX:\n");
377 }
378
379 dds.print_xml(cout, false);
380 }
381 }
382
383 else if (build_ddx) {
384 for (int j = 0; j < times; ++j) {
385 BaseTypeFactory factory;
386 DDS dds(&factory);
387 try {
388 url->request_dds(dds, expr);
389 DAS das;
390 url->request_das(das);
391 dds.transfer_attributes(&das);
392 }
393 catch (Error & e) {
394 cerr << e.get_error_message() << endl;
395 continue; // Goto the next URL or exit the loop.
396 }
397
398 if (verbose) {
399 fprintf(stderr, "DAP version: %s, Server version: %s\n",
400 url->get_protocol().c_str(),
401 url->get_version().c_str());
402
403 fprintf(stderr, "Client-built DDX:\n");
404 }
405
406 dds.print_xml(cout, false);
407 }
408 }
409
410 else if (get_data) {
411 for (int j = 0; j < times; ++j) {
412 BaseTypeFactory factory;
413 DataDDS dds(&factory);
414 try {
415 DBG(cerr << "URL: " << url->URL(false) << endl);
416 DBG(cerr << "CE: " << expr << endl);
417 url->request_data(dds, expr);
418
419 if (verbose)
420 fprintf(stderr, "DAP version: %s, Server version: %s\n",
421 url->get_protocol().c_str(),
422 url->get_version().c_str());
423
424 print_data(dds, print_rows);
425 }
426 catch (Error & e) {
427 cerr << e.get_error_message() << endl;
428 delete url;
429 url = 0;
430 continue;
431 }
432 }
433 }
434 else {
435 // if (!get_das && !get_dds && !get_data ...) This code uses
436 // HTTPConnect::fetch_url which cannot be accessed using an
437 // instance of Connect. So some of the options supported by
438 // other URLs won't work here (e.g., the verbose option
439 // doesn't show the server version number).
440 HTTPConnect http(RCReader::instance());
441
442 // This overrides the value set in the .dodsrc file.
443 if (accept_deflate)
444 http.set_accept_deflate(accept_deflate);
445
446 if (dap_client_major > 2)
447 url->set_xdap_protocol(dap_client_major, dap_client_minor);
448
449 string url_string = argv[i];
450 for (int j = 0; j < times; ++j) {
451 try {
452 Response *r = http.fetch_url(url_string);
453 if (!read_data(r->get_stream())) {
454 continue;
455 }
456 delete r;
457 r = 0;
458 }
459 catch (Error & e) {
460 cerr << e.get_error_message() << endl;
461 continue;
462 }
463 }
464 }
465
466 delete url;
467 url = 0;
468 }
469 }
470 catch (Error &e) {
471 cerr << e.get_error_message() << endl;
472 //return 1;
473 return EXIT_FAILURE;
474 }
475 catch (exception &e) {
476 cerr << "C++ library exception: " << e.what() << endl;
477 //return 1;
478 return EXIT_FAILURE;
479 }
480
481 //return 0;
482 return EXIT_SUCCESS;
483}
Definition: GetOpt.h:39
The basic data type for the DODS DAP types.
Definition: BaseType.h:120
virtual void print_val(FILE *out, string space="", bool print_decl_p=true)
Prints the value of the variable.
Definition: BaseType.cc:1096
Holds information about the link from a DAP2 client to a dataset.
Definition: Connect.h:130
void set_accept_deflate(bool deflate)
Definition: Connect.cc:1168
string get_version()
Definition: Connect.h:185
string get_protocol()
Definition: Connect.h:193
virtual void request_ddx(DDS &dds, string expr="")
Get the DDX from a server.
Definition: Connect.cc:705
virtual void read_data_no_mime(DataDDS &data, Response *rs)
Read data from a file which does not have response MIME headers. This method is a companion to read_d...
Definition: Connect.cc:1062
void set_xdap_protocol(int major, int minor)
Definition: Connect.cc:1179
virtual string URL(bool CE=true)
Get the object's URL.
Definition: Connect.cc:1127
virtual void request_data(DataDDS &data, string expr="")
Get the DAS from a server.
Definition: Connect.cc:845
virtual void request_das(DAS &das)
Get the DAS from a server.
Definition: Connect.cc:430
virtual string request_protocol()
Definition: Connect.cc:398
virtual void request_dds(DDS &dds, string expr="")
Get the DDS from a server.
Definition: Connect.cc:562
virtual void read_data(DataDDS &data, Response *rs)
Read data which is preceded by MIME headers. This method works for both data dds and data ddx respons...
Definition: Connect.cc:989
Hold attribute data for a DAP2 dataset.
Definition: DAS.h:122
virtual void print(FILE *out, bool dereference=false)
Definition: DAS.cc:329
virtual void transfer_attributes(DAS *das)
Definition: DDS.cc:246
void print(FILE *out)
Print the entire DDS to the specified file.
Definition: DDS.cc:925
void print_xml(FILE *out, bool constrained, const string &blob="")
Definition: DDS.cc:1250
Vars_iter var_begin()
Definition: DDS.h:344
Vars_iter var_end()
Return an iterator.
Definition: DDS.h:349
Holds a DAP2 DDS.
Definition: DataDDS.h:78
A class for error processing.
Definition: Error.h:94
std::string get_error_message() const
Definition: Error.cc:243
Holds a sequence.
Definition: Sequence.h:163
Encapsulate a response read from stdin.
Definition: StdinResponse.h:45
top level DAP object to house generic methods
Definition: AlarmHandler.h:36