2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: proxy.h,v 1.27 2004-01-06 21:17:42 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>
20 #define MAX_ZURL_PLEX 10
22 #define PROXY_LOG_APDU_CLIENT 1
23 #define PROXY_LOG_APDU_SERVER 2
24 #define PROXY_LOG_REQ_CLIENT 4
25 #define PROXY_LOG_REQ_SERVER 8
27 struct Yaz_RecordCache_Entry;
29 class YAZ_EXPORT Yaz_ProxyConfig {
33 int read_xml(const char *fname);
35 int get_target_no(int no,
44 int *keepalive_limit_bw,
45 int *keepalive_limit_pdu,
47 const char **cql2rpn);
49 void get_generic_info(int *log_mask, int *max_clients);
51 void get_target_info(const char *name, const char **url,
52 int *limit_bw, int *limit_pdu, int *limit_req,
53 int *target_idletime, int *client_idletime,
55 int *keepalive_limit_bw, int *keepalive_limit_pdu,
57 const char **cql2rpn);
59 int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
60 int check_syntax(ODR odr, const char *name,
61 Odr_oid *syntax, Z_RecordComposition *comp,
62 char **addinfo, char **stylesheet);
63 char *get_explain(ODR odr, const char *name, const char *db,
66 void operator=(const Yaz_ProxyConfig &conf);
67 int mycmp(const char *hay, const char *item, size_t len);
69 int check_esn(xmlNodePtr ptr, Z_RecordComposition *comp);
71 xmlNodePtr m_proxyPtr;
72 void return_target_info(xmlNodePtr ptr, const char **url,
73 int *limit_bw, int *limit_pdu, int *limit_req,
74 int *target_idletime, int *client_idletime,
75 int *keepalive_limit_bw, int *keepalive_limit_pdu,
76 int *pre_init, const char **cql2rpn);
77 void return_limit(xmlNodePtr ptr,
78 int *limit_bw, int *limit_pdu, int *limit_req);
79 int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
81 xmlNodePtr find_target_node(const char *name, const char *db);
82 xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
83 const char *get_text(xmlNodePtr ptr);
84 int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
85 Z_AttributeList *attrs,
87 int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
91 int match_list(int v, const char *m);
92 int atoi_l(const char **cp);
96 class YAZ_EXPORT Yaz_RecordCache {
100 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
102 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
103 Odr_oid *syntax, Z_RecordComposition *comp);
106 void copy_searchRequest(Z_SearchRequest *sr);
107 void copy_presentRequest(Z_PresentRequest *pr);
108 void set_max_size(int sz);
111 Yaz_RecordCache_Entry *m_entries;
112 Z_SearchRequest *m_searchRequest;
113 Z_PresentRequest *m_presentRequest;
114 int match (Yaz_RecordCache_Entry *entry,
115 Odr_oid *syntax, int offset,
116 Z_RecordComposition *comp);
120 class YAZ_EXPORT Yaz_bw {
124 void add_bytes(int m);
127 long m_sec; // time of most recent bucket
134 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
135 friend class Yaz_Proxy;
136 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
139 void recv_GDU(Z_GDU *apdu, int len);
140 void recv_Z_PDU(Z_APDU *apdu, int len);
141 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
142 IYaz_PDU_Observer* sessionNotify
143 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
147 void timeoutNotify();
148 void connectNotify();
149 int send_to_target(Z_APDU *apdu);
150 const char *get_session_str();
152 Yaz_ProxyClient *m_next;
153 Yaz_ProxyClient **m_prev;
155 Yaz_Z_Query *m_last_query;
156 Yaz_Z_Databases m_last_databases;
157 char *m_last_resultSetId;
159 int m_last_resultCount;
163 int m_resultSetStartPoint;
168 Z_APDU *m_initResponse;
169 Yaz_RecordCache m_cache;
170 void pre_init_client();
171 int m_target_idletime;
175 class YAZ_EXPORT Yaz_cql2rpn {
179 void set_pqf_file(const char *fname);
180 int query_transform(const char *cql, Z_RPNQuery **rpnquery, ODR o,
183 cql_transform_t m_transform;
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 void convert_to_marcxml(Z_NamePlusRecordList *p);
242 void convert_xsl(Z_NamePlusRecordList *p);
243 Z_APDU *m_initRequest_apdu;
244 NMEM m_initRequest_mem;
245 Z_APDU *m_apdu_invalid_session;
246 NMEM m_mem_invalid_session;
247 int send_PDU_convert(Z_APDU *apdu, int *len);
249 ODR m_s2z_odr_search;
252 Z_APDU *m_s2z_init_apdu;
253 Z_APDU *m_s2z_search_apdu;
254 Z_APDU *m_s2z_present_apdu;
256 int send_to_srw_client_error(int error);
257 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
258 int send_http_response(int code);
259 int send_srw_response(Z_SRW_PDU *srw_pdu);
260 int send_srw_explain();
262 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
263 Z_DefaultDiagFormat *ddf);
264 int m_http_keepalive;
265 const char *m_http_version;
266 Yaz_cql2rpn m_cql2rpn;
267 struct timeval m_time_tv;
269 Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
271 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
272 Yaz_Proxy *parent = 0);
274 void inc_request_no();
275 void recv_GDU(Z_GDU *apdu, int len);
276 void handle_incoming_HTTP(Z_HTTP_Request *req);
277 void handle_incoming_Z_PDU(Z_APDU *apdu);
278 IYaz_PDU_Observer* sessionNotify
279 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
281 void timeoutNotify();
282 void connectNotify();
283 const char *option(const char *name, const char *value);
284 void set_default_target(const char *target);
285 void set_proxy_authentication (const char *auth);
286 char *get_proxy_target() { return m_proxyTarget; };
287 char *get_session_str() { return m_session_str; };
288 void set_max_clients(int m) { m_max_clients = m; };
289 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
290 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
291 int get_target_idletime () { return m_target_idletime; }
292 int set_config(const char *name);
293 void reconfig() { m_reconfig_flag = 1; }
294 int send_to_client(Z_APDU *apdu);
295 int server(const char *addr);
297 int get_log_mask() { return m_log_mask; };
298 int handle_init_response_for_invalid_session(Z_APDU *apdu);