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