1 /* $Id: proxy.h,v 1.3 2004-04-11 12:25:01 adam Exp $
2 Copyright (c) 1998-2004, Index Data.
4 This file is part of the yaz-proxy.
6 Zebra 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 Zebra 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
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>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
34 #include <libxslt/xsltutils.h>
35 #include <libxslt/transform.h>
40 #define MAX_ZURL_PLEX 10
42 #define PROXY_LOG_APDU_CLIENT 1
43 #define PROXY_LOG_APDU_SERVER 2
44 #define PROXY_LOG_REQ_CLIENT 4
45 #define PROXY_LOG_REQ_SERVER 8
47 struct Yaz_RecordCache_Entry;
49 class YAZ_EXPORT Yaz_ProxyConfig {
53 int read_xml(const char *fname);
55 int get_target_no(int no,
64 int *keepalive_limit_bw,
65 int *keepalive_limit_pdu,
67 const char **cql2rpn);
69 void get_generic_info(int *log_mask, int *max_clients);
71 void get_target_info(const char *name, const char **url,
72 int *limit_bw, int *limit_pdu, int *limit_req,
73 int *target_idletime, int *client_idletime,
75 int *keepalive_limit_bw, int *keepalive_limit_pdu,
77 const char **cql2rpn);
79 int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
80 int check_syntax(ODR odr, const char *name,
81 Odr_oid *syntax, Z_RecordComposition *comp,
82 char **addinfo, char **stylesheet, char **schema);
83 char *get_explain(ODR odr, const char *name, const char *db,
86 void operator=(const Yaz_ProxyConfig &conf);
87 int mycmp(const char *hay, const char *item, size_t len);
89 int check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
90 const char *schema_identifier);
92 xmlNodePtr m_proxyPtr;
93 void return_target_info(xmlNodePtr ptr, const char **url,
94 int *limit_bw, int *limit_pdu, int *limit_req,
95 int *target_idletime, int *client_idletime,
96 int *keepalive_limit_bw, int *keepalive_limit_pdu,
97 int *pre_init, const char **cql2rpn);
98 void return_limit(xmlNodePtr ptr,
99 int *limit_bw, int *limit_pdu, int *limit_req);
100 int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
102 xmlNodePtr find_target_node(const char *name, const char *db);
103 xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
104 const char *get_text(xmlNodePtr ptr);
105 int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
106 Z_AttributeList *attrs,
108 int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
112 int match_list(int v, const char *m);
113 int atoi_l(const char **cp);
116 class YAZ_EXPORT Yaz_RecordCache {
120 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
122 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
123 Odr_oid *syntax, Z_RecordComposition *comp);
126 void copy_searchRequest(Z_SearchRequest *sr);
127 void copy_presentRequest(Z_PresentRequest *pr);
128 void set_max_size(int sz);
131 Yaz_RecordCache_Entry *m_entries;
132 Z_SearchRequest *m_searchRequest;
133 Z_PresentRequest *m_presentRequest;
134 int match (Yaz_RecordCache_Entry *entry,
135 Odr_oid *syntax, int offset,
136 Z_RecordComposition *comp);
141 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
142 friend class Yaz_Proxy;
143 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
146 void recv_GDU(Z_GDU *apdu, int len);
147 void recv_Z_PDU(Z_APDU *apdu, int len);
148 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
149 IYaz_PDU_Observer* sessionNotify
150 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
154 void timeoutNotify();
155 void connectNotify();
156 int send_to_target(Z_APDU *apdu);
157 const char *get_session_str();
159 Yaz_ProxyClient *m_next;
160 Yaz_ProxyClient **m_prev;
162 Yaz_Z_Query *m_last_query;
163 Yaz_Z_Databases m_last_databases;
164 char *m_last_resultSetId;
166 int m_last_resultCount;
170 int m_resultSetStartPoint;
175 Z_APDU *m_initResponse;
176 Z_Options *m_initResponse_options;
177 Z_ProtocolVersion *m_initResponse_version;
178 int m_initResponse_preferredMessageSize;
179 int m_initResponse_maximumRecordSize;
180 Yaz_RecordCache m_cache;
181 void pre_init_client();
182 int m_target_idletime;
187 /// Information Retrieval Proxy Server.
188 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
190 char *get_cookie(Z_OtherInformation **otherInfo);
191 char *get_proxy(Z_OtherInformation **otherInfo);
192 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
193 const char *proxy_host);
194 Z_APDU *result_set_optimize(Z_APDU *apdu);
197 Yaz_ProxyClient *m_client;
198 IYaz_PDU_Observable *m_PDU_Observable;
199 Yaz_ProxyClient *m_clientPool;
204 int m_keepalive_limit_bw;
205 int m_keepalive_limit_pdu;
206 int m_client_idletime;
207 int m_target_idletime;
209 char *m_default_target;
210 char *m_proxy_authentication;
213 int m_session_no; // sequence for each client session
214 char m_session_str[30]; // session string (time:session_no)
215 Yaz_ProxyConfig *m_config;
216 char *m_config_fname;
223 Z_GDU *m_bw_hold_PDU;
224 int m_max_record_retrieve;
225 void handle_max_record_retrieve(Z_APDU *apdu);
226 void display_diagrecs(Z_DiagRec **pp, int num);
227 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
228 const char *addinfo);
230 Z_APDU *handle_query_validation(Z_APDU *apdu);
231 Z_APDU *handle_query_transformation(Z_APDU *apdu);
233 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
234 const char *load_balance(const char **url);
236 Yaz_ProxyConfig *check_reconfigure();
238 int m_invalid_session;
241 xsltStylesheetPtr m_stylesheet_xsp;
243 void *m_stylesheet_xsp;
245 int m_stylesheet_offset;
246 Z_APDU *m_stylesheet_apdu;
247 Z_NamePlusRecordList *m_stylesheet_nprl;
249 void convert_to_marcxml(Z_NamePlusRecordList *p);
250 int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
251 void convert_xsl_delay();
252 Z_APDU *m_initRequest_apdu;
253 int m_initRequest_preferredMessageSize;
254 int m_initRequest_maximumRecordSize;
255 Z_Options *m_initRequest_options;
256 Z_ProtocolVersion *m_initRequest_version;
257 NMEM m_initRequest_mem;
258 Z_APDU *m_apdu_invalid_session;
259 NMEM m_mem_invalid_session;
260 int send_PDU_convert(Z_APDU *apdu);
262 ODR m_s2z_odr_search;
265 char *m_s2z_database;
266 Z_APDU *m_s2z_init_apdu;
267 Z_APDU *m_s2z_search_apdu;
268 Z_APDU *m_s2z_present_apdu;
269 char *m_s2z_stylesheet;
271 int send_to_srw_client_error(int error, const char *add);
272 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
273 int send_http_response(int code);
274 int send_srw_response(Z_SRW_PDU *srw_pdu);
275 int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
276 int num_diagnostics);
277 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
278 Z_DefaultDiagFormat *ddf);
279 int m_http_keepalive;
280 const char *m_http_version;
281 Yaz_cql2rpn m_cql2rpn;
282 #if HAVE_GETTIMEOFDAY
283 struct timeval m_time_tv;
286 Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
287 Z_ReferenceId *m_referenceId;
288 NMEM m_referenceId_mem;
289 #define NO_SPARE_SOLARIS_FD 10
290 int m_lo_fd[NO_SPARE_SOLARIS_FD];
291 void low_socket_open();
292 void low_socket_close();
294 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
295 Yaz_Proxy *parent = 0);
297 void inc_request_no();
298 void recv_GDU(Z_GDU *apdu, int len);
299 void handle_incoming_HTTP(Z_HTTP_Request *req);
300 void handle_incoming_Z_PDU(Z_APDU *apdu);
301 IYaz_PDU_Observer* sessionNotify
302 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
304 void timeoutNotify();
305 void connectNotify();
307 const char *option(const char *name, const char *value);
308 void set_default_target(const char *target);
309 void set_proxy_authentication (const char *auth);
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);