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..");
94 DDXParser ddx_parser(data.get_factory());
98 DBG(cerr <<
"MPM Boundary: " << boundary << endl);
104 ddx_parser.intern_stream(rs->get_stream(), &data, data_cid, boundary);
108 DBG(cerr <<
"Data CID: " << data_cid << endl);
115 #if FILE_UN_MARSHALLER 116 XDRFileUnMarshaller um(rs->get_stream());
118 fpistream in ( rs->get_stream() );
119 XDRStreamUnMarshaller um( in );
121 for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
122 (*i)->deserialize(um, &data);
130 data.parse(rs->get_stream());
131 #if FILE_UN_MARSHALLER 132 XDRFileUnMarshaller um(rs->get_stream());
134 fpistream in ( rs->get_stream() );
135 XDRStreamUnMarshaller um( in );
138 for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
139 (*i)->deserialize(um, &data);
148 void Connect::process_data(DDS &data, Response *rs)
150 DBG(cerr <<
"Entering Connect::process_data" << endl);
153 data.set_dap_version(rs->get_protocol());
155 DBG(cerr <<
"Entering process_data: d_stream = " << rs << endl);
156 switch (rs->get_type()) {
159 if (!e.parse(rs->get_stream()))
160 throw InternalErr(__FILE__, __LINE__,
"Could not parse the Error object returned by the server!");
167 throw InternalErr(__FILE__, __LINE__,
168 "An error was reported by the remote httpd; this should have been processed by HTTPConnect.");
171 case dods_data_ddx: {
173 DDXParser ddx_parser(data.get_factory());
177 DBG(cerr <<
"MPM Boundary: " << boundary << endl);
183 ddx_parser.intern_stream(rs->get_stream(), &data, data_cid, boundary);
187 DBG(cerr <<
"Data CID: " << data_cid << endl);
194 XDRFileUnMarshaller um(rs->get_stream());
195 for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
196 (*i)->deserialize(um, &data);
204 data.parse(rs->get_stream());
206 XDRFileUnMarshaller um(rs->get_stream());
209 for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
210 (*i)->deserialize(um, &data);
238 void Connect::parse_mime(Response *rs)
240 rs->set_version(
"dods/0.0");
241 rs->set_protocol(
"2.0");
243 FILE *data_source = rs->get_stream();
245 while (!mime.empty()) {
246 string header, value;
250 if (header ==
"content-description:") {
251 DBG(cout << header <<
": " << value << endl);
255 else if (header ==
"xdods-server:" && rs->get_version() ==
"dods/0.0") {
256 DBG(cout << header <<
": " << value << endl);
257 rs->set_version(value);
260 else if (header ==
"xopendap-server:") {
261 DBG(cout << header <<
": " << value << endl);
262 rs->set_version(value);
264 else if (header ==
"xdap:") {
265 DBG(cout << header <<
": " << value << endl);
266 rs->set_protocol(value);
269 else if (rs->get_version() ==
"dods/0.0" && header ==
"server:") {
270 DBG(cout << header <<
": " << value << endl);
271 rs->set_version(value);
287 Connect::Connect(
const string &n,
string uname,
string password) :
288 d_http(0), d_version(
"unknown"), d_protocol(
"2.0")
294 if (name.find(
"http") == 0) {
295 DBG(cerr <<
"Connect: The identifier is an http URL" << endl);
299 string::size_type dotpos = name.find(
'?');
300 if (dotpos != name.npos) {
301 _URL = name.substr(0, dotpos);
302 string expr = name.substr(dotpos + 1);
304 dotpos = expr.find(
'&');
305 if (dotpos != expr.npos) {
306 _proj = expr.substr(0, dotpos);
307 _sel = expr.substr(dotpos);
323 DBG(cerr <<
"Connect: The identifier is a local data source." << endl);
335 DBG2(cerr <<
"Entering the Connect dtor" << endl);
341 DBG2(cerr <<
"Leaving the Connect dtor" << endl);
353 string version_url = _URL +
".ver";
354 if (_proj.length() + _sel.length())
355 version_url = version_url +
"?" +
id2www_ce(_proj + _sel);
367 d_version = rs->get_version();
368 d_protocol = rs->get_protocol();
389 string version_url = _URL +
".ver";
390 if (_proj.length() + _sel.length())
391 version_url = version_url +
"?" +
id2www_ce(_proj + _sel);
403 d_version = rs->get_version();
404 d_protocol = rs->get_protocol();
421 string das_url = _URL +
".das";
422 if (_proj.length() + _sel.length())
423 das_url = das_url +
"?" +
id2www_ce(_proj + _sel);
435 d_version = rs->get_version();
436 d_protocol = rs->get_protocol();
438 switch (rs->get_type()) {
441 if (!e.
parse(rs->get_stream())) {
444 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
460 das.
parse(rs->get_stream());
487 string use_url = _URL +
"?" + _proj + _sel;
498 d_version = rs->get_version();
499 d_protocol = rs->get_protocol();
501 switch (rs->get_type()) {
504 if (!e.
parse(rs->get_stream())) {
507 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
523 das.
parse(rs->get_stream());
554 string::size_type dotpos = expr.find(
'&');
555 if (dotpos != expr.npos) {
556 proj = expr.substr(0, dotpos);
557 sel = expr.substr(dotpos);
564 string dds_url = _URL +
".dds" +
"?" +
id2www_ce(_proj + proj + _sel + sel);
576 d_version = rs->get_version();
577 d_protocol = rs->get_protocol();
579 switch (rs->get_type()) {
582 if (!e.
parse(rs->get_stream())) {
585 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
601 dds.
parse(rs->get_stream());
633 string use_url = _URL +
"?" + _proj + _sel;
644 d_version = rs->get_version();
645 d_protocol = rs->get_protocol();
647 switch (rs->get_type()) {
650 if (!e.
parse(rs->get_stream())) {
653 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
669 dds.
parse(rs->get_stream());
697 string::size_type dotpos = expr.find(
'&');
698 if (dotpos != expr.npos) {
699 proj = expr.substr(0, dotpos);
700 sel = expr.substr(dotpos);
707 string ddx_url = _URL +
".ddx" +
"?" +
id2www_ce(_proj + proj + _sel + sel);
718 d_version = rs->get_version();
719 d_protocol = rs->get_protocol();
721 switch (rs->get_type()) {
724 if (!e.
parse(rs->get_stream())) {
727 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
754 throw Error(
"Invalid response type when requesting a DDX response. Response type: " + long_to_string(ot));
764 string use_url = _URL +
"?" + _proj + _sel;
775 d_version = rs->get_version();
776 d_protocol = rs->get_protocol();
778 switch (rs->get_type()) {
781 if (!e.
parse(rs->get_stream())) {
783 throw InternalErr(__FILE__, __LINE__,
"Could not parse error returned from server.");
793 throw InternalErr(__FILE__, __LINE__,
"Web error.");
812 throw Error(
"Invalid response type when requesting a DDX response. Response type: " + long_to_string(ot));
837 string::size_type dotpos = expr.find(
'&');
838 if (dotpos != expr.npos) {
839 proj = expr.substr(0, dotpos);
840 sel = expr.substr(dotpos);
847 string data_url = _URL +
".dods?" +
id2www_ce(_proj + proj + _sel + sel);
854 d_version = rs->get_version();
855 d_protocol = rs->get_protocol();
857 process_data(data, rs);
887 string use_url = _URL +
"?" + _proj + _sel;
893 d_version = rs->get_version();
894 d_protocol = rs->get_protocol();
896 process_data(data, rs);
908 void Connect::request_data_ddx(
DataDDS &data,
string expr)
911 string::size_type dotpos = expr.find(
'&');
912 if (dotpos != expr.npos) {
913 proj = expr.substr(0, dotpos);
914 sel = expr.substr(dotpos);
921 string data_url = _URL +
".dap?" +
id2www_ce(_proj + proj + _sel + sel);
928 d_version = rs->get_version();
929 d_protocol = rs->get_protocol();
931 process_data(data, rs);
943 void Connect::request_data_ddx_url(
DataDDS &data)
945 string use_url = _URL +
"?" + _proj + _sel;
951 d_version = rs->get_version();
952 d_protocol = rs->get_protocol();
954 process_data(data, rs);
981 throw InternalErr(__FILE__, __LINE__,
"Response object is null.");
992 throw InternalErr(__FILE__, __LINE__,
"Response object is null.");
1005 static void divine_type_information(
Response *rs)
1008 int c = getc(rs->get_stream());
1009 while (!feof(rs->get_stream()) && !ferror(rs->get_stream()) && isspace(c)) {
1010 c = getc(rs->get_stream());
1014 if (ferror(rs->get_stream()))
1015 throw Error(
"Error reading response type information: " +
string(strerror(errno)));
1016 if (feof(rs->get_stream()))
1017 throw Error(
"Error reading response type information: Found EOF");
1026 rs->set_type(dods_data_ddx);
1030 rs->set_type(dods_data);
1033 throw InternalErr(__FILE__, __LINE__,
"Could not determine type of response object in stream.");
1036 ungetc(c, rs->get_stream());
1053 if (rs->get_type() == unknown_type)
1054 divine_type_information(rs);
1056 switch (rs->get_type()) {
1058 d_version = rs->get_version();
1059 d_protocol = rs->get_protocol();
1060 process_data(data, rs);
1063 process_data(data, rs);
1064 d_version = rs->get_version();
1065 d_protocol = data.get_protocol();
1068 throw InternalErr(__FILE__, __LINE__,
"Should have been a DataDDS or DataDDX.");
1073 if (rs->get_type() == unknown_type)
1074 divine_type_information(rs);
1076 switch (rs->get_type()) {
1078 d_version = rs->get_version();
1079 d_protocol = rs->get_protocol();
1080 process_data(data, rs);
1083 process_data(data, rs);
1084 d_version = rs->get_version();
1087 d_protocol = data.get_dap_version();
1090 throw InternalErr(__FILE__, __LINE__,
"Should have been a DataDDS or DataDDX.");
1119 throw InternalErr(__FILE__, __LINE__,
"URL(): This call is only valid for a DAP data source.");
1122 return _URL +
"?" + _proj + _sel;
1138 throw InternalErr(__FILE__, __LINE__,
"CE(): This call is only valid for a DAP data source.");
1140 return _proj + _sel;
1183 bool Connect::is_cache_enabled()
1186 DBG(cerr <<
"Entering is_cache_enabled (" << hex << d_http << dec
1192 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)
BaseTypeFactory * get_factory() const
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.
virtual void request_ddx_url(DDS &dds)
The 'url' version of request_ddx.