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