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