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