1 /* $Id: proxy.h,v 1.8 2004-10-23 23:12:24 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,
60 const char **authentication);
62 void get_generic_info(int *log_mask, int *max_clients);
64 void get_target_info(const char *name, const char **url,
65 int *limit_bw, int *limit_pdu, int *limit_req,
66 int *target_idletime, int *client_idletime,
68 int *keepalive_limit_bw, int *keepalive_limit_pdu,
71 const char **authentication);
73 int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
74 int check_syntax(ODR odr, const char *name,
75 Odr_oid *syntax, Z_RecordComposition *comp,
76 char **addinfo, char **stylesheet, char **schema,
77 char **backend_type, char **backend_charset);
78 char *get_explain_doc(ODR odr, const char *name, const char *db,
80 const char *get_explain_name(const char *db, const char **backend_db);
82 void operator=(const Yaz_ProxyConfig &conf);
83 class Yaz_ProxyConfigP *m_cp;
86 class YAZ_EXPORT Yaz_RecordCache {
90 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
92 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
93 Odr_oid *syntax, Z_RecordComposition *comp);
96 void copy_searchRequest(Z_SearchRequest *sr);
97 void copy_presentRequest(Z_PresentRequest *pr);
98 void set_max_size(int sz);
101 Yaz_RecordCache_Entry *m_entries;
102 Z_SearchRequest *m_searchRequest;
103 Z_PresentRequest *m_presentRequest;
104 int match (Yaz_RecordCache_Entry *entry,
105 Odr_oid *syntax, int offset,
106 Z_RecordComposition *comp);
111 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
112 friend class Yaz_Proxy;
113 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
116 void recv_GDU(Z_GDU *apdu, int len);
117 void recv_Z_PDU(Z_APDU *apdu, int len);
118 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
119 IYaz_PDU_Observer* sessionNotify
120 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
124 void timeoutNotify();
125 void connectNotify();
126 int send_to_target(Z_APDU *apdu);
127 const char *get_session_str();
129 Yaz_ProxyClient *m_next;
130 Yaz_ProxyClient **m_prev;
132 Yaz_Z_Query *m_last_query;
133 Yaz_Z_Databases m_last_databases;
134 char *m_last_resultSetId;
136 int m_last_resultCount;
140 int m_resultSetStartPoint;
145 Z_APDU *m_initResponse;
146 Z_Options *m_initResponse_options;
147 Z_ProtocolVersion *m_initResponse_version;
148 int m_initResponse_preferredMessageSize;
149 int m_initResponse_maximumRecordSize;
150 Yaz_RecordCache m_cache;
151 void pre_init_client();
152 int m_target_idletime;
157 /// Information Retrieval Proxy Server.
158 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
160 char *get_cookie(Z_OtherInformation **otherInfo);
161 char *get_proxy(Z_OtherInformation **otherInfo);
162 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
163 const char *proxy_host);
164 void srw_get_client(const char *db, const char **backend_db);
165 Z_APDU *result_set_optimize(Z_APDU *apdu);
167 void releaseClient();
168 Yaz_ProxyClient *m_client;
169 IYaz_PDU_Observable *m_PDU_Observable;
170 Yaz_ProxyClient *m_clientPool;
175 int m_keepalive_limit_bw;
176 int m_keepalive_limit_pdu;
177 int m_client_idletime;
178 int m_target_idletime;
180 char *m_default_target;
181 char *m_proxy_authentication;
184 int m_session_no; // sequence for each client session
185 char m_session_str[30]; // session string (time:session_no)
186 Yaz_ProxyConfig *m_config;
187 char *m_config_fname;
194 Z_GDU *m_bw_hold_PDU;
195 int m_max_record_retrieve;
196 void handle_max_record_retrieve(Z_APDU *apdu);
197 void display_diagrecs(Z_DiagRec **pp, int num);
198 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
199 const char *addinfo);
201 Z_APDU *handle_query_validation(Z_APDU *apdu);
202 Z_APDU *handle_query_transformation(Z_APDU *apdu);
204 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
205 const char *load_balance(const char **url);
207 Yaz_ProxyConfig *check_reconfigure();
209 int m_invalid_session;
211 void *m_stylesheet_xsp; // Really libxslt's xsltStylesheetPtr
212 int m_stylesheet_offset;
213 Z_APDU *m_stylesheet_apdu;
214 Z_NamePlusRecordList *m_stylesheet_nprl;
216 char *m_backend_type;
217 char *m_backend_charset;
219 void convert_to_frontend_type(Z_NamePlusRecordList *p);
220 void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
221 int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
222 void convert_xsl_delay();
223 Z_APDU *m_initRequest_apdu;
224 int m_initRequest_preferredMessageSize;
225 int m_initRequest_maximumRecordSize;
226 Z_Options *m_initRequest_options;
227 Z_ProtocolVersion *m_initRequest_version;
228 NMEM m_initRequest_mem;
229 Z_APDU *m_apdu_invalid_session;
230 NMEM m_mem_invalid_session;
231 int send_PDU_convert(Z_APDU *apdu);
233 ODR m_s2z_odr_search;
236 char *m_s2z_database;
237 Z_APDU *m_s2z_init_apdu;
238 Z_APDU *m_s2z_search_apdu;
239 Z_APDU *m_s2z_present_apdu;
240 char *m_s2z_stylesheet;
242 int send_to_srw_client_error(int error, const char *add);
243 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
244 int send_http_response(int code);
245 int send_srw_response(Z_SRW_PDU *srw_pdu);
246 int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
247 int num_diagnostics);
248 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
249 Z_DefaultDiagFormat *ddf);
250 int m_http_keepalive;
251 const char *m_http_version;
252 Yaz_cql2rpn m_cql2rpn;
255 Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
256 Z_ReferenceId *m_referenceId;
257 NMEM m_referenceId_mem;
258 #define NO_SPARE_SOLARIS_FD 10
259 int m_lo_fd[NO_SPARE_SOLARIS_FD];
260 void low_socket_open();
261 void low_socket_close();
263 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
264 Yaz_Proxy *parent = 0);
266 void inc_request_no();
267 void recv_GDU(Z_GDU *apdu, int len);
268 void handle_incoming_HTTP(Z_HTTP_Request *req);
269 void handle_incoming_Z_PDU(Z_APDU *apdu);
270 IYaz_PDU_Observer* sessionNotify
271 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
273 void timeoutNotify();
274 void connectNotify();
276 const char *option(const char *name, const char *value);
277 void set_default_target(const char *target);
278 void set_proxy_authentication (const char *auth);
279 char *get_proxy_target() { return m_proxyTarget; };
280 char *get_session_str() { return m_session_str; };
281 void set_max_clients(int m) { m_max_clients = m; };
282 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
283 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
284 int get_target_idletime () { return m_target_idletime; }
285 int set_config(const char *name);
286 void reconfig() { m_reconfig_flag = 1; }
287 int send_to_client(Z_APDU *apdu);
288 int server(const char *addr);
290 int get_log_mask() { return m_log_mask; };
291 int handle_init_response_for_invalid_session(Z_APDU *apdu);