File: getdap4.cc

package info (click to toggle)
libdap 3.20.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,116 kB
  • sloc: cpp: 52,168; sh: 40,006; xml: 23,505; ansic: 20,020; yacc: 2,452; exp: 1,544; makefile: 1,068; lex: 305; perl: 52; fortran: 8
file content (430 lines) | stat: -rw-r--r-- 14,589 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

// (c) COPYRIGHT URI/MIT 1997-1999
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
//      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>

// This is the source to `getdap'; a simple tool to exercise the Connect
// class. It can be used to get naked URLs as well as the DAP2 DAS and DDS
// objects.  jhrg.

#include "config.h"

#ifdef WIN32
#include <io.h>
#include <fcntl.h>
#endif

#include <cstring>
#include <string>
#include <sstream>

#include "GetOpt.h"

#include "DMR.h"
#include "XMLWriter.h"
#include "D4BaseTypeFactory.h"
#include "D4Group.h"
#include "D4Sequence.h"
#include "D4Connect.h"
#include "StdinResponse.h"
#include "HTTPConnect.h"
#include "RCReader.h"

using namespace std;
using namespace libdap ;

const char *version = CVER " (" DVR " DAP/" DAP_PROTOCOL_VERSION ")";
#if 0
extern int libdap::dods_keep_temps;     // defined in HTTPResponse.h
extern int libdap::www_trace;
#endif
static void usage(const string &name)
{
	cerr << "Usage: " << name << endl;
	cerr << " [dD vVikmzstM][-c <expr>][-m <num>] <url> [<url> ...] | <file> [<file> ...]" << endl;
	cerr << endl;
	cerr << "In the first form of the command, dereference the URL and" << endl;
	cerr << "perform the requested operations. This includes routing" << endl;
	cerr << "the returned information through the DAP processing" << endl;
	cerr << "library (parsing the returned objects, et c.). If none" << endl;
	cerr << "of a, d, or D are used with a URL, then the DAP library" << endl;
	cerr << "routines are NOT used and the URLs contents are dumped" << endl;
	cerr << "to standard output." << endl;
	cerr << "Note: If the URL contains a query string the query string" << endl;
	cerr << "will be preserved in the request. However, if the query " << endl;
	cerr << "string contains DAP4 keys they may interfere with the" << endl;
	cerr << "operation of " << name << ". A warning will be" << endl;
	cerr << "written to stderr when "<< name << " identifies" << endl;
	cerr << "the presence of a DAP4 query key in the submitted" << endl;
	cerr << "URL's query string." << endl;
	cerr << endl;
	cerr << "In the second form of the command, assume the files are" << endl;
	cerr << "DataDDS objects (stored in files or read from pipes)" << endl;
	cerr << "and process them as if -D were given. In this case the" << endl;
	cerr << "information *must* contain valid MIME header in order" << endl;
	cerr << "to be processed." << endl;
	cerr << endl;
	cerr << "Options:" << endl;
	cerr << "        d: For each URL, get the (DAP4) DMR object. Does not get data." << endl;
	cerr << "        D: For each URL, get the DAP4 Data response." << endl;
	cerr << endl;
	cerr << "        v: Verbose output." << endl;
	cerr << "        V: Version of this client; see 'i' for server version." << endl;
	cerr << "        i: For each URL, get the server version." << endl;
	cerr << "        k: Keep temporary files created by libdap." << endl;
	cerr << "        m: Request the same URL <num> times." << endl;
	cerr << "        z: Ask the server to compress data." << endl;
	cerr << "        s: Print Sequences using numbered rows." << endl;
	cerr << "        t: Trace www accesses." << endl;
	cerr << "        M: Assume data read from a file has no MIME headers; use only with files" << endl;
	cerr << endl;
	cerr << "        c: <expr> is a constraint expression. Used with -d/D" << endl;
	cerr << "           NB: You can use a `?' for the CE also." << endl;
}

// Used for raw http access/transfer
bool read_data(FILE * fp)
{
    if (!fp) {
        fprintf(stderr, "getdap4: Whoa!!! Null stream pointer.\n");
        return false;
    }
    // Changed from a loop that used getc() to one that uses fread(). getc()
    // worked fine for transfers of text information, but *not* for binary
    // transfers. fread() will handle both.
    char c = 0;
    while (fp && !feof(fp) && fread(&c, 1, 1, fp))
        printf("%c", c);        // stick with stdio

    return true;
}

