cb96fc1364b8e05b10ba4fd7b20c4385d4d92de7
[yazpp-moved-to-github.git] / include / yaz++ / proxy.h
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: proxy.h,v 1.18 2003-10-16 13:40:41 adam Exp $
6  */
7
8 #include <yaz++/z-assoc.h>
9 #include <yaz++/z-query.h>
10 #include <yaz++/z-databases.h>
11
12 #if HAVE_XML2
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
15 #endif
16
17 class Yaz_Proxy;
18
19 #define MAX_ZURL_PLEX 10
20
21 #define PROXY_LOG_APDU 1
22 #define PROXY_LOG_REQ 1
23
24 struct Yaz_RecordCache_Entry;
25
26 class YAZ_EXPORT Yaz_ProxyConfig {
27 public:
28     Yaz_ProxyConfig();
29     ~Yaz_ProxyConfig();
30     int read_xml(const char *fname);
31
32     int get_target_no(int no,
33                       const char **name,
34                       const char **url,
35                       int *limit_bw,
36                       int *limit_pdu,
37                       int *limit_req,
38                       int *target_idletime,
39                       int *client_idletime,
40                       int *max_clients,
41                       int *keepalive_limit_bw,
42                       int *keepalive_limit_pdu,
43                       int *pre_init,
44                       int *log_mask);
45
46     void get_target_info(const char *name, const char **url,
47                          int *limit_bw, int *limit_pdu, int *limit_req,
48                          int *target_idletime, int *client_idletime,
49                          int *max_clients,
50                          int *keepalive_limit_bw, int *keepalive_limit_pdu,
51                          int *pre_init, int *log_mask);
52
53     int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
54     int check_syntax(ODR odr, const char *name,
55                      Odr_oid *syntax, char **addinfo);
56 private:
57     void operator=(const Yaz_ProxyConfig &conf);
58 #if HAVE_XML2
59     xmlDocPtr m_docPtr;
60     xmlNodePtr m_proxyPtr;
61     void return_target_info(xmlNodePtr ptr, const char **url,
62                             int *limit_bw, int *limit_pdu, int *limit_req,
63                             int *target_idletime, int *client_idletime,
64                             int *keepalive_limit_bw, int *keepalive_limit_pdu,
65                             int *pre_init, int *log_mask);
66     void return_limit(xmlNodePtr ptr,
67                       int *limit_bw, int *limit_pdu, int *limit_req);
68     int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
69                      char **addinfo);
70     xmlNodePtr find_target_node(const char *name);
71     const char *get_text(xmlNodePtr ptr);
72     int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
73                                 Z_AttributeList *attrs,
74                                 char **addinfo);
75     int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
76                                char **addinfo);
77 #endif
78     int m_copy;
79     int match_list(int v, const char *m);
80     int atoi_l(const char **cp);
81
82 };
83
84 class YAZ_EXPORT Yaz_RecordCache {
85  public:
86     Yaz_RecordCache ();
87     ~Yaz_RecordCache ();
88     void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
89     
90     int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
91                 Odr_oid *syntax, Z_RecordComposition *comp);
92     void clear();
93
94     void copy_searchRequest(Z_SearchRequest *sr);
95     void copy_presentRequest(Z_PresentRequest *pr);
96     void set_max_size(int sz);
97  private:
98     NMEM m_mem;
99     Yaz_RecordCache_Entry *m_entries;
100     Z_SearchRequest *m_searchRequest;
101     Z_PresentRequest *m_presentRequest;
102     int match (Yaz_RecordCache_Entry *entry,
103                Odr_oid *syntax, int offset,
104                Z_RecordComposition *comp);
105     int m_max_size;
106 };
107
108 class YAZ_EXPORT Yaz_bw {
109  public:
110     Yaz_bw(int sz);
111     ~Yaz_bw();
112     void add_bytes(int m);
113     int get_total();
114  private:
115     long m_sec;   // time of most recent bucket
116     int *m_bucket;
117     int m_ptr;
118     int m_size;
119 };
120
121 /// Private class
122 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
123     friend class Yaz_Proxy;
124     Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
125                     Yaz_Proxy *parent);
126     ~Yaz_ProxyClient();
127     void recv_Z_PDU(Z_APDU *apdu, int len);
128     IYaz_PDU_Observer* sessionNotify
129         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
130     void shutdown();
131     Yaz_Proxy *m_server;
132     void failNotify();
133     void timeoutNotify();
134     void connectNotify();
135     int send_to_target(Z_APDU *apdu);
136     const char *get_session_str();
137     char *m_cookie;
138     Yaz_ProxyClient *m_next;
139     Yaz_ProxyClient **m_prev;
140     int m_init_flag;
141     Yaz_Z_Query *m_last_query;
142     Yaz_Z_Databases m_last_databases;
143     char *m_last_resultSetId;
144     int m_last_ok;
145     int m_last_resultCount;
146     int m_sr_transform;
147     int m_seqno;
148     int m_waiting;
149     int m_resultSetStartPoint;
150     int m_bytes_sent;
151     int m_bytes_recv;
152     int m_pdu_recv;
153     ODR m_init_odr;
154     Z_APDU *m_initResponse;
155     Yaz_RecordCache m_cache;
156     void pre_init_client();
157     int m_target_idletime;
158     Yaz_Proxy *m_root;
159 };
160
161 /// Information Retrieval Proxy Server.
162 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
163  private:
164     char *get_cookie(Z_OtherInformation **otherInfo);
165     char *get_proxy(Z_OtherInformation **otherInfo);
166     Yaz_ProxyClient *get_client(Z_APDU *apdu);
167     Z_APDU *result_set_optimize(Z_APDU *apdu);
168     void shutdown();
169     
170     Yaz_ProxyClient *m_client;
171     IYaz_PDU_Observable *m_PDU_Observable;
172     Yaz_ProxyClient *m_clientPool;
173     Yaz_Proxy *m_parent;
174     int m_seqno;
175     int m_max_clients;
176     int m_keepalive_limit_bw;
177     int m_keepalive_limit_pdu;
178     int m_client_idletime;
179     int m_target_idletime;
180     char *m_proxyTarget;
181     char *m_default_target;
182     char *m_proxy_authentication;
183     long m_seed;
184     char *m_optimize;
185     int m_session_no;         // sequence for each client session
186     char m_session_str[30];  // session string (time:session_no)
187     Yaz_ProxyConfig *m_config;
188     char *m_config_fname;
189     int m_bytes_sent;
190     int m_bytes_recv;
191     int m_bw_max;
192     Yaz_bw m_bw_stat;
193     int m_pdu_max;
194     Yaz_bw m_pdu_stat;
195     Z_APDU *m_bw_hold_PDU;
196     int m_max_record_retrieve;
197     void handle_max_record_retrieve(Z_APDU *apdu);
198     void display_diagrecs(Z_DiagRec **pp, int num);
199     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
200                                               const char *addinfo);
201
202     Z_APDU *handle_query_validation(Z_APDU *apdu);
203     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
204     const char *load_balance(const char **url);
205     int m_reconfig_flag;
206     Yaz_ProxyConfig *check_reconfigure();
207     int m_request_no;
208     int m_invalid_session;
209     int m_marcxml_flag;
210     void convert_to_marcxml(Z_NamePlusRecordList *p);
211  public:
212     Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
213               Yaz_Proxy *parent = 0);
214     ~Yaz_Proxy();
215     void recv_Z_PDU(Z_APDU *apdu, int len);
216     void recv_Z_PDU_0(Z_APDU *apdu);
217     IYaz_PDU_Observer* sessionNotify
218         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
219     void failNotify();
220     void timeoutNotify();
221     void connectNotify();
222     const char *option(const char *name, const char *value);
223     void set_default_target(const char *target);
224     void set_proxy_authentication (const char *auth);
225     char *get_proxy_target() { return m_proxyTarget; };
226     char *get_session_str() { return m_session_str; };
227     void set_max_clients(int m) { m_max_clients = m; };
228     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
229     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
230     int get_target_idletime () { return m_target_idletime; }
231     int set_config(const char *name);
232     void reconfig() { m_reconfig_flag = 1; }
233     int send_to_client(Z_APDU *apdu);
234     void server(const char *addr);
235     void pre_init();
236 };
237