User-defined error handling for queries in proxy
[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.9 2003-10-03 13:01:42 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 struct Yaz_RecordCache_Entry;
20
21 class YAZ_EXPORT Yaz_ProxyConfig {
22 public:
23     Yaz_ProxyConfig();
24     ~Yaz_ProxyConfig();
25     int read_xml(const char *fname);
26     void get_target_info(const char *name, const char **url, int *keepalive,
27                          int *limit_bw, int *limit_pdu, int *limit_req,
28                          int *target_idletime, int *client_idletime,
29                          int *max_clients);
30     void operator=(const Yaz_ProxyConfig &conf);
31     int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
32 private:
33 #if HAVE_XML2
34     xmlDocPtr m_docPtr;
35     xmlNodePtr m_proxyPtr;
36     void return_target_info(xmlNodePtr ptr, const char **url, int *keepalive,
37                             int *limit_bw, int *limit_pdu, int *limit_req,
38                             int *target_idletime, int *client_idletime);
39     void return_limit(xmlNodePtr ptr,
40                       int *limit_bw, int *limit_pdu, int *limit_req);
41     int check_type_1(ODR odr, xmlNodePtr ptr, Z_RPNQuery *query,
42                      char **addinfo);
43     xmlNodePtr find_target_node(const char *name);
44     const char *get_text(xmlNodePtr ptr);
45     int check_type_1_attributes(ODR odr, xmlNodePtr ptr,
46                                 Z_AttributeList *attrs,
47                                 char **addinfo);
48     int check_type_1_structure(ODR odr, xmlNodePtr ptr, Z_RPNStructure *q,
49                                char **addinfo);
50 #endif
51     int m_copy;
52 };
53
54 class YAZ_EXPORT Yaz_RecordCache {
55  public:
56     Yaz_RecordCache ();
57     ~Yaz_RecordCache ();
58     void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
59     
60     int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
61                 Odr_oid *syntax, Z_RecordComposition *comp);
62     void clear();
63
64     void copy_searchRequest(Z_SearchRequest *sr);
65     void copy_presentRequest(Z_PresentRequest *pr);
66  private:
67     NMEM m_mem;
68     Yaz_RecordCache_Entry *m_entries;
69     Z_SearchRequest *m_searchRequest;
70     Z_PresentRequest *m_presentRequest;
71     int match (Yaz_RecordCache_Entry *entry,
72                Odr_oid *syntax, int offset,
73                Z_RecordComposition *comp);
74 };
75
76 class YAZ_EXPORT Yaz_bw {
77  public:
78     Yaz_bw(int sz);
79     ~Yaz_bw();
80     void add_bytes(int m);
81     int get_total();
82  private:
83     long m_sec;   // time of most recent bucket
84     int *m_bucket;
85     int m_ptr;
86     int m_size;
87 };
88
89 /// Private class
90 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
91     friend class Yaz_Proxy;
92     Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable);
93     ~Yaz_ProxyClient();
94     void recv_Z_PDU(Z_APDU *apdu, int len);
95     IYaz_PDU_Observer* sessionNotify
96         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
97     void shutdown();
98     Yaz_Proxy *m_server;
99     void failNotify();
100     void timeoutNotify();
101     void connectNotify();
102     int send_to_target(Z_APDU *apdu);
103     const char *get_session_str();
104     char *m_cookie;
105     Yaz_ProxyClient *m_next;
106     Yaz_ProxyClient **m_prev;
107     int m_init_flag;
108     Yaz_Z_Query *m_last_query;
109     Yaz_Z_Databases m_last_databases;
110     char *m_last_resultSetId;
111     int m_last_ok;
112     int m_last_resultCount;
113     int m_sr_transform;
114     int m_seqno;
115     int m_waiting;
116     int m_resultSetStartPoint;
117     int m_bytes_sent;
118     int m_bytes_recv;
119     ODR m_init_odr;
120     Z_APDU *m_initResponse;
121     Yaz_RecordCache m_cache;
122 };
123
124 /// Information Retrieval Proxy Server.
125 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
126  private:
127     char *get_cookie(Z_OtherInformation **otherInfo);
128     char *get_proxy(Z_OtherInformation **otherInfo);
129     Yaz_ProxyClient *get_client(Z_APDU *apdu);
130     Z_APDU *result_set_optimize(Z_APDU *apdu);
131     void shutdown();
132     
133     Yaz_ProxyClient *m_client;
134     IYaz_PDU_Observable *m_PDU_Observable;
135     Yaz_ProxyClient *m_clientPool;
136     Yaz_Proxy *m_parent;
137     int m_seqno;
138     int m_max_clients;
139     int m_keepalive;
140     int m_client_idletime;
141     int m_target_idletime;
142     char *m_proxyTarget;
143     char *m_default_target;
144     char *m_proxy_authentication;
145     long m_seed;
146     char *m_optimize;
147     int m_session_no;         // sequence for each client session
148     char m_session_str[20];  // session string (time:session_no)
149     Yaz_ProxyConfig m_config;
150     int m_bytes_sent;
151     int m_bytes_recv;
152     int m_bw_max;
153     Yaz_bw m_bw_stat;
154     int m_pdu_max;
155     Yaz_bw m_pdu_stat;
156     Z_APDU *m_bw_hold_PDU;
157     int m_max_record_retrieve;
158     void handle_max_record_retrieve(Z_APDU *apdu);
159     void display_diagrecs(Z_DiagRec **pp, int num);
160     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
161                                               const char *addinfo);
162
163     Z_APDU *handle_query_validation(Z_APDU *apdu);
164  public:
165     Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable);
166     ~Yaz_Proxy();
167     void recv_Z_PDU(Z_APDU *apdu, int len);
168     void recv_Z_PDU_0(Z_APDU *apdu);
169     IYaz_PDU_Observer* sessionNotify
170         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
171     void failNotify();
172     void timeoutNotify();
173     void connectNotify();
174     const char *option(const char *name, const char *value);
175     void set_default_target(const char *target);
176     void set_proxy_authentication (const char *auth);
177     char *get_proxy_target() { return m_proxyTarget; };
178     char *get_session_str() { return m_session_str; };
179     void set_max_clients(int m) { m_max_clients = m; };
180     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
181     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
182     int get_target_idletime () { return m_target_idletime; }
183     int set_config(const char *name);
184     int send_to_client(Z_APDU *apdu);
185 };
186