2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: proxy.h,v 1.22 2003-12-16 14:17:01 adam Exp $
8 #include <yaz++/z-assoc.h>
9 #include <yaz++/z-query.h>
10 #include <yaz++/z-databases.h>
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
19 #define MAX_ZURL_PLEX 10
21 #define PROXY_LOG_APDU_CLIENT 1
22 #define PROXY_LOG_APDU_SERVER 2
23 #define PROXY_LOG_REQ_CLIENT 4
24 #define PROXY_LOG_REQ_SERVER 8
26 struct Yaz_RecordCache_Entry;
28 class YAZ_EXPORT Yaz_ProxyConfig {
32 int read_xml(const char *fname);
34 int get_target_no(int no,
43 int *keepalive_limit_bw,
44 int *keepalive_limit_pdu,
46 const char **cql2rpn);
48 void get_generic_info(int *log_mask, int *max_clients);
50 void get_target_info(const char *name, const char **url,
51 int *limit_bw, int *limit_pdu, int *limit_req,
52 int *target_idletime, int *client_idletime,
54 int *keepalive_limit_bw, int *keepalive_limit_pdu,
56 const char **cql2rpn);
58 int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
59 int check_syntax(ODR odr, const char *name,
60 Odr_oid *syntax, char **addinfo);
62 void operator=(const Yaz_ProxyConfig &conf);
63 int mycmp(const char *hay, const char *item, size_t len);
66 xmlNodePtr m_proxyPtr;
67 void return_target_info(xmlNodePtr ptr, const char **url,
68 int *limit_bw, int *limit_pdu, int *limit_req,
69 int *target_idletime, int *client_idletime,
70 int *keepalive_limit_bw, int *keepalive_limit_pdu,
71 int *pre_init, const char **cql2rpn);
72 void return_limit(xmlNodePtr ptr,
73 int *limit_bw, int *limit_pdu, int *limit_req);
74 int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
76 xmlNodePtr find_target_node(const char *name);
77 const char *get_text(xmlNodePtr ptr);
78 int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
79 Z_AttributeList *attrs,
81 int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
85 int match_list(int v, const char *m);
86 int atoi_l(const char **cp);
90 class YAZ_EXPORT Yaz_RecordCache {
94 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
96 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
97 Odr_oid *syntax, Z_RecordComposition *comp);
100 void copy_searchRequest(Z_SearchRequest *sr);
101 void copy_presentRequest(Z_PresentRequest *pr);
102 void set_max_size(int sz);
105 Yaz_RecordCache_Entry *m_entries;
106 Z_SearchRequest *m_searchRequest;
107 Z_PresentRequest *m_presentRequest;
108 int match (Yaz_RecordCache_Entry *entry,
109 Odr_oid *syntax, int offset,
110 Z_RecordComposition *comp);
114 class YAZ_EXPORT Yaz_bw {
118 void add_bytes(int m);
121 long m_sec; // time of most recent bucket
128 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
129 friend class Yaz_Proxy;
130 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
133 void recv_GDU(Z_GDU *apdu, int len);
134 void recv_Z_PDU(Z_APDU *apdu, int len);
135 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
136 IYaz_PDU_Observer* sessionNotify
137 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
141 void timeoutNotify();
142 void connectNotify();
143 int send_to_target(Z_APDU *apdu);
144 const char *get_session_str();
146 Yaz_ProxyClient *m_next;
147 Yaz_ProxyClient **m_prev;
149 Yaz_Z_Query *m_last_query;
150 Yaz_Z_Databases m_last_databases;
151 char *m_last_resultSetId;
153 int m_last_resultCount;
157 int m_resultSetStartPoint;
162 Z_APDU *m_initResponse;
163 Yaz_RecordCache m_cache;
164 void pre_init_client();
165 int m_target_idletime;
169 class YAZ_EXPORT Yaz_cql2rpn {
173 void set_pqf_file(const char *fname);
174 int query_transform(const char *cql, Z_RPNQuery **rpnquery, ODR o);
176 cql_transform_t m_transform;
180 /// Information Retrieval Proxy Server.
181 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
183 char *get_cookie(Z_OtherInformation **otherInfo);
184 char *get_proxy(Z_OtherInformation **otherInfo);
185 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
186 const char *proxy_host);
187 Z_APDU *result_set_optimize(Z_APDU *apdu);
190 Yaz_ProxyClient *m_client;
191 IYaz_PDU_Observable *m_PDU_Observable;
192 Yaz_ProxyClient *m_clientPool;
197 int m_keepalive_limit_bw;
198 int m_keepalive_limit_pdu;
199 int m_client_idletime;
200 int m_target_idletime;
202 char *m_default_target;
203 char *m_proxy_authentication;
206 int m_session_no; // sequence for each client session
207 char m_session_str[30]; // session string (time:session_no)
208 Yaz_ProxyConfig *m_config;
209 char *m_config_fname;
216 Z_GDU *m_bw_hold_PDU;
217 int m_max_record_retrieve;
218 void handle_max_record_retrieve(Z_APDU *apdu);
219 void display_diagrecs(Z_DiagRec **pp, int num);
220 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
221 const char *addinfo);
223 Z_APDU *handle_query_validation(Z_APDU *apdu);
224 Z_APDU *handle_query_transformation(Z_APDU *apdu);
226 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
227 const char *load_balance(const char **url);
229 Yaz_ProxyConfig *check_reconfigure();
231 int m_invalid_session;
233 void convert_to_marcxml(Z_NamePlusRecordList *p);
234 Z_APDU *m_initRequest_apdu;
235 NMEM m_initRequest_mem;
236 Z_APDU *m_apdu_invalid_session;
237 NMEM m_mem_invalid_session;
238 int send_PDU_convert(Z_APDU *apdu, int *len);
242 Z_APDU *m_s2z_init_apdu;
243 Z_APDU *m_s2z_search_apdu;
244 Z_APDU *m_s2z_present_apdu;
246 int send_to_srw_client_error(int error);
247 int send_to_srw_client_ok(int hits, Z_Records *records);
248 int send_http_response(int code);
249 int send_srw_response(Z_SRW_PDU *srw_pdu);
251 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
252 Z_DefaultDiagFormat *ddf);
253 int m_http_keepalive;
254 const char *m_http_version;
255 Yaz_cql2rpn m_cql2rpn;
257 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
258 Yaz_Proxy *parent = 0);
260 void recv_GDU(Z_GDU *apdu, int len);
261 void handle_incoming_HTTP(Z_HTTP_Request *req);
262 void handle_incoming_Z_PDU(Z_APDU *apdu);
263 IYaz_PDU_Observer* sessionNotify
264 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
266 void timeoutNotify();
267 void connectNotify();
268 const char *option(const char *name, const char *value);
269 void set_default_target(const char *target);
270 void set_proxy_authentication (const char *auth);
271 char *get_proxy_target() { return m_proxyTarget; };
272 char *get_session_str() { return m_session_str; };
273 void set_max_clients(int m) { m_max_clients = m; };
274 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
275 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
276 int get_target_idletime () { return m_target_idletime; }
277 int set_config(const char *name);
278 void reconfig() { m_reconfig_flag = 1; }
279 int send_to_client(Z_APDU *apdu);
280 int server(const char *addr);
282 int get_log_mask() { return m_log_mask; };
283 int handle_init_response_for_invalid_session(Z_APDU *apdu);