1 /* $Id: proxy.h,v 1.2 2004-03-30 09:05:53 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 Zebra; see the file LICENSE.proxy. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <yaz++/z-assoc.h>
24 #include <yaz++/z-query.h>
25 #include <yaz++/z-databases.h>
26 #include <yaz++/cql2rpn.h>
28 #include <yaz++/proxy/bw.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32 #include <libxslt/xsltutils.h>
33 #include <libxslt/transform.h>
38 #define MAX_ZURL_PLEX 10
40 #define PROXY_LOG_APDU_CLIENT 1
41 #define PROXY_LOG_APDU_SERVER 2
42 #define PROXY_LOG_REQ_CLIENT 4
43 #define PROXY_LOG_REQ_SERVER 8
45 struct Yaz_RecordCache_Entry;
47 class YAZ_EXPORT Yaz_ProxyConfig {
51 int read_xml(const char *fname);
53 int get_target_no(int no,
62 int *keepalive_limit_bw,
63 int *keepalive_limit_pdu,
65 const char **cql2rpn);
67 void get_generic_info(int *log_mask, int *max_clients);
69 void get_target_info(const char *name, const char **url,
70 int *limit_bw, int *limit_pdu, int *limit_req,
71 int *target_idletime, int *client_idletime,
73 int *keepalive_limit_bw, int *keepalive_limit_pdu,
75 const char **cql2rpn);
77 int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
78 int check_syntax(ODR odr, const char *name,
79 Odr_oid *syntax, Z_RecordComposition *comp,
80 char **addinfo, char **stylesheet, char **schema);
81 char *get_explain(ODR odr, const char *name, const char *db,
84 void operator=(const Yaz_ProxyConfig &conf);
85 int mycmp(const char *hay, const char *item, size_t len);
87 int check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
88 const char *schema_identifier);
90 xmlNodePtr m_proxyPtr;
91 void return_target_info(xmlNodePtr ptr, const char **url,
92 int *limit_bw, int *limit_pdu, int *limit_req,
93 int *target_idletime, int *client_idletime,
94 int *keepalive_limit_bw, int *keepalive_limit_pdu,
95 int *pre_init, const char **cql2rpn);
96 void return_limit(xmlNodePtr ptr,
97 int *limit_bw, int *limit_pdu, int *limit_req);
98 int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
100 xmlNodePtr find_target_node(const char *name, const char *db);
101 xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
102 const char *get_text(xmlNodePtr ptr);
103 int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
104 Z_AttributeList *attrs,
106 int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
110 int match_list(int v, const char *m);
111 int atoi_l(const char **cp);
114 class YAZ_EXPORT Yaz_RecordCache {
118 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
120 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
121 Odr_oid *syntax, Z_RecordComposition *comp);
124 void copy_searchRequest(Z_SearchRequest *sr);
125 void copy_presentRequest(Z_PresentRequest *pr);
126 void set_max_size(int sz);
129 Yaz_RecordCache_Entry *m_entries;
130 Z_SearchRequest *m_searchRequest;
131 Z_PresentRequest *m_presentRequest;
132 int match (Yaz_RecordCache_Entry *entry,
133 Odr_oid *syntax, int offset,
134 Z_RecordComposition *comp);
139 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
140 friend class Yaz_Proxy;
141 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
144 void recv_GDU(Z_GDU *apdu, int len);
145 void recv_Z_PDU(Z_APDU *apdu, int len);
146 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
147 IYaz_PDU_Observer* sessionNotify
148 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
152 void timeoutNotify();
153 void connectNotify();
154 int send_to_target(Z_APDU *apdu);
155 const char *get_session_str();
157 Yaz_ProxyClient *m_next;
158 Yaz_ProxyClient **m_prev;
160 Yaz_Z_Query *m_last_query;
161 Yaz_Z_Databases m_last_databases;
162 char *m_last_resultSetId;
164 int m_last_resultCount;
168 int m_resultSetStartPoint;
173 Z_APDU *m_initResponse;
174 Z_Options *m_initResponse_options;
175 Z_ProtocolVersion *m_initResponse_version;
176 int m_initResponse_preferredMessageSize;
177 int m_initResponse_maximumRecordSize;
178 Yaz_RecordCache m_cache;
179 void pre_init_client();
180 int m_target_idletime;
185 /// Information Retrieval Proxy Server.
186 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
188 char *get_cookie(Z_OtherInformation **otherInfo);
189 char *get_proxy(Z_OtherInformation **otherInfo);
190 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
191 const char *proxy_host);
192 Z_APDU *result_set_optimize(Z_APDU *apdu);
195 Yaz_ProxyClient *m_client;
196 IYaz_PDU_Observable *m_PDU_Observable;
197 Yaz_ProxyClient *m_clientPool;
202 int m_keepalive_limit_bw;
203 int m_keepalive_limit_pdu;
204 int m_client_idletime;
205 int m_target_idletime;
207 char *m_default_target;
208 char *m_proxy_authentication;
211 int m_session_no; // sequence for each client session
212 char m_session_str[30]; // session string (time:session_no)
213 Yaz_ProxyConfig *m_config;
214 char *m_config_fname;
221 Z_GDU *m_bw_hold_PDU;
222 int m_max_record_retrieve;
223 void handle_max_record_retrieve(Z_APDU *apdu);
224 void display_diagrecs(Z_DiagRec **pp, int num);
225 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
226 const char *addinfo);
228 Z_APDU *handle_query_validation(Z_APDU *apdu);
229 Z_APDU *handle_query_transformation(Z_APDU *apdu);
231 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
232 const char *load_balance(const char **url);
234 Yaz_ProxyConfig *check_reconfigure();
236 int m_invalid_session;
239 xsltStylesheetPtr m_stylesheet_xsp;
241 void *m_stylesheet_xsp;
243 int m_stylesheet_offset;
244 Z_APDU *m_stylesheet_apdu;
245 Z_NamePlusRecordList *m_stylesheet_nprl;
247 void convert_to_marcxml(Z_NamePlusRecordList *p);
248 int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
249 void convert_xsl_delay();
250 Z_APDU *m_initRequest_apdu;
251 int m_initRequest_preferredMessageSize;
252 int m_initRequest_maximumRecordSize;
253 Z_Options *m_initRequest_options;
254 Z_ProtocolVersion *m_initRequest_version;
255 NMEM m_initRequest_mem;
256 Z_APDU *m_apdu_invalid_session;
257 NMEM m_mem_invalid_session;
258 int send_PDU_convert(Z_APDU *apdu);
260 ODR m_s2z_odr_search;
263 char *m_s2z_database;
264 Z_APDU *m_s2z_init_apdu;
265 Z_APDU *m_s2z_search_apdu;
266 Z_APDU *m_s2z_present_apdu;
267 char *m_s2z_stylesheet;
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 struct timeval m_time_tv;
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();
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 char *get_proxy_target() { return m_proxyTarget; };
307 char *get_session_str() { return m_session_str; };
308 void set_max_clients(int m) { m_max_clients = m; };
309 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
310 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
311 int get_target_idletime () { return m_target_idletime; }
312 int set_config(const char *name);
313 void reconfig() { m_reconfig_flag = 1; }
314 int send_to_client(Z_APDU *apdu);
315 int server(const char *addr);
317 int get_log_mask() { return m_log_mask; };
318 int handle_init_response_for_invalid_session(Z_APDU *apdu);