static void read_response_from_file(D4Connect *url, DMR &dmr, Response &r, bool mime_headers, bool get_dap4_data, bool get_dmr)
{
    if (mime_headers) {
    	if (get_dap4_data)
    		url->read_data(dmr, r);
    	else if (get_dmr)
    		url->read_dmr(dmr, r);
    	else
    		throw Error("Only supports Data or DMR responses");
    }
    else {
    	if (get_dap4_data)
    		url->read_data_no_mime(dmr, r);
    	else if (get_dmr)
    		url->read_dmr_no_mime(dmr, r);
    	else
    		throw Error("Only supports Data or DMR responses");
    }
}

static void print_group_data(D4Group *g, bool print_rows = false)
{
    for (Constructor::Vars_iter i = g->var_begin(), e = g->var_end(); i != e; i++) {
        if (print_rows && (*i)->type() == dods_sequence_c)
            dynamic_cast<D4Sequence &>(**i).print_val_by_rows(cout);
        else
            (*i)->print_val(cout);
    }

    for (D4Group::groupsIter gi = g->grp_begin(), ge = g->grp_end(); gi != ge; ++gi) {
        print_group_data(*gi, print_rows);
    }
}

static void print_data(DMR &dmr, bool print_rows = false)
{
    cout << "The data:" << endl;

    D4Group *g = dmr.root();

    print_group_data(g, print_rows);

    cout << endl << flush;
}

