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