1 /* $Id: proxy.h,v 1.10 2004-12-13 20:52:33 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 #ifndef YAZ_PROXY_H_INCLUDED
23 #define YAZ_PROXY_H_INCLUDED
25 #include <yaz++/z-assoc.h>
26 #include <yaz++/z-query.h>
27 #include <yaz++/z-databases.h>
28 #include <yaz++/cql2rpn.h>
30 #include <yazproxy/bw.h>
34 #define MAX_ZURL_PLEX 10
36 #define PROXY_LOG_APDU_CLIENT 1
37 #define PROXY_LOG_APDU_SERVER 2
38 #define PROXY_LOG_REQ_CLIENT 4
39 #define PROXY_LOG_REQ_SERVER 8
41 struct Yaz_RecordCache_Entry;
42 class Yaz_ProxyConfigP;
45 class YAZ_EXPORT Yaz_ProxyConfig {
49 int read_xml(const char *fname);
51 int get_target_no(int no,
60 int *keepalive_limit_bw,
61 int *keepalive_limit_pdu,
64 const char **authentication);
66 void get_generic_info(int *log_mask, int *max_clients);
68 void get_target_info(const char *name, const char **url,
69 int *limit_bw, int *limit_pdu, int *limit_req,
70 int *target_idletime, int *client_idletime,
72 int *keepalive_limit_bw, int *keepalive_limit_pdu,
75 const char **authentication);
77 const char *check_mime_type(const char *path);
78 int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
79 int check_syntax(ODR odr, const char *name,
80 Odr_oid *syntax, Z_RecordComposition *comp,
81 char **addinfo, char **stylesheet, char **schema,
82 char **backend_type, char **backend_charset,
83 char **usemarcon_ini_stage1, char **usemarcon_ini_stage2
85 char *get_explain_doc(ODR odr, const char *name, const char *db,
87 const char *get_explain_name(const char *db, const char **backend_db);
89 void operator=(const Yaz_ProxyConfig &conf);
90 class Yaz_ProxyConfigP *m_cp;
93 class YAZ_EXPORT Yaz_RecordCache {
97 void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
99 int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
100 Odr_oid *syntax, Z_RecordComposition *comp);
103 void copy_searchRequest(Z_SearchRequest *sr);
104 void copy_presentRequest(Z_PresentRequest *pr);
105 void set_max_size(int sz);
108 Yaz_RecordCache_Entry *m_entries;
109 Z_SearchRequest *m_searchRequest;
110 Z_PresentRequest *m_presentRequest;
111 int match (Yaz_RecordCache_Entry *entry,
112 Odr_oid *syntax, int offset,
113 Z_RecordComposition *comp);
118 class YAZ_EXPORT Yaz_ProxyClient : public Yaz_Z_Assoc {
119 friend class Yaz_Proxy;
120 Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
123 void recv_GDU(Z_GDU *apdu, int len);
124 void recv_Z_PDU(Z_APDU *apdu, int len);
125 void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
126 IYaz_PDU_Observer* sessionNotify
127 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
131 void timeoutNotify();
132 void connectNotify();
133 int send_to_target(Z_APDU *apdu);
134 const char *get_session_str();
136 Yaz_ProxyClient *m_next;
137 Yaz_ProxyClient **m_prev;
139 Yaz_Z_Query *m_last_query;
140 Yaz_Z_Databases m_last_databases;
141 char *m_last_resultSetId;
143 int m_last_resultCount;
147 int m_resultSetStartPoint;
152 Z_APDU *m_initResponse;
153 Z_Options *m_initResponse_options;
154 Z_ProtocolVersion *m_initResponse_version;
155 int m_initResponse_preferredMessageSize;
156 int m_initResponse_maximumRecordSize;
157 Yaz_RecordCache m_cache;
158 void pre_init_client();
159 int m_target_idletime;
164 /// Information Retrieval Proxy Server.
165 class YAZ_EXPORT Yaz_Proxy : public Yaz_Z_Assoc {
167 char *get_cookie(Z_OtherInformation **otherInfo);
168 char *get_proxy(Z_OtherInformation **otherInfo);
169 Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
170 const char *proxy_host);
171 void srw_get_client(const char *db, const char **backend_db);
172 Z_APDU *result_set_optimize(Z_APDU *apdu);
174 void releaseClient();
175 Yaz_ProxyClient *m_client;
176 IYaz_PDU_Observable *m_PDU_Observable;
177 Yaz_ProxyClient *m_clientPool;
182 int m_keepalive_limit_bw;
183 int m_keepalive_limit_pdu;
184 int m_client_idletime;
185 int m_target_idletime;
187 char *m_default_target;
188 char *m_proxy_authentication;
191 int m_session_no; // sequence for each client session
192 char m_session_str[30]; // session string (time:session_no)
193 Yaz_ProxyConfig *m_config;
194 char *m_config_fname;
201 Z_GDU *m_bw_hold_PDU;
202 int m_max_record_retrieve;
203 void handle_max_record_retrieve(Z_APDU *apdu);
204 void display_diagrecs(Z_DiagRec **pp, int num);
205 Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
206 const char *addinfo);
208 Z_APDU *handle_query_validation(Z_APDU *apdu);
209 Z_APDU *handle_query_transformation(Z_APDU *apdu);
211 Z_APDU *handle_syntax_validation(Z_APDU *apdu);
212 const char *load_balance(const char **url);
214 Yaz_ProxyConfig *check_reconfigure();
216 int m_invalid_session;
218 void *m_stylesheet_xsp; // Really libxslt's xsltStylesheetPtr
219 int m_stylesheet_offset;
220 Z_APDU *m_stylesheet_apdu;
221 Z_NamePlusRecordList *m_stylesheet_nprl;
223 char *m_backend_type;
224 char *m_backend_charset;
226 void convert_to_frontend_type(Z_NamePlusRecordList *p);
227 void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
228 int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
229 void convert_xsl_delay();
230 Z_APDU *m_initRequest_apdu;
231 int m_initRequest_preferredMessageSize;
232 int m_initRequest_maximumRecordSize;
233 Z_Options *m_initRequest_options;
234 Z_ProtocolVersion *m_initRequest_version;
235 NMEM m_initRequest_mem;
236 Z_APDU *m_apdu_invalid_session;
237 NMEM m_mem_invalid_session;
238 int send_PDU_convert(Z_APDU *apdu);
240 ODR m_s2z_odr_search;
243 char *m_s2z_database;
244 Z_APDU *m_s2z_init_apdu;
245 Z_APDU *m_s2z_search_apdu;
246 Z_APDU *m_s2z_present_apdu;
247 char *m_s2z_stylesheet;
249 int file_access(Z_HTTP_Request *hreq);
250 int send_to_srw_client_error(int error, const char *add);
251 int send_to_srw_client_ok(int hits, Z_Records *records, int start);
252 int send_http_response(int code);
253 int send_srw_response(Z_SRW_PDU *srw_pdu);
254 int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
255 int num_diagnostics);
256 int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
257 Z_DefaultDiagFormat *ddf);
258 int m_http_keepalive;
259 const char *m_http_version;
260 Yaz_cql2rpn m_cql2rpn;
263 Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
264 Z_ReferenceId *m_referenceId;
265 NMEM m_referenceId_mem;
266 #define NO_SPARE_SOLARIS_FD 10
267 int m_lo_fd[NO_SPARE_SOLARIS_FD];
268 void low_socket_open();
269 void low_socket_close();
270 char *m_usemarcon_ini_stage1;
271 char *m_usemarcon_ini_stage2;
272 Yaz_usemarcon *m_usemarcon;
274 Yaz_Proxy(IYaz_PDU_Observable *the_PDU_Observable,
275 Yaz_Proxy *parent = 0);
277 void inc_request_no();
278 void recv_GDU(Z_GDU *apdu, int len);
279 void handle_incoming_HTTP(Z_HTTP_Request *req);
280 void handle_incoming_Z_PDU(Z_APDU *apdu);
281 IYaz_PDU_Observer* sessionNotify
282 (IYaz_PDU_Observable *the_PDU_Observable, int fd);
284 void timeoutNotify();
285 void connectNotify();
287 const char *option(const char *name, const char *value);
288 void set_default_target(const char *target);
289 void set_proxy_authentication (const char *auth);
290 char *get_proxy_target() { return m_proxyTarget; };
291 char *get_session_str() { return m_session_str; };
292 void set_max_clients(int m) { m_max_clients = m; };
293 void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
294 void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
295 int get_target_idletime () { return m_target_idletime; }
296 int set_config(const char *name);
297 void reconfig() { m_reconfig_flag = 1; }
298 int send_to_client(Z_APDU *apdu);
299 int server(const char *addr);
301 int get_log_mask() { return m_log_mask; };
302 int handle_init_response_for_invalid_session(Z_APDU *apdu);