factorizing http utils and sru utild code out of SRUtoZ3959 filter and into util...
[metaproxy-moved-to-github.git] / src / util.hpp
1 /* $Id: util.hpp,v 1.20 2006-10-02 13:44:48 marc Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #ifndef YP2_UTIL_HPP
8 #define YP2_UTIL_HPP
9
10 #include "package.hpp"
11
12 #include <yaz/z-core.h>
13
14 #include <boost/utility.hpp>
15 #include <boost/scoped_ptr.hpp>
16
17 #include <list>
18 #include <vector>
19 #include <sstream>
20 #include <string>
21
22
23 namespace metaproxy_1 {
24     namespace util  {
25
26         
27         template<typename T> 
28         std::string to_string(const T& t)
29         {
30             std::ostringstream o;
31             if(o << t)
32                 return o.str();    
33             return std::string();
34         };
35
36         std::string http_header_value(const Z_HTTP_Header* header, 
37                                                const std::string name);
38
39         std::string http_headers_debug(const Z_HTTP_Request &http_req);        
40
41         int memcmp2(const void *buf1, int len1, const void *buf2, int len2);
42
43         std::string database_name_normalize(const std::string &s);
44
45         bool pqf(ODR odr, Z_APDU *apdu, const std::string &q);
46
47         std::string zQueryToString(Z_Query *query);
48
49         Z_ReferenceId **get_referenceId(const Z_APDU *apdu);
50
51         void transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst);
52
53         Z_APDU *create_APDU(ODR odr, int type, const Z_APDU *in_apdu);
54
55         bool set_databases_from_zurl(ODR odr, std::string zurl,
56                                      int *db_num, char ***db_strings);
57
58         void split_zurl(std::string zurl, std::string &host,
59                         std::list<std::string> &db);
60         
61         void get_vhost_otherinfo(Z_OtherInformation *otherInformation,
62                                  std::list<std::string> &vhosts);
63         
64         int remove_vhost_otherinfo(Z_OtherInformation **otherInformation,
65                                    std::list<std::string> &vhosts);
66
67         void set_vhost_otherinfo(Z_OtherInformation **otherInformation,
68                                  ODR odr,
69                                  const std::list<std::string> &vhosts);
70
71         int get_or_remove_vhost_otherinfo(
72             Z_OtherInformation **otherInformation,
73             bool remove_flag,
74             std::list<std::string> &vhosts);
75
76         void get_init_diagnostics(Z_InitResponse *res,
77                                   int &error_code, std::string &addinfo);
78
79         void get_default_diag(Z_DefaultDiagFormat *r,
80                               int &error_code, std::string &addinfo);
81
82         void piggyback(int smallSetUpperBound,
83                        int largeSetLowerBound,
84                        int mediumSetPresentNumber,
85                        int result_set_size,
86                        int &number_to_present);
87     };
88
89     class odr : public boost::noncopyable {
90     public:
91         odr(int type);
92         odr();
93         ~odr();
94         operator ODR() const;
95         Z_APDU *create_close(const Z_APDU *in_apdu, 
96                              int reason, const char *addinfo);
97         Z_APDU *create_initResponse(const Z_APDU *in_apdu, 
98                                     int error, const char *addinfo);
99         Z_APDU *create_searchResponse(const Z_APDU *in_apdu,
100                                       int error, const char *addinfo);
101         Z_APDU *create_presentResponse(const Z_APDU *in_apdu,
102                                        int error, const char *addinfo);
103         Z_APDU *create_scanResponse(const Z_APDU *in_apdu,
104                                     int error, const char *addinfo);
105         Z_APDU *create_APDU(int type, const Z_APDU *in_apdu);
106
107         Z_GDU *create_HTTP_Response(metaproxy_1::Session &session,
108                                     Z_HTTP_Request *req, int code);
109     private:
110         ODR m_odr;
111     };
112
113     class PlainFile {
114         class Rep;
115         boost::scoped_ptr<Rep> m_p;
116     public:
117         PlainFile();
118         ~PlainFile();
119         bool open(const std::string &fname);
120         bool getline(std::vector<std::string> &args);
121     };
122 }
123 #endif
124 /*
125  * Local variables:
126  * c-basic-offset: 4
127  * indent-tabs-mode: nil
128  * c-file-style: "stroustrup"
129  * End:
130  * vim: shiftwidth=4 tabstop=8 expandtab
131  */