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