idleTime, timeout (0) sets immediate timeout; -1 disabled timeout.
[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.26 2004-01-05 11:31:04 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  private:
182     cql_transform_t m_transform;
183 };
184
185
186 /// Information Retrieval Proxy Server.
187 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
188  private:
189     char *get_cookie(Z_OtherInformation **otherInfo);
190     char *get_proxy(Z_OtherInformation **otherInfo);
191     Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
192                                 const char *proxy_host);
193     Z_APDU *result_set_optimize(Z_APDU *apdu);
194     void shutdown();
195     
196     Yaz_ProxyClient *m_client;
197     IYaz_PDU_Observable *m_PDU_Observable;
198     Yaz_ProxyClient *m_clientPool;
199     Yaz_Proxy *m_parent;
200     int m_seqno;
201     int m_max_clients;
202     int m_log_mask;
203     int m_keepalive_limit_bw;
204     int m_keepalive_limit_pdu;
205     int m_client_idletime;
206     int m_target_idletime;
207     char *m_proxyTarget;
208     char *m_default_target;
209     char *m_proxy_authentication;
210     long m_seed;
211     char *m_optimize;
212     int m_session_no;         // sequence for each client session
213     char m_session_str[30];  // session string (time:session_no)
214     Yaz_ProxyConfig *m_config;
215     char *m_config_fname;
216     int m_bytes_sent;
217     int m_bytes_recv;
218     int m_bw_max;
219     Yaz_bw m_bw_stat;
220     int m_pdu_max;
221     Yaz_bw m_pdu_stat;
222     Z_GDU *m_bw_hold_PDU;
223     int m_max_record_retrieve;
224     void handle_max_record_retrieve(Z_APDU *apdu);
225     void display_diagrecs(Z_DiagRec **pp, int num);
226     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
227                                               const char *addinfo);
228
229     Z_APDU *handle_query_validation(Z_APDU *apdu);
230     Z_APDU *handle_query_transformation(Z_APDU *apdu);
231
232     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
233     const char *load_balance(const char **url);
234     int m_reconfig_flag;
235     Yaz_ProxyConfig *check_reconfigure();
236     int m_request_no;
237     int m_invalid_session;
238     int m_marcxml_flag;
239     char *m_stylesheet;
240     void convert_to_marcxml(Z_NamePlusRecordList *p);
241     void convert_xsl(Z_NamePlusRecordList *p);
242     Z_APDU *m_initRequest_apdu;
243     NMEM m_initRequest_mem;
244     Z_APDU *m_apdu_invalid_session;
245     NMEM m_mem_invalid_session;
246     int send_PDU_convert(Z_APDU *apdu, int *len);
247     ODR m_s2z_odr_init;
248     ODR m_s2z_odr_search;
249     int m_s2z_hit_count;
250     int m_s2z_packing;
251     Z_APDU *m_s2z_init_apdu;
252     Z_APDU *m_s2z_search_apdu;
253     Z_APDU *m_s2z_present_apdu;
254     char *m_soap_ns;
255     int send_to_srw_client_error(int error);
256     int send_to_srw_client_ok(int hits, Z_Records *records, int start);
257     int send_http_response(int code);
258     int send_srw_response(Z_SRW_PDU *srw_pdu);
259     int send_srw_explain();
260
261     int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
262                       Z_DefaultDiagFormat *ddf);
263     int m_http_keepalive;
264     const char *m_http_version;
265     Yaz_cql2rpn m_cql2rpn;
266     struct timeval m_time_tv;
267     void logtime();
268     Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
269  public:
270     Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
271               Yaz_Proxy *parent = 0);
272     ~Yaz_Proxy();
273     void inc_request_no();
274     void recv_GDU(Z_GDU *apdu, int len);
275     void handle_incoming_HTTP(Z_HTTP_Request *req);
276     void handle_incoming_Z_PDU(Z_APDU *apdu);
277     IYaz_PDU_Observer* sessionNotify
278         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
279     void failNotify();
280     void timeoutNotify();
281     void connectNotify();
282     const char *option(const char *name, const char *value);
283     void set_default_target(const char *target);
284     void set_proxy_authentication (const char *auth);
285     char *get_proxy_target() { return m_proxyTarget; };
286     char *get_session_str() { return m_session_str; };
287     void set_max_clients(int m) { m_max_clients = m; };
288     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
289     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
290     int get_target_idletime () { return m_target_idletime; }
291     int set_config(const char *name);
292     void reconfig() { m_reconfig_flag = 1; }
293     int send_to_client(Z_APDU *apdu);
294     int server(const char *addr);
295     void pre_init();
296     int get_log_mask() { return m_log_mask; };
297     int handle_init_response_for_invalid_session(Z_APDU *apdu);
298 };
299