2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: proxy.h,v 1.41 2004-02-27 00:42:57 adam Exp $
9 #include <yaz++/z-assoc.h>
10 #include <yaz++/z-query.h>
11 #include <yaz++/z-databases.h>
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
16 #include <libxslt/xsltutils.h>
17 #include <libxslt/transform.h>
22 #define MAX_ZURL_PLEX 10
24 #define PROXY_LOG_APDU_CLIENT 1
25 #define PROXY_LOG_APDU_SERVER 2
26 #define PROXY_LOG_REQ_CLIENT 4
27 #define PROXY_LOG_REQ_SERVER 8
29 struct Yaz_RecordCache_Entry;
31 class YAZ_EXPORT Yaz_ProxyConfig {
35 int read_xml(const char *fname);
37 int get_target_no(int no,
46 int *keepalive_limit_bw,
47 int *keepalive_limit_pdu,
49 const char **cql2rpn);
51 void get_generic_info(int *log_mask, int *max_clients);
53 void get_target_info(const char *name, const char **url,
54 int *limit_bw, int *limit_pdu, int *limit_req,
55 int *target_idletime, int *client_idletime,
57 int *keepalive_limit_bw, int *keepalive_limit_pdu,
59 const char **cql2rpn);
61 int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
62 int check_syntax(ODR odr, const char *name,
63 Odr_oid *syntax, Z_RecordComposition *comp,
64 char **addinfo, char **stylesheet, char **schema);
65 char *get_explain(ODR odr, const char *name, const char *db,
68 void operator=(const Yaz_ProxyConfig &conf);
69 int mycmp(const char *hay, const char *item, size_t len);
71 int check_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
72 const char *schema_identifier);
74 xmlNodePtr m_proxyPtr;
75 void return_target_info(xmlNodePtr ptr, const char **url,
76 int *limit_bw, int *limit_pdu, int *limit_req,
77 int *target_idletime, int *client_idletime,
78 int *keepalive_limit_bw, int *keepalive_limit_pdu,
79 int *pre_init, const char **cql2rpn);
80 void return_limit(xmlNodePtr ptr,
81 int *limit_bw, int *limit_pdu, int *limit_req);
82 int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
84 xmlNodePtr find_target_node(const char *name, const char *db);
85 xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
86 const char *get_text(xmlNodePtr ptr);
87 int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
88 Z_AttributeList *attrs,
90 int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
94 int match_list(int v, const char *m);
95 int atoi_l(const char **cp);
98 class YAZ_EXPORT Yaz_RecordCache {
102 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
104 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
105 Odr_oid *syntax, Z_RecordComposition *comp);
108 void copy_searchRequest(Z_SearchRequest *sr);
109 void copy_presentRequest(Z_PresentRequest *pr);
110 void set_max_size(int sz);
113 Yaz_RecordCache_Entry *m_entries;
114 Z_SearchRequest *m_searchRequest;
115 Z_PresentRequest *m_presentRequest;
116 int match (Yaz_RecordCache_Entry *entry,
117 Odr_oid *syntax, int offset,
118 Z_RecordComposition *comp);
122 class YAZ_EXPORT Yaz_bw {
126 void add_bytes(int m);
129 long m_sec; // time of most recent bucket
136 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
137 friend class Yaz_Proxy;
138 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
141 void recv_GDU(Z_GDU *apdu, int len);
142 void recv_Z_PDU(Z_APDU *apdu, int len);
143 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
144 IYaz_PDU_Observer* sessionNotify
145 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
149 void timeoutNotify();
150 void connectNotify();
151 int send_to_target(Z_APDU *apdu);
152 const char *get_session_str();
154 Yaz_ProxyClient *m_next;
155 Yaz_ProxyClient **m_prev;
157 Yaz_Z_Query *m_last_query;
158 Yaz_Z_Databases m_last_databases;
159 char *m_last_resultSetId;
161 int m_last_resultCount;
165 int m_resultSetStartPoint;
170 Z_APDU *m_initResponse;
171 Z_Options *m_initResponse_options;
172 Z_ProtocolVersion *m_initResponse_version;
173 int m_initResponse_preferredMessageSize;
174 int m_initResponse_maximumRecordSize;
175 Yaz_RecordCache m_cache;
176 void pre_init_client();
177 int m_target_idletime;
181 class YAZ_EXPORT Yaz_cql2rpn {
185 void set_pqf_file(const char *fname);
186 int query_transform(const char *cql, Z_RPNQuery **rpnquery, ODR o,
189 cql_transform_t m_transform;
193 /// Information Retrieval Proxy Server.
194 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
196 char *get_cookie(Z_OtherInformation **otherInfo);
197 char *get_proxy(Z_OtherInformation **otherInfo);
198 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
199 const char *proxy_host);
200 Z_APDU *result_set_optimize(Z_APDU *apdu);
203 Yaz_ProxyClient *m_client;
204 IYaz_PDU_Observable *m_PDU_Observable;
205 Yaz_ProxyClient *m_clientPool;
210 int m_keepalive_limit_bw;
211 int m_keepalive_limit_pdu;
212 int m_client_idletime;
213 int m_target_idletime;
215 char *m_default_target;
216 char *m_proxy_authentication;
219 int m_session_no; // sequence for each client session
220 char m_session_str[30]; // session string (time:session_no)
221 Yaz_ProxyConfig *m_config;
222 char *m_config_fname;
229 Z_GDU *m_bw_hold_PDU;
230 int m_max_record_retrieve;
231 void handle_max_record_retrieve(Z_APDU *apdu);
232 void display_diagrecs(Z_DiagRec **pp, int num);
233 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
234 const char *addinfo);
236 Z_APDU *handle_query_validation(Z_APDU *apdu);
237 Z_APDU *handle_query_transformation(Z_APDU *apdu);
239 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
240 const char *load_balance(const char **url);
242 Yaz_ProxyConfig *check_reconfigure();
244 int m_invalid_session;
246 xsltStylesheetPtr m_stylesheet_xsp;
247 int m_stylesheet_offset;
248 Z_APDU *m_stylesheet_apdu;
249 Z_NamePlusRecordList *m_stylesheet_nprl;
251 void convert_to_marcxml(Z_NamePlusRecordList *p);
252 int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
253 void convert_xsl_delay();
254 Z_APDU *m_initRequest_apdu;
255 int m_initRequest_preferredMessageSize;
256 int m_initRequest_maximumRecordSize;
257 Z_Options *m_initRequest_options;
258 Z_ProtocolVersion *m_initRequest_version;
259 NMEM m_initRequest_mem;
260 Z_APDU *m_apdu_invalid_session;
261 NMEM m_mem_invalid_session;
262 int send_PDU_convert(Z_APDU *apdu);
264 ODR m_s2z_odr_search;
267 char *m_s2z_database;
268 Z_APDU *m_s2z_init_apdu;
269 Z_APDU *m_s2z_search_apdu;
270 Z_APDU *m_s2z_present_apdu;
271 char *m_s2z_stylesheet;
273 int send_to_srw_client_error(int error, const char *add);
274 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
275 int send_http_response(int code);
276 int send_srw_response(Z_SRW_PDU *srw_pdu);
277 int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
278 int num_diagnostics);
279 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
280 Z_DefaultDiagFormat *ddf);
281 int m_http_keepalive;
282 const char *m_http_version;
283 Yaz_cql2rpn m_cql2rpn;
284 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);