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