Version 1.3.10
[yazproxy-moved-to-github.git] / include / yazproxy / proxy.h
1 /* This file is part of YAZ proxy
2    Copyright (C) 1998-2011 Index Data
3
4 YAZ proxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #ifndef YAZ_PROXY_H_INCLUDED
20 #define YAZ_PROXY_H_INCLUDED
21
22 #include <yazpp/socket-observer.h>
23 #include <yazpp/z-assoc.h>
24 #include <yazpp/z-query.h>
25 #include <yazpp/z-databases.h>
26 #include <yazpp/cql2rpn.h>
27 #include <yaz/cql.h>
28 #include <yazpp/gdu.h>
29 #include <yazpp/gduqueue.h>
30 #include <yazproxy/bw.h>
31 #include <yazproxy/limit-connect.h>
32
33 class Yaz_Proxy;
34
35 #define MAX_ZURL_PLEX 10
36
37 #define PROXY_LOG_APDU_CLIENT 1
38 #define PROXY_LOG_APDU_SERVER 2
39 #define PROXY_LOG_REQ_CLIENT 4
40 #define PROXY_LOG_REQ_SERVER 8
41 #define PROXY_LOG_IP_CLIENT 16
42
43 class Yaz_usemarcon;
44 class Yaz_ProxyConfig;
45 class Yaz_ProxyClient;
46 class Yaz_CharsetConverter;
47
48 enum YAZ_Proxy_MARCXML_mode {
49     none,
50     marcxml
51 };
52
53 class Msg_Thread;
54
55 /// Information Retrieval Proxy Server.
56 class YAZ_EXPORT Yaz_Proxy : public yazpp_1::Z_Assoc {
57     friend class Proxy_Msg;
58  private:
59     char *m_peername;
60     int m_ref_count;
61     bool m_main_ptr_dec;
62     char *get_cookie(Z_OtherInformation **otherInfo);
63     char *get_proxy(Z_OtherInformation **otherInfo);
64     void get_charset_and_lang_negotiation(Z_OtherInformation **otherInfo,
65         char **charstes, char **langs, int *selected);
66     void HTTP_Forwarded(Z_GDU *z_gdu);
67     void connect_stat(bool &block, int &reduce);
68     Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
69                                 const char *proxy_host, int *http_code);
70     void srw_get_client(const char *db, const char **backend_db);
71     int get_number_of_connections();
72     Z_APDU *result_set_optimize(Z_APDU *apdu);
73     void releaseClient();
74     Yaz_ProxyClient *m_client;
75     yazpp_1::IPDU_Observable *m_PDU_Observable;
76     yazpp_1::ISocketObservable *m_socket_observable;
77     Yaz_ProxyClient *m_clientPool;
78     Yaz_Proxy *m_parent;
79     int m_seqno;
80     int m_max_clients;
81     int m_log_mask;
82     int m_keepalive_limit_bw;
83     int m_keepalive_limit_pdu;
84     int m_client_idletime;
85     int m_target_idletime;
86     int m_max_sockets;
87     int m_debug_mode;
88     char *m_proxyTarget;
89     char *m_default_target;
90     char *m_proxy_negotiation_charset;
91     char *m_proxy_negotiation_lang;
92     char *m_proxy_negotiation_default_charset;
93     long m_seed;
94     char *m_optimize;
95     int m_session_no;         // sequence for each client session
96     char m_session_str[200];  // session string (time:session_no)
97     Yaz_ProxyConfig *m_config;
98     char *m_config_fname;
99     int m_bytes_sent;
100     int m_bytes_recv;
101     int m_bw_max;
102
103     yazpp_1::GDU *m_timeout_gdu;
104     enum timeout_mode {
105         timeout_busy,
106         timeout_normal,
107         timeout_reduce,
108         timeout_xsl
109     } m_timeout_mode;
110
111     int m_max_connect;
112     int m_max_connect_period;
113     int m_limit_connect;
114     int m_limit_connect_period;
115     int m_search_max;
116     Yaz_bw m_bw_stat;
117     int m_pdu_max;
118     Yaz_bw m_pdu_stat;
119     int m_max_record_retrieve;
120     Yaz_bw m_search_stat;
121
122     void handle_max_record_retrieve(Z_APDU *apdu);
123     void display_diagrecs(Z_DiagRec **pp, int num);
124     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
125                                               const char *addinfo);
126     Z_ListEntries *create_nonSurrogateDiagnostics2(ODR o, int error,
127                                               const char *addinfo);
128
129     Z_APDU *handle_query_validation(Z_APDU *apdu);
130     Z_APDU *handle_query_transformation(Z_APDU *apdu);
131     Z_APDU *handle_target_charset_conversion(Z_APDU *apdu);
132
133     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
134
135     void handle_charset_lang_negotiation(Z_APDU *apdu);
136
137     const char *load_balance(const char **url);
138     int m_reconfig_flag;
139     Yaz_ProxyConfig *check_reconfigure();
140     int m_request_no;
141     int m_flag_invalid_session;
142     YAZ_Proxy_MARCXML_mode m_marcxml_mode;
143     void *m_stylesheet_xsp;  // Really libxslt's xsltStylesheetPtr
144     int m_stylesheet_offset;
145     Z_APDU *m_stylesheet_apdu;
146     Z_NamePlusRecordList *m_stylesheet_nprl;
147     char *m_schema;
148     char *m_backend_type;
149     char *m_backend_charset;
150     Odr_oid m_frontend_type[OID_SIZE];
151     void convert_to_frontend_type(Z_NamePlusRecordList *p);
152     void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
153     void convert_records_charset(Z_NamePlusRecordList *p, const char *charset);
154     int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
155     void convert_xsl_delay();
156     Z_APDU *m_initRequest_apdu;
157     int m_initRequest_preferredMessageSize;
158     int m_initRequest_maximumRecordSize;
159     Z_Options *m_initRequest_options;
160     Z_ProtocolVersion *m_initRequest_version;
161     char **m_initRequest_oi_negotiation_charsets;
162     int m_initRequest_oi_negotiation_num_charsets;
163     char **m_initRequest_oi_negotiation_langs;
164     int m_initRequest_oi_negotiation_num_langs;
165     int m_initRequest_oi_negotiation_selected;
166     NMEM m_initRequest_mem;
167     Z_APDU *m_apdu_invalid_session;
168     NMEM m_mem_invalid_session;
169     int send_PDU_convert(Z_APDU *apdu);
170     ODR m_s2z_odr_init;
171     ODR m_s2z_odr_search;
172     ODR m_s2z_odr_scan;
173     int m_s2z_hit_count;
174     int m_s2z_packing;
175     char *m_s2z_database;
176     Z_APDU *m_s2z_init_apdu;
177     Z_APDU *m_s2z_search_apdu;
178     Z_APDU *m_s2z_present_apdu;
179     Z_APDU *m_s2z_scan_apdu;
180     char *m_s2z_stylesheet;
181     char *m_soap_ns;
182     int file_access(Z_HTTP_Request *hreq);
183     int send_to_srw_client_error(int error, const char *add);
184     int send_to_srw_client_ok(int hits, Z_Records *records, int start);
185     int send_to_srw_client_ok(Z_ListEntries *entries);
186     int send_http_response(int code);
187     int send_srw_response(Z_SRW_PDU *srw_pdu, int http_code = 200);
188     int send_srw_search_response(Z_SRW_diagnostic *diagnostics,
189                                  int num_diagnostics,
190                                  int http_code = 200);
191     int send_srw_scan_response(Z_SRW_diagnostic *diagnostics,
192                                  int num_diagnostics,
193                                  int http_code = 200);
194     int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
195                                   int num_diagnostics);
196     int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
197                       Z_DefaultDiagFormat *ddf);
198     int z_to_srw_diag(ODR o, Z_SRW_scanResponse *srw_res,
199                       Z_DiagRec *dr);
200     int m_http_keepalive;
201     const char *m_http_version;
202     yazpp_1::Yaz_cql2rpn m_cql2rpn;
203     void *m_time_tv;
204     void logtime();
205     Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
206     Z_ReferenceId *m_referenceId;
207     NMEM m_referenceId_mem;
208
209 #define NO_SPARE_SOLARIS_FD 10
210     int m_lo_fd[NO_SPARE_SOLARIS_FD];
211     void low_socket_open();
212     void low_socket_close();
213     char *m_usemarcon_ini_stage1;
214     char *m_usemarcon_ini_stage2;
215     char *m_backend_elementset;
216     Yaz_usemarcon *m_usemarcon;
217     Yaz_CharsetConverter *m_charset_converter;
218     yazpp_1::GDUQueue m_in_queue;
219     LimitConnect m_connect;
220  public:
221     Yaz_Proxy(yazpp_1::IPDU_Observable *the_PDU_Observable,
222               yazpp_1::ISocketObservable *the_socket_observable,
223               Yaz_Proxy *parent = 0);
224     ~Yaz_Proxy();
225
226     void inc_ref();
227     bool dec_ref();
228
229     int handle_authentication(Z_APDU *apdu);
230     int handle_global_authentication(Z_APDU *apdu);
231     void result_authentication(Z_APDU *apdu, int ret);
232     void handle_init(Z_APDU *apdu);
233     void inc_request_no();
234     void recv_GDU(Z_GDU *apdu, int len);
235     void recv_GDU_reduce(yazpp_1::GDU *gdu);
236     void recv_GDU_normal(yazpp_1::GDU *gdu);
237     void recv_GDU_more(bool normal);
238     void handle_incoming_HTTP(Z_HTTP_Request *req);
239     void handle_incoming_Z_PDU(Z_APDU *apdu);
240     void handle_incoming_Z_PDU_2(Z_APDU *apdu);
241     IPDU_Observer *sessionNotify(yazpp_1::IPDU_Observable *the_PDU_Observable,
242                                  int fd);
243     void failNotify();
244     void timeoutNotify();
245     void connectNotify();
246     void markInvalid();
247     const char *option(const char *name, const char *value);
248     void set_default_target(const char *target);
249     void set_proxy_negotiation(const char *charset, const char *lang,
250                                const char *default_charset);
251     void set_target_charset(const char *charset);
252     char *get_proxy_target() { return m_proxyTarget; };
253     char *get_session_str() { return m_session_str; };
254     void set_max_clients(int m) { m_max_clients = m; };
255     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
256     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
257     int get_target_idletime () { return m_target_idletime; }
258     int set_config(const char *name);
259     void reconfig() { m_reconfig_flag = 1; }
260     int send_to_client(Z_APDU *apdu);
261     int server(const char *addr);
262     void pre_init();
263     int get_log_mask() { return m_log_mask; };
264     int handle_init_response_for_invalid_session(Z_APDU *apdu);
265     void set_debug_mode(int mode);
266     void send_response_fail_client(const char *addr);
267     int m_num_msg_threads;
268     Msg_Thread *m_my_thread;
269     void base64_decode(const char *base64, char *buf, int buf_len);
270 };
271
272 #endif
273 /*
274  * Local variables:
275  * c-basic-offset: 4
276  * c-file-style: "Stroustrup"
277  * indent-tabs-mode: nil
278  * End:
279  * vim: shiftwidth=4 tabstop=8 expandtab
280  */
281