50 #include "XMLWriter.h" 51 #include "D4BaseTypeFactory.h" 53 #include "D4Sequence.h" 54 #include "D4Connect.h" 55 #include "StdinResponse.h" 56 #include "HTTPConnect.h" 62 const char *version = CVER
" (" DVR
" DAP/" DAP_PROTOCOL_VERSION
")";
64 extern int libdap::dods_keep_temps;
65 extern int libdap::www_trace;
67 static void usage(
const string &name)
69 cerr <<
"Usage: " << name << endl;
70 cerr <<
" [dD vVikmzstM][-c <expr>][-m <num>] <url> [<url> ...] | <file> [<file> ...]" << 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;
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;
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;
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;
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;
112 bool read_data(FILE * fp)
115 fprintf(stderr,
"getdap4: Whoa!!! Null stream pointer.\n");
122 while (fp && !feof(fp) && fread(&c, 1, 1, fp))
128 static void read_response_from_file(
D4Connect *url,
DMR &dmr,
Response &r,
bool mime_headers,
bool get_dap4_data,
bool get_dmr)
132 url->read_data(dmr, r);
134 url->read_dmr(dmr, r);
136 throw Error(
"Only supports Data or DMR responses");
140 url->read_data_no_mime(dmr, r);
142 url->read_dmr_no_mime(dmr, r);
144 throw Error(
"Only supports Data or DMR responses");
148 static void print_group_data(
D4Group *g,
bool print_rows =
false)
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);
154 (*i)->print_val(cout);
157 for (D4Group::groupsIter gi = g->
grp_begin(), ge = g->
grp_end(); gi != ge; ++gi) {
158 print_group_data(*gi, print_rows);
162 static void print_data(
DMR &dmr,
bool print_rows =
false)
164 cout <<
"The data:" << endl;
168 print_group_data(g, print_rows);
170 cout << endl << flush;
173 int main(
int argc,
char *argv[])
175 GetOpt getopt(argc, argv,
"[dDvVikrm:Mzstc:]");
178 bool get_dmr =
false;
179 bool get_dap4_data =
false;
180 bool get_version =
false;
182 bool verbose =
false;
184 bool accept_deflate =
false;
185 bool print_rows =
false;
186 bool mime_headers =
true;
187 bool report_errors =
false;
189 int dap_client_major = 4;
190 int dap_client_minor = 0;
194 _setmode(_fileno(stdout), _O_BINARY);
197 while ((option_char = getopt()) != -1)
198 switch (option_char) {
203 get_dap4_data =
true;
209 cerr <<
"getdap4 version: " << version << endl;
220 report_errors =
true;
224 times = atoi(getopt.optarg);
227 accept_deflate =
true;
233 mime_headers =
false;
242 expr = getopt.optarg;
255 for (
int i = getopt.optind; i < argc; ++i) {
257 cerr <<
"Fetching: " << argv[i] << endl;
259 string name = argv[i];
268 if (dap_client_major > 2)
271 if (url->is_local()) {
273 cerr <<
"Assuming " << argv[i] <<
" is a file that contains a response object; decoding." << endl;
279 if (strcmp(argv[i],
"-") == 0) {
282 if (!r.get_cpp_stream())
283 throw Error(
"Could not open standard input.");
285 read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
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]);
294 read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
298 cerr <<
"DAP version: " << url->
get_protocol().c_str() <<
" Server version: " 304 cout << xml.get_doc() << endl;
307 print_data(dmr, print_rows);
317 for (
int j = 0; j < times; ++j) {
321 url->request_dmr(dmr, expr);
325 cout <<
"DMR:" << endl;
330 cout << xml.get_doc() << endl;
340 else if (get_dap4_data) {
341 for (
int j = 0; j < times; ++j) {
345 url->request_dap4_data(dmr, expr);
349 cout <<
"DMR:" << endl;
354 cout << xml.get_doc() << endl;
356 print_data(dmr, print_rows);
371 http.set_accept_deflate(accept_deflate);
373 if (dap_client_major > 2)
376 string url_string = argv[i];
377 for (
int j = 0; j < times; ++j) {
381 vector<string> *headers = r->get_headers();
382 copy(headers->begin(), headers->end(), ostream_iterator<string>(cout,
"\n"));
384 if (!read_data(r->get_stream())) {
400 else if (get_version) {
401 fprintf(stderr,
"DAP version: %s, Server version: %s\n",
402 url->request_protocol().c_str(),
421 cerr <<
"Exiting." << endl;
424 catch (exception &e) {
425 cerr <<
"C++ library exception: " << e.what() << endl;
426 cerr <<
"Exiting." << endl;
void print_dap4(XMLWriter &xml, bool constrained=false)
string get_error_message() const
std::string get_version()
void set_xdap_protocol(int major, int minor)
std::string get_protocol()
void set_accept_deflate(bool deflate)
groupsIter grp_end()
Get an iterator to the end of the values.
groupsIter grp_begin()
Get an iterator to the start of the values.
Encapsulate a response read from stdin.
ErrorCode get_error_code() const
A class for error processing.