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