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