2 * Copyright (c) 1998-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: proxy.h,v 1.35 2004-01-15 15:47:52 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, char **schema);
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_schema(xmlNodePtr ptr, Z_RecordComposition *comp,
70 const char *schema_identifier);
72 xmlNodePtr m_proxyPtr;
73 void return_target_info(xmlNodePtr ptr, const char **url,
74 int *limit_bw, int *limit_pdu, int *limit_req,
75 int *target_idletime, int *client_idletime,
76 int *keepalive_limit_bw, int *keepalive_limit_pdu,
77 int *pre_init, const char **cql2rpn);
78 void return_limit(xmlNodePtr ptr,
79 int *limit_bw, int *limit_pdu, int *limit_req);
80 int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
82 xmlNodePtr find_target_node(const char *name, const char *db);
83 xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
84 const char *get_text(xmlNodePtr ptr);
85 int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
86 Z_AttributeList *attrs,
88 int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
92 int match_list(int v, const char *m);
93 int atoi_l(const char **cp);
97 class YAZ_EXPORT Yaz_RecordCache {
101 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
103 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
104 Odr_oid *syntax, Z_RecordComposition *comp);
107 void copy_searchRequest(Z_SearchRequest *sr);
108 void copy_presentRequest(Z_PresentRequest *pr);
109 void set_max_size(int sz);
112 Yaz_RecordCache_Entry *m_entries;
113 Z_SearchRequest *m_searchRequest;
114 Z_PresentRequest *m_presentRequest;
115 int match (Yaz_RecordCache_Entry *entry,
116 Odr_oid *syntax, int offset,
117 Z_RecordComposition *comp);
121 class YAZ_EXPORT Yaz_bw {
125 void add_bytes(int m);
128 long m_sec; // time of most recent bucket
135 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
136 friend class Yaz_Proxy;
137 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
140 void recv_GDU(Z_GDU *apdu, int len);
141 void recv_Z_PDU(Z_APDU *apdu, int len);
142 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
143 IYaz_PDU_Observer* sessionNotify
144 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
148 void timeoutNotify();
149 void connectNotify();
150 int send_to_target(Z_APDU *apdu);
151 const char *get_session_str();
153 Yaz_ProxyClient *m_next;
154 Yaz_ProxyClient **m_prev;
156 Yaz_Z_Query *m_last_query;
157 Yaz_Z_Databases m_last_databases;
158 char *m_last_resultSetId;
160 int m_last_resultCount;
164 int m_resultSetStartPoint;
169 Z_APDU *m_initResponse;
170 Z_Options *m_initResponse_options;
171 Z_ProtocolVersion *m_initResponse_version;
172 Yaz_RecordCache m_cache;
173 void pre_init_client();
174 int m_target_idletime;
178 class YAZ_EXPORT Yaz_cql2rpn {
182 void set_pqf_file(const char *fname);
183 int query_transform(const char *cql, Z_RPNQuery **rpnquery, ODR o,
186 cql_transform_t m_transform;
190 /// Information Retrieval Proxy Server.
191 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
193 char *get_cookie(Z_OtherInformation **otherInfo);
194 char *get_proxy(Z_OtherInformation **otherInfo);
195 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
196 const char *proxy_host);
197 Z_APDU *result_set_optimize(Z_APDU *apdu);
200 Yaz_ProxyClient *m_client;
201 IYaz_PDU_Observable *m_PDU_Observable;
202 Yaz_ProxyClient *m_clientPool;
207 int m_keepalive_limit_bw;
208 int m_keepalive_limit_pdu;
209 int m_client_idletime;
210 int m_target_idletime;
212 char *m_default_target;
213 char *m_proxy_authentication;
216 int m_session_no; // sequence for each client session
217 char m_session_str[30]; // session string (time:session_no)
218 Yaz_ProxyConfig *m_config;
219 char *m_config_fname;
226 Z_GDU *m_bw_hold_PDU;
227 int m_max_record_retrieve;
228 void handle_max_record_retrieve(Z_APDU *apdu);
229 void display_diagrecs(Z_DiagRec **pp, int num);
230 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
231 const char *addinfo);
233 Z_APDU *handle_query_validation(Z_APDU *apdu);
234 Z_APDU *handle_query_transformation(Z_APDU *apdu);
236 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
237 const char *load_balance(const char **url);
239 Yaz_ProxyConfig *check_reconfigure();
241 int m_invalid_session;
243 char *m_stylesheet_schema;
245 void convert_to_marcxml(Z_NamePlusRecordList *p);
246 void convert_xsl(Z_NamePlusRecordList *p);
247 Z_APDU *m_initRequest_apdu;
248 Z_Options *m_initRequest_options;
249 Z_ProtocolVersion *m_initRequest_version;
250 NMEM m_initRequest_mem;
251 Z_APDU *m_apdu_invalid_session;
252 NMEM m_mem_invalid_session;
253 int send_PDU_convert(Z_APDU *apdu, int *len);
255 ODR m_s2z_odr_search;
258 Z_APDU *m_s2z_init_apdu;
259 Z_APDU *m_s2z_search_apdu;
260 Z_APDU *m_s2z_present_apdu;
261 char *m_s2z_stylesheet;
263 int send_to_srw_client_error(int error, const char *add);
264 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
265 int send_http_response(int code);
266 int send_srw_response(Z_SRW_PDU *srw_pdu);
267 int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
268 int num_diagnostics);
269 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
270 Z_DefaultDiagFormat *ddf);
271 int m_http_keepalive;
272 const char *m_http_version;
273 Yaz_cql2rpn m_cql2rpn;
274 struct timeval m_time_tv;
276 Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
278 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
279 Yaz_Proxy *parent = 0);
281 void inc_request_no();
282 void recv_GDU(Z_GDU *apdu, int len);
283 void handle_incoming_HTTP(Z_HTTP_Request *req);
284 void handle_incoming_Z_PDU(Z_APDU *apdu);
285 IYaz_PDU_Observer* sessionNotify
286 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
288 void timeoutNotify();
289 void connectNotify();
291 const char *option(const char *name, const char *value);
292 void set_default_target(const char *target);
293 void set_proxy_authentication (const char *auth);
294 char *get_proxy_target() { return m_proxyTarget; };
295 char *get_session_str() { return m_session_str; };
296 void set_max_clients(int m) { m_max_clients = m; };
297 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
298 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
299 int get_target_idletime () { return m_target_idletime; }
300 int set_config(const char *name);
301 void reconfig() { m_reconfig_flag = 1; }
302 int send_to_client(Z_APDU *apdu);
303 int server(const char *addr);
305 int get_log_mask() { return m_log_mask; };
306 int handle_init_response_for_invalid_session(Z_APDU *apdu);