38 #define FILE_UN_MARSHALLER 1 51 #include "DDXParserSAX2.h" 52 #if FILE_UN_MARSHALLER 53 #include "XDRFileUnMarshaller.h" 55 #include "fdiostream.h" 56 #include "XDRStreamUnMarshaller.h" 58 #include "mime_util.h" 70 void Connect::process_data(DataDDS &data, Response *rs)
72 DBG(cerr <<
"Entering Connect::process_data" << endl);
74 data.set_version(rs->get_version());
75 data.set_protocol(rs->get_protocol());
77 DBG(cerr <<
"Entering process_data: d_stream = " << rs << endl);
78 switch (rs->get_type()) {
81 if (!e.parse(rs->get_stream()))
82 throw InternalErr(__FILE__, __LINE__,
"Could not parse the Error object returned by the server!");
89 throw InternalErr(__FILE__, __LINE__,
90 "An error was reported by the remote httpd; this should have been processed by HTTPConnect..");
97 DDXParser ddx_parser(data.get_factory());
101 DBG(cerr <<
"MPM Boundary: " << boundary << endl);
107 ddx_parser.intern_stream(rs->get_stream(), &data, data_cid, boundary);
111 DBG(cerr <<
"Data CID: " << data_cid << endl);
118 #if FILE_UN_MARSHALLER 119 XDRFileUnMarshaller um(rs->get_stream());
121 fpistream in ( rs->get_stream() );
122 XDRStreamUnMarshaller um( in );
124 for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
125 (*i)->deserialize(um, &data);
134 data.parse(rs->get_stream());
135 #if FILE_UN_MARSHALLER 136 XDRFileUnMarshaller um(rs->get_stream());
138 fpistream in ( rs->get_stream() );
139 XDRStreamUnMarshaller um( in );
142 for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
143 (*i)->deserialize(um, &data);
152 void Connect::process_data(DDS &data, Response *rs)
154 DBG(cerr <<
"Entering Connect::process_data" << endl);
156 data.set_dap_version(rs->get_protocol());
158 DBG(cerr <<
"Entering process_data: d_stream = " << rs << endl);
159 switch (rs->get_type()) {
162 if (!e.parse(rs->get_stream()))
163 throw InternalErr(__FILE__, __LINE__,
"Could not parse the Error object returned by the server!");
170 throw InternalErr(__FILE__, __LINE__,
171 "An error was reported by the remote web server; this should have been processed by HTTPConnect.");
177 case dods_data_ddx: {
179 DDXParser ddx_parser(data.get_factory());
183 DBG(cerr <<
"MPM Boundary: " << boundary << endl);
189 ddx_parser.intern_stream(rs->get_stream(), &data, data_cid, boundary);
193 DBG(cerr <<
"Data CID: " << data_cid << endl);
200 XDRFileUnMarshaller um(rs->get_stream());
201 for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
202 (*i)->deserialize(um, &data);
211 data.parse(rs->get_stream());
213 XDRFileUnMarshaller um(rs->get_stream());
216 for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
217 (*i)->deserialize(um, &data);
246 void Connect::parse_mime(Response *rs)
248 rs->set_version(
"dods/0.0");
249 rs->set_protocol(
"2.0");
251 FILE *data_source = rs->get_stream();
253 while (!mime.empty()) {
254 string header, value;
258 if (header ==
"content-description:") {
259 DBG(cout << header <<
": " << value << endl);
263 else if (header ==
"xdods-server:" && rs->get_version() ==
"dods/0.0") {
264 DBG(cout << header <<
": " << value << endl);
265 rs->set_version(value);
268 else if (header ==
"xopendap-server:") {
269 DBG(cout << header <<
": " << value << endl);
270 rs->set_version(value);
272 else if (header ==
"xdap:") {
273 DBG(cout << header <<
": " << value << endl);
274 rs->set_protocol(value);
277 else if (rs->get_version() ==
"dods/0.0" && header ==
"server:") {
278 DBG(cout << header <<
": " << value << endl);
279 rs->set_version(value);
295 Connect::Connect(
const string &n,
string uname,
string password) :
296 d_http(0), d_version(
"unknown"), d_protocol(
"2.0")
302 if (name.find(
"http") == 0) {
303 DBG(cerr <<
"Connect: The identifier is an http URL" << endl);
307 string::size_type dotpos = name.find(
'?');
308 if (dotpos != name.npos) {
309 _URL = name.substr(0, dotpos);
310 string expr = name.substr(dotpos + 1);
312 dotpos = expr.find(
'&');
313 if (dotpos != expr.npos) {
314 _proj = expr.substr(0, dotpos);
315 _sel = expr.substr(dotpos);
331 DBG(cerr <<
"Connect: The identifier is a local data source." << endl);
343 DBG2(cerr <<
"Entering the Connect dtor" << endl);
349 DBG2(cerr <<
"Leaving the Connect dtor" << endl);
361 string version_url = _URL +
".ver";
362 if (_proj.length() + _sel.length())
363 version_url = version_url +
"?" +
id2www_ce(_proj + _sel);
375 d_version = rs->get_version();
376 d_protocol = rs->get_protocol();
397 string version_url = _URL +
".ver";
398 if (_proj.length() + _sel.length())
399 version_url = version_url +
"?" +
id2www_ce(_proj + _sel);
411 d_version = rs->get_version();
412 d_protocol = rs->get_protocol();
429 string das_url = _URL +
".das";
430 if (_proj.length() + _sel.length())
431 das_url = das_url +
"?" +
id2www_ce(_proj + _sel);
443 d_version = rs->get_version();
444 d_protocol = rs->get_protocol();
446 switch (rs->get_type()) {
449 if (!e.
parse(rs->get_stream())) {
452 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
468 das.
parse(rs->get_stream());
495 string use_url = _URL +
"?" + _proj + _sel;
506 d_version = rs->get_version();
507 d_protocol = rs->get_protocol();
509 switch (rs->get_type()) {
512 if (!e.
parse(rs->get_stream())) {
515 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
531 das.
parse(rs->get_stream());
562 string::size_type dotpos = expr.find(
'&');
563 if (dotpos != expr.npos) {
564 proj = expr.substr(0, dotpos);
565 sel = expr.substr(dotpos);
572 string dds_url = _URL +
".dds" +
"?" +
id2www_ce(_proj + proj + _sel + sel);
584 d_version = rs->get_version();
585 d_protocol = rs->get_protocol();
587 switch (rs->get_type()) {
590 if (!e.
parse(rs->get_stream())) {
593 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
609 dds.
parse(rs->get_stream());
641 string use_url = _URL +
"?" + _proj + _sel;
652 d_version = rs->get_version();
653 d_protocol = rs->get_protocol();
655 switch (rs->get_type()) {
658 if (!e.
parse(rs->get_stream())) {
661 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
677 dds.
parse(rs->get_stream());
705 string::size_type dotpos = expr.find(
'&');
706 if (dotpos != expr.npos) {
707 proj = expr.substr(0, dotpos);
708 sel = expr.substr(dotpos);
715 string ddx_url = _URL +
".ddx" +
"?" +
id2www_ce(_proj + proj + _sel + sel);
726 d_version = rs->get_version();
727 d_protocol = rs->get_protocol();
729 switch (rs->get_type()) {
732 if (!e.
parse(rs->get_stream())) {
735 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
762 throw Error(
"Invalid response type when requesting a DDX response. Response type: " + long_to_string(ot));
772 string use_url = _URL +
"?" + _proj + _sel;
783 d_version = rs->get_version();
784 d_protocol = rs->get_protocol();
786 switch (rs->get_type()) {
789 if (!e.
parse(rs->get_stream())) {
791 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
801 throw InternalErr(__FILE__, __LINE__,
"Web error.");
820 throw Error(
"Invalid response type when requesting a DDX response. Response type: " + long_to_string(ot));
845 string::size_type dotpos = expr.find(
'&');
846 if (dotpos != expr.npos) {
847 proj = expr.substr(0, dotpos);
848 sel = expr.substr(dotpos);
855 string data_url = _URL +
".dods?" +
id2www_ce(_proj + proj + _sel + sel);
862 d_version = rs->get_version();
863 d_protocol = rs->get_protocol();
865 process_data(data, rs);
895 string use_url = _URL +
"?" + _proj + _sel;
901 d_version = rs->get_version();
902 d_protocol = rs->get_protocol();
904 process_data(data, rs);
916 void Connect::request_data_ddx(
DataDDS &data,
string expr)
919 string::size_type dotpos = expr.find(
'&');
920 if (dotpos != expr.npos) {
921 proj = expr.substr(0, dotpos);
922 sel = expr.substr(dotpos);
929 string data_url = _URL +
".dap?" +
id2www_ce(_proj + proj + _sel + sel);
936 d_version = rs->get_version();
937 d_protocol = rs->get_protocol();
939 process_data(data, rs);
951 void Connect::request_data_ddx_url(DataDDS &data)
953 string use_url = _URL +
"?" + _proj + _sel;
959 d_version = rs->get_version();
960 d_protocol = rs->get_protocol();
962 process_data(data, rs);
989 throw InternalErr(__FILE__, __LINE__,
"Response object is null.");
1000 throw InternalErr(__FILE__, __LINE__,
"Response object is null.");
1013 static void divine_type_information(Response *rs)
1016 int c = getc(rs->get_stream());
1017 while (!feof(rs->get_stream()) && !ferror(rs->get_stream()) && isspace(c)) {
1018 c = getc(rs->get_stream());
1022 if (ferror(rs->get_stream()))
1023 throw Error(
"Error reading response type information: " +
string(strerror(errno)));
1024 if (feof(rs->get_stream()))
1025 throw Error(
"Error reading response type information: Found EOF");
1034 rs->set_type(dods_data_ddx);
1038 rs->set_type(dods_data);
1041 throw InternalErr(__FILE__, __LINE__,
"Could not determine type of response object in stream.");
1044 ungetc(c, rs->get_stream());
1061 if (rs->get_type() == unknown_type)
1062 divine_type_information(rs);
1064 switch (rs->get_type()) {
1066 d_version = rs->get_version();
1067 d_protocol = rs->get_protocol();
1068 process_data(data, rs);
1071 process_data(data, rs);
1072 d_version = rs->get_version();
1073 d_protocol = data.get_protocol();
1076 throw InternalErr(__FILE__, __LINE__,
"Should have been a DataDDS or DataDDX.");
1081 if (rs->get_type() == unknown_type)
1082 divine_type_information(rs);
1084 switch (rs->get_type()) {
1086 d_version = rs->get_version();
1087 d_protocol = rs->get_protocol();
1088 process_data(data, rs);
1091 process_data(data, rs);
1092 d_version = rs->get_version();
1095 d_protocol = data.get_dap_version();
1098 throw InternalErr(__FILE__, __LINE__,
"Should have been a DataDDS or DataDDX.");
1127 throw InternalErr(__FILE__, __LINE__,
"URL(): This call is only valid for a DAP data source.");
1130 return _URL +
"?" + _proj + _sel;
1146 throw InternalErr(__FILE__, __LINE__,
"CE(): This call is only valid for a DAP data source.");
1148 return _proj + _sel;
1191 bool Connect::is_cache_enabled()
1194 DBG(cerr <<
"Entering is_cache_enabled (" << hex << d_http << dec
1200 DBGN(cerr <<
"exiting" << endl);
virtual string CE()
Get the Connect's constraint expression.
virtual void request_das_url(DAS &das)
Get the DAS from a server.
string get_next_mime_header(FILE *in)
void intern_stream(FILE *in, DDS *dds, string &cid, const string &boundary="")
Read the DDX from a stream instead of a file.
virtual void request_ddx(DDS &dds, string expr="")
Get the DDX from a server.
virtual string URL(bool CE=true)
Get the object's URL.
string id2www_ce(string in, const string &allowable)
string prune_spaces(const string &name)
void set_credentials(const string &u, const string &p)
void set_xdap_protocol(int major, int minor)
void read_multipart_headers(FILE *in, const string &content_type, const ObjectType object_type, const string &cid)
virtual void request_dds_url(DDS &dds)
Get the DDS from a server.
bool parse(FILE *fp)
Parse an Error object.
void set_cache_enabled(bool enabled)
string read_multipart_boundary(FILE *in, const string &boundary)
ObjectType
The type of object in the stream coming from the data server.
HTTPResponse * fetch_url(const string &url)
A class for software fault reporting.
void parse(string fname)
Parse a DDS from a file with the given d_name.
void parse_mime_header(const string &header, string &name, string &value)
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...
ObjectType get_description_type(const string &value)
void set_cache_enabled(bool enabled)
virtual void request_dds(DDS &dds, string expr="")
Get the DDS from a server.
void set_accept_deflate(bool deflate)
virtual void request_data(DataDDS &data, string expr="")
Get the DAS from a server.
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...
string cid_to_header_value(const string &cid)
virtual void request_das(DAS &das)
Get the DAS from a server.
void set_accept_deflate(bool defalte)
void set_xdap_protocol(int major, int minor)
virtual void parse(string fname)
Reads a DAS from the named file.
virtual string request_version()
void set_credentials(string u, string p)
Set the credentials for responding to challenges while dereferencing URLs.
virtual string request_protocol()
Hold attribute data for a DAP2 dataset.
virtual void request_data_url(DataDDS &data)
Get the DAS from a server.
A class for error processing.
BaseTypeFactory * get_factory() const
virtual void request_ddx_url(DDS &dds)
The 'url' version of request_ddx.