1 /* $Id: proxy.h,v 1.12 2005-02-11 15:19:08 adam Exp $
2 Copyright (c) 1998-2005, Index Data.
4 This file is part of the yaz-proxy.
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
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
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
22 #ifndef YAZ_PROXY_H_INCLUDED
23 #define YAZ_PROXY_H_INCLUDED
25 #include <yaz++/z-assoc.h>
26 #include <yaz++/z-query.h>
27 #include <yaz++/z-databases.h>
28 #include <yaz++/cql2rpn.h>
30 #include <yazproxy/bw.h>
34 #define MAX_ZURL_PLEX 10
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
41 struct Yaz_RecordCache_Entry;
42 class Yaz_ProxyConfigP;
45 class YAZ_EXPORT Yaz_ProxyConfig {
49 int read_xml(const char *fname);
51 int get_target_no(int no,
60 int *keepalive_limit_bw,
61 int *keepalive_limit_pdu,
64 const char **authentication,
65 const char **negotiation_charset,
66 const char **negotiation_lang);
68 void get_generic_info(int *log_mask, int *max_clients);
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,
74 int *keepalive_limit_bw, int *keepalive_limit_pdu,
77 const char **authentication,
78 const char **negotiation_charset,
79 const char **negotiation_lang);
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);
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,
93 const char *get_explain_name(const char *db, const char **backend_db);
95 void operator=(const Yaz_ProxyConfig &conf);
96 class Yaz_ProxyConfigP *m_cp;
99 class YAZ_EXPORT Yaz_RecordCache {
103 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
105 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
106 Odr_oid *syntax, Z_RecordComposition *comp);
109 void copy_searchRequest(Z_SearchRequest *sr);
110 void copy_presentRequest(Z_PresentRequest *pr);
111 void set_max_size(int sz);
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);
124 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
125 friend class Yaz_Proxy;
126 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
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);
137 void timeoutNotify();
138 void connectNotify();
139 int send_to_target(Z_APDU *apdu);
140 const char *get_session_str();
142 Yaz_ProxyClient *m_next;
143 Yaz_ProxyClient **m_prev;
145 Yaz_Z_Query *m_last_query;
146 Yaz_Z_Databases m_last_databases;
147 char *m_last_resultSetId;
149 int m_last_resultCount;
153 int m_resultSetStartPoint;
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;
170 /// Information Retrieval Proxy Server.
171 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
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);
182 void releaseClient();
183 Yaz_ProxyClient *m_client;
184 IYaz_PDU_Observable *m_PDU_Observable;
185 Yaz_ProxyClient *m_clientPool;
190 int m_keepalive_limit_bw;
191 int m_keepalive_limit_pdu;
192 int m_client_idletime;
193 int m_target_idletime;
195 char *m_default_target;
196 char *m_proxy_authentication;
197 char *m_proxy_negotiation_charset;
198 char *m_proxy_negotiation_lang;
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;
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);
219 Z_APDU *handle_query_validation(Z_APDU *apdu);
220 Z_APDU *handle_query_transformation(Z_APDU *apdu);
222 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
224 void handle_charset_lang_negotiation(Z_APDU *apdu);
226 const char *load_balance(const char **url);
228 Yaz_ProxyConfig *check_reconfigure();
230 int m_invalid_session;
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;
237 char *m_backend_type;
238 char *m_backend_charset;
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);
259 ODR m_s2z_odr_search;
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;
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;
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;
293 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
294 Yaz_Proxy *parent = 0);
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);
303 void timeoutNotify();
304 void connectNotify();
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);
321 int get_log_mask() { return m_log_mask; };
322 int handle_init_response_for_invalid_session(Z_APDU *apdu);