factorizing HTTP specific code out of SRU2Z3950 filter into utils.hpp
[metaproxy-moved-to-github.git] / src / util.hpp
1 /* $Id: util.hpp,v 1.21 2006-10-03 07:57:40 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         void http_response(metaproxy_1::Package &package, 
42                            const std::string &content, 
43                            int http_code = 200);
44
45
46         int memcmp2(const void *buf1, int len1, const void *buf2, int len2);
47
48         std::string database_name_normalize(const std::string &s);
49
50         bool pqf(ODR odr, Z_APDU *apdu, const std::string &q);
51
52         std::string zQueryToString(Z_Query *query);
53
54         Z_ReferenceId **get_referenceId(const Z_APDU *apdu);
55
56         void transfer_referenceId(ODR odr, const Z_APDU *src, Z_APDU *dst);
57
58         Z_APDU *create_APDU(ODR odr, int type, const Z_APDU *in_apdu);
59
60         bool set_databases_from_zurl(ODR odr, std::string zurl,
61                                      int *db_num, char ***db_strings);
62
63         void split_zurl(std::string zurl, std::string &host,
64                         std::list<std::string> &db);
65         
66         void get_vhost_otherinfo(Z_OtherInformation *otherInformation,
67                                  std::list<std::string> &vhosts);
68         
69         int remove_vhost_otherinfo(Z_OtherInformation **otherInformation,
70                                    std::list<std::string> &vhosts);
71
72         void set_vhost_otherinfo(Z_OtherInformation **otherInformation,
73                                  ODR odr,
74                                  const std::list<std::string> &vhosts);
75
76         int get_or_remove_vhost_otherinfo(
77             Z_OtherInformation **otherInformation,
78             bool remove_flag,
79             std::list<std::string> &vhosts);
80
81         void get_init_diagnostics(Z_InitResponse *res,
82                                   int &error_code, std::string &addinfo);
83
84         void get_default_diag(Z_DefaultDiagFormat *r,
85                               int &error_code, std::string &addinfo);
86
87         void piggyback(int smallSetUpperBound,
88                        int largeSetLowerBound,
89                        int mediumSetPresentNumber,
90                        int result_set_size,
91                        int &number_to_present);
92     };
93
94     class odr : public boost::noncopyable {
95     public:
96         odr(int type);
97         odr();
98         ~odr();
99         operator ODR() const;
100         Z_APDU *create_close(const Z_APDU *in_apdu, 
101                              int reason, const char *addinfo);
102         Z_APDU *create_initResponse(const Z_APDU *in_apdu, 
103                                     int error, const char *addinfo);
104         Z_APDU *create_searchResponse(const Z_APDU *in_apdu,
105                                       int error, const char *addinfo);
106         Z_APDU *create_presentResponse(const Z_APDU *in_apdu,
107                                        int error, const char *addinfo);
108         Z_APDU *create_scanResponse(const Z_APDU *in_apdu,
109                                     int error, const char *addinfo);
110         Z_APDU *create_APDU(int type, const Z_APDU *in_apdu);
111
112         Z_GDU *create_HTTP_Response(metaproxy_1::Session &session,
113                                     Z_HTTP_Request *req, int code);
114     private:
115         ODR m_odr;
116     };
117
118     class PlainFile {
119         class Rep;
120         boost::scoped_ptr<Rep> m_p;
121     public:
122         PlainFile();
123         ~PlainFile();
124         bool open(const std::string &fname);
125         bool getline(std::vector<std::string> &args);
126     };
127 }
128 #endif
129 /*
130  * Local variables:
131  * c-basic-offset: 4
132  * indent-tabs-mode: nil
133  * c-file-style: "stroustrup"
134  * End:
135  * vim: shiftwidth=4 tabstop=8 expandtab
136  */