Added support for Z39.50 character set negotiation. This allows
[yazproxy-moved-to-github.git] / src / proxyp.h
1 /* $Id: proxyp.h,v 1.5 2005-05-18 20:15:22 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 #if HAVE_XSLT
23 #include <libxml/parser.h>
24 #include <libxml/tree.h>
25 #include <libxml/xinclude.h>
26 #include <libxslt/xsltutils.h>
27 #include <libxslt/transform.h>
28 #endif
29
30 #if HAVE_USEMARCON
31 #include <objectlist.h>
32 #endif
33
34 #include <yazproxy/proxy.h>
35 #include <yazproxy/module.h>
36
37 struct Yaz_RecordCache_Entry;
38
39 class Yaz_usemarcon {
40  public:
41     Yaz_usemarcon();
42     ~Yaz_usemarcon();
43
44     int convert(const char *stage1, const char *stage2,
45                 const char *input, int input_len,
46                 char **output, int *output_len);
47 #if HAVE_USEMARCON
48     CDetails *m_stage1;
49     CDetails *m_stage2;
50 #else
51     int dummy;
52 #endif
53 };
54
55 class Yaz_CharsetConverter {
56 public:
57     Yaz_CharsetConverter();
58     ~Yaz_CharsetConverter();
59     void set_target_query_charset(const char *s);
60     void set_client_query_charset(const char *org);
61     const char *get_client_query_charset(void);
62     const char *get_target_query_charset(void);
63     void convert_type_1(Z_RPNQuery *q, ODR o);
64     void set_client_charset_selected(int sel);
65     int get_client_charset_selected();
66 private:
67     void convert_type_1(char *buf_in, int len_in,
68                         char **buf_out, int *len_out,
69                         ODR o);
70     void convert_type_1(Z_Term *q, ODR o);
71     void convert_type_1(Z_RPNStructure *q, ODR o);
72     void convert_type_1(Z_Operand *q, ODR o);
73     char *m_target_query_charset;
74     char *m_client_query_charset;
75     int m_client_charset_selected;
76     yaz_iconv_t m_ct;
77     WRBUF m_wrbuf;
78 };
79
80 class Yaz_ProxyConfig {
81 public:
82     Yaz_ProxyConfig();
83     ~Yaz_ProxyConfig();
84     int read_xml(const char *fname);
85
86     int get_target_no(int no,
87                       const char **name,
88                       const char **url,
89                       int *limit_bw,
90                       int *limit_pdu,
91                       int *limit_req,
92                       int *target_idletime,
93                       int *client_idletime,
94                       int *max_clients,
95                       int *keepalive_limit_bw,
96                       int *keepalive_limit_pdu,
97                       int *pre_init,
98                       const char **cql2rpn,
99                       const char **authentication,
100                       const char **negotiation_charset,
101                       const char **negotiation_lang,
102                       const char **query_charset);
103     
104     void get_generic_info(int *log_mask, int *max_clients);
105
106     void get_target_info(const char *name, const char **url,
107                          int *limit_bw, int *limit_pdu, int *limit_req,
108                          int *target_idletime, int *client_idletime,
109                          int *max_clients,
110                          int *keepalive_limit_bw, int *keepalive_limit_pdu,
111                          int *pre_init,
112                          const char **cql2rpn,
113                          const char **negotiation_charset,
114                          const char **negotiation_lang,
115                          const char **query_charset);
116
117     const char *check_mime_type(const char *path);
118     int check_query(ODR odr, const char *name, Z_Query *query, char **addinfo);
119     int check_syntax(ODR odr, const char *name,
120                      Odr_oid *syntax, Z_RecordComposition *comp,
121                      char **addinfo, char **stylesheet, char **schema,
122                      char **backend_type, char **backend_charset,
123                      char **usemarcon_ini_stage1, char **usemarcon_ini_stage2);
124
125     void target_authentication(const char *name,
126                                ODR odr,
127                                Z_InitRequest *req);
128
129     int client_authentication(const char *name,
130                               const char *user, const char *group,
131                               const char *password);
132     char *get_explain_doc(ODR odr, const char *name, const char *db,
133                           int *len);
134     const char *get_explain_name(const char *db, const char **backend_db);
135  private:
136     void operator=(const Yaz_ProxyConfig &conf);
137     class Yaz_ProxyConfigP *m_cp;
138 };
139
140 class Yaz_RecordCache {
141  public:
142     Yaz_RecordCache ();
143     ~Yaz_RecordCache ();
144     void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits);
145     
146     int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num,
147                 Odr_oid *syntax, Z_RecordComposition *comp);
148     void clear();
149
150     void copy_searchRequest(Z_SearchRequest *sr);
151     void copy_presentRequest(Z_PresentRequest *pr);
152     void set_max_size(int sz);
153  private:
154     NMEM m_mem;
155     Yaz_RecordCache_Entry *m_entries;
156     Z_SearchRequest *m_searchRequest;
157     Z_PresentRequest *m_presentRequest;
158     int match (Yaz_RecordCache_Entry *entry,
159                Odr_oid *syntax, int offset,
160                Z_RecordComposition *comp);
161     int m_max_size;
162 };
163
164 class Yaz_ProxyClient : public Yaz_Z_Assoc {
165     friend class Yaz_Proxy;
166     Yaz_ProxyClient(IYaz_PDU_Observable *the_PDU_Observable,
167                     Yaz_Proxy *parent);
168     ~Yaz_ProxyClient();
169     void recv_GDU(Z_GDU *apdu, int len);
170     void recv_Z_PDU(Z_APDU *apdu, int len);
171     void recv_HTTP_response(Z_HTTP_Response *apdu, int len);
172     IYaz_PDU_Observer* sessionNotify
173         (IYaz_PDU_Observable *the_PDU_Observable, int fd);
174     void shutdown();
175     Yaz_Proxy *m_server;
176     void failNotify();
177     void timeoutNotify();
178     void connectNotify();
179     int send_to_target(Z_APDU *apdu);
180     const char *get_session_str();
181     char *m_cookie;
182     Yaz_ProxyClient *m_next;
183     Yaz_ProxyClient **m_prev;
184     int m_init_flag;
185     Yaz_Z_Query *m_last_query;
186     Yaz_Z_Databases m_last_databases;
187     char *m_last_resultSetId;
188     int m_last_ok;
189     int m_last_resultCount;
190     int m_sr_transform;
191     int m_seqno;
192     int m_waiting;
193     int m_resultSetStartPoint;
194     int m_bytes_sent;
195     int m_bytes_recv;
196     int m_pdu_recv;
197     ODR m_init_odr;
198     Z_APDU *m_initResponse;
199     Z_Options *m_initResponse_options;
200     Z_ProtocolVersion *m_initResponse_version;
201     int m_initResponse_preferredMessageSize;
202     int m_initResponse_maximumRecordSize;
203     Yaz_RecordCache m_cache;
204     void pre_init_client();
205     int m_target_idletime;
206     Yaz_Proxy *m_root;
207 };
208