1 /* $Id: proxy.h,v 1.11 2005-02-10 08:09:42 oleg Exp $
2 Copyright (c) 1998-2004, 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 char *get_explain_doc(ODR odr, const char *name, const char *db,
91 const char *get_explain_name(const char *db, const char **backend_db);
93 void operator=(const Yaz_ProxyConfig &conf);
94 class Yaz_ProxyConfigP *m_cp;
97 class YAZ_EXPORT Yaz_RecordCache {
101 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
103 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
104 Odr_oid *syntax, Z_RecordComposition *comp);
107 void copy_searchRequest(Z_SearchRequest *sr);
108 void copy_presentRequest(Z_PresentRequest *pr);
109 void set_max_size(int sz);
112 Yaz_RecordCache_Entry *m_entries;
113 Z_SearchRequest *m_searchRequest;
114 Z_PresentRequest *m_presentRequest;
115 int match (Yaz_RecordCache_Entry *entry,
116 Odr_oid *syntax, int offset,
117 Z_RecordComposition *comp);
122 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
123 friend class Yaz_Proxy;
124 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
127 void recv_GDU(Z_GDU *apdu, int len);
128 void recv_Z_PDU(Z_APDU *apdu, int len);
129 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
130 IYaz_PDU_Observer* sessionNotify
131 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
135 void timeoutNotify();
136 void connectNotify();
137 int send_to_target(Z_APDU *apdu);
138 const char *get_session_str();
140 Yaz_ProxyClient *m_next;
141 Yaz_ProxyClient **m_prev;
143 Yaz_Z_Query *m_last_query;
144 Yaz_Z_Databases m_last_databases;
145 char *m_last_resultSetId;
147 int m_last_resultCount;
151 int m_resultSetStartPoint;
156 Z_APDU *m_initResponse;
157 Z_Options *m_initResponse_options;
158 Z_ProtocolVersion *m_initResponse_version;
159 int m_initResponse_preferredMessageSize;
160 int m_initResponse_maximumRecordSize;
161 Yaz_RecordCache m_cache;
162 void pre_init_client();
163 int m_target_idletime;
168 /// Information Retrieval Proxy Server.
169 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
171 char *get_cookie(Z_OtherInformation **otherInfo);
172 char *get_proxy(Z_OtherInformation **otherInfo);
173 void get_charset_and_lang_negotiation(Z_OtherInformation **otherInfo,
174 char **charstes, char **langs, int *selected);
175 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
176 const char *proxy_host);
177 void srw_get_client(const char *db, const char **backend_db);
178 Z_APDU *result_set_optimize(Z_APDU *apdu);
180 void releaseClient();
181 Yaz_ProxyClient *m_client;
182 IYaz_PDU_Observable *m_PDU_Observable;
183 Yaz_ProxyClient *m_clientPool;
188 int m_keepalive_limit_bw;
189 int m_keepalive_limit_pdu;
190 int m_client_idletime;
191 int m_target_idletime;
193 char *m_default_target;
194 char *m_proxy_authentication;
195 char *m_proxy_negotiation_charset;
196 char *m_proxy_negotiation_lang;
199 int m_session_no; // sequence for each client session
200 char m_session_str[30]; // session string (time:session_no)
201 Yaz_ProxyConfig *m_config;
202 char *m_config_fname;
209 Z_GDU *m_bw_hold_PDU;
210 int m_max_record_retrieve;
211 void handle_max_record_retrieve(Z_APDU *apdu);
212 void display_diagrecs(Z_DiagRec **pp, int num);
213 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
214 const char *addinfo);
216 Z_APDU *handle_query_validation(Z_APDU *apdu);
217 Z_APDU *handle_query_transformation(Z_APDU *apdu);
219 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
221 void handle_charset_lang_negotiation(Z_APDU *apdu);
223 const char *load_balance(const char **url);
225 Yaz_ProxyConfig *check_reconfigure();
227 int m_invalid_session;
229 void *m_stylesheet_xsp; // Really libxslt's xsltStylesheetPtr
230 int m_stylesheet_offset;
231 Z_APDU *m_stylesheet_apdu;
232 Z_NamePlusRecordList *m_stylesheet_nprl;
234 char *m_backend_type;
235 char *m_backend_charset;
237 void convert_to_frontend_type(Z_NamePlusRecordList *p);
238 void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
239 int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
240 void convert_xsl_delay();
241 Z_APDU *m_initRequest_apdu;
242 int m_initRequest_preferredMessageSize;
243 int m_initRequest_maximumRecordSize;
244 Z_Options *m_initRequest_options;
245 Z_ProtocolVersion *m_initRequest_version;
246 char **m_initRequest_oi_negotiation_charsets;
247 int m_initRequest_oi_negotiation_num_charsets;
248 char **m_initRequest_oi_negotiation_langs;
249 int m_initRequest_oi_negotiation_num_langs;
250 int m_initRequest_oi_negotiation_selected;
251 NMEM m_initRequest_mem;
252 Z_APDU *m_apdu_invalid_session;
253 NMEM m_mem_invalid_session;
254 int send_PDU_convert(Z_APDU *apdu);
256 ODR m_s2z_odr_search;
259 char *m_s2z_database;
260 Z_APDU *m_s2z_init_apdu;
261 Z_APDU *m_s2z_search_apdu;
262 Z_APDU *m_s2z_present_apdu;
263 char *m_s2z_stylesheet;
265 int file_access(Z_HTTP_Request *hreq);
266 int send_to_srw_client_error(int error, const char *add);
267 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
268 int send_http_response(int code);
269 int send_srw_response(Z_SRW_PDU *srw_pdu);
270 int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
271 int num_diagnostics);
272 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
273 Z_DefaultDiagFormat *ddf);
274 int m_http_keepalive;
275 const char *m_http_version;
276 Yaz_cql2rpn m_cql2rpn;
279 Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
280 Z_ReferenceId *m_referenceId;
281 NMEM m_referenceId_mem;
282 #define NO_SPARE_SOLARIS_FD 10
283 int m_lo_fd[NO_SPARE_SOLARIS_FD];
284 void low_socket_open();
285 void low_socket_close();
286 char *m_usemarcon_ini_stage1;
287 char *m_usemarcon_ini_stage2;
288 Yaz_usemarcon *m_usemarcon;
290 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
291 Yaz_Proxy *parent = 0);
293 void inc_request_no();
294 void recv_GDU(Z_GDU *apdu, int len);
295 void handle_incoming_HTTP(Z_HTTP_Request *req);
296 void handle_incoming_Z_PDU(Z_APDU *apdu);
297 IYaz_PDU_Observer* sessionNotify
298 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
300 void timeoutNotify();
301 void connectNotify();
303 const char *option(const char *name, const char *value);
304 void set_default_target(const char *target);
305 void set_proxy_authentication (const char *auth);
306 void set_proxy_negotiation (const char *charset, const char *lang);
307 char *get_proxy_target() { return m_proxyTarget; };
308 char *get_session_str() { return m_session_str; };
309 void set_max_clients(int m) { m_max_clients = m; };
310 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
311 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
312 int get_target_idletime () { return m_target_idletime; }
313 int set_config(const char *name);
314 void reconfig() { m_reconfig_flag = 1; }
315 int send_to_client(Z_APDU *apdu);
316 int server(const char *addr);
318 int get_log_mask() { return m_log_mask; };
319 int handle_init_response_for_invalid_session(Z_APDU *apdu);