2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: proxy.h,v 1.26 2004-01-05 11:31:04 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);
182 cql_transform_t m_transform;
186 /// Information Retrieval Proxy Server.
187 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
189 char *get_cookie(Z_OtherInformation **otherInfo);
190 char *get_proxy(Z_OtherInformation **otherInfo);
191 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
192 const char *proxy_host);
193 Z_APDU *result_set_optimize(Z_APDU *apdu);
196 Yaz_ProxyClient *m_client;
197 IYaz_PDU_Observable *m_PDU_Observable;
198 Yaz_ProxyClient *m_clientPool;
203 int m_keepalive_limit_bw;
204 int m_keepalive_limit_pdu;
205 int m_client_idletime;
206 int m_target_idletime;
208 char *m_default_target;
209 char *m_proxy_authentication;
212 int m_session_no; // sequence for each client session
213 char m_session_str[30]; // session string (time:session_no)
214 Yaz_ProxyConfig *m_config;
215 char *m_config_fname;
222 Z_GDU *m_bw_hold_PDU;
223 int m_max_record_retrieve;
224 void handle_max_record_retrieve(Z_APDU *apdu);
225 void display_diagrecs(Z_DiagRec **pp, int num);
226 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
227 const char *addinfo);
229 Z_APDU *handle_query_validation(Z_APDU *apdu);
230 Z_APDU *handle_query_transformation(Z_APDU *apdu);
232 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
233 const char *load_balance(const char **url);
235 Yaz_ProxyConfig *check_reconfigure();
237 int m_invalid_session;
240 void convert_to_marcxml(Z_NamePlusRecordList *p);
241 void convert_xsl(Z_NamePlusRecordList *p);
242 Z_APDU *m_initRequest_apdu;
243 NMEM m_initRequest_mem;
244 Z_APDU *m_apdu_invalid_session;
245 NMEM m_mem_invalid_session;
246 int send_PDU_convert(Z_APDU *apdu, int *len);
248 ODR m_s2z_odr_search;
251 Z_APDU *m_s2z_init_apdu;
252 Z_APDU *m_s2z_search_apdu;
253 Z_APDU *m_s2z_present_apdu;
255 int send_to_srw_client_error(int error);
256 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
257 int send_http_response(int code);
258 int send_srw_response(Z_SRW_PDU *srw_pdu);
259 int send_srw_explain();
261 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
262 Z_DefaultDiagFormat *ddf);
263 int m_http_keepalive;
264 const char *m_http_version;
265 Yaz_cql2rpn m_cql2rpn;
266 struct timeval m_time_tv;
268 Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
270 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
271 Yaz_Proxy *parent = 0);
273 void inc_request_no();
274 void recv_GDU(Z_GDU *apdu, int len);
275 void handle_incoming_HTTP(Z_HTTP_Request *req);
276 void handle_incoming_Z_PDU(Z_APDU *apdu);
277 IYaz_PDU_Observer* sessionNotify
278 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
280 void timeoutNotify();
281 void connectNotify();
282 const char *option(const char *name, const char *value);
283 void set_default_target(const char *target);
284 void set_proxy_authentication (const char *auth);
285 char *get_proxy_target() { return m_proxyTarget; };
286 char *get_session_str() { return m_session_str; };
287 void set_max_clients(int m) { m_max_clients = m; };
288 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
289 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
290 int get_target_idletime () { return m_target_idletime; }
291 int set_config(const char *name);
292 void reconfig() { m_reconfig_flag = 1; }
293 int send_to_client(Z_APDU *apdu);
294 int server(const char *addr);
296 int get_log_mask() { return m_log_mask; };
297 int handle_init_response_for_invalid_session(Z_APDU *apdu);