New directive default-client-charset which serves as charset
[yazproxy-moved-to-github.git] / include / yazproxy / proxy.h
1 /* $Id: proxy.h,v 1.27 2006-03-25 10:59:14 adam Exp $
2    Copyright (c) 1998-2005, Index Data.
3
4 This file is part of the yaz-proxy.
5
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
9 version.
10
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
14 for more details.
15
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
19 02111-1307, USA.
20  */
21
22 #ifndef YAZ_PROXY_H_INCLUDED
23 #define YAZ_PROXY_H_INCLUDED
24
25 #include <yaz++/socket-observer.h>
26 #include <yaz++/z-assoc.h>
27 #include <yaz++/z-query.h>
28 #include <yaz++/z-databases.h>
29 #include <yaz++/cql2rpn.h>
30 #include <yaz/cql.h>
31 #include <yaz++/gdu.h>
32 #include <yaz++/gduqueue.h>
33 #include <yazproxy/bw.h>
34
35 class Yaz_Proxy;
36
37 #define MAX_ZURL_PLEX 10
38
39 #define PROXY_LOG_APDU_CLIENT 1
40 #define PROXY_LOG_APDU_SERVER 2
41 #define PROXY_LOG_REQ_CLIENT 4
42 #define PROXY_LOG_REQ_SERVER 8
43
44 class Yaz_usemarcon;
45 class Yaz_ProxyConfig;
46 class Yaz_ProxyClient;
47 class Yaz_CharsetConverter;
48
49 enum YAZ_Proxy_MARCXML_mode {
50     none,
51     marcxml,
52 };
53
54 class Msg_Thread;
55
56 /// Information Retrieval Proxy Server.
57 class YAZ_EXPORT Yaz_Proxy : public yazpp_1::Z_Assoc {
58     friend class Proxy_Msg;
59  private:
60     char *m_peername;
61     int m_ref_count;
62     bool m_main_ptr_dec;
63     char *get_cookie(Z_OtherInformation **otherInfo);
64     char *get_proxy(Z_OtherInformation **otherInfo);
65     void get_charset_and_lang_negotiation(Z_OtherInformation **otherInfo,
66         char **charstes, char **langs, int *selected);
67     Yaz_ProxyClient *get_client(Z_APDU *apdu, const char *cookie,
68                                 const char *proxy_host);
69     void srw_get_client(const char *db, const char **backend_db);
70     Z_APDU *result_set_optimize(Z_APDU *apdu);
71     void releaseClient();
72     Yaz_ProxyClient *m_client;
73     yazpp_1::IPDU_Observable *m_PDU_Observable;
74     yazpp_1::ISocketObservable *m_socket_observable;
75     Yaz_ProxyClient *m_clientPool;
76     Yaz_Proxy *m_parent;
77     int m_seqno;
78     int m_max_clients;
79     int m_log_mask;
80     int m_keepalive_limit_bw;
81     int m_keepalive_limit_pdu;
82     int m_client_idletime;
83     int m_target_idletime;
84     int m_debug_mode;
85     char *m_proxyTarget;
86     char *m_default_target;
87     char *m_proxy_negotiation_charset;
88     char *m_proxy_negotiation_lang;
89     char *m_proxy_negotiation_default_charset;
90     long m_seed;
91     char *m_optimize;
92     int m_session_no;         // sequence for each client session
93     char m_session_str[30];  // session string (time:session_no)
94     Yaz_ProxyConfig *m_config;
95     char *m_config_fname;
96     int m_bytes_sent;
97     int m_bytes_recv;
98     int m_bw_max;
99
100     yazpp_1::GDU *m_timeout_gdu;
101     enum timeout_mode {
102         timeout_busy,
103         timeout_normal,
104         timeout_reduce,
105         timeout_xsl
106     } m_timeout_mode;
107
108     int m_connect_max;
109     int m_search_max;
110     Yaz_bw m_bw_stat;
111     int m_pdu_max;
112     Yaz_bw m_pdu_stat;
113     int m_max_record_retrieve;
114     void handle_max_record_retrieve(Z_APDU *apdu);
115     void display_diagrecs(Z_DiagRec **pp, int num);
116     Z_Records *create_nonSurrogateDiagnostics(ODR o, int error,
117                                               const char *addinfo);
118
119     Z_APDU *handle_query_validation(Z_APDU *apdu);
120     Z_APDU *handle_query_transformation(Z_APDU *apdu);
121     Z_APDU *handle_target_charset_conversion(Z_APDU *apdu);
122
123     Z_APDU *handle_syntax_validation(Z_APDU *apdu);
124
125     void handle_charset_lang_negotiation(Z_APDU *apdu);
126
127     const char *load_balance(const char **url);
128     int m_reconfig_flag;
129     Yaz_ProxyConfig *check_reconfigure();
130     int m_request_no;
131     int m_flag_invalid_session;
132     YAZ_Proxy_MARCXML_mode m_marcxml_mode;
133     void *m_stylesheet_xsp;  // Really libxslt's xsltStylesheetPtr
134     int m_stylesheet_offset;
135     Z_APDU *m_stylesheet_apdu;
136     Z_NamePlusRecordList *m_stylesheet_nprl;
137     char *m_schema;
138     char *m_backend_type;
139     char *m_backend_charset;
140     int m_frontend_type;
141     void convert_to_frontend_type(Z_NamePlusRecordList *p);
142     void convert_to_marcxml(Z_NamePlusRecordList *p, const char *charset);
143     void convert_records_charset(Z_NamePlusRecordList *p, const char *charset);
144     int convert_xsl(Z_NamePlusRecordList *p, Z_APDU *apdu);
145     void convert_xsl_delay();
146     Z_APDU *m_initRequest_apdu;
147     int m_initRequest_preferredMessageSize;
148     int m_initRequest_maximumRecordSize;
149     Z_Options *m_initRequest_options;
150     Z_ProtocolVersion *m_initRequest_version;
151     char **m_initRequest_oi_negotiation_charsets;
152     int m_initRequest_oi_negotiation_num_charsets;
153     char **m_initRequest_oi_negotiation_langs;
154     int m_initRequest_oi_negotiation_num_langs;
155     int m_initRequest_oi_negotiation_selected;
156     NMEM m_initRequest_mem;
157     Z_APDU *m_apdu_invalid_session;
158     NMEM m_mem_invalid_session;
159     int send_PDU_convert(Z_APDU *apdu);
160     ODR m_s2z_odr_init;
161     ODR m_s2z_odr_search;
162     int m_s2z_hit_count;
163     int m_s2z_packing;
164     char *m_s2z_database;
165     Z_APDU *m_s2z_init_apdu;
166     Z_APDU *m_s2z_search_apdu;
167     Z_APDU *m_s2z_present_apdu;
168     char *m_s2z_stylesheet;
169     char *m_soap_ns;
170     int file_access(Z_HTTP_Request *hreq);
171     int send_to_srw_client_error(int error, const char *add);
172     int send_to_srw_client_ok(int hits, Z_Records *records, int start);
173     int send_http_response(int code);
174     int send_srw_response(Z_SRW_PDU *srw_pdu, int http_code = 200);
175     int send_srw_search_response(Z_SRW_diagnostic *diagnostics,
176                                  int num_diagnostics,
177                                  int http_code = 200);
178     int send_srw_explain_response(Z_SRW_diagnostic *diagnostics,
179                                   int num_diagnostics);
180     int z_to_srw_diag(ODR o, Z_SRW_searchRetrieveResponse *srw_res,
181                       Z_DefaultDiagFormat *ddf);
182     int m_http_keepalive;
183     const char *m_http_version;
184     yazpp_1::Yaz_cql2rpn m_cql2rpn;
185     void *m_time_tv;
186     void logtime();
187     Z_ElementSetNames *mk_esn_from_schema(ODR o, const char *schema);
188     Z_ReferenceId *m_referenceId;
189     NMEM m_referenceId_mem;
190
191 #define NO_SPARE_SOLARIS_FD 10
192     int m_lo_fd[NO_SPARE_SOLARIS_FD];
193     void low_socket_open();
194     void low_socket_close();
195     char *m_usemarcon_ini_stage1;
196     char *m_usemarcon_ini_stage2;
197     Yaz_usemarcon *m_usemarcon;
198     Yaz_CharsetConverter *m_charset_converter;
199     yazpp_1::GDUQueue m_in_queue;
200  public:
201     Yaz_Proxy(yazpp_1::IPDU_Observable *the_PDU_Observable,
202               yazpp_1::ISocketObservable *the_socket_observable,
203               Yaz_Proxy *parent = 0);
204     ~Yaz_Proxy();
205
206
207     void inc_ref();
208     bool dec_ref(bool main_ptr);
209
210
211     int handle_authentication(Z_APDU *apdu);
212     void result_authentication(Z_APDU *apdu, int ret);
213     void handle_init(Z_APDU *apdu);
214     void inc_request_no();
215     void recv_GDU(Z_GDU *apdu, int len);
216     void recv_GDU_reduce(yazpp_1::GDU *gdu);
217     void recv_GDU_normal(yazpp_1::GDU *gdu);
218     void recv_GDU_more(bool normal);
219     void handle_incoming_HTTP(Z_HTTP_Request *req);
220     void handle_incoming_Z_PDU(Z_APDU *apdu);
221     void handle_incoming_Z_PDU_2(Z_APDU *apdu);
222     IPDU_Observer *sessionNotify(yazpp_1::IPDU_Observable *the_PDU_Observable,
223                                  int fd);
224     void failNotify();
225     void timeoutNotify();
226     void connectNotify();
227     void markInvalid();
228     const char *option(const char *name, const char *value);
229     void set_default_target(const char *target);
230     void set_proxy_negotiation(const char *charset, const char *lang,
231                                const char *default_charset);
232     void set_target_charset(const char *charset);
233     char *get_proxy_target() { return m_proxyTarget; };
234     char *get_session_str() { return m_session_str; };
235     void set_max_clients(int m) { m_max_clients = m; };
236     void set_client_idletime (int t) { m_client_idletime = (t > 1) ? t : 600; };
237     void set_target_idletime (int t) { m_target_idletime = (t > 1) ? t : 600; };
238     int get_target_idletime () { return m_target_idletime; }
239     int set_config(const char *name);
240     void reconfig() { m_reconfig_flag = 1; }
241     int send_to_client(Z_APDU *apdu);
242     int server(const char *addr);
243     void pre_init();
244     int get_log_mask() { return m_log_mask; };
245     int handle_init_response_for_invalid_session(Z_APDU *apdu);
246     void set_debug_mode(int mode);
247     void send_response_fail_client(const char *addr);
248     Msg_Thread *m_my_thread;
249     void base64_decode(const char *base64, char *buf, int buf_len);
250 };
251
252 #endif
253 /*
254  * Local variables:
255  * c-basic-offset: 4
256  * indent-tabs-mode: nil
257  * End:
258  * vim: shiftwidth=4 tabstop=8 expandtab
259  */
260