1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "gduutil.hpp"
20 #include <metaproxy/util.hpp>
22 #include <yaz/wrbuf.h>
23 #include <yaz/oid_db.h>
24 #include <yaz/querytowrbuf.h>
28 namespace mp = metaproxy_1;
30 // Doxygen doesn't like mp::gdu, so we use this instead
31 namespace mp_util = metaproxy_1::util;
33 std::ostream& std::operator<<(std::ostream& os, Z_GDU& zgdu)
35 if (zgdu.which == Z_GDU_Z3950)
39 os << *(zgdu.u.z3950);
41 else if (zgdu.which == Z_GDU_HTTP_Request)
44 if (zgdu.u.HTTP_Request)
45 os << " " << *(zgdu.u.HTTP_Request);
47 else if (zgdu.which == Z_GDU_HTTP_Response)
49 os << "HTTP_Response";
50 if (zgdu.u.HTTP_Response)
51 os << " " << *(zgdu.u.HTTP_Response);
58 std::ostream& std::operator<<(std::ostream& os, Z_HTTP_Request& httpreq)
60 os << httpreq.method << " ";
65 std::ostream& std::operator<<(std::ostream& os, Z_HTTP_Response& httpres)
67 os << httpres.code << " ";
68 os << httpres.content_len;
72 std::ostream& std::operator<<(std::ostream& os, Z_Records & rs)
76 case Z_Records_DBOSD :
79 if (rs.u.nonSurrogateDiagnostic)
80 os << *(rs.u.nonSurrogateDiagnostic);
82 case Z_Records_multipleNSD:
83 os << *(rs.u.multipleNonSurDiagnostics);
92 std::ostream& std::operator<<(std::ostream& os, Z_DiagRecs& dr)
94 if (dr.num_diagRecs >= 1)
96 os << *dr.diagRecs[0];
106 std::ostream& std::operator<<(std::ostream& os, Z_DiagRec& dr)
110 case Z_DiagRec_defaultFormat:
111 if (dr.u.defaultFormat)
112 os << *(dr.u.defaultFormat);
114 case Z_DiagRec_externallyDefined :
115 os << "Z_DiagRec_externallyDefined";
124 std::ostream& std::operator<<(std::ostream& os, Z_DefaultDiagFormat& ddf)
127 os << *(ddf.condition) << " ";
131 case Z_DefaultDiagFormat_v2Addinfo:
133 os << ddf.u.v2Addinfo;
136 case Z_DefaultDiagFormat_v3Addinfo:
138 os << ddf.u.v3Addinfo;
148 static void dump_opt_string(std::ostream& os, const char *s)
157 static void dump_opt_int_l(std::ostream& os, const Odr_int *i, const char *lead)
166 static void dump_opt_int(std::ostream& os, const Odr_int *i)
168 dump_opt_int_l(os, i, " ");
171 std::ostream& std::operator<<(std::ostream& os, Z_APDU& zapdu)
173 switch(zapdu.which) {
175 case Z_APDU_initRequest:
176 os << " " << "initRequest";
180 = zapdu.u.initRequest;
182 Z_IdAuthentication *a = ir->idAuthentication;
183 if (a && a->which == Z_IdAuthentication_idPass)
184 dump_opt_string(os, a->u.idPass->userId);
185 else if (a && a->which == Z_IdAuthentication_open)
186 dump_opt_string(os, a->u.open);
188 dump_opt_string(os, 0);
191 std::list<std::string> vhosts;
192 mp::util::get_vhost_otherinfo(ir->otherInfo, vhosts);
194 copy(vhosts.begin(), vhosts.end(),
195 ostream_iterator<string>(os, " "));
200 dump_opt_string(os, ir->implementationId);
201 dump_opt_string(os, ir->implementationName);
202 dump_opt_string(os, ir->implementationVersion);
205 case Z_APDU_initResponse:
206 os << " " << "initResponse ";
209 = zapdu.u.initResponse;
210 if (ir->result && *(ir->result))
218 dump_opt_string(os, ir->implementationId);
219 dump_opt_string(os, ir->implementationName);
220 dump_opt_string(os, ir->implementationVersion);
223 case Z_APDU_searchRequest:
224 os << " " << "searchRequest" << " ";
227 = zapdu.u.searchRequest;
229 for (int i = 0; i < sr->num_databaseNames; i++)
231 os << sr->databaseNames[i];
232 if (i+1 != sr->num_databaseNames)
236 dump_opt_string(os, sr->resultSetName);
239 if (sr->preferredRecordSyntax)
241 char oid_name_str[OID_STR_MAX];
242 os << yaz_oid_to_string_buf(
243 sr->preferredRecordSyntax, 0, oid_name_str);
250 yaz_query_to_wrbuf(wr, sr->query);
251 os << wrbuf_cstr(wr);
254 case Z_APDU_searchResponse:
255 os << " " << "searchResponse ";
257 Z_SearchResponse *sr = zapdu.u.searchResponse;
258 if (!sr->searchStatus)
260 else if (*sr->searchStatus)
264 if (sr->records && sr->records->which != Z_Records_DBOSD)
266 os << " DIAG " << *sr->records;
270 dump_opt_int(os, sr->resultCount);
271 dump_opt_int_l(os, sr->numberOfRecordsReturned, " 1+");
272 dump_opt_int(os, sr->nextResultSetPosition);
276 case Z_APDU_presentRequest:
277 os << " " << "presentRequest";
279 Z_PresentRequest *pr = zapdu.u.presentRequest;
280 dump_opt_string(os, pr->resultSetId);
281 dump_opt_int(os, pr->resultSetStartPoint);
282 dump_opt_int_l(os, pr->numberOfRecordsRequested, "+");
283 if (pr->preferredRecordSyntax)
285 char oid_name_str[OID_STR_MAX];
286 const char *oid_name = yaz_oid_to_string_buf(
287 pr->preferredRecordSyntax, 0, oid_name_str);
289 os << " " << oid_name;
293 const char * msg = 0;
294 if (pr->recordComposition)
295 msg = mp_util::record_composition_to_esn(pr->recordComposition);
296 dump_opt_string(os, msg);
299 case Z_APDU_presentResponse:
300 os << " " << "presentResponse" << " ";
302 Z_PresentResponse *pr
303 = zapdu.u.presentResponse;
304 if (!pr->presentStatus)
308 switch (*pr->presentStatus)
310 case Z_PresentStatus_success:
312 case Z_PresentStatus_partial_1:
313 os << "Partial-1"; break;
314 case Z_PresentStatus_partial_2:
315 os << "Partial-2"; break;
316 case Z_PresentStatus_partial_3:
317 os << "Partial-3"; break;
318 case Z_PresentStatus_partial_4:
319 os << "Partial-4"; break;
320 case Z_PresentStatus_failure:
321 os << "Failure"; break;
323 os << "Unknown"; break;
326 if (pr->records && pr->records->which != Z_Records_DBOSD)
327 os << " DIAG " << *pr->records;
330 dump_opt_int(os, pr->numberOfRecordsReturned);
331 dump_opt_int(os, pr->nextResultSetPosition);
335 case Z_APDU_deleteResultSetRequest:
336 os << " " << "deleteResultSetRequest";
338 case Z_APDU_deleteResultSetResponse:
339 os << " " << "deleteResultSetResponse";
341 case Z_APDU_accessControlRequest:
342 os << " " << "accessControlRequest";
344 case Z_APDU_accessControlResponse:
345 os << " " << "accessControlResponse";
347 case Z_APDU_resourceControlRequest:
348 os << " " << "resourceControlRequest";
350 case Z_APDU_resourceControlResponse:
351 os << " " << "resourceControlResponse";
353 case Z_APDU_triggerResourceControlRequest:
354 os << " " << "triggerResourceControlRequest";
356 case Z_APDU_resourceReportRequest:
357 os << " " << "resourceReportRequest";
359 case Z_APDU_resourceReportResponse:
360 os << " " << "resourceReportResponse";
362 case Z_APDU_scanRequest:
363 os << " " << "scanRequest" << " ";
365 Z_ScanRequest *sr = zapdu.u.scanRequest;
369 for (int i = 0; i < sr->num_databaseNames; i++)
371 os << sr->databaseNames[i];
372 if (i+1 != sr->num_databaseNames)
375 dump_opt_int(os, sr->numberOfTermsRequested);
376 dump_opt_int(os, sr->preferredPositionInResponse);
377 dump_opt_int(os, sr->stepSize);
380 if (sr->termListAndStartPoint)
383 yaz_scan_to_wrbuf(wr, sr->termListAndStartPoint,
385 os << wrbuf_cstr(wr);
392 case Z_APDU_scanResponse:
393 os << " " << "scanResponse" << " ";
395 Z_ScanResponse *sr = zapdu.u.scanResponse;
402 switch (*sr->scanStatus)
407 case Z_Scan_partial_1:
410 case Z_Scan_partial_2:
413 case Z_Scan_partial_3:
416 case Z_Scan_partial_4:
419 case Z_Scan_partial_5:
429 dump_opt_int(os, sr->numberOfEntriesReturned);
430 dump_opt_int(os, sr->positionOfTerm);
431 dump_opt_int(os, sr->stepSize);
435 case Z_APDU_sortRequest:
436 os << " " << "sortRequest" << " ";
438 case Z_APDU_sortResponse:
439 os << " " << "sortResponse" << " ";
441 case Z_APDU_segmentRequest:
442 os << " " << "segmentRequest" << " ";
444 case Z_APDU_extendedServicesRequest:
445 os << " " << "extendedServicesRequest";
447 Z_ExtendedServicesRequest *er
448 = zapdu.u.extendedServicesRequest;
454 switch(*(er->function))
456 case Z_ExtendedServicesRequest_create:
459 case Z_ExtendedServicesRequest_delete:
462 case Z_ExtendedServicesRequest_modify:
474 os << " " << er->userId ;
479 os << " " << er->packageName;
484 os << " " << er->description;
490 case Z_APDU_extendedServicesResponse:
491 os << " " << "extendedServicesResponse";
493 Z_ExtendedServicesResponse *er
494 = zapdu.u.extendedServicesResponse;
497 if (er->operationStatus)
500 switch (*er->operationStatus)
502 case Z_ExtendedServicesResponse_done:
505 case Z_ExtendedServicesResponse_accepted:
508 case Z_ExtendedServicesResponse_failure:
509 if (er->num_diagnostics)
510 os << "DIAG " << **(er->diagnostics);
524 os << " " << "close" << " ";
526 Z_Close *c = zapdu.u.close;
531 os << *(c->closeReason) << " ";
533 switch (*c->closeReason)
535 case Z_Close_finished:
538 case Z_Close_shutdown:
541 case Z_Close_systemProblem:
542 os << "systemProblem";
544 case Z_Close_costLimit:
547 case Z_Close_resources:
550 case Z_Close_securityViolation:
551 os << "securityViolation";
553 case Z_Close_protocolError:
554 os << "protocolError";
556 case Z_Close_lackOfActivity:
557 os << "lackOfActivity";
559 case Z_Close_peerAbort:
562 case Z_Close_unspecified:
570 if (c->diagnosticInformation)
571 os << " " << c->diagnosticInformation;
575 case Z_APDU_duplicateDetectionRequest:
576 os << " " << "duplicateDetectionRequest";
578 case Z_APDU_duplicateDetectionResponse:
579 os << " " << "duplicateDetectionResponse";
582 os << " " << "Z_APDU " << "UNKNOWN";
594 * c-file-style: "Stroustrup"
595 * indent-tabs-mode: nil
597 * vim: shiftwidth=4 tabstop=8 expandtab