08c1f41f59432e3d11232bc4aa73390d9ad3c347
[metaproxy-moved-to-github.git] / include / metaproxy / util.hpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 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 #ifndef YP2_UTIL_HPP
20 #define YP2_UTIL_HPP
21
22 #include "package.hpp"
23
24 #include <yaz/z-core.h>
25
26 #include <boost/utility.hpp>
27 #include <boost/scoped_ptr.hpp>
28
29 #include <list>
30 #include <vector>
31 #include <sstream>
32 #include <string>
33
34
35 namespace metaproxy_1 {
36     namespace util  {
37
38
39         template<typename T>
40         std::string to_string(const T& t)
41         {
42             std::ostringstream o;
43             if(o << t)
44                 return o.str();
45             return std::string();
46         };
47
48         const char *
49         record_composition_to_esn(Z_RecordComposition *comp);
50
51         std::string http_header_value(const Z_HTTP_Header* header,
52                                                const std::string name);
53
54         std::string http_headers_debug(const Z_HTTP_Request &http_req);
55
56         void http_response(metaproxy_1::Package &package,
57                            const std::string &content,
58                            int http_code = 200);
59
60
61         int memcmp2(const void *buf1, int len1, const void *buf2, int len2);
62
63         std::string database_name_normalize(const std::string &s);
64
65         bool pqf(ODR odr, Z_APDU *apdu, const std::string &q);
66
67         std::string zQueryToString(Z_Query *query);
68
69         Z_ReferenceId **get_referenceId(const Z_APDU *apdu);
70
71         void transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst);
72
73         Z_APDU *create_APDU(ODR odr, int type, const Z_APDU *in_apdu);
74
75         bool set_databases_from_zurl(ODR odr, std::string zurl,
76                                      int *db_num, char ***db_strings);
77
78         void split_zurl(std::string zurl, std::string &host,
79                         std::list<std::string> &db);
80
81         void get_vhost_otherinfo(Z_OtherInformation *otherInformation,
82                                  std::list<std::string> &vhosts);
83
84         int remove_vhost_otherinfo(Z_OtherInformation **otherInformation,
85                                    std::list<std::string> &vhosts);
86
87         void set_vhost_otherinfo(Z_OtherInformation **otherInformation,
88                                  ODR odr,
89                                  const std::string vhost,
90                                  const int cat);
91
92         void set_vhost_otherinfo(Z_OtherInformation **otherInformation,
93                                  ODR odr,
94                                  const std::list<std::string> &vhosts);
95
96         int get_or_remove_vhost_otherinfo(
97             Z_OtherInformation **otherInformation,
98             bool remove_flag,
99             std::list<std::string> &vhosts);
100
101         void get_init_diagnostics(Z_InitResponse *res,
102                                   int &error_code, std::string &addinfo);
103
104         void get_default_diag(Z_DefaultDiagFormat *r,
105                               int &error_code, std::string &addinfo);
106
107         Z_RecordComposition *piggyback_to_RecordComposition(
108             ODR odr, Odr_int result_set_size, Z_SearchRequest *sreq);
109
110         void piggyback(int smallSetUpperBound,
111                        int largeSetLowerBound,
112                        int mediumSetPresentNumber,
113                        int result_set_size,
114                        int &number_to_present)
115 #ifdef __GNUC__
116              __attribute__ ((deprecated))
117 #endif
118             ;
119         void piggyback_sr(Z_SearchRequest *sreq,
120                           Odr_int result_set_size,
121                           Odr_int &number_to_present,
122                           const char **element_set_name);
123
124         void piggyback(Odr_int smallSetUpperBound,
125                        Odr_int largeSetLowerBound,
126                        Odr_int mediumSetPresentNumber,
127                        const char *smallSetElementSetNames,
128                        const char *mediumSetElementSetNames,
129                        Odr_int result_set_size,
130                        Odr_int &number_to_present,
131                        const char **element_set_name);
132
133         bool match_ip(const std::string &pattern, const std::string &value);
134
135         std::string uri_encode(std::string s);
136         std::string uri_decode(std::string s);
137     };
138
139     class odr : public boost::noncopyable {
140     public:
141         odr(int type);
142         odr();
143         ~odr();
144         operator ODR() const;
145         Z_APDU *create_close(const Z_APDU *in_apdu,
146                              int reason, const char *addinfo);
147         Z_APDU *create_initResponse(const Z_APDU *in_apdu,
148                                     int error, const char *addinfo);
149         Z_APDU *create_searchResponse(const Z_APDU *in_apdu,
150                                       int error, const char *addinfo);
151         Z_APDU *create_presentResponse(const Z_APDU *in_apdu,
152                                        int error, const char *addinfo);
153         Z_APDU *create_scanResponse(const Z_APDU *in_apdu,
154                                     int error, const char *addinfo);
155         Z_APDU *create_APDU(int type, const Z_APDU *in_apdu);
156
157         Z_GDU *create_HTTP_Response(metaproxy_1::Session &session,
158                                     Z_HTTP_Request *req, int code);
159         Z_GDU *create_HTTP_Response_details(metaproxy_1::Session &session,
160                                             Z_HTTP_Request *req, int code,
161                                             const char *details);
162     private:
163         ODR m_odr;
164     };
165
166     class wrbuf : public boost::noncopyable
167     {
168     public:
169         wrbuf();
170         ~wrbuf();
171         operator WRBUF() const;
172         size_t len();
173         const char *buf();
174         const char *c_str();
175         const char *c_str_null();
176     private:
177         WRBUF m_wrbuf;
178     };
179
180     class PlainFile {
181         class Rep;
182         boost::scoped_ptr<Rep> m_p;
183     public:
184         PlainFile();
185         ~PlainFile();
186         bool open(const std::string &fname);
187         bool getline(std::vector<std::string> &args);
188     };
189 }
190 #endif
191 /*
192  * Local variables:
193  * c-basic-offset: 4
194  * c-file-style: "Stroustrup"
195  * indent-tabs-mode: nil
196  * End:
197  * vim: shiftwidth=4 tabstop=8 expandtab
198  */
199