Reformat of HTMLparser source
[metaproxy-moved-to-github.git] / src / gduutil.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "gduutil.hpp"
20 #include <metaproxy/util.hpp>
21
22 #include <yaz/wrbuf.h>
23 #include <yaz/oid_db.h>
24 #include <yaz/querytowrbuf.h>
25
26 #include <iostream>
27
28 namespace mp = metaproxy_1;
29
30 // Doxygen doesn't like mp::gdu, so we use this instead
31 namespace mp_util = metaproxy_1::util;
32
33 std::ostream& std::operator<<(std::ostream& os,  Z_GDU& zgdu)
34 {
35     if (zgdu.which == Z_GDU_Z3950)
36     {
37         os << "Z3950";
38         if (zgdu.u.z3950)
39             os << *(zgdu.u.z3950);
40     }
41     else if (zgdu.which == Z_GDU_HTTP_Request)
42     {
43         os << "HTTP_Request";
44         if (zgdu.u.HTTP_Request)
45             os << " " << *(zgdu.u.HTTP_Request);
46     }
47     else if (zgdu.which == Z_GDU_HTTP_Response)
48     {
49         os << "HTTP_Response";
50         if (zgdu.u.HTTP_Response)
51             os << " " << *(zgdu.u.HTTP_Response);
52     }
53     else
54         os << "Z_GDU";
55     return os;
56 }
57
58 std::ostream& std::operator<<(std::ostream& os, Z_HTTP_Request& httpreq)
59 {
60     os << httpreq.method << " ";
61     os << httpreq.path;
62     return os;
63 }
64
65 std::ostream& std::operator<<(std::ostream& os, Z_HTTP_Response& httpres)
66 {
67     os << httpres.code << " ";
68     os << httpres.content_len;
69     return os;
70 }
71
72 std::ostream& std::operator<<(std::ostream& os, Z_Records & rs)
73 {
74     switch (rs.which)
75     {
76     case Z_Records_DBOSD :
77         break;
78     case Z_Records_NSD:
79         if (rs.u.nonSurrogateDiagnostic)
80             os << *(rs.u.nonSurrogateDiagnostic);
81         break;
82     case Z_Records_multipleNSD:
83         os << *(rs.u.multipleNonSurDiagnostics);
84         break;
85     default:
86         os << "Z_Records" ;
87     }
88
89     return os;
90 }
91
92 std::ostream& std::operator<<(std::ostream& os, Z_DiagRecs& dr)
93 {
94     if (dr.num_diagRecs >= 1)
95     {
96         os << *dr.diagRecs[0];
97     }
98     else
99     {
100         os << "multipleNSD";
101     }
102     return os;
103 }
104
105
106 std::ostream& std::operator<<(std::ostream& os, Z_DiagRec& dr)
107 {
108     switch (dr.which)
109     {
110     case Z_DiagRec_defaultFormat:
111         if (dr.u.defaultFormat)
112             os << *(dr.u.defaultFormat);
113         break;
114     case Z_DiagRec_externallyDefined :
115         os << "Z_DiagRec_externallyDefined";
116         break;
117     default:
118         os << "Z_DiagRec" ;
119     }
120
121     return os;
122 }
123
124 std::ostream& std::operator<<(std::ostream& os, Z_DefaultDiagFormat& ddf)
125 {
126     if (ddf.condition)
127         os << *(ddf.condition) << " ";
128
129     switch (ddf.which)
130     {
131     case Z_DefaultDiagFormat_v2Addinfo:
132         os << "\"";
133         os << ddf.u.v2Addinfo;
134         os << "\"";
135         break;
136     case Z_DefaultDiagFormat_v3Addinfo:
137         os << "\"";
138         os << ddf.u.v3Addinfo;
139         os << "\"";
140         break;
141     default:
142         os << "-";
143     }
144
145     return os;
146 }
147
148 static void dump_opt_string(std::ostream& os, const char *s)
149 {
150     os << " ";
151     if (s)
152         os << s;
153     else
154         os << "-";
155 }
156
157 static void dump_opt_int_l(std::ostream& os, const Odr_int *i, const char *lead)
158 {
159     os << lead;
160     if (i)
161         os << *i;
162     else
163         os << "-";
164 }
165
166 static void dump_opt_int(std::ostream& os, const Odr_int *i)
167 {
168     dump_opt_int_l(os, i, " ");
169 }
170
171 std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
172 {
173     switch(zapdu.which) {
174
175     case Z_APDU_initRequest:
176         os << " " << "initRequest";
177
178         {
179             Z_InitRequest *ir
180                 = zapdu.u.initRequest;
181
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);
187             else
188                 dump_opt_string(os, 0);
189
190             os << " ";
191             std::list<std::string> vhosts;
192             mp::util::get_vhost_otherinfo(ir->otherInfo, vhosts);
193             if (vhosts.size()){
194                 copy(vhosts.begin(), vhosts.end(),
195                      ostream_iterator<string>(os, " "));
196             }
197             else
198                 os << "-" ;
199
200             dump_opt_string(os, ir->implementationId);
201             dump_opt_string(os, ir->implementationName);
202             dump_opt_string(os, ir->implementationVersion);
203         }
204         break;
205     case Z_APDU_initResponse:
206         os << " " << "initResponse ";
207         {
208             Z_InitResponse *ir
209                 = zapdu.u.initResponse;
210             if (ir->result && *(ir->result))
211             {
212                 os << "OK";
213             }
214             else
215             {
216                 os << "FAIL";
217             }
218             dump_opt_string(os, ir->implementationId);
219             dump_opt_string(os, ir->implementationName);
220             dump_opt_string(os, ir->implementationVersion);
221         }
222         break;
223     case Z_APDU_searchRequest:
224         os << " " << "searchRequest" << " ";
225         {
226             Z_SearchRequest *sr
227                 = zapdu.u.searchRequest;
228
229             for (int i = 0; i < sr->num_databaseNames; i++)
230             {
231                 os << sr->databaseNames[i];
232                 if (i+1 !=  sr->num_databaseNames)
233                     os << "+";
234             }
235
236             dump_opt_string(os, sr->resultSetName);
237
238             os << " ";
239             if (sr->preferredRecordSyntax)
240             {
241                 char oid_name_str[OID_STR_MAX];
242                 os << yaz_oid_to_string_buf(
243                     sr->preferredRecordSyntax, 0, oid_name_str);
244             }
245             else
246                 os << "-";
247
248             os << " ";
249             mp::wrbuf wr;
250             yaz_query_to_wrbuf(wr, sr->query);
251             os << wrbuf_cstr(wr);
252         }
253         break;
254     case Z_APDU_searchResponse:
255         os << " " << "searchResponse ";
256         {
257             Z_SearchResponse *sr = zapdu.u.searchResponse;
258             if (!sr->searchStatus)
259                 os << "Unknown";
260             else if (*sr->searchStatus)
261                 os << "OK";
262             else
263                 os << "Failure";
264             if (sr->records && sr->records->which != Z_Records_DBOSD)
265             {
266                 os << " DIAG " << *sr->records;
267             }
268             else
269             {
270                 dump_opt_int(os, sr->resultCount);
271                 dump_opt_int_l(os, sr->numberOfRecordsReturned, " 1+");
272                 dump_opt_int(os, sr->nextResultSetPosition);
273             }
274         }
275         break;
276     case Z_APDU_presentRequest:
277         os << " " << "presentRequest";
278         {
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)
284             {
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);
288
289                 os << " " << oid_name;
290             }
291             else
292                 os << " -";
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);
297         }
298         break;
299     case Z_APDU_presentResponse:
300         os << " " << "presentResponse" << " ";
301         {
302             Z_PresentResponse *pr
303                 = zapdu.u.presentResponse;
304             if (!pr->presentStatus)
305                 os << "Unknown";
306             else
307             {
308                 switch (*pr->presentStatus)
309                 {
310                 case Z_PresentStatus_success:
311                     os << "OK"; break;
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;
322                 default:
323                     os << "Unknown"; break;
324                 }
325             }
326             if (pr->records && pr->records->which != Z_Records_DBOSD)
327                 os << " DIAG " << *pr->records;
328             else
329             {
330                 dump_opt_int(os, pr->numberOfRecordsReturned);
331                 dump_opt_int(os, pr->nextResultSetPosition);
332             }
333         }
334         break;
335     case Z_APDU_deleteResultSetRequest:
336         os << " " << "deleteResultSetRequest";
337         break;
338     case Z_APDU_deleteResultSetResponse:
339         os << " " << "deleteResultSetResponse";
340         break;
341     case Z_APDU_accessControlRequest:
342         os << " " << "accessControlRequest";
343         break;
344     case Z_APDU_accessControlResponse:
345         os << " " << "accessControlResponse";
346         break;
347     case Z_APDU_resourceControlRequest:
348         os << " " << "resourceControlRequest";
349         break;
350     case Z_APDU_resourceControlResponse:
351         os << " " << "resourceControlResponse";
352         break;
353     case Z_APDU_triggerResourceControlRequest:
354         os << " " << "triggerResourceControlRequest";
355         break;
356     case Z_APDU_resourceReportRequest:
357         os << " " << "resourceReportRequest";
358         break;
359     case Z_APDU_resourceReportResponse:
360         os << " " << "resourceReportResponse";
361         break;
362     case Z_APDU_scanRequest:
363         os << " " << "scanRequest" << " ";
364         {
365             Z_ScanRequest *sr = zapdu.u.scanRequest;
366
367             if (sr)
368             {
369                 for (int i = 0; i < sr->num_databaseNames; i++)
370                 {
371                     os << sr->databaseNames[i];
372                     if (i+1 !=  sr->num_databaseNames)
373                         os << "+";
374                 }
375                 dump_opt_int(os, sr->numberOfTermsRequested);
376                 dump_opt_int(os, sr->preferredPositionInResponse);
377                 dump_opt_int(os, sr->stepSize);
378
379                 os << " ";
380                 if (sr->termListAndStartPoint)
381                 {
382                     mp::wrbuf wr;
383                     yaz_scan_to_wrbuf(wr, sr->termListAndStartPoint,
384                                       sr->attributeSet);
385                     os << wrbuf_cstr(wr);
386                 }
387                 else
388                     os << "-";
389             }
390         }
391         break;
392     case Z_APDU_scanResponse:
393         os << " " << "scanResponse" << " ";
394         {
395             Z_ScanResponse *sr = zapdu.u.scanResponse;
396             if (sr)
397             {
398                 if (!sr->scanStatus)
399                     os << "OK";
400                 else
401                 {
402                     switch (*sr->scanStatus)
403                     {
404                     case Z_Scan_success:
405                         os << "OK";
406                         break;
407                     case Z_Scan_partial_1:
408                         os << "partial_1";
409                         break;
410                     case Z_Scan_partial_2:
411                         os << "partial_2";
412                         break;
413                     case Z_Scan_partial_3:
414                         os << "partial_3";
415                         break;
416                     case Z_Scan_partial_4:
417                         os << "partial_4";
418                         break;
419                     case Z_Scan_partial_5:
420                         os << "partial_5";
421                         break;
422                     case Z_Scan_failure:
423                         os << "failure";
424                         break;
425                     default:
426                         os << "unknown";
427                     }
428                 }
429                 dump_opt_int(os, sr->numberOfEntriesReturned);
430                 dump_opt_int(os, sr->positionOfTerm);
431                 dump_opt_int(os, sr->stepSize);
432             }
433         }
434         break;
435     case Z_APDU_sortRequest:
436         os << " " << "sortRequest" << " ";
437         break;
438     case Z_APDU_sortResponse:
439         os << " " << "sortResponse" << " ";
440         break;
441     case Z_APDU_segmentRequest:
442         os << " " << "segmentRequest" << " ";
443         break;
444     case Z_APDU_extendedServicesRequest:
445         os << " " << "extendedServicesRequest";
446         {
447             Z_ExtendedServicesRequest *er
448                 = zapdu.u.extendedServicesRequest;
449             if (er)
450             {
451                 if (er->function)
452                 {
453                     os << " ";
454                     switch(*(er->function))
455                     {
456                     case Z_ExtendedServicesRequest_create:
457                         os << "create";
458                         break;
459                     case Z_ExtendedServicesRequest_delete:
460                         os << "delete";
461                         break;
462                     case Z_ExtendedServicesRequest_modify:
463                         os << "modify";
464                         break;
465                     default:
466                         os << "unknown";
467                     }
468                 }
469                 else
470                     os << " -";
471
472
473                 if (er->userId)
474                     os << " " << er->userId ;
475                 else
476                     os << " -";
477
478                 if (er->packageName)
479                     os << " " << er->packageName;
480                 else
481                     os << " -";
482
483                 if (er->description)
484                     os << " " << er->description;
485                 else
486                     os << " -";
487             }
488         }
489         break;
490     case Z_APDU_extendedServicesResponse:
491         os << " " << "extendedServicesResponse";
492         {
493             Z_ExtendedServicesResponse *er
494                 = zapdu.u.extendedServicesResponse;
495             if (er)
496             {
497                 if (er->operationStatus)
498                 {
499                     os << " ";
500                     switch (*er->operationStatus)
501                     {
502                     case Z_ExtendedServicesResponse_done:
503                         os << "OK";
504                         break;
505                     case Z_ExtendedServicesResponse_accepted:
506                         os << "ACCEPT";
507                         break;
508                     case Z_ExtendedServicesResponse_failure:
509                         if (er->num_diagnostics)
510                             os << "DIAG " << **(er->diagnostics);
511                         else
512                             os << "ERROR";
513                         break;
514                     default:
515                         os << "unknown";
516                     }
517                 }
518                 else
519                     os << " -";
520             }
521         }
522         break;
523     case Z_APDU_close:
524         os  << " " << "close" << " ";
525         {
526             Z_Close *c = zapdu.u.close;
527             if (c)
528             {
529                 if (c->closeReason)
530                 {
531                     os << *(c->closeReason) << " ";
532
533                     switch (*c->closeReason)
534                     {
535                     case Z_Close_finished:
536                         os << "finished";
537                         break;
538                     case Z_Close_shutdown:
539                         os << "shutdown";
540                         break;
541                     case Z_Close_systemProblem:
542                         os << "systemProblem";
543                         break;
544                     case Z_Close_costLimit:
545                         os << "costLimit";
546                         break;
547                     case Z_Close_resources:
548                         os << "resources";
549                         break;
550                     case Z_Close_securityViolation:
551                         os << "securityViolation";
552                         break;
553                     case Z_Close_protocolError:
554                         os << "protocolError";
555                         break;
556                     case Z_Close_lackOfActivity:
557                         os << "lackOfActivity";
558                         break;
559                     case Z_Close_peerAbort:
560                         os << "peerAbort";
561                         break;
562                     case Z_Close_unspecified:
563                         os << "unspecified";
564                         break;
565                     default:
566                         os << "unknown";
567                     }
568                 }
569
570                 if (c->diagnosticInformation)
571                     os << " " << c->diagnosticInformation;
572             }
573         }
574         break;
575     case Z_APDU_duplicateDetectionRequest:
576         os << " " << "duplicateDetectionRequest";
577         break;
578     case Z_APDU_duplicateDetectionResponse:
579         os << " " << "duplicateDetectionResponse";
580         break;
581     default:
582         os << " " << "Z_APDU " << "UNKNOWN";
583     }
584
585     return os;
586 }
587
588
589
590
591 /*
592  * Local variables:
593  * c-basic-offset: 4
594  * c-file-style: "Stroustrup"
595  * indent-tabs-mode: nil
596  * End:
597  * vim: shiftwidth=4 tabstop=8 expandtab
598  */
599