1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2012 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 << "Z_Records_multipleNSD";
84 //os << *(rs.u.multipleNonSurDiagnostics);
93 std::ostream& std::operator<<(std::ostream& os, Z_DiagRec& dr)
97 case Z_DiagRec_defaultFormat:
98 if (dr.u.defaultFormat)
99 os << *(dr.u.defaultFormat);
101 case Z_DiagRec_externallyDefined :
102 os << "Z_DiagRec_externallyDefined";
111 std::ostream& std::operator<<(std::ostream& os, Z_DefaultDiagFormat& ddf)
114 os << *(ddf.condition) << " ";
118 case Z_DefaultDiagFormat_v2Addinfo:
119 os << ddf.u.v2Addinfo;
121 case Z_DefaultDiagFormat_v3Addinfo:
122 os << ddf.u.v3Addinfo;
125 os << "Z_DefaultDiagFormat" ;
131 static void dump_opt_string(std::ostream& os, const char *s)
140 static void dump_opt_int(std::ostream& os, const Odr_int *i)
149 std::ostream& std::operator<<(std::ostream& os, Z_APDU& zapdu)
151 switch(zapdu.which) {
153 case Z_APDU_initRequest:
154 os << " " << "initRequest";
158 = zapdu.u.initRequest;
160 Z_IdAuthentication *a = ir->idAuthentication;
161 if (a && a->which == Z_IdAuthentication_idPass)
162 dump_opt_string(os, a->u.idPass->userId);
163 else if (a && a->which == Z_IdAuthentication_open)
164 dump_opt_string(os, a->u.open);
166 dump_opt_string(os, 0);
169 std::list<std::string> vhosts;
170 mp::util::get_vhost_otherinfo(ir->otherInfo, vhosts);
172 copy(vhosts.begin(), vhosts.end(),
173 ostream_iterator<string>(os, " "));
178 dump_opt_string(os, ir->implementationId);
179 dump_opt_string(os, ir->implementationName);
180 dump_opt_string(os, ir->implementationVersion);
183 case Z_APDU_initResponse:
184 os << " " << "initResponse ";
187 = zapdu.u.initResponse;
188 if (ir->result && *(ir->result))
196 dump_opt_string(os, ir->implementationId);
197 dump_opt_string(os, ir->implementationName);
198 dump_opt_string(os, ir->implementationVersion);
201 case Z_APDU_searchRequest:
202 os << " " << "searchRequest" << " ";
205 = zapdu.u.searchRequest;
207 for (int i = 0; i < sr->num_databaseNames; i++)
209 os << sr->databaseNames[i];
210 if (i+1 != sr->num_databaseNames)
214 dump_opt_string(os, sr->resultSetName);
217 if (sr->preferredRecordSyntax)
219 char oid_name_str[OID_STR_MAX];
220 const char *oid_name = yaz_oid_to_string_buf(
221 sr->preferredRecordSyntax, 0, oid_name_str);
229 WRBUF wr = wrbuf_alloc();
230 yaz_query_to_wrbuf(wr, sr->query);
231 os << wrbuf_cstr(wr);
235 case Z_APDU_searchResponse:
236 os << " " << "searchResponse ";
238 Z_SearchResponse *sr = zapdu.u.searchResponse;
239 if (!sr->searchStatus)
241 else if (*sr->searchStatus)
245 if (sr->records && sr->records->which != Z_Records_DBOSD)
247 os << " DIAG " << *sr->records;
251 dump_opt_int(os, sr->resultCount);
252 dump_opt_int(os, sr->numberOfRecordsReturned);
253 dump_opt_int(os, sr->nextResultSetPosition);
257 case Z_APDU_presentRequest:
258 os << " " << "presentRequest";
260 Z_PresentRequest *pr = zapdu.u.presentRequest;
261 dump_opt_string(os, pr->resultSetId);
262 dump_opt_int(os, pr->resultSetStartPoint);
263 dump_opt_int(os, pr->numberOfRecordsRequested);
264 if (pr->preferredRecordSyntax)
266 char oid_name_str[OID_STR_MAX];
267 const char *oid_name = yaz_oid_to_string_buf(
268 pr->preferredRecordSyntax, 0, oid_name_str);
270 os << " " << oid_name;
274 const char * msg = 0;
275 if (pr->recordComposition)
276 msg = mp_util::record_composition_to_esn(pr->recordComposition);
277 dump_opt_string(os, msg);
280 case Z_APDU_presentResponse:
281 os << " " << "presentResponse" << " ";
283 Z_PresentResponse *pr
284 = zapdu.u.presentResponse;
285 if (!pr->presentStatus)
289 switch (*pr->presentStatus)
291 case Z_PresentStatus_success:
293 case Z_PresentStatus_partial_1:
294 os << "Partial-1"; break;
295 case Z_PresentStatus_partial_2:
296 os << "Partial-2"; break;
297 case Z_PresentStatus_partial_3:
298 os << "Partial-3"; break;
299 case Z_PresentStatus_partial_4:
300 os << "Partial-4"; break;
301 case Z_PresentStatus_failure:
302 os << "Failure"; break;
304 os << "Unknown"; break;
307 if (pr->records && pr->records->which != Z_Records_DBOSD)
308 os << " DIAG " << *pr->records;
311 dump_opt_int(os, pr->numberOfRecordsReturned);
312 dump_opt_int(os, pr->nextResultSetPosition);
316 case Z_APDU_deleteResultSetRequest:
317 os << " " << "deleteResultSetRequest";
319 case Z_APDU_deleteResultSetResponse:
320 os << " " << "deleteResultSetResponse";
322 case Z_APDU_accessControlRequest:
323 os << " " << "accessControlRequest";
325 case Z_APDU_accessControlResponse:
326 os << " " << "accessControlResponse";
328 case Z_APDU_resourceControlRequest:
329 os << " " << "resourceControlRequest";
331 case Z_APDU_resourceControlResponse:
332 os << " " << "resourceControlResponse";
334 case Z_APDU_triggerResourceControlRequest:
335 os << " " << "triggerResourceControlRequest";
337 case Z_APDU_resourceReportRequest:
338 os << " " << "resourceReportRequest";
340 case Z_APDU_resourceReportResponse:
341 os << " " << "resourceReportResponse";
343 case Z_APDU_scanRequest:
344 os << " " << "scanRequest" << " ";
346 Z_ScanRequest *sr = zapdu.u.scanRequest;
350 for (int i = 0; i < sr->num_databaseNames; i++)
352 os << sr->databaseNames[i];
353 if (i+1 != sr->num_databaseNames)
356 dump_opt_int(os, sr->numberOfTermsRequested);
357 dump_opt_int(os, sr->preferredPositionInResponse);
358 dump_opt_int(os, sr->stepSize);
361 if (sr->termListAndStartPoint)
363 WRBUF wr = wrbuf_alloc();
364 yaz_scan_to_wrbuf(wr, sr->termListAndStartPoint,
366 os << wrbuf_cstr(wr);
374 case Z_APDU_scanResponse:
375 os << " " << "scanResponse" << " ";
377 Z_ScanResponse *sr = zapdu.u.scanResponse;
384 switch (*sr->scanStatus)
389 case Z_Scan_partial_1:
392 case Z_Scan_partial_2:
395 case Z_Scan_partial_3:
398 case Z_Scan_partial_4:
401 case Z_Scan_partial_5:
411 dump_opt_int(os, sr->numberOfEntriesReturned);
412 dump_opt_int(os, sr->positionOfTerm);
413 dump_opt_int(os, sr->stepSize);
417 case Z_APDU_sortRequest:
418 os << " " << "sortRequest" << " ";
420 case Z_APDU_sortResponse:
421 os << " " << "sortResponse" << " ";
423 case Z_APDU_segmentRequest:
424 os << " " << "segmentRequest" << " ";
426 case Z_APDU_extendedServicesRequest:
427 os << " " << "extendedServicesRequest";
429 Z_ExtendedServicesRequest *er
430 = zapdu.u.extendedServicesRequest;
436 switch(*(er->function))
438 case Z_ExtendedServicesRequest_create:
441 case Z_ExtendedServicesRequest_delete:
444 case Z_ExtendedServicesRequest_modify:
456 os << " " << er->userId ;
461 os << " " << er->packageName;
466 os << " " << er->description;
472 case Z_APDU_extendedServicesResponse:
473 os << " " << "extendedServicesResponse";
475 Z_ExtendedServicesResponse *er
476 = zapdu.u.extendedServicesResponse;
479 if (er->operationStatus)
482 switch (*er->operationStatus)
484 case Z_ExtendedServicesResponse_done:
487 case Z_ExtendedServicesResponse_accepted:
490 case Z_ExtendedServicesResponse_failure:
491 if (er->num_diagnostics)
492 os << "DIAG " << **(er->diagnostics);
506 os << " " << "close" << " ";
508 Z_Close *c = zapdu.u.close;
513 os << *(c->closeReason) << " ";
515 switch (*c->closeReason)
517 case Z_Close_finished:
520 case Z_Close_shutdown:
523 case Z_Close_systemProblem:
524 os << "systemProblem";
526 case Z_Close_costLimit:
529 case Z_Close_resources:
532 case Z_Close_securityViolation:
533 os << "securityViolation";
535 case Z_Close_protocolError:
536 os << "protocolError";
538 case Z_Close_lackOfActivity:
539 os << "lackOfActivity";
541 case Z_Close_peerAbort:
544 case Z_Close_unspecified:
552 if (c->diagnosticInformation)
553 os << " " << c->diagnosticInformation;
557 case Z_APDU_duplicateDetectionRequest:
558 os << " " << "duplicateDetectionRequest";
560 case Z_APDU_duplicateDetectionResponse:
561 os << " " << "duplicateDetectionResponse";
564 os << " " << "Z_APDU " << "UNKNOWN";
576 * c-file-style: "Stroustrup"
577 * indent-tabs-mode: nil
579 * vim: shiftwidth=4 tabstop=8 expandtab