Bug fixes. Explain document in config. Logging changes - dsp elapsed time.
[yazpp-moved-to-github.git] / include / yaz++ / proxy.h
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: proxy.h,v 1.24 2003-12-22 15:16:23 adam Exp $
6  */
7
8 #include <sys/time.h>
9 #include <yaz++/z-assoc.h>
10 #include <yaz++/z-query.h>
11 #include <yaz++/z-databases.h>
12 #include <yaz/cql.h>
13 #if HAVE_XML2
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16 #endif
17
18 class Yaz_Proxy;
19
20 #define MAX_ZURL_PLEX 10
21
22 #define PROXY_LOG_APDU_CLIENT 1
23 #define PROXY_LOG_APDU_SERVER 2
24 #define PROXY_LOG_REQ_CLIENT 4
25 #define PROXY_LOG_REQ_SERVER 8
26
27 struct Yaz_RecordCache_Entry;
28
29 class YAZ_EXPORT Yaz_ProxyConfig {
30 public:
31     Yaz_ProxyConfig();
32     ~Yaz_ProxyConfig();
33     int read_xml(const char *fname);
34
35     int get_target_no(int no,
36                       const char **name,
37                       const char **url,
38                       int *limit_bw,
39                       int *limit_pdu,
40                       int *limit_req,
41                       int *target_idletime,
42                       int *client_idletime,
43                       int *max_clients,
44                       int *keepalive_limit_bw,
45                       int *keepalive_limit_pdu,
46                       int *pre_init,
47                       const char **cql2rpn);
48     
49     void get_generic_info(int *log_mask, int *max_clients);
50
51     void get_target_info(const char *name, const char **url,
52                          int *limit_bw, int *limit_pdu, int *limit_req,
53                          int *target_idletime, int *client_idletime,
54                          int *max_clients,
55                          int *keepalive_limit_bw, int *keepalive_limit_pdu,
56                          int *pre_init,
57                          const char **cql2rpn);
58
59     int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
60     int check_syntax(ODR odr, const char *name,
61                      Odr_oid *syntax, char **addinfo);
62     char *get_explain(ODR odr, const char *name, const char *db,
63                       int *len);
64 private:
65     void operator=(const Yaz_ProxyConfig &conf);
66     int mycmp(const char *hay, const char *item, size_t len);
67 #if HAVE_XML2
68     xmlDocPtr m_docPtr;
69     xmlNodePtr m_proxyPtr;
70     void return_target_info(xmlNodePtr ptr, const char **url,
71                             int *limit_bw, int *limit_pdu, int *limit_req,
72                             int *target_idletime, int *client_idletime,
73                             int *keepalive_limit_bw, int *keepalive_limit_pdu,
74                             int *pre_init, const char **cql2rpn);
75     void return_limit(xmlNodePtr ptr,
76                       int *limit_bw, int *limit_pdu, int *limit_req);
77     int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
78                      char **addinfo);
79     xmlNodePtr find_target_node(const char *name, const char *db);
80     xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
81     const char *get_text(xmlNodePtr ptr);
82     int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
83                                 Z_AttributeList *attrs,
84                                 char **addinfo);
85     int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
86                                char **addinfo);
87 #endif
88     int m_copy;
89     int match_list(int v, const char *m);
90     int atoi_l(const char **cp);
91
92 };
93
94 class YAZ_EXPORT Yaz_RecordCache {
95  public:
96     Yaz_RecordCache ();
97     ~Yaz_RecordCache ();
98     void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
99     
100     int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
101                 Odr_oid *syntax, Z_RecordComposition *comp);
102     void clear();
103
104     void copy_searchRequest(Z_SearchRequest *sr);
105     void copy_presentRequest(Z_PresentRequest *pr);
106     void set_max_size(int sz);
107  private:
108     NMEM m_mem;
109     Yaz_RecordCache_Entry *m_entries;
110     Z_SearchRequest *m_searchRequest;
111     Z_PresentRequest *m_presentRequest;
112     int match (Yaz_RecordCache_Entry *entry,
113                Odr_oid *syntax, int offset,
114                Z_RecordComposition *comp);
115     int m_max_size;
116 };
117
118 class YAZ_EXPORT Yaz_bw {
119  public:
120     Yaz_bw(int sz);
121     ~Yaz_bw();
122     void add_bytes(int m);
123     int get_total();
124  private:
125     long m_sec;   // time of most recent bucket
126     int *m_bucket;
127     int m_ptr;
128     int m_size;
129 };
130
131 /// Private class
132 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
133     friend class Yaz_Proxy;
134     Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
135                     Yaz_Proxy *parent);
136     ~Yaz_ProxyClient();
137     void recv_GDU(Z_GDU *apdu, int len);
138     void recv_Z_PDU(Z_APDU *apdu, int len);
139     void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
140     IYaz_PDU_Observer* sessionNotify
141         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
142     void shutdown();
143     Yaz_Proxy *m_server;
144     void failNotify();
145     void timeoutNotify();
146     void connectNotify();
147     int send_to_target(Z_APDU *apdu);
148     const char *get_session_str();
149     char *m_cookie;
150     Yaz_ProxyClient *m_next;
151     Yaz_ProxyClient **m_prev;
152     int m_init_flag;
153     Yaz_Z_Query *m_last_query;
154     Yaz_Z_Databases m_last_databases;
155     char *m_last_resultSetId;
156     int m_last_ok;
157     int m_last_resultCount;
158     int m_sr_transform;
159     int m_seqno;
160     int m_waiting;
161     int m_resultSetStartPoint;
162     int m_bytes_sent;
163     int m_bytes_recv;
164     int m_pdu_recv;
165     ODR m_init_odr;
166     Z_APDU *m_initResponse;
167     Yaz_RecordCache m_cache;
168     void pre_init_client();
169     int m_target_idletime;
170     Yaz_Proxy *m_root;
171 };
172
173 class YAZ_EXPORT Yaz_cql2rpn {
174  public:
175     Yaz_cql2rpn();
176     ~Yaz_cql2rpn();
177     void set_pqf_file(const char *fname);
178     int query_transform(const char *cql, Z_RPNQuery **rpnquery, ODR o);
179  private:
180     cql_transform_t m_transform;
181 };
182
183
184 /// Information Retrieval Proxy Server.
185 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
186  private:
187     char *get_cookie(Z_OtherInformation **otherInfo);
188     char *get_proxy(Z_OtherInformation **otherInfo);
189     Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
190                                 const char *proxy_host);
191     Z_APDU *result_set_optimize(Z_APDU *apdu);
192     void shutdown();
193     
194     Yaz_ProxyClient *m_client;
195     IYaz_PDU_Observable *m_PDU_Observable;
196     Yaz_ProxyClient *m_clientPool;
197     Yaz_Proxy *m_parent;
198     int m_seqno;
199     int m_max_clients;
200     int m_log_mask;
201     int m_keepalive_limit_bw;
202     int m_keepalive_limit_pdu;
203     int m_client_idletime;
204     int m_target_idletime;
205     char *m_proxyTarget;
206     char *m_default_target;
207     char *m_proxy_authentication;
208     long m_seed;
209     char *m_optimize;
210     int m_session_no;         // sequence for each client session
211     char m_session_str[30];  // session string (time:session_no)
212     Yaz_ProxyConfig *m_config;
213     char *m_config_fname;
214     int m_bytes_sent;
215     int m_bytes_recv;
216     int m_bw_max;
217     Yaz_bw m_bw_stat;
218     int m_pdu_max;
219     Yaz_bw m_pdu_stat;
220     Z_GDU *m_bw_hold_PDU;
221     int m_max_record_retrieve;
222     void handle_max_record_retrieve(Z_APDU *apdu);
223     void display_diagrecs(Z_DiagRec **pp, int num);
224     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
225                                               const char *addinfo);
226
227     Z_APDU *handle_query_validation(Z_APDU *apdu);
228     Z_APDU *handle_query_transformation(Z_APDU *apdu);
229
230     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
231     const char *load_balance(const char **url);
232     int m_reconfig_flag;
233     Yaz_ProxyConfig *check_reconfigure();
234     int m_request_no;
235     int m_invalid_session;
236     int m_marcxml_flag;
237     void convert_to_marcxml(Z_NamePlusRecordList *p);
238     Z_APDU *m_initRequest_apdu;
239     NMEM m_initRequest_mem;
240     Z_APDU *m_apdu_invalid_session;
241     NMEM m_mem_invalid_session;
242     int send_PDU_convert(Z_APDU *apdu, int *len);
243     ODR m_s2z_odr_init;
244     ODR m_s2z_odr_search;
245     int m_s2z_hit_count;
246     int m_s2z_packing;
247     Z_APDU *m_s2z_init_apdu;
248     Z_APDU *m_s2z_search_apdu;
249     Z_APDU *m_s2z_present_apdu;
250     char *m_soap_ns;
251     int send_to_srw_client_error(int error);
252     int send_to_srw_client_ok(int hits, Z_Records *records, int start);
253     int send_http_response(int code);
254     int send_srw_response(Z_SRW_PDU *srw_pdu);
255     int send_srw_explain();
256
257     int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
258                       Z_DefaultDiagFormat *ddf);
259     int m_http_keepalive;
260     const char *m_http_version;
261     Yaz_cql2rpn m_cql2rpn;
262     struct timeval m_time_tv;
263     void logtime();
264  public:
265     Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
266               Yaz_Proxy *parent = 0);
267     ~Yaz_Proxy();
268     void inc_request_no();
269     void recv_GDU(Z_GDU *apdu, int len);
270     void handle_incoming_HTTP(Z_HTTP_Request *req);
271     void handle_incoming_Z_PDU(Z_APDU *apdu);
272     IYaz_PDU_Observer* sessionNotify
273         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
274     void failNotify();
275     void timeoutNotify();
276     void connectNotify();
277     const char *option(const char *name, const char *value);
278     void set_default_target(const char *target);
279     void set_proxy_authentication (const char *auth);
280     char *get_proxy_target() { return m_proxyTarget; };
281     char *get_session_str() { return m_session_str; };
282     void set_max_clients(int m) { m_max_clients = m; };
283     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
284     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
285     int get_target_idletime () { return m_target_idletime; }
286     int set_config(const char *name);
287     void reconfig() { m_reconfig_flag = 1; }
288     int send_to_client(Z_APDU *apdu);
289     int server(const char *addr);
290     void pre_init();
291     int get_log_mask() { return m_log_mask; };
292     int handle_init_response_for_invalid_session(Z_APDU *apdu);
293 };
294