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