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