forked from OPENDAP/libdap4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect.cc
1146 lines (966 loc) · 35.2 KB
/
Connect.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <[email protected]>
// Dan Holloway <[email protected]>
// Reza Nekovei <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
// (c) COPYRIGHT URI/MIT 1994-2002
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
// jhrg,jimg James Gallagher <[email protected]>
// dan Dan Holloway <[email protected]>
// reza Reza Nekovei <[email protected]>
#include "config.h"
// #define DODS_DEBUG
#define FILE_UN_MARSHALLER 1
#include <cerrno>
#include <cstring>
#include <algorithm>
#include <fstream>
#include "Connect.h"
#include "DataDDS.h"
#include "debug.h"
#include "escaping.h"
// #include "RCReader.h"
#include "DDXParserSAX2.h"
#if FILE_UN_MARSHALLER
#include "XDRFileUnMarshaller.h"
#else
#include "XDRStreamUnMarshaller.h"
#include "fdiostream.h"
#endif
#include "mime_util.h"
using std::cerr;
using std::endl;
using std::ifstream;
using std::min;
using std::ofstream;
namespace libdap {
/** This private method process data from both local and remote sources. It
exists to eliminate duplication of code. */
void Connect::process_data(DataDDS &data, Response *rs) {
DBG(cerr << "Entering Connect::process_data" << endl);
data.set_version(rs->get_version());
data.set_protocol(rs->get_protocol());
DBG(cerr << "Entering process_data: d_stream = " << rs << endl);
switch (rs->get_type()) {
case dods_error: {
Error e;
if (!e.parse(rs->get_stream()))
throw InternalErr(__FILE__, __LINE__, "Could not parse the Error object returned by the server!");
throw e;
}
case web_error:
// Web errors (those reported in the return document's MIME header)
// are processed by the WWW library.
throw InternalErr(
__FILE__, __LINE__,
"An error was reported by the remote httpd; this should have been processed by HTTPConnect..");
#if 0
// This code triggers a security warning from Coverity; since it is not used,
// I have removed it. jhrg 5/5/16
case dods_data_ddx: {
// Parse the DDX; throw an exception on error.
DDXParser ddx_parser(data.get_factory());
// Read the MPM boundary and then read the subsequent headers
string boundary = read_multipart_boundary(rs->get_stream());
DBG(cerr << "MPM Boundary: " << boundary << endl);
read_multipart_headers(rs->get_stream(), "text/xml", dods_ddx);
// Parse the DDX, reading up to and including the next boundary.
// Return the CID for the matching data part
string data_cid;
ddx_parser.intern_stream(rs->get_stream(), &data, data_cid, boundary);
// Munge the CID into something we can work with
data_cid = cid_to_header_value(data_cid);
DBG(cerr << "Data CID: " << data_cid << endl);
// Read the data part's MPM part headers (boundary was read by
// DDXParse::intern)
read_multipart_headers(rs->get_stream(), "application/octet-stream", dap4_data, data_cid);
// Now read the data
#if FILE_UN_MARSHALLER
XDRFileUnMarshaller um(rs->get_stream());
#else
fpistream in ( rs->get_stream() );
XDRStreamUnMarshaller um( in );
#endif
for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
(*i)->deserialize(um, &data);
}
return;
}
#endif
case dods_data:
default: {
// Parse the DDS; throw an exception on error.
data.parse(rs->get_stream());
#if FILE_UN_MARSHALLER
XDRFileUnMarshaller um(rs->get_stream());
#else
fpistream in(rs->get_stream());
XDRStreamUnMarshaller um(in);
#endif
// Load the DDS with data.
for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
(*i)->deserialize(um, &data);
}
return;
}
}
}
/** This private method process data from both local and remote sources. It
exists to eliminate duplication of code. */
void Connect::process_data(DDS &data, Response *rs) {
DBG(cerr << "Entering Connect::process_data" << endl);
data.set_dap_version(rs->get_protocol());
DBG(cerr << "Entering process_data: d_stream = " << rs << endl);
switch (rs->get_type()) {
case dods_error: {
Error e;
if (!e.parse(rs->get_stream()))
throw InternalErr(__FILE__, __LINE__, "Could not parse the Error object returned by the server!");
throw e;
}
case web_error:
// Web errors (those reported in the return document's MIME header)
// are processed by the WWW library.
throw InternalErr(
__FILE__, __LINE__,
"An error was reported by the remote web server; this should have been processed by HTTPConnect.");
#if 0
// FIXME: The following case is never used. There is no such response. jhrg 10/20/15
// This code triggers a security warning from Coverity; since it is not used,
// I have removed it. jhrg 5/5/16
case dods_data_ddx: {
// Parse the DDX; throw an exception on error.
DDXParser ddx_parser(data.get_factory());
// Read the MPM boundary and then read the subsequent headers
string boundary = read_multipart_boundary(rs->get_stream());
DBG(cerr << "MPM Boundary: " << boundary << endl);
read_multipart_headers(rs->get_stream(), "text/xml", dods_ddx);
// Parse the DDX, reading up to and including the next boundary.
// Return the CID for the matching data part
string data_cid;
ddx_parser.intern_stream(rs->get_stream(), &data, data_cid, boundary);
// Munge the CID into something we can work with
data_cid = cid_to_header_value(data_cid);
DBG(cerr << "Data CID: " << data_cid << endl);
// Read the data part's MPM part headers (boundary was read by
// DDXParse::intern)
read_multipart_headers(rs->get_stream(), "application/octet-stream", dap4_data, data_cid);
// Now read the data
XDRFileUnMarshaller um(rs->get_stream());
for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
(*i)->deserialize(um, &data);
}
return;
}
#endif
case dods_data:
default: {
// Parse the DDS; throw an exception on error.
data.parse(rs->get_stream());
XDRFileUnMarshaller um(rs->get_stream());
// Load the DDS with data.
for (DDS::Vars_iter i = data.var_begin(); i != data.var_end(); i++) {
(*i)->deserialize(um, &data);
}
return;
}
}
}
// Barely a parser... This is used when reading from local sources of DODS
// Data objects. It simulates the important actions of the libwww MIME header
// parser. Those actions fill in certain fields in the Connect object. jhrg
// 5/20/97
//
// Make sure that this parser reads from data_source without disturbing the
// information in data_source that follows the MIME header. Since the DDS
// (which follows the MIME header) is parsed by a flex/bison scanner/parser,
// make sure to use I/O calls that will mesh with ANSI C I/O calls. In the
// old GNU libg++, the C++ calls were synchronized with the C calls, but that
// may no longer be the case. 5/31/99 jhrg
/** Use when you cannot use libcurl.
@note This method tests for MIME headers with lines terminated by CRLF
(\r\n) and Newlines (\n). In either case, the line terminators are removed
before each header is processed.
@note FIXME The code uses tainted data via get_next_mime_header() whcih
should be fixed. See the note in mime_util.cc
@param data_source Read from this stream.
@param rs Value/Result parameter. Dump version and type information here.
*/
void Connect::parse_mime(Response *rs) {
rs->set_version("dods/0.0"); // initial value; for backward compatibility.
rs->set_protocol("2.0");
FILE *data_source = rs->get_stream();
string mime = get_next_mime_header(data_source);
while (!mime.empty()) {
string header, value;
parse_mime_header(mime, header, value);
// Note that this is an ordered list
if (header == "content-description:") {
DBG(cout << header << ": " << value << endl);
rs->set_type(get_description_type(value));
}
// Use the value of xdods-server only if no other value has been read
else if (header == "xdods-server:" && rs->get_version() == "dods/0.0") {
DBG(cout << header << ": " << value << endl);
rs->set_version(value);
}
// This trumps 'xdods-server' and 'server'
else if (header == "xopendap-server:") {
DBG(cout << header << ": " << value << endl);
rs->set_version(value);
} else if (header == "xdap:") {
DBG(cout << header << ": " << value << endl);
rs->set_protocol(value);
}
// Only look for 'server' if no other header supplies this info.
else if (rs->get_version() == "dods/0.0" && header == "server:") {
DBG(cout << header << ": " << value << endl);
rs->set_version(value);
}
mime = get_next_mime_header(data_source);
}
}
// public mfuncs
/** The Connect constructor requires a <tt>name</tt>, which is the URL to
which the connection is to be made.
@param n The URL for the virtual connection.
@param uname Use this username for authentication. Null by default.
@param password Password to use for authentication. Null by default.
@brief Create an instance of Connect. */
Connect::Connect(const string &n, string uname, string password) : d_http(0), d_version("unknown"), d_protocol("2.0") {
string name = prune_spaces(n);
// Figure out if the URL starts with 'http', if so, make sure that we
// talk to an instance of HTTPConnect.
if (name.find("http") == 0) {
DBG(cerr << "Connect: The identifier is an http URL" << endl);
d_http = new HTTPConnect(RCReader::instance());
// Find and store any CE given with the URL.
string::size_type dotpos = name.find('?');
if (dotpos != name.npos) {
_URL = name.substr(0, dotpos);
string expr = name.substr(dotpos + 1);
dotpos = expr.find('&');
if (dotpos != expr.npos) {
_proj = expr.substr(0, dotpos);
_sel = expr.substr(dotpos); // XXX includes '&'
} else {
_proj = expr;
_sel = "";
}
} else {
_URL = name;
_proj = "";
_sel = "";
}
_local = false;
} else {
DBG(cerr << "Connect: The identifier is a local data source." << endl);
d_http = 0;
_URL = "";
_local = true; // local in this case means non-DAP
}
set_credentials(uname, password);
}
Connect::~Connect() {
DBG2(cerr << "Entering the Connect dtor" << endl);
if (d_http)
delete d_http;
d_http = 0;
DBG2(cerr << "Leaving the Connect dtor" << endl);
}
/** Get version information from the server. This is a new method which will
ease the transition to DAP 4.
@note Use request_protocol() to get the DAP protocol version.
@return The DAP version string.
@see request_protocol() */
string Connect::request_version() {
string version_url = _URL + ".ver";
if (_proj.length() + _sel.length())
version_url = version_url + "?" + id2www_ce(_proj + _sel);
Response *rs = 0;
try {
rs = d_http->fetch_url(version_url);
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
d_version = rs->get_version();
d_protocol = rs->get_protocol();
delete rs;
rs = 0;
return d_version;
}
/** Get protocol version information from the server. This is a new method
which will ease the transition to DAP 4. Note that this method returns
the version of the DAP protocol implemented by the server. The
request_version() method returns the \e server's version number, not
the DAP protocol version.
@note This method actually asks the server for the protocol version - use
get_protocol() to get the protocol information from the most recent
response (e.g., from the last DDX response returned by the server).
@return The DAP protocol version string. */
string Connect::request_protocol() {
string version_url = _URL + ".ver";
if (_proj.length() + _sel.length())
version_url = version_url + "?" + id2www_ce(_proj + _sel);
Response *rs = 0;
try {
rs = d_http->fetch_url(version_url);
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
d_version = rs->get_version();
d_protocol = rs->get_protocol();
delete rs;
rs = 0;
return d_protocol;
}
/** Reads the DAS corresponding to the dataset in the Connect
object's URL. Although DAP does not support using CEs with DAS
requests, if present in the Connect object's instance, they will be
escaped and passed as the query string of the request.
@brief Get the DAS from a server.
@param das Result. */
void Connect::request_das(DAS &das) {
string das_url = _URL + ".das";
if (_proj.length() + _sel.length())
das_url = das_url + "?" + id2www_ce(_proj + _sel);
Response *rs = 0;
try {
rs = d_http->fetch_url(das_url);
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
d_version = rs->get_version();
d_protocol = rs->get_protocol();
switch (rs->get_type()) {
case dods_error: {
Error e;
if (!e.parse(rs->get_stream())) {
delete rs;
rs = 0;
throw InternalErr(__FILE__, __LINE__, "Could not parse error returned from server.");
}
delete rs;
rs = 0;
throw e;
}
case web_error:
// We should never get here; a web error should be picked up read_url
// (called by fetch_url) and result in a thrown Error object.
break;
case dods_das:
default:
// DAS::parse throws an exception on error.
try {
das.parse(rs->get_stream()); // read and parse the das from a file
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
break;
}
delete rs;
rs = 0;
}
/** Reads the DAS corresponding to the dataset in the Connect
object's URL. Although DAP does not support using CEs with DAS
requests, if present in the Connect object's instance, they will be
escaped and passed as the query string of the request.
Different from request_das method in that this method uses the URL as
given without attaching .das or projections or selections.
@brief Get the DAS from a server.
@param das Result. */
void Connect::request_das_url(DAS &das) {
string use_url = _URL + "?" + _proj + _sel;
Response *rs = 0;
try {
rs = d_http->fetch_url(use_url);
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
d_version = rs->get_version();
d_protocol = rs->get_protocol();
switch (rs->get_type()) {
case dods_error: {
Error e;
if (!e.parse(rs->get_stream())) {
delete rs;
rs = 0;
throw InternalErr(__FILE__, __LINE__, "Could not parse error returned from server.");
}
delete rs;
rs = 0;
throw e;
}
case web_error:
// We should never get here; a web error should be picked up read_url
// (called by fetch_url) and result in a thrown Error object.
break;
case dods_das:
default:
// DAS::parse throws an exception on error.
try {
das.parse(rs->get_stream()); // read and parse the das from a file
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
break;
}
delete rs;
rs = 0;
}
/** Reads the DDS corresponding to the dataset in the Connect object's URL.
If present in the Connect object's instance, a CE will be escaped,
combined with \c expr and passed as the query string of the request.
@note If you need the DDS to hold specializations of the type classes,
be sure to include the factory class which will instantiate those
specializations in the DDS. Either pass a pointer to the factory to
DDS constructor or use the DDS::set_factory() method after the
object is built.
@brief Get the DDS from a server.
@param dds Result.
@param expr Send this constraint expression to the server. */
void Connect::request_dds(DDS &dds, string expr) {
string proj, sel;
string::size_type dotpos = expr.find('&');
if (dotpos != expr.npos) {
proj = expr.substr(0, dotpos);
sel = expr.substr(dotpos);
} else {
proj = expr;
sel = "";
}
string dds_url = _URL + ".dds" + "?" + id2www_ce(_proj + proj + _sel + sel);
Response *rs = 0;
try {
rs = d_http->fetch_url(dds_url);
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
d_version = rs->get_version();
d_protocol = rs->get_protocol();
switch (rs->get_type()) {
case dods_error: {
Error e;
if (!e.parse(rs->get_stream())) {
delete rs;
rs = 0;
throw InternalErr(__FILE__, __LINE__, "Could not parse error returned from server.");
}
delete rs;
rs = 0;
throw e;
}
case web_error:
// We should never get here; a web error should be picked up read_url
// (called by fetch_url) and result in a thrown Error object.
break;
case dods_dds:
default:
// DDS::prase throws an exception on error.
try {
dds.parse(rs->get_stream()); // read and parse the dds from a file
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
break;
}
delete rs;
rs = 0;
}
/** Reads the DDS corresponding to the dataset in the Connect object's URL.
If present in the Connect object's instance, a CE will be escaped,
combined with \c expr and passed as the query string of the request.
Different from request_dds method above in that this method assumes
URL is complete and does not add anything to the command, such as .dds
or projections or selections.
@note If you need the DDS to hold specializations of the type classes,
be sure to include the factory class which will instantiate those
specializations in the DDS. Either pass a pointer to the factory to
DDS constructor or use the DDS::set_factory() method after the
object is built.
@brief Get the DDS from a server.
@param dds Result. */
void Connect::request_dds_url(DDS &dds) {
string use_url = _URL + "?" + _proj + _sel;
Response *rs = 0;
try {
rs = d_http->fetch_url(use_url);
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
d_version = rs->get_version();
d_protocol = rs->get_protocol();
switch (rs->get_type()) {
case dods_error: {
Error e;
if (!e.parse(rs->get_stream())) {
delete rs;
rs = 0;
throw InternalErr(__FILE__, __LINE__, "Could not parse error returned from server.");
}
delete rs;
rs = 0;
throw e;
}
case web_error:
// We should never get here; a web error should be picked up read_url
// (called by fetch_url) and result in a thrown Error object.
break;
case dods_dds:
default:
// DDS::prase throws an exception on error.
try {
dds.parse(rs->get_stream()); // read and parse the dds from a file
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
break;
}
delete rs;
rs = 0;
}
/** Reads the DDX corresponding to the dataset in the Connect object's URL.
If present in the Connect object's instance, a CE will be escaped,
combined with \c expr and passed as the query string of the request.
@note A DDX is represented as XML on the wire but in memory libdap uses a
DDS object with variables that hold their own attributes (the DDS itself holds
the global attributes).
@brief Get the DDX from a server.
@param dds Result.
@param expr Send this constraint expression to the server. */
void Connect::request_ddx(DDS &dds, string expr) {
string proj, sel;
string::size_type dotpos = expr.find('&');
if (dotpos != expr.npos) {
proj = expr.substr(0, dotpos);
sel = expr.substr(dotpos);
} else {
proj = expr;
sel = "";
}
string ddx_url = _URL + ".ddx" + "?" + id2www_ce(_proj + proj + _sel + sel);
Response *rs = 0;
try {
rs = d_http->fetch_url(ddx_url);
} catch (Error &e) {
delete rs;
throw;
}
d_version = rs->get_version();
d_protocol = rs->get_protocol();
switch (rs->get_type()) {
case dods_error: {
Error e;
if (!e.parse(rs->get_stream())) {
delete rs;
rs = 0;
throw InternalErr(__FILE__, __LINE__, "Could not parse error returned from server.");
}
delete rs;
throw e;
}
case web_error:
// We should never get here; a web error should be picked up read_url
// (called by fetch_url) and result in a thrown Error object.
break;
case dods_ddx:
try {
string blob;
DDXParser ddxp(dds.get_factory());
ddxp.intern_stream(rs->get_stream(), &dds, blob);
} catch (Error &e) {
delete rs;
throw;
}
break;
default:
ObjectType ot = rs->get_type();
delete rs;
throw Error("Invalid response type when requesting a DDX response. Response type: " + long_to_string(ot));
}
delete rs;
}
/** @brief The 'url' version of request_ddx
@see Connect::request_ddx. */
void Connect::request_ddx_url(DDS &dds) {
string use_url = _URL + "?" + _proj + _sel;
Response *rs = 0;
try {
rs = d_http->fetch_url(use_url);
} catch (Error &e) {
delete rs;
throw;
}
d_version = rs->get_version();
d_protocol = rs->get_protocol();
switch (rs->get_type()) {
case dods_error: {
Error e;
if (!e.parse(rs->get_stream())) {
delete rs;
throw InternalErr(__FILE__, __LINE__, "Could not parse error returned from server.");
}
delete rs;
throw e;
}
case web_error:
// We should never get here; a web error should be picked up read_url
// (called by fetch_url) and result in a thrown Error object.
delete rs;
throw InternalErr(__FILE__, __LINE__, "Web error.");
case dods_ddx:
try {
string blob;
DDXParser ddxp(dds.get_factory());
ddxp.intern_stream(rs->get_stream(), &dds, blob);
} catch (Error &e) {
delete rs;
throw;
}
break;
default: {
ObjectType ot = rs->get_type();
delete rs;
throw Error("Invalid response type when requesting a DDX response. Response type: " + long_to_string(ot));
}
}
delete rs;
}
/** Reads the DataDDS object corresponding to the dataset in the Connect
object's URL. If present in the Connect object's instance, a CE will be
escaped, combined with \c expr and passed as the query string of the
request. The result is a DataDDS which contains the data values bound to
variables.
@note If you need the DataDDS to hold specializations of the type classes,
be sure to include the factory class which will instantiate those
specializations in the DataDDS. Either pass a pointer to the factory to
DataDDS constructor or use the DDS::set_factory() method after the
object is built.
@brief Get the DAS from a server.
@param data Result.
@param expr Send this constraint expression to the server. */
void Connect::request_data(DataDDS &data, string expr) {
string proj, sel;
string::size_type dotpos = expr.find('&');
if (dotpos != expr.npos) {
proj = expr.substr(0, dotpos);
sel = expr.substr(dotpos);
} else {
proj = expr;
sel = "";
}
string data_url = _URL + ".dods?" + id2www_ce(_proj + proj + _sel + sel);
Response *rs = 0;
// We need to catch Error exceptions to ensure calling close_output.
try {
rs = d_http->fetch_url(data_url);
d_version = rs->get_version();
d_protocol = rs->get_protocol();
process_data(data, rs);
delete rs;
rs = 0;
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
}
/** Reads the DataDDS object corresponding to the dataset in the Connect
object's URL. If present in the Connect object's instance, a CE will be
escaped, combined with \c expr and passed as the query string of the
request. The result is a DataDDS which contains the data values bound to
variables.
Different from request_data in that this method uses the syntax of the
new OPeNDAP server commands using dispatch
@note If you need the DataDDS to hold specializations of the type classes,
be sure to include the factory class which will instantiate those
specializations in the DataDDS. Either pass a pointer to the factory to
DataDDS constructor or use the DDS::set_factory() method after the
object is built.
@brief Get the DAS from a server.
@param data Result. */
void Connect::request_data_url(DataDDS &data) {
string use_url = _URL + "?" + _proj + _sel;
Response *rs = 0;
// We need to catch Error exceptions to ensure calling close_output.
try {
rs = d_http->fetch_url(use_url);
d_version = rs->get_version();
d_protocol = rs->get_protocol();
process_data(data, rs);
delete rs;
rs = 0;
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
}
// FIXME Unused?
void Connect::request_data_ddx(DataDDS &data, string expr) {
string proj, sel;
string::size_type dotpos = expr.find('&');
if (dotpos != expr.npos) {
proj = expr.substr(0, dotpos);
sel = expr.substr(dotpos);
} else {
proj = expr;
sel = "";
}
string data_url = _URL + ".dap?" + id2www_ce(_proj + proj + _sel + sel);
Response *rs = 0;
// We need to catch Error exceptions to ensure calling close_output.
try {
rs = d_http->fetch_url(data_url);
d_version = rs->get_version();
d_protocol = rs->get_protocol();
process_data(data, rs);
delete rs;
rs = 0;
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
}
// FIXME Unused?
void Connect::request_data_ddx_url(DataDDS &data) {
string use_url = _URL + "?" + _proj + _sel;
Response *rs = 0;
// We need to catch Error exceptions to ensure calling close_output.
try {
rs = d_http->fetch_url(use_url);
d_version = rs->get_version();
d_protocol = rs->get_protocol();
process_data(data, rs);
delete rs;
rs = 0;
} catch (Error &e) {
delete rs;
rs = 0;
throw;
}
}
/** @brief Read data which is preceded by MIME headers.
This method works for both data dds and data ddx responses.
@note If you need the DataDDS to hold specializations of the type classes,
be sure to include the factory class which will instantiate those
specializations in the DataDDS. Either pass a pointer to the factory to
DataDDS constructor or use the DDS::set_factory() method after the
object is built.
@see read_data_no_mime()
@param data Result.
@param rs Read from this Response object. */
void Connect::read_data(DataDDS &data, Response *rs) {
if (!rs)
throw InternalErr(__FILE__, __LINE__, "Response object is null.");
// Read from data_source and parse the MIME headers specific to DAP2/4.
parse_mime(rs);
read_data_no_mime(data, rs);
}
void Connect::read_data(DDS &data, Response *rs) {
if (!rs)
throw InternalErr(__FILE__, __LINE__, "Response object is null.");
// Read from data_source and parse the MIME headers specific to DAP2/4.
parse_mime(rs);
read_data_no_mime(data, rs);
}
// This function looks at the input stream and makes its best guess at what
// lies in store for downstream processing code. Definitely heuristic.
// Assumptions:
// #1 The current file position is past any MIME headers (if they were present).
// #2 We must reset the FILE* position to the start of the DDS or DDX headers
static void divine_type_information(Response *rs) {
// Consume whitespace
int c = getc(rs->get_stream());
while (!feof(rs->get_stream()) && !ferror(rs->get_stream()) && isspace(c)) {
c = getc(rs->get_stream());
}
if (ferror(rs->get_stream()))
throw Error("Error reading response type information: " + string(strerror(errno)));
if (feof(rs->get_stream()))
throw Error("Error reading response type information: Found EOF");
// The heuristic here is that a DataDDX is a multipart MIME document and
// The first non space character found after the headers is the start of
// the first part which looks like '--<boundary>' while a DataDDS starts
// with a DDS (;Dataset {' ...). I take into account that our parsers have
// accepted both 'Dataset' and 'dataset' for a long time.
switch (c) {
case '-':
rs->set_type(dods_data_ddx);
break;
case 'D':
case 'd':
rs->set_type(dods_data);
break;
default:
throw InternalErr(__FILE__, __LINE__, "Could not determine type of response object in stream.");
}
ungetc(c, rs->get_stream());