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