62 void D4Connect::process_dmr(DMR &dmr, Response &rs)
64 DBG(cerr <<
"Entering D4Connect::process_dmr" << endl);
66 dmr.set_dap_version(rs.get_protocol());
68 DBG(cerr <<
"Entering process_data: d_stream = " << rs << endl);
69 switch (rs.get_type()) {
73 if (!e.parse(rs.get_stream()))
74 throw InternalErr(__FILE__, __LINE__,
"Could not parse the Error object returned by the server!");
77 throw InternalErr(__FILE__, __LINE__,
"DAP4 errors not processed yet: FIXME!");
83 throw InternalErr(__FILE__, __LINE__,
84 "An error was reported by the remote httpd; this should have been processed by HTTPConnect..");
90 parser.intern(*rs.get_cpp_stream(), &dmr,
false);
93 cerr <<
"Exception: " << e.get_error_message() << endl;
96 catch (std::exception &e) {
97 cerr <<
"Exception: " << e.what() << endl;
101 cerr <<
"Exception: unknown error" << endl;
109 throw Error(
"Unknown response type");
115 void D4Connect::process_data(DMR &data, Response &rs)
117 DBG(cerr <<
"Entering D4Connect::process_data" << endl);
119 assert(rs.get_cpp_stream());
121 data.set_dap_version(rs.get_protocol());
123 DBG(cerr <<
"Entering process_data: d_stream = " << rs << endl);
124 switch (rs.get_type()) {
128 if (!e.parse(rs.get_cpp_stream()))
129 throw InternalErr(__FILE__, __LINE__,
"Could not parse the Error object returned by the server!");
132 throw InternalErr(__FILE__, __LINE__,
"DAP4 errors not processed yet: FIXME!");
138 throw InternalErr(__FILE__, __LINE__,
139 "An error was reported by the remote httpd; this should have been processed by HTTPConnect..");
142 #if BYTE_ORDER_PREFIX
145 *rs.get_cpp_stream() >> byte_order;
149 #if BYTE_ORDER_PREFIX
150 chunked_istream cis(*rs.get_cpp_stream(), 1024, byte_order);
152 chunked_istream cis(*(rs.get_cpp_stream()),
CHUNK_SIZE);
158 int chunk_size = cis.read_next_chunk();
160 char chunk[chunk_size];
161 cis.read(chunk, chunk_size);
165 parser.intern(chunk, chunk_size - 2, &data,
false);
168 cerr <<
"Exception: " << e.get_error_message() << endl;
171 catch (std::exception &e) {
172 cerr <<
"Exception: " << e.what() << endl;
176 cerr <<
"Exception: unknown error" << endl;
180 #if BYTE_ORDER_PREFIX
181 D4StreamUnMarshaller um(cis, byte_order);
183 D4StreamUnMarshaller um(cis, cis.twiddle_bytes());
185 data.root()->deserialize(um, data);
191 throw Error(
"Unknown response type");
203 void D4Connect::parse_mime(Response &rs)
205 rs.set_version(
"dods/0.0");
206 rs.set_protocol(
"2.0");
208 istream &data_source = *rs.get_cpp_stream();
210 while (!mime.empty()) {
211 string header, value;
215 if (header ==
"content-description") {
216 DBG(cout << header <<
": " << value << endl);
220 else if (header ==
"xdods-server" && rs.get_version() ==
"dods/0.0") {
221 DBG(cout << header <<
": " << value << endl);
222 rs.set_version(value);
225 else if (header ==
"xopendap-server") {
226 DBG(cout << header <<
": " << value << endl);
227 rs.set_version(value);
229 else if (header ==
"xdap") {
230 DBG(cout << header <<
": " << value << endl);
231 rs.set_protocol(value);
234 else if (rs.get_version() ==
"dods/0.0" && header ==
"server") {
235 DBG(cout << header <<
": " << value << endl);
236 rs.set_version(value);
251 D4Connect::D4Connect(
const string &url,
string uname,
string password) :
252 d_http(0), d_local(false), d_URL(
""), d_dap4ce(
""), d_server(
"unknown"), d_protocol(
"4.0")
258 if (name.find(
"http") == 0) {
259 DBG(cerr <<
"Connect: The identifier is an http URL" << endl);
261 d_http->set_use_cpp_streams(
true);
264 string::size_type dotpos = name.find(
'?');
265 if (dotpos != name.npos) {
266 d_URL = name.substr(0, dotpos);
267 d_dap4ce = name.substr(dotpos + 1);
274 DBG(cerr <<
"Connect: The identifier is a local data source." << endl);
278 set_credentials(uname, password);
283 if (d_http)
delete d_http;
288 string url = d_URL +
".dmr" +
"?" +
id2www_ce(d_dap4ce + expr);
299 cerr <<
"Response type unknown, assuming it's a DMR response." << endl;
308 throw InternalErr(__FILE__, __LINE__,
"DAP4 errors are not processed yet.");
313 throw InternalErr(__FILE__, __LINE__,
"Web error found where it should never be.");
317 throw InternalErr(__FILE__, __LINE__,
"Response type not handled (got "
331 string url = d_URL +
".dap" +
"?" +
id2www_ce(d_dap4ce + expr);
342 cerr <<
"Response type unknown, assuming it's a DAP4 Data response." << endl;
346 #if BYTE_ORDER_PREFIX
354 #if BYTE_ORDER_PREFIX
366 char chunk[chunk_size];
367 cis.read(chunk, chunk_size);
371 parser.
intern(chunk, chunk_size - 2, &dmr,
false );
374 #if BYTE_ORDER_PREFIX
385 throw InternalErr(__FILE__, __LINE__,
"DAP4 errors are not processed yet.");
390 throw InternalErr(__FILE__, __LINE__,
"Web error found where it should never be.");
394 throw InternalErr(__FILE__, __LINE__,
"Response type not handled (got "
414 string D4D4Connect::request_version()
416 string version_url = _URL +
".ver";
417 if (_proj.length() + _sel.length())
418 version_url = version_url +
"?" +
id2www_ce(_proj + _sel);
422 rs = d_http->fetch_url(version_url);
429 d_server = rs.get_server();
448 string D4D4Connect::request_protocol()
450 string version_url = _URL +
".ver";
451 if (_proj.length() + _sel.length())
452 version_url = version_url +
"?" +
id2www_ce(_proj + _sel);
456 rs = d_http->fetch_url(version_url);
465 d_protocol = rs.get_protocol();
480 void D4Connect::request_das(DAS &das)
482 string das_url = _URL +
".das";
483 if (_proj.length() + _sel.length())
484 das_url = das_url +
"?" +
id2www_ce(_proj + _sel);
497 d_protocol = rs.get_protocol();
499 switch (rs.get_type()) {
502 if (!e.parse(rs.get_stream())) {
505 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
521 das.parse(rs.get_stream());
546 void D4Connect::request_das_url(DAS &das)
548 string use_url = _URL +
"?" + _proj + _sel;
560 d_protocol = rs.get_protocol();
562 switch (rs.get_type()) {
565 if (!e.parse(rs.get_stream())) {
568 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
584 das.parse(rs.get_stream());
612 void D4Connect::request_dds(DDS &dds,
string expr)
615 string::size_type dotpos = expr.find(
'&');
616 if (dotpos != expr.npos) {
617 proj = expr.substr(0, dotpos);
618 sel = expr.substr(dotpos);
625 string dds_url = _URL +
".dds" +
"?" +
id2www_ce(_proj + proj + _sel + sel);
638 d_protocol = rs.get_protocol();
640 switch (rs.get_type()) {
643 if (!e.parse(rs.get_stream())) {
646 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
662 dds.parse(rs.get_stream());
692 void D4Connect::request_dds_url(DDS &dds)
694 string use_url = _URL +
"?" + _proj + _sel;
706 d_protocol = rs.get_protocol();
708 switch (rs.get_type()) {
711 if (!e.parse(rs.get_stream())) {
714 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
730 dds.parse(rs.get_stream());
755 void D4Connect::request_ddx(DDS &dds,
string expr)
758 string::size_type dotpos = expr.find(
'&');
759 if (dotpos != expr.npos) {
760 proj = expr.substr(0, dotpos);
761 sel = expr.substr(dotpos);
768 string ddx_url = _URL +
".ddx" +
"?" +
id2www_ce(_proj + proj + _sel + sel);
781 d_protocol = rs.get_protocol();
783 switch (rs.get_type()) {
786 if (!e.parse(rs.get_stream())) {
789 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
806 DDXParser ddxp(dds.get_factory());
807 ddxp.intern_stream(rs.get_stream(), &dds, blob);
820 "The site did not return a valid response (it lacked the\n\
821 expected content description header value of 'dap4-ddx' and\n\
825 This may indicate that the server at the site is not correctly\n\
826 configured, or that the URL has changed.");
835 void D4Connect::request_ddx_url(DDS &dds)
837 string use_url = _URL +
"?" + _proj + _sel;
850 d_protocol = rs.get_protocol();
852 switch (rs.get_type()) {
855 if (!e.parse(rs.get_stream())) {
858 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
875 DDXParser ddxp(dds.get_factory());
876 ddxp.intern_stream(rs.get_stream(), &dds, blob);
889 "The site did not return a valid response (it lacked the\n\
890 expected content description header value of 'dap4-ddx' and\n\
894 This may indicate that the server at the site is not correctly\n\
895 configured, or that the URL has changed.");
917 void D4Connect::request_data(DataDDS &data,
string expr)
920 string::size_type dotpos = expr.find(
'&');
921 if (dotpos != expr.npos) {
922 proj = expr.substr(0, dotpos);
923 sel = expr.substr(dotpos);
930 string data_url = _URL +
".dods?" +
id2www_ce(_proj + proj + _sel + sel);
938 d_protocol = rs.get_protocol();
940 process_data(data, rs);
968 void D4Connect::request_data_url(DataDDS &data)
970 string use_url = _URL +
"?" + _proj + _sel;
977 d_protocol = rs.get_protocol();
979 process_data(data, rs);
990 void D4Connect::request_data_ddx(DataDDS &data,
string expr)
993 string::size_type dotpos = expr.find(
'&');
994 if (dotpos != expr.npos) {
995 proj = expr.substr(0, dotpos);
996 sel = expr.substr(dotpos);
1003 string data_url = _URL +
".dap?" +
id2www_ce(_proj + proj + _sel + sel);
1011 d_protocol = rs.get_protocol();
1013 process_data(data, rs);
1024 void D4Connect::request_data_ddx_url(DataDDS &data)
1026 string use_url = _URL +
"?" + _proj + _sel;
1033 d_protocol = rs.get_protocol();
1035 process_data(data, rs);
1052 throw Error(
"Unknown response type.");
1066 process_dmr(dmr, rs);
1071 throw Error(
"Expected a DAP4 DMR response.");
1080 throw Error(
"Unknown response type.");
1093 process_data(data, rs);
1098 throw Error(
"Expected a DAP4 Data response.");
string dap_version() const
string get_next_mime_header(FILE *in)
void set_credentials(std::string u, std::string p)
Set the credentials for responding to challenges while dereferencing URLs.
void intern(istream &f, DMR *dest_dmr, bool debug=false)
string id2www_ce(string in, const string &allowable)
string prune_spaces(const string &name)
void set_credentials(const string &u, const string &p)
Read data from the stream made by D4StreamMarshaller.
virtual void set_type(ObjectType o)
void set_xdap_protocol(int major, int minor)
virtual std::istream * get_cpp_stream() const
void set_cache_enabled(bool enabled)
virtual std::string get_version() const
virtual ObjectType get_type() const
HTTPResponse * fetch_url(const string &url)
A class for software fault reporting.
void parse_mime_header(const string &header, string &name, string &value)
void set_accept_deflate(bool deflate)
ObjectType get_description_type(const string &value)
void set_cache_enabled(bool enabled)
virtual void request_dmr(DMR &dmr, const std::string expr="")
virtual void deserialize(D4StreamUnMarshaller &um, DMR &dmr)
virtual std::string get_protocol() const
virtual void read_dmr(DMR &dmr, Response &rs)
void set_accept_deflate(bool defalte)
string long_to_string(long val, int base)
virtual void request_dap4_data(DMR &dmr, const std::string expr="")
virtual void read_dmr_no_mime(DMR &dmr, Response &rs)
void set_xdap_protocol(int major, int minor)
virtual void read_data_no_mime(DMR &data, Response &rs)
bool twiddle_bytes() const
A class for error processing.
virtual void read_data(DMR &data, Response &rs)
static RCReader * instance()