2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: proxy.h,v 1.24 2003-12-22 15:16:23 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, char **addinfo);
62 char *get_explain(ODR odr, const char *name, const char *db,
65 void operator=(const Yaz_ProxyConfig &conf);
66 int mycmp(const char *hay, const char *item, size_t len);
69 xmlNodePtr m_proxyPtr;
70 void return_target_info(xmlNodePtr ptr, const char **url,
71 int *limit_bw, int *limit_pdu, int *limit_req,
72 int *target_idletime, int *client_idletime,
73 int *keepalive_limit_bw, int *keepalive_limit_pdu,
74 int *pre_init, const char **cql2rpn);
75 void return_limit(xmlNodePtr ptr,
76 int *limit_bw, int *limit_pdu, int *limit_req);
77 int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
79 xmlNodePtr find_target_node(const char *name, const char *db);
80 xmlNodePtr find_target_db(xmlNodePtr ptr, const char *db);
81 const char *get_text(xmlNodePtr ptr);
82 int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
83 Z_AttributeList *attrs,
85 int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
89 int match_list(int v, const char *m);
90 int atoi_l(const char **cp);
94 class YAZ_EXPORT Yaz_RecordCache {
98 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
100 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
101 Odr_oid *syntax, Z_RecordComposition *comp);
104 void copy_searchRequest(Z_SearchRequest *sr);
105 void copy_presentRequest(Z_PresentRequest *pr);
106 void set_max_size(int sz);
109 Yaz_RecordCache_Entry *m_entries;
110 Z_SearchRequest *m_searchRequest;
111 Z_PresentRequest *m_presentRequest;
112 int match (Yaz_RecordCache_Entry *entry,
113 Odr_oid *syntax, int offset,
114 Z_RecordComposition *comp);
118 class YAZ_EXPORT Yaz_bw {
122 void add_bytes(int m);
125 long m_sec; // time of most recent bucket
132 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
133 friend class Yaz_Proxy;
134 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
137 void recv_GDU(Z_GDU *apdu, int len);
138 void recv_Z_PDU(Z_APDU *apdu, int len);
139 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
140 IYaz_PDU_Observer* sessionNotify
141 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
145 void timeoutNotify();
146 void connectNotify();
147 int send_to_target(Z_APDU *apdu);
148 const char *get_session_str();
150 Yaz_ProxyClient *m_next;
151 Yaz_ProxyClient **m_prev;
153 Yaz_Z_Query *m_last_query;
154 Yaz_Z_Databases m_last_databases;
155 char *m_last_resultSetId;
157 int m_last_resultCount;
161 int m_resultSetStartPoint;
166 Z_APDU *m_initResponse;
167 Yaz_RecordCache m_cache;
168 void pre_init_client();
169 int m_target_idletime;
173 class YAZ_EXPORT Yaz_cql2rpn {
177 void set_pqf_file(const char *fname);
178 int query_transform(const char *cql, Z_RPNQuery **rpnquery, ODR o);
180 cql_transform_t m_transform;
184 /// Information Retrieval Proxy Server.
185 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
187 char *get_cookie(Z_OtherInformation **otherInfo);
188 char *get_proxy(Z_OtherInformation **otherInfo);
189 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
190 const char *proxy_host);
191 Z_APDU *result_set_optimize(Z_APDU *apdu);
194 Yaz_ProxyClient *m_client;
195 IYaz_PDU_Observable *m_PDU_Observable;
196 Yaz_ProxyClient *m_clientPool;
201 int m_keepalive_limit_bw;
202 int m_keepalive_limit_pdu;
203 int m_client_idletime;
204 int m_target_idletime;
206 char *m_default_target;
207 char *m_proxy_authentication;
210 int m_session_no; // sequence for each client session
211 char m_session_str[30]; // session string (time:session_no)
212 Yaz_ProxyConfig *m_config;
213 char *m_config_fname;
220 Z_GDU *m_bw_hold_PDU;
221 int m_max_record_retrieve;
222 void handle_max_record_retrieve(Z_APDU *apdu);
223 void display_diagrecs(Z_DiagRec **pp, int num);
224 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
225 const char *addinfo);
227 Z_APDU *handle_query_validation(Z_APDU *apdu);
228 Z_APDU *handle_query_transformation(Z_APDU *apdu);
230 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
231 const char *load_balance(const char **url);
233 Yaz_ProxyConfig *check_reconfigure();
235 int m_invalid_session;
237 void convert_to_marcxml(Z_NamePlusRecordList *p);
238 Z_APDU *m_initRequest_apdu;
239 NMEM m_initRequest_mem;
240 Z_APDU *m_apdu_invalid_session;
241 NMEM m_mem_invalid_session;
242 int send_PDU_convert(Z_APDU *apdu, int *len);
244 ODR m_s2z_odr_search;
247 Z_APDU *m_s2z_init_apdu;
248 Z_APDU *m_s2z_search_apdu;
249 Z_APDU *m_s2z_present_apdu;
251 int send_to_srw_client_error(int error);
252 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
253 int send_http_response(int code);
254 int send_srw_response(Z_SRW_PDU *srw_pdu);
255 int send_srw_explain();
257 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
258 Z_DefaultDiagFormat *ddf);
259 int m_http_keepalive;
260 const char *m_http_version;
261 Yaz_cql2rpn m_cql2rpn;
262 struct timeval m_time_tv;
265 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
266 Yaz_Proxy *parent = 0);
268 void inc_request_no();
269 void recv_GDU(Z_GDU *apdu, int len);
270 void handle_incoming_HTTP(Z_HTTP_Request *req);
271 void handle_incoming_Z_PDU(Z_APDU *apdu);
272 IYaz_PDU_Observer* sessionNotify
273 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
275 void timeoutNotify();
276 void connectNotify();
277 const char *option(const char *name, const char *value);
278 void set_default_target(const char *target);
279 void set_proxy_authentication (const char *auth);
280 char *get_proxy_target() { return m_proxyTarget; };
281 char *get_session_str() { return m_session_str; };
282 void set_max_clients(int m) { m_max_clients = m; };
283 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
284 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
285 int get_target_idletime () { return m_target_idletime; }
286 int set_config(const char *name);
287 void reconfig() { m_reconfig_flag = 1; }
288 int send_to_client(Z_APDU *apdu);
289 int server(const char *addr);
291 int get_log_mask() { return m_log_mask; };
292 int handle_init_response_for_invalid_session(Z_APDU *apdu);