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