Added patch, which involve yazproxy to Character Set and Language
[yazproxy-moved-to-github.git] / include / yazproxy / proxy.h
1 /* $Id: proxy.h,v 1.11 2005-02-10 08:09:42 oleg 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                       const char **negotiation_charset,
66                       const char **negotiation_lang);
67     
68     void get_generic_info(int *log_mask, int *max_clients);
69
70     void get_target_info(const char *name, const char **url,
71                          int *limit_bw, int *limit_pdu, int *limit_req,
72                          int *target_idletime, int *client_idletime,
73                          int *max_clients,
74                          int *keepalive_limit_bw, int *keepalive_limit_pdu,
75                          int *pre_init,
76                          const char **cql2rpn,
77                          const char **authentication,
78                          const char **negotiation_charset,
79                          const char **negotiation_lang);
80
81     const char *check_mime_type(const char *path);
82     int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
83     int check_syntax(ODR odr, const char *name,
84                      Odr_oid *syntax, Z_RecordComposition *comp,
85                      char **addinfo, char **stylesheet, char **schema,
86                      char **backend_type, char **backend_charset,
87                      char **usemarcon_ini_stage1, char **usemarcon_ini_stage2
88         );
89     char *get_explain_doc(ODR odr, const char *name, const char *db,
90                           int *len);
91     const char *get_explain_name(const char *db, const char **backend_db);
92  private:
93     void operator=(const Yaz_ProxyConfig &conf);
94     class Yaz_ProxyConfigP *m_cp;
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 /// Private class
122 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
123     friend class Yaz_Proxy;
124     Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
125                     Yaz_Proxy *parent);
126     ~Yaz_ProxyClient();
127     void recv_GDU(Z_GDU *apdu, int len);
128     void recv_Z_PDU(Z_APDU *apdu, int len);
129     void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
130     IYaz_PDU_Observer* sessionNotify
131         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
132     void shutdown();
133     Yaz_Proxy *m_server;
134     void failNotify();
135     void timeoutNotify();
136     void connectNotify();
137     int send_to_target(Z_APDU *apdu);
138     const char *get_session_str();
139     char *m_cookie;
140     Yaz_ProxyClient *m_next;
141     Yaz_ProxyClient **m_prev;
142     int m_init_flag;
143     Yaz_Z_Query *m_last_query;
144     Yaz_Z_Databases m_last_databases;
145     char *m_last_resultSetId;
146     int m_last_ok;
147     int m_last_resultCount;
148     int m_sr_transform;
149     int m_seqno;
150     int m_waiting;
151     int m_resultSetStartPoint;
152     int m_bytes_sent;
153     int m_bytes_recv;
154     int m_pdu_recv;
155     ODR m_init_odr;
156     Z_APDU *m_initResponse;
157     Z_Options *m_initResponse_options;
158     Z_ProtocolVersion *m_initResponse_version;
159     int m_initResponse_preferredMessageSize;
160     int m_initResponse_maximumRecordSize;
161     Yaz_RecordCache m_cache;
162     void pre_init_client();
163     int m_target_idletime;
164     Yaz_Proxy *m_root;
165 };
166
167
168 /// Information Retrieval Proxy Server.
169 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
170  private:
171     char *get_cookie(Z_OtherInformation **otherInfo);
172     char *get_proxy(Z_OtherInformation **otherInfo);
173     void get_charset_and_lang_negotiation(Z_OtherInformation **otherInfo,
174         char **charstes, char **langs, int *selected);
175     Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
176                                 const char *proxy_host);
177     void srw_get_client(const char *db, const char **backend_db);
178     Z_APDU *result_set_optimize(Z_APDU *apdu);
179     void shutdown();
180     void releaseClient();    
181     Yaz_ProxyClient *m_client;
182     IYaz_PDU_Observable *m_PDU_Observable;
183     Yaz_ProxyClient *m_clientPool;
184     Yaz_Proxy *m_parent;
185     int m_seqno;
186     int m_max_clients;
187     int m_log_mask;
188     int m_keepalive_limit_bw;
189     int m_keepalive_limit_pdu;
190     int m_client_idletime;
191     int m_target_idletime;
192     char *m_proxyTarget;
193     char *m_default_target;
194     char *m_proxy_authentication;
195     char *m_proxy_negotiation_charset;
196     char *m_proxy_negotiation_lang;
197     long m_seed;
198     char *m_optimize;
199     int m_session_no;         // sequence for each client session
200     char m_session_str[30];  // session string (time:session_no)
201     Yaz_ProxyConfig *m_config;
202     char *m_config_fname;
203     int m_bytes_sent;
204     int m_bytes_recv;
205     int m_bw_max;
206     Yaz_bw m_bw_stat;
207     int m_pdu_max;
208     Yaz_bw m_pdu_stat;
209     Z_GDU *m_bw_hold_PDU;
210     int m_max_record_retrieve;
211     void handle_max_record_retrieve(Z_APDU *apdu);
212     void display_diagrecs(Z_DiagRec **pp, int num);
213     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
214                                               const char *addinfo);
215
216     Z_APDU *handle_query_validation(Z_APDU *apdu);
217     Z_APDU *handle_query_transformation(Z_APDU *apdu);
218
219     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
220
221     void handle_charset_lang_negotiation(Z_APDU *apdu);
222
223     const char *load_balance(const char **url);
224     int m_reconfig_flag;
225     Yaz_ProxyConfig *check_reconfigure();
226     int m_request_no;
227     int m_invalid_session;
228     int m_marcxml_flag;
229     void *m_stylesheet_xsp;  // Really libxslt's xsltStylesheetPtr 
230     int m_stylesheet_offset;
231     Z_APDU *m_stylesheet_apdu;
232     Z_NamePlusRecordList *m_stylesheet_nprl;
233     char *m_schema;
234     char *m_backend_type;
235     char *m_backend_charset;
236     int m_frontend_type;
237     void convert_to_frontend_type(Z_NamePlusRecordList *p);
238     void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
239     int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
240     void convert_xsl_delay();
241     Z_APDU *m_initRequest_apdu;
242     int m_initRequest_preferredMessageSize;
243     int m_initRequest_maximumRecordSize;
244     Z_Options *m_initRequest_options;
245     Z_ProtocolVersion *m_initRequest_version;
246     char **m_initRequest_oi_negotiation_charsets;
247     int m_initRequest_oi_negotiation_num_charsets;
248     char **m_initRequest_oi_negotiation_langs;
249     int m_initRequest_oi_negotiation_num_langs;
250     int m_initRequest_oi_negotiation_selected;
251     NMEM m_initRequest_mem;
252     Z_APDU *m_apdu_invalid_session;
253     NMEM m_mem_invalid_session;
254     int send_PDU_convert(Z_APDU *apdu);
255     ODR m_s2z_odr_init;
256     ODR m_s2z_odr_search;
257     int m_s2z_hit_count;
258     int m_s2z_packing;
259     char *m_s2z_database;
260     Z_APDU *m_s2z_init_apdu;
261     Z_APDU *m_s2z_search_apdu;
262     Z_APDU *m_s2z_present_apdu;
263     char *m_s2z_stylesheet;
264     char *m_soap_ns;
265     int file_access(Z_HTTP_Request *hreq);
266     int send_to_srw_client_error(int error, const char *add);
267     int send_to_srw_client_ok(int hits, Z_Records *records, int start);
268     int send_http_response(int code);
269     int send_srw_response(Z_SRW_PDU *srw_pdu);
270     int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
271                                   int num_diagnostics);
272     int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
273                       Z_DefaultDiagFormat *ddf);
274     int m_http_keepalive;
275     const char *m_http_version;
276     Yaz_cql2rpn m_cql2rpn;
277     void *m_time_tv;
278     void logtime();
279     Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
280     Z_ReferenceId *m_referenceId;
281     NMEM m_referenceId_mem;
282 #define NO_SPARE_SOLARIS_FD 10
283     int m_lo_fd[NO_SPARE_SOLARIS_FD];
284     void low_socket_open();
285     void low_socket_close();
286     char *m_usemarcon_ini_stage1;
287     char *m_usemarcon_ini_stage2;
288     Yaz_usemarcon *m_usemarcon;
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     void set_proxy_negotiation (const char *charset, const char *lang);
307     char *get_proxy_target() { return m_proxyTarget; };
308     char *get_session_str() { return m_session_str; };
309     void set_max_clients(int m) { m_max_clients = m; };
310     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
311     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
312     int get_target_idletime () { return m_target_idletime; }
313     int set_config(const char *name);
314     void reconfig() { m_reconfig_flag = 1; }
315     int send_to_client(Z_APDU *apdu);
316     int server(const char *addr);
317     void pre_init();
318     int get_log_mask() { return m_log_mask; };
319     int handle_init_response_for_invalid_session(Z_APDU *apdu);
320 };
321
322 #endif