2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: proxy.h,v 1.23 2003-12-20 22:44:30 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,
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,
60 int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
61 int check_syntax(ODR odr, const char *name,
62 Odr_oid *syntax, char **addinfo);
64 void operator=(const Yaz_ProxyConfig &conf);
65 int mycmp(const char *hay, const char *item, size_t len);
68 xmlNodePtr m_proxyPtr;
69 void return_target_info(xmlNodePtr ptr, const char **url,
70 int *limit_bw, int *limit_pdu, int *limit_req,
71 int *target_idletime, int *client_idletime,
72 int *keepalive_limit_bw, int *keepalive_limit_pdu,
73 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);
80 const char *get_text(xmlNodePtr ptr);
81 int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
82 Z_AttributeList *attrs,
84 int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
88 int match_list(int v, const char *m);
89 int atoi_l(const char **cp);
93 class YAZ_EXPORT Yaz_RecordCache {
97 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
99 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
100 Odr_oid *syntax, Z_RecordComposition *comp);
103 void copy_searchRequest(Z_SearchRequest *sr);
104 void copy_presentRequest(Z_PresentRequest *pr);
105 void set_max_size(int sz);
108 Yaz_RecordCache_Entry *m_entries;
109 Z_SearchRequest *m_searchRequest;
110 Z_PresentRequest *m_presentRequest;
111 int match (Yaz_RecordCache_Entry *entry,
112 Odr_oid *syntax, int offset,
113 Z_RecordComposition *comp);
117 class YAZ_EXPORT Yaz_bw {
121 void add_bytes(int m);
124 long m_sec; // time of most recent bucket
131 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
132 friend class Yaz_Proxy;
133 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
136 void recv_GDU(Z_GDU *apdu, int len);
137 void recv_Z_PDU(Z_APDU *apdu, int len);
138 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
139 IYaz_PDU_Observer* sessionNotify
140 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
144 void timeoutNotify();
145 void connectNotify();
146 int send_to_target(Z_APDU *apdu);
147 const char *get_session_str();
149 Yaz_ProxyClient *m_next;
150 Yaz_ProxyClient **m_prev;
152 Yaz_Z_Query *m_last_query;
153 Yaz_Z_Databases m_last_databases;
154 char *m_last_resultSetId;
156 int m_last_resultCount;
160 int m_resultSetStartPoint;
165 Z_APDU *m_initResponse;
166 Yaz_RecordCache m_cache;
167 void pre_init_client();
168 int m_target_idletime;
172 class YAZ_EXPORT Yaz_cql2rpn {
176 void set_pqf_file(const char *fname);
177 int query_transform(const char *cql, Z_RPNQuery **rpnquery, ODR o);
179 cql_transform_t m_transform;
183 /// Information Retrieval Proxy Server.
184 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
186 char *get_cookie(Z_OtherInformation **otherInfo);
187 char *get_proxy(Z_OtherInformation **otherInfo);
188 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
189 const char *proxy_host);
190 Z_APDU *result_set_optimize(Z_APDU *apdu);
193 Yaz_ProxyClient *m_client;
194 IYaz_PDU_Observable *m_PDU_Observable;
195 Yaz_ProxyClient *m_clientPool;
200 int m_keepalive_limit_bw;
201 int m_keepalive_limit_pdu;
202 int m_client_idletime;
203 int m_target_idletime;
205 char *m_default_target;
206 char *m_proxy_authentication;
209 int m_session_no; // sequence for each client session
210 char m_session_str[30]; // session string (time:session_no)
211 Yaz_ProxyConfig *m_config;
212 char *m_config_fname;
219 Z_GDU *m_bw_hold_PDU;
220 int m_max_record_retrieve;
221 void handle_max_record_retrieve(Z_APDU *apdu);
222 void display_diagrecs(Z_DiagRec **pp, int num);
223 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
224 const char *addinfo);
226 Z_APDU *handle_query_validation(Z_APDU *apdu);
227 Z_APDU *handle_query_transformation(Z_APDU *apdu);
229 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
230 const char *load_balance(const char **url);
232 Yaz_ProxyConfig *check_reconfigure();
234 int m_invalid_session;
236 void convert_to_marcxml(Z_NamePlusRecordList *p);
237 Z_APDU *m_initRequest_apdu;
238 NMEM m_initRequest_mem;
239 Z_APDU *m_apdu_invalid_session;
240 NMEM m_mem_invalid_session;
241 int send_PDU_convert(Z_APDU *apdu, int *len);
243 ODR m_s2z_odr_search;
246 Z_APDU *m_s2z_init_apdu;
247 Z_APDU *m_s2z_search_apdu;
248 Z_APDU *m_s2z_present_apdu;
250 int send_to_srw_client_error(int error);
251 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
252 int send_http_response(int code);
253 int send_srw_response(Z_SRW_PDU *srw_pdu);
254 int send_srw_explain();
256 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
257 Z_DefaultDiagFormat *ddf);
258 int m_http_keepalive;
259 const char *m_http_version;
260 Yaz_cql2rpn m_cql2rpn;
261 const char *m_zeerex_fname;
263 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
264 Yaz_Proxy *parent = 0);
266 void recv_GDU(Z_GDU *apdu, int len);
267 void handle_incoming_HTTP(Z_HTTP_Request *req);
268 void handle_incoming_Z_PDU(Z_APDU *apdu);
269 IYaz_PDU_Observer* sessionNotify
270 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
272 void timeoutNotify();
273 void connectNotify();
274 const char *option(const char *name, const char *value);
275 void set_default_target(const char *target);
276 void set_proxy_authentication (const char *auth);
277 char *get_proxy_target() { return m_proxyTarget; };
278 char *get_session_str() { return m_session_str; };
279 void set_max_clients(int m) { m_max_clients = m; };
280 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
281 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
282 int get_target_idletime () { return m_target_idletime; }
283 int set_config(const char *name);
284 void reconfig() { m_reconfig_flag = 1; }
285 int send_to_client(Z_APDU *apdu);
286 int server(const char *addr);
288 int get_log_mask() { return m_log_mask; };
289 int handle_init_response_for_invalid_session(Z_APDU *apdu);