52 #include "XMLWriter.h" 53 #include "D4BaseTypeFactory.h" 55 #include "D4Sequence.h" 56 #include "D4Connect.h" 57 #include "StdinResponse.h" 58 #include "HTTPConnect.h" 64 const char *version = CVER
" (" DVR
" DAP/" DAP_PROTOCOL_VERSION
")";
66 extern int libdap::dods_keep_temps;
67 extern int libdap::www_trace;
69 static void usage(
const string &name)
71 cerr <<
"Usage: " << name << endl;
72 cerr <<
" [dD vVikmzstM][-c <expr>][-m <num>] <url> [<url> ...] | <file> [<file> ...]" << 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;
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;
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;
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;
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;
114 bool read_data(FILE * fp)
117 fprintf(stderr,
"getdap4: Whoa!!! Null stream pointer.\n");
124 while (fp && !feof(fp) && fread(&c, 1, 1, fp))
130 static void read_response_from_file(
D4Connect *url,
DMR &dmr,
Response &r,
bool mime_headers,
bool get_dap4_data,
bool get_dmr)
134 url->read_data(dmr, r);
136 url->read_dmr(dmr, r);
138 throw Error(
"Only supports Data or DMR responses");
142 url->read_data_no_mime(dmr, r);
144 url->read_dmr_no_mime(dmr, r);
146 throw Error(
"Only supports Data or DMR responses");
150 static void print_group_data(
D4Group *g,
bool print_rows =
false)
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);
156 (*i)->print_val(cout);
159 for (D4Group::groupsIter gi = g->
grp_begin(), ge = g->
grp_end(); gi != ge; ++gi) {
160 print_group_data(*gi, print_rows);
164 static void print_data(
DMR &dmr,
bool print_rows =
false)
166 cout <<
"The data:" << endl;
170 print_group_data(g, print_rows);
172 cout << endl << flush;
175 int main(
int argc,
char *argv[])
177 GetOpt getopt(argc, argv,
"[dDvVikrm:Mzstc:]");
180 bool get_dmr =
false;
181 bool get_dap4_data =
false;
182 bool get_version =
false;
184 bool verbose =
false;
186 bool accept_deflate =
false;
187 bool print_rows =
false;
188 bool mime_headers =
true;
189 bool report_errors =
false;
191 int dap_client_major = 4;
192 int dap_client_minor = 0;
196 _setmode(_fileno(stdout), _O_BINARY);
199 while ((option_char = getopt()) != -1)
200 switch (option_char) {
205 get_dap4_data =
true;
211 cerr <<
"getdap4 version: " << version << endl;
222 report_errors =
true;
226 times = atoi(getopt.optarg);
229 accept_deflate =
true;
235 mime_headers =
false;
244 expr = getopt.optarg;
256 for (
int i = getopt.optind; i < argc; ++i) {
258 cerr <<
"Fetching: " << argv[i] << endl;
260 string name = argv[i];
269 if (dap_client_major > 2)
272 if (url->is_local()) {
274 cerr <<
"Assuming " << argv[i] <<
" is a file that contains a response object; decoding." << endl;
280 if (strcmp(argv[i],
"-") == 0) {
283 if (!r.get_cpp_stream())
284 throw Error(
"Could not open standard input.");
286 read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
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]);
295 read_response_from_file(url, dmr, r, mime_headers, get_dap4_data, get_dmr);
299 cerr <<
"DAP version: " << url->
get_protocol().c_str() <<
" Server version: " 305 cout << xml.get_doc() << endl;
308 print_data(dmr, print_rows);
318 for (
int j = 0; j < times; ++j) {
322 url->request_dmr(dmr, expr);
326 cout <<
"DMR:" << endl;
331 cout << xml.get_doc() << endl;
341 else if (get_dap4_data) {
342 for (
int j = 0; j < times; ++j) {
346 url->request_dap4_data(dmr, expr);
350 cout <<
"DMR:" << endl;
355 cout << xml.get_doc() << endl;
357 print_data(dmr, print_rows);
372 http.set_accept_deflate(accept_deflate);
374 if (dap_client_major > 2)
377 string url_string = argv[i];
378 for (
int j = 0; j < times; ++j) {
382 vector<string> *headers = r->get_headers();
383 copy(headers->begin(), headers->end(), ostream_iterator<string>(cout,
"\n"));
385 if (!read_data(r->get_stream())) {
401 else if (get_version) {
402 fprintf(stderr,
"DAP version: %s, Server version: %s\n",
403 url->request_protocol().c_str(),
422 cerr <<
"Exiting." << endl;
426 catch (exception &e) {
427 cerr <<
"C++ library exception: " << e.what() << endl;
428 cerr <<
"Exiting." << endl;
void print_dap4(XMLWriter &xml, bool constrained=false)
std::string get_version()
void set_xdap_protocol(int major, int minor)
std::string get_protocol()
top level DAP object to house generic methods
void set_accept_deflate(bool deflate)
ErrorCode get_error_code() const
groupsIter grp_end()
Get an iterator to the end of the values.
std::string get_error_message() const
groupsIter grp_begin()
Get an iterator to the start of the values.
Encapsulate a response read from stdin.
A class for error processing.