Added support for MARC record conversion with Usemarcon
[yazproxy-moved-to-github.git] / include / yazproxy / proxy.h
1 /* $Id: proxy.h,v 1.9 2004-12-03 14:28:18 adam Exp $
2    Copyright (c) 1998-2004, Index Data.
3
4 This file is part of the yaz-proxy.
5
6 YAZ proxy 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 YAZ proxy 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 #ifndef YAZ_PROXY_H_INCLUDED
23 #define YAZ_PROXY_H_INCLUDED
24
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
32 class Yaz_Proxy;
33
34 #define MAX_ZURL_PLEX 10
35
36 #define PROXY_LOG_APDU_CLIENT 1
37 #define PROXY_LOG_APDU_SERVER 2
38 #define PROXY_LOG_REQ_CLIENT 4
39 #define PROXY_LOG_REQ_SERVER 8
40
41 struct Yaz_RecordCache_Entry;
42 class Yaz_ProxyConfigP;
43 class Yaz_usemarcon;
44
45 class YAZ_EXPORT Yaz_ProxyConfig {
46 public:
47     Yaz_ProxyConfig();
48     ~Yaz_ProxyConfig();
49     int read_xml(const char *fname);
50
51     int get_target_no(int no,
52                       const char **name,
53                       const char **url,
54                       int *limit_bw,
55                       int *limit_pdu,
56                       int *limit_req,
57                       int *target_idletime,
58                       int *client_idletime,
59                       int *max_clients,
60                       int *keepalive_limit_bw,
61                       int *keepalive_limit_pdu,
62                       int *pre_init,
63                       const char **cql2rpn,
64                       const char **authentication);
65     
66     void get_generic_info(int *log_mask, int *max_clients);
67
68     void get_target_info(const char *name, const char **url,
69                          int *limit_bw, int *limit_pdu, int *limit_req,
70                          int *target_idletime, int *client_idletime,
71                          int *max_clients,
72                          int *keepalive_limit_bw, int *keepalive_limit_pdu,
73                          int *pre_init,
74                          const char **cql2rpn,
75                          const char **authentication);
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 **backend_type, char **backend_charset,
82                      char **usemarcon_ini_stage1, char **usemarcon_ini_stage2
83         );
84     char *get_explain_doc(ODR odr, const char *name, const char *db,
85                           int *len);
86     const char *get_explain_name(const char *db, const char **backend_db);
87  private:
88     void operator=(const Yaz_ProxyConfig &conf);
89     class Yaz_ProxyConfigP *m_cp;
90 };
91
92 class YAZ_EXPORT Yaz_RecordCache {
93  public:
94     Yaz_RecordCache ();
95     ~Yaz_RecordCache ();
96     void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
97     
98     int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
99                 Odr_oid *syntax, Z_RecordComposition *comp);
100     void clear();
101
102     void copy_searchRequest(Z_SearchRequest *sr);
103     void copy_presentRequest(Z_PresentRequest *pr);
104     void set_max_size(int sz);
105  private:
106     NMEM m_mem;
107     Yaz_RecordCache_Entry *m_entries;
108     Z_SearchRequest *m_searchRequest;
109     Z_PresentRequest *m_presentRequest;
110     int match (Yaz_RecordCache_Entry *entry,
111                Odr_oid *syntax, int offset,
112                Z_RecordComposition *comp);
113     int m_max_size;
114 };
115
116 /// Private class
117 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
118     friend class Yaz_Proxy;
119     Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
120                     Yaz_Proxy *parent);
121     ~Yaz_ProxyClient();
122     void recv_GDU(Z_GDU *apdu, int len);
123     void recv_Z_PDU(Z_APDU *apdu, int len);
124     void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
125     IYaz_PDU_Observer* sessionNotify
126         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
127     void shutdown();
128     Yaz_Proxy *m_server;
129     void failNotify();
130     void timeoutNotify();
131     void connectNotify();
132     int send_to_target(Z_APDU *apdu);
133     const char *get_session_str();
134     char *m_cookie;
135     Yaz_ProxyClient *m_next;
136     Yaz_ProxyClient **m_prev;
137     int m_init_flag;
138     Yaz_Z_Query *m_last_query;
139     Yaz_Z_Databases m_last_databases;
140     char *m_last_resultSetId;
141     int m_last_ok;
142     int m_last_resultCount;
143     int m_sr_transform;
144     int m_seqno;
145     int m_waiting;
146     int m_resultSetStartPoint;
147     int m_bytes_sent;
148     int m_bytes_recv;
149     int m_pdu_recv;
150     ODR m_init_odr;
151     Z_APDU *m_initResponse;
152     Z_Options *m_initResponse_options;
153     Z_ProtocolVersion *m_initResponse_version;
154     int m_initResponse_preferredMessageSize;
155     int m_initResponse_maximumRecordSize;
156     Yaz_RecordCache m_cache;
157     void pre_init_client();
158     int m_target_idletime;
159     Yaz_Proxy *m_root;
160 };
161
162
163 /// Information Retrieval Proxy Server.
164 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
165  private:
166     char *get_cookie(Z_OtherInformation **otherInfo);
167     char *get_proxy(Z_OtherInformation **otherInfo);
168     Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
169                                 const char *proxy_host);
170     void srw_get_client(const char *db, const char **backend_db);
171     Z_APDU *result_set_optimize(Z_APDU *apdu);
172     void shutdown();
173     void releaseClient();    
174     Yaz_ProxyClient *m_client;
175     IYaz_PDU_Observable *m_PDU_Observable;
176     Yaz_ProxyClient *m_clientPool;
177     Yaz_Proxy *m_parent;
178     int m_seqno;
179     int m_max_clients;
180     int m_log_mask;
181     int m_keepalive_limit_bw;
182     int m_keepalive_limit_pdu;
183     int m_client_idletime;
184     int m_target_idletime;
185     char *m_proxyTarget;
186     char *m_default_target;
187     char *m_proxy_authentication;
188     long m_seed;
189     char *m_optimize;
190     int m_session_no;         // sequence for each client session
191     char m_session_str[30];  // session string (time:session_no)
192     Yaz_ProxyConfig *m_config;
193     char *m_config_fname;
194     int m_bytes_sent;
195     int m_bytes_recv;
196     int m_bw_max;
197     Yaz_bw m_bw_stat;
198     int m_pdu_max;
199     Yaz_bw m_pdu_stat;
200     Z_GDU *m_bw_hold_PDU;
201     int m_max_record_retrieve;
202     void handle_max_record_retrieve(Z_APDU *apdu);
203     void display_diagrecs(Z_DiagRec **pp, int num);
204     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
205                                               const char *addinfo);
206
207     Z_APDU *handle_query_validation(Z_APDU *apdu);
208     Z_APDU *handle_query_transformation(Z_APDU *apdu);
209
210     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
211     const char *load_balance(const char **url);
212     int m_reconfig_flag;
213     Yaz_ProxyConfig *check_reconfigure();
214     int m_request_no;
215     int m_invalid_session;
216     int m_marcxml_flag;
217     void *m_stylesheet_xsp;  // Really libxslt's xsltStylesheetPtr 
218     int m_stylesheet_offset;
219     Z_APDU *m_stylesheet_apdu;
220     Z_NamePlusRecordList *m_stylesheet_nprl;
221     char *m_schema;
222     char *m_backend_type;
223     char *m_backend_charset;
224     int m_frontend_type;
225     void convert_to_frontend_type(Z_NamePlusRecordList *p);
226     void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
227     int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
228     void convert_xsl_delay();
229     Z_APDU *m_initRequest_apdu;
230     int m_initRequest_preferredMessageSize;
231     int m_initRequest_maximumRecordSize;
232     Z_Options *m_initRequest_options;
233     Z_ProtocolVersion *m_initRequest_version;
234     NMEM m_initRequest_mem;
235     Z_APDU *m_apdu_invalid_session;
236     NMEM m_mem_invalid_session;
237     int send_PDU_convert(Z_APDU *apdu);
238     ODR m_s2z_odr_init;
239     ODR m_s2z_odr_search;
240     int m_s2z_hit_count;
241     int m_s2z_packing;
242     char *m_s2z_database;
243     Z_APDU *m_s2z_init_apdu;
244     Z_APDU *m_s2z_search_apdu;
245     Z_APDU *m_s2z_present_apdu;
246     char *m_s2z_stylesheet;
247     char *m_soap_ns;
248     int send_to_srw_client_error(int error, const char *add);
249     int send_to_srw_client_ok(int hits, Z_Records *records, int start);
250     int send_http_response(int code);
251     int send_srw_response(Z_SRW_PDU *srw_pdu);
252     int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
253                                   int num_diagnostics);
254     int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
255                       Z_DefaultDiagFormat *ddf);
256     int m_http_keepalive;
257     const char *m_http_version;
258     Yaz_cql2rpn m_cql2rpn;
259     void *m_time_tv;
260     void logtime();
261     Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
262     Z_ReferenceId *m_referenceId;
263     NMEM m_referenceId_mem;
264 #define NO_SPARE_SOLARIS_FD 10
265     int m_lo_fd[NO_SPARE_SOLARIS_FD];
266     void low_socket_open();
267     void low_socket_close();
268     char *m_usemarcon_ini_stage1;
269     char *m_usemarcon_ini_stage2;
270     Yaz_usemarcon *m_usemarcon;
271  public:
272     Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
273               Yaz_Proxy *parent = 0);
274     ~Yaz_Proxy();
275     void inc_request_no();
276     void recv_GDU(Z_GDU *apdu, int len);
277     void handle_incoming_HTTP(Z_HTTP_Request *req);
278     void handle_incoming_Z_PDU(Z_APDU *apdu);
279     IYaz_PDU_Observer* sessionNotify
280         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
281     void failNotify();
282     void timeoutNotify();
283     void connectNotify();
284     void markInvalid();
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
303 #endif