int main(int argc, char *argv[])
{
    GetOpt getopt(argc, argv, "[dDvVikrm:Mzstc:]");
    int option_char;

    bool get_dmr = false;
    bool get_dap4_data = false;
    bool get_version = false;
    bool cexpr = false;
    bool verbose = false;
    bool multi = false;
    bool accept_deflate = false;
    bool print_rows = false;
    bool mime_headers = true;
    bool report_errors = false;
    int times = 1;
    int dap_client_major = 4;
    int dap_client_minor = 0;
    string expr = "";

#ifdef WIN32
    _setmode(_fileno(stdout), _O_BINARY);
#endif

    while ((option_char = getopt()) != -1)
        switch (option_char) {
        case 'd':
            get_dmr = true;
            break;
        case 'D':
            get_dap4_data = true;
            break;
        case 'v':
            verbose = true;
            break;
        case 'V':
        	cerr << "getdap4 version: " << version << endl;
            exit(0);
        case 'i':
            get_version = true;
            break;
#if 0
        case 'k':
            dods_keep_temps = 1;
            break;              // keep_temp is in Connect.cc
#endif
        case 'r':
        	report_errors = true;
        	break;
        case 'm':
            multi = true;
            times = atoi(getopt.optarg);
            break;
        case 'z':
            accept_deflate = true;
            break;
        case 's':
            print_rows = true;
            break;
        case 'M':
            mime_headers = false;
            break;
#if 0
        case 't':
            www_trace = 1;
            break;
#endif
        case 'c':
            cexpr = true;
            expr = getopt.optarg;
            break;
        case 'h':
        case '?':
        default:
            usage(argv[0]);
            exit(1);
        }

    try {
        // If after processing all the command line options there is nothing
        // left (no URL or file) assume that we should read from stdin.
        for (int i = getopt.optind; i < argc; ++i) {
            if (verbose)
                cerr << "Fetching: " << argv[i] << endl;

            string name = argv[i];
            D4Connect *url = 0;
            // auto_ptr? jhrg 10/19/15
            url = new D4Connect(name);

            // This overrides the value set in the .dodsrc file.
            if (accept_deflate)
                url->set_accept_deflate(accept_deflate);

            if (dap_client_major > 2)
                url->set_xdap_protocol(dap_client_major, dap_client_minor);

            if (url->is_local()) {
                if (verbose)
                    cerr << "Assuming " << argv[i] << " is a file that contains a response object; decoding." << endl;

                try {
                    D4BaseTypeFactory factory;
                    DMR dmr(&factory);

                    if (strcmp(argv[i], "-") == 0) {
                        StdinResponse r(cin);

                        if (!r.get_cpp_stream())
                            throw Error("Could not open standard input.");

                        read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
                    }
                    else {
                    	fstream f(argv[i], std::ios_base::in);
                    	if (!f.is_open() || f.bad() || f.eof())
                    		throw Error((string)"Could not open: " + argv[i]);

                    	Response r(&f, 0);

                        read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
                    }

                    if (verbose)
                        cerr << "DAP version: " << url->get_protocol().c_str() << " Server version: "
    						<< url->get_version().c_str() << endl;

                    // Always write the DMR
                    XMLWriter xml;
                    dmr.print_dap4(xml);
                    cout << xml.get_doc() << endl;

                    if (get_dap4_data)
                    	print_data(dmr, print_rows);
                }
                catch (Error & e) {
                    cerr << "Error: " << e.get_error_message() << endl;
                    delete url; url = 0;
                    if (report_errors)
                        return EXIT_FAILURE;
                }
            }
            else if (get_dmr) {
                for (int j = 0; j < times; ++j) {
                    D4BaseTypeFactory factory;
                    DMR dmr(&factory);
                    try {
                        url->request_dmr(dmr, expr);

                        if (verbose) {
                            cout << "DAP version: " << url->get_protocol() << ", Server version: " << url->get_version() << endl;
                            cout << "DMR:" << endl;
                        }

                        XMLWriter xml;
                        dmr.print_dap4(xml);
                        cout << xml.get_doc() << endl;
                    }
                    catch (Error & e) {
                        cerr << e.get_error_message() << endl;
                        if (report_errors)
                        	return EXIT_FAILURE;
                        continue;       // Goto the next URL or exit the loop.
                    }
                }
            }
            else if (get_dap4_data) {
                 for (int j = 0; j < times; ++j) {
                     D4BaseTypeFactory factory;
                     DMR dmr(&factory);
                     try {
                         url->request_dap4_data(dmr, expr);

                         if (verbose) {
                             cout << "DAP version: " << url->get_protocol() << ", Server version: " << url->get_version() << endl;
                             cout << "DMR:" << endl;
                         }

                         XMLWriter xml;
                         dmr.print_dap4(xml);
                         cout << xml.get_doc() << endl;

                         print_data(dmr, print_rows);
                    }
                     catch (Error & e) {
                         cerr << e.get_error_message() << endl;
                         if (report_errors)
                             return EXIT_FAILURE;
                         continue;       // Goto the next URL or exit the loop.
                     }
                 }
            }
            else {
                HTTPConnect http(RCReader::instance());

                // This overrides the value set in the .dodsrc file.
                if (accept_deflate)
                    http.set_accept_deflate(accept_deflate);

                if (dap_client_major > 2)
                    url->set_xdap_protocol(dap_client_major, dap_client_minor);

                string url_string = argv[i];
                for (int j = 0; j < times; ++j) {
                    try {
                        HTTPResponse *r = http.fetch_url(url_string);
                        if (verbose) {
                        	vector<string> *headers = r->get_headers();
                        	copy(headers->begin(), headers->end(), ostream_iterator<string>(cout, "\n"));
                        }
                        if (!read_data(r->get_stream())) {
                            continue;
                        }
                        delete r;
                        r = 0;
                    }
                    catch (Error & e) {
                        cerr << e.get_error_message() << endl;
                        if (report_errors)
                            return EXIT_FAILURE;
                        continue;
                    }
                }
            }

#if 0
            else if (get_version) {
                fprintf(stderr, "DAP version: %s, Server version: %s\n",
                        url->request_protocol().c_str(),
                        url->get_version().c_str());
            }
#endif

            delete url;  url = 0;
        }
    }
    catch (Error &e) {

    	if(e.get_error_code() == malformed_expr){
        	cerr << e.get_error_message() << endl;
        	usage(argv[0]);
    	}
    	else {
        	cerr << e.get_error_message() << endl;

    	}

        cerr << "Exiting." << endl;
        return 1;
    }
    catch (exception &e) {
        cerr << "C++ library exception: " << e.what() << endl;
        cerr << "Exiting." << endl;
        return 1;
    }

    return 0;
}