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