1 /* $Id: proxy.h,v 1.6 2004-08-29 13:01:43 adam Exp $
2 Copyright (c) 1998-2004, Index Data.
4 This file is part of the yaz-proxy.
6 YAZ proxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with YAZ proxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include <yaz++/z-assoc.h>
23 #include <yaz++/z-query.h>
24 #include <yaz++/z-databases.h>
25 #include <yaz++/cql2rpn.h>
27 #include <yazproxy/bw.h>
31 #define MAX_ZURL_PLEX 10
33 #define PROXY_LOG_APDU_CLIENT 1
34 #define PROXY_LOG_APDU_SERVER 2
35 #define PROXY_LOG_REQ_CLIENT 4
36 #define PROXY_LOG_REQ_SERVER 8
38 struct Yaz_RecordCache_Entry;
39 class Yaz_ProxyConfigP;
41 class YAZ_EXPORT Yaz_ProxyConfig {
45 int read_xml(const char *fname);
47 int get_target_no(int no,
56 int *keepalive_limit_bw,
57 int *keepalive_limit_pdu,
59 const char **cql2rpn);
61 void get_generic_info(int *log_mask, int *max_clients);
63 void get_target_info(const char *name, const char **url,
64 int *limit_bw, int *limit_pdu, int *limit_req,
65 int *target_idletime, int *client_idletime,
67 int *keepalive_limit_bw, int *keepalive_limit_pdu,
69 const char **cql2rpn);
71 int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
72 int check_syntax(ODR odr, const char *name,
73 Odr_oid *syntax, Z_RecordComposition *comp,
74 char **addinfo, char **stylesheet, char **schema,
75 char **backend_type, char **backend_charset);
76 char *get_explain(ODR odr, const char *name, const char *db,
79 void operator=(const Yaz_ProxyConfig &conf);
80 class Yaz_ProxyConfigP *m_cp;
83 class YAZ_EXPORT Yaz_RecordCache {
87 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
89 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
90 Odr_oid *syntax, Z_RecordComposition *comp);
93 void copy_searchRequest(Z_SearchRequest *sr);
94 void copy_presentRequest(Z_PresentRequest *pr);
95 void set_max_size(int sz);
98 Yaz_RecordCache_Entry *m_entries;
99 Z_SearchRequest *m_searchRequest;
100 Z_PresentRequest *m_presentRequest;
101 int match (Yaz_RecordCache_Entry *entry,
102 Odr_oid *syntax, int offset,
103 Z_RecordComposition *comp);
108 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
109 friend class Yaz_Proxy;
110 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
113 void recv_GDU(Z_GDU *apdu, int len);
114 void recv_Z_PDU(Z_APDU *apdu, int len);
115 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
116 IYaz_PDU_Observer* sessionNotify
117 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
121 void timeoutNotify();
122 void connectNotify();
123 int send_to_target(Z_APDU *apdu);
124 const char *get_session_str();
126 Yaz_ProxyClient *m_next;
127 Yaz_ProxyClient **m_prev;
129 Yaz_Z_Query *m_last_query;
130 Yaz_Z_Databases m_last_databases;
131 char *m_last_resultSetId;
133 int m_last_resultCount;
137 int m_resultSetStartPoint;
142 Z_APDU *m_initResponse;
143 Z_Options *m_initResponse_options;
144 Z_ProtocolVersion *m_initResponse_version;
145 int m_initResponse_preferredMessageSize;
146 int m_initResponse_maximumRecordSize;
147 Yaz_RecordCache m_cache;
148 void pre_init_client();
149 int m_target_idletime;
154 /// Information Retrieval Proxy Server.
155 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
157 char *get_cookie(Z_OtherInformation **otherInfo);
158 char *get_proxy(Z_OtherInformation **otherInfo);
159 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
160 const char *proxy_host);
161 Z_APDU *result_set_optimize(Z_APDU *apdu);
164 Yaz_ProxyClient *m_client;
165 IYaz_PDU_Observable *m_PDU_Observable;
166 Yaz_ProxyClient *m_clientPool;
171 int m_keepalive_limit_bw;
172 int m_keepalive_limit_pdu;
173 int m_client_idletime;
174 int m_target_idletime;
176 char *m_default_target;
177 char *m_proxy_authentication;
180 int m_session_no; // sequence for each client session
181 char m_session_str[30]; // session string (time:session_no)
182 Yaz_ProxyConfig *m_config;
183 char *m_config_fname;
190 Z_GDU *m_bw_hold_PDU;
191 int m_max_record_retrieve;
192 void handle_max_record_retrieve(Z_APDU *apdu);
193 void display_diagrecs(Z_DiagRec **pp, int num);
194 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
195 const char *addinfo);
197 Z_APDU *handle_query_validation(Z_APDU *apdu);
198 Z_APDU *handle_query_transformation(Z_APDU *apdu);
200 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
201 const char *load_balance(const char **url);
203 Yaz_ProxyConfig *check_reconfigure();
205 int m_invalid_session;
207 void *m_stylesheet_xsp; // Really libxslt's xsltStylesheetPtr
208 int m_stylesheet_offset;
209 Z_APDU *m_stylesheet_apdu;
210 Z_NamePlusRecordList *m_stylesheet_nprl;
212 char *m_backend_type;
213 char *m_backend_charset;
215 void convert_to_frontend_type(Z_NamePlusRecordList *p);
216 void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
217 int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
218 void convert_xsl_delay();
219 Z_APDU *m_initRequest_apdu;
220 int m_initRequest_preferredMessageSize;
221 int m_initRequest_maximumRecordSize;
222 Z_Options *m_initRequest_options;
223 Z_ProtocolVersion *m_initRequest_version;
224 NMEM m_initRequest_mem;
225 Z_APDU *m_apdu_invalid_session;
226 NMEM m_mem_invalid_session;
227 int send_PDU_convert(Z_APDU *apdu);
229 ODR m_s2z_odr_search;
232 char *m_s2z_database;
233 Z_APDU *m_s2z_init_apdu;
234 Z_APDU *m_s2z_search_apdu;
235 Z_APDU *m_s2z_present_apdu;
236 char *m_s2z_stylesheet;
238 int send_to_srw_client_error(int error, const char *add);
239 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
240 int send_http_response(int code);
241 int send_srw_response(Z_SRW_PDU *srw_pdu);
242 int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
243 int num_diagnostics);
244 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
245 Z_DefaultDiagFormat *ddf);
246 int m_http_keepalive;
247 const char *m_http_version;
248 Yaz_cql2rpn m_cql2rpn;
251 Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
252 Z_ReferenceId *m_referenceId;
253 NMEM m_referenceId_mem;
254 #define NO_SPARE_SOLARIS_FD 10
255 int m_lo_fd[NO_SPARE_SOLARIS_FD];
256 void low_socket_open();
257 void low_socket_close();
259 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
260 Yaz_Proxy *parent = 0);
262 void inc_request_no();
263 void recv_GDU(Z_GDU *apdu, int len);
264 void handle_incoming_HTTP(Z_HTTP_Request *req);
265 void handle_incoming_Z_PDU(Z_APDU *apdu);
266 IYaz_PDU_Observer* sessionNotify
267 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
269 void timeoutNotify();
270 void connectNotify();
272 const char *option(const char *name, const char *value);
273 void set_default_target(const char *target);
274 void set_proxy_authentication (const char *auth);
275 char *get_proxy_target() { return m_proxyTarget; };
276 char *get_session_str() { return m_session_str; };
277 void set_max_clients(int m) { m_max_clients = m; };
278 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
279 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
280 int get_target_idletime () { return m_target_idletime; }
281 int set_config(const char *name);
282 void reconfig() { m_reconfig_flag = 1; }
283 int send_to_client(Z_APDU *apdu);
284 int server(const char *addr);
286 int get_log_mask() { return m_log_mask; };
287 int handle_init_response_for_invalid_session(Z_APDU *apdu);