743c1d0513e201770ab240a64aea38d8c7d6a815
[yaz-moved-to-github.git] / include / yaz / srw.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /**
29  * \file srw.h
30  * \brief Header for SRW/SRU
31  */
32
33 #ifndef YAZ_SRW_H
34 #define YAZ_SRW_H
35
36 #include <yaz/soap.h>
37 #include <yaz/zgdu.h>
38 #include <yaz/diagsrw.h>
39 #include <yaz/diagsru_update.h>
40 #include "facet.h"
41
42 YAZ_BEGIN_CDECL
43
44 typedef struct {
45     char *extraRecordData_buf;
46     int extraRecordData_len;
47     char *recordIdentifier;
48 } Z_SRW_extra_record;
49
50 typedef struct {
51     char *recordSchema;
52
53     int recordPacking; /* recordXMLEscaping in SRU 2.0 */
54 #define Z_SRW_recordPacking_string 0
55 #define Z_SRW_recordPacking_XML 1
56 #define Z_SRW_recordPacking_URL 2
57
58 #if 0
59     int packing;   /* recordPacking in SRU 2.0 */
60 #define Z_SRW_packed   0
61 #define Z_SRW_unpacked     1
62 #endif
63
64     char *recordData_buf;
65     int recordData_len;
66     Odr_int *recordPosition;
67 } Z_SRW_record;
68
69 typedef struct {
70     char *uri;
71     char *details;
72     char *message;
73 } Z_SRW_diagnostic;
74
75 typedef struct {
76
77     char *queryType;
78     char *query;
79
80 #define Z_SRW_sort_type_none 1
81 #define Z_SRW_sort_type_sort 2
82 #define Z_SRW_sort_type_xSort 3
83     int sort_type;
84     union {
85         char *none;
86         char *sortKeys;
87         char *xSortKeys;
88     } sort;
89     Odr_int *startRecord;
90     Odr_int *maximumRecords;
91     char *recordSchema;
92
93     char *recordPacking;       /* recordXMLEscaping in SRU 2.0 */
94     char *packing;             /* recordPacking in SRU 2.0 */
95
96     char *recordXPath;
97     char *database;
98     char *stylesheet;
99     Odr_int *resultSetTTL;
100     Z_FacetList *facetList;
101 } Z_SRW_searchRetrieveRequest;
102
103 typedef struct {
104     Odr_int *numberOfRecords;
105     char * resultSetId;
106     Odr_int *resultSetIdleTime;
107
108     Z_SRW_record *records;
109     int num_records;
110
111     Z_SRW_diagnostic *diagnostics;
112     int num_diagnostics;
113     Odr_int *nextRecordPosition;
114
115     Z_SRW_extra_record **extra_records;  /* of size num_records */
116     Z_FacetList *facetList;
117     char *suggestions;
118 } Z_SRW_searchRetrieveResponse;
119
120 typedef struct {
121     char *recordPacking;
122     char *packing;
123     char *database;
124     char *stylesheet;
125 } Z_SRW_explainRequest;
126
127 typedef struct {
128     Z_SRW_record record;
129     Z_SRW_diagnostic *diagnostics;
130     int num_diagnostics;
131     Z_SRW_extra_record *extra_record;
132 } Z_SRW_explainResponse;
133
134 typedef struct {
135     char *queryType;
136     char *scanClause;
137     Odr_int *responsePosition;
138     Odr_int *maximumTerms;
139     char *stylesheet;
140     char *database;
141 } Z_SRW_scanRequest;
142
143 typedef struct {
144     char *value;
145     Odr_int *numberOfRecords;
146     char *displayTerm;
147     char *whereInList;
148 } Z_SRW_scanTerm;
149
150 typedef struct {
151     Z_SRW_scanTerm *terms;
152     int num_terms;
153     Z_SRW_diagnostic *diagnostics;
154     int num_diagnostics;
155 } Z_SRW_scanResponse;
156
157
158 typedef struct {
159     char *versionType;
160     char *versionValue;
161 } Z_SRW_recordVersion;
162
163 typedef struct {
164     char *database;
165     char *operation;
166     char *recordId;
167     Z_SRW_recordVersion *recordVersions;
168     int num_recordVersions;
169     Z_SRW_record *record;
170     Z_SRW_extra_record *extra_record;
171     char *extraRequestData_buf;
172     int extraRequestData_len;
173     char *stylesheet;
174 } Z_SRW_updateRequest;
175
176 typedef struct Z_SRW_extra_arg Z_SRW_extra_arg;
177 struct Z_SRW_extra_arg {
178     char *name;
179     char *value;
180     Z_SRW_extra_arg *next;
181 };
182
183 typedef struct {
184     char *operationStatus;
185     char *recordId;
186     Z_SRW_recordVersion *recordVersions;
187     int num_recordVersions;
188     Z_SRW_record *record;
189     Z_SRW_extra_record *extra_record;
190     char *extraResponseData_buf;
191     int extraResponseData_len;
192     Z_SRW_diagnostic *diagnostics;
193     int num_diagnostics;
194 } Z_SRW_updateResponse;
195
196 #define Z_SRW_searchRetrieve_request  1
197 #define Z_SRW_searchRetrieve_response 2
198 #define Z_SRW_explain_request 3
199 #define Z_SRW_explain_response 4
200 #define Z_SRW_scan_request 5
201 #define Z_SRW_scan_response 6
202 #define Z_SRW_update_request 7
203 #define Z_SRW_update_response 8
204
205 typedef struct {
206     int which;
207     union {
208         Z_SRW_searchRetrieveRequest *request;
209         Z_SRW_searchRetrieveResponse *response;
210         Z_SRW_explainRequest *explain_request;
211         Z_SRW_explainResponse *explain_response;
212         Z_SRW_scanRequest *scan_request;
213         Z_SRW_scanResponse *scan_response;
214         Z_SRW_updateRequest *update_request;
215         Z_SRW_updateResponse *update_response;
216     } u;
217     char *srw_version;
218     char *username; /* From HTTP header or request */
219     char *password; /* From HTTP header or request  */
220     Z_SRW_extra_arg *extra_args; /* extraRequestData SRU GET/POST */
221     char *extraResponseData_buf;
222     int extraResponseData_len;
223 } Z_SRW_PDU;
224
225 YAZ_EXPORT int yaz_srw_codec(ODR o, void * pptr,
226                              Z_SRW_PDU **handler_data,
227                              void *client_data, const char *ns);
228 YAZ_EXPORT int yaz_ucp_codec(ODR o, void * pptr,
229                              Z_SRW_PDU **handler_data,
230                              void *client_data, const char *ns);
231 YAZ_EXPORT Z_SRW_PDU *yaz_srw_get_core_v_2_0(ODR o);
232 YAZ_EXPORT Z_SRW_PDU *yaz_srw_get_pdu(ODR o, int which, const char *version);
233 YAZ_EXPORT Z_SRW_PDU *yaz_srw_get(ODR o, int which);
234 YAZ_EXPORT Z_SRW_recordVersion *yaz_srw_get_record_versions(ODR o, int num);
235 YAZ_EXPORT Z_SRW_extra_record *yaz_srw_get_extra_record(ODR o);
236 YAZ_EXPORT Z_SRW_record *yaz_srw_get_record(ODR o);
237 YAZ_EXPORT Z_SRW_record *yaz_srw_get_records(ODR o, int num);
238
239 YAZ_EXPORT int yaz_diag_bib1_to_srw (int bib1_code);
240
241 YAZ_EXPORT int yaz_diag_srw_to_bib1(int srw_code);
242
243 YAZ_EXPORT const char *yaz_srw_pack_to_str(int pack);
244 YAZ_EXPORT int yaz_srw_str_to_pack(const char *str);
245
246 /** \brief deprecated */
247 YAZ_EXPORT char *yaz_uri_val(const char *path, const char *name, ODR o);
248
249 /** \brief deprecated */
250 YAZ_EXPORT void yaz_uri_val_int(const char *path, const char *name,
251                                 ODR o, Odr_int **intp);
252
253 YAZ_EXPORT int yaz_uri_to_array(const char *path, ODR o,
254                                 char ***name, char ***val);
255 YAZ_EXPORT void yaz_array_to_uri(char **path, ODR o,
256                                  char **name, char **value);
257
258 /** \brief encodes URI component
259     \param dst destination string (should be at least 3*strlen(uri)+1)
260     \param uri URI component C-string (source)
261 */
262 YAZ_EXPORT void yaz_encode_uri_component(char *dst, const char *uri);
263
264 /** \brief decodes URI component
265     \param dst destination string (should be at least strlen(uri)+1)
266     \param uri URI component buffer (source)
267     \param len number of bytes to decode from uri
268 */
269 YAZ_EXPORT void yaz_decode_uri_component(char *dst, const char *uri,
270                                          size_t len);
271
272 YAZ_EXPORT int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
273                               Z_SOAP **soap_package, ODR decode, char **charset);
274
275 YAZ_EXPORT int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
276                               Z_SOAP **soap_package, ODR decode,
277                               char **charset,
278                               Z_SRW_diagnostic **, int *num_diagnostic);
279
280 /** \brief decode SOLR response (HTTP)
281     \param o ODR for result
282     \param hres HTTP response to be decoded
283     \param pdup SRW response pointer (set if successful)
284     \retval -1 fail
285     \retval 0 OK
286 */
287 YAZ_EXPORT int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres,
288                                         Z_SRW_PDU **pdup);
289
290
291 YAZ_EXPORT void yaz_add_srw_diagnostic(ODR o, Z_SRW_diagnostic **d,
292                                        int *num, int code,
293                                        const char *addinfo);
294
295 YAZ_EXPORT void yaz_add_sru_update_diagnostic(ODR o, Z_SRW_diagnostic **d,
296                                               int *num, int code,
297                                               const char *addinfo);
298
299 YAZ_EXPORT void yaz_mk_std_diagnostic(ODR o, Z_SRW_diagnostic *d,
300                                       int code, const char *details);
301
302 YAZ_EXPORT void yaz_add_srw_diagnostic_uri(ODR o, Z_SRW_diagnostic **d,
303                                            int *num, const char *uri,
304                                            const char *message,
305                                            const char *details);
306
307 YAZ_EXPORT void yaz_mk_srw_diagnostic(ODR o, Z_SRW_diagnostic *d,
308                                       const char *uri, const char *message,
309                                       const char *details);
310
311 YAZ_EXPORT int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
312                                   ODR encode, const char *charset);
313 YAZ_EXPORT int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
314                                    ODR encode, const char *charset);
315 YAZ_EXPORT int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
316                                    ODR odr, const char *charset);
317
318 /** \brief encode SOLR request (HTTP)
319     \param hreq HTTP request to be modified (with encoded request)
320     \param srw_pdu SRW request
321     \param encode ODR for result
322     \param charset character set for request (may be NULL)
323     \retval -1 fail
324     \retval 0 OK
325 */
326 YAZ_EXPORT int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
327                                        ODR encode, const char *charset);
328
329 YAZ_EXPORT char *yaz_negotiate_sru_version(char *input_ver);
330
331 YAZ_EXPORT
332 void yaz_encode_sru_extra(Z_SRW_PDU *sr, ODR odr, const char *extra_args);
333
334
335 #define YAZ_XMLNS_SRU_v1_0 "http://www.loc.gov/zing/srw/v1.0/"
336 #define YAZ_XMLNS_SRU_v1_1 "http://www.loc.gov/zing/srw/"
337 #define YAZ_XMLNS_DIAG_v1_1 "http://www.loc.gov/zing/srw/diagnostic/"
338 #define YAZ_XMLNS_UPDATE_v0_9 "http://www.loc.gov/zing/srw/update/"
339 #define YAZ_XMLNS_SRU_v2_mask "http://docs.oasis-open.org/ns/search-ws/*"
340 #define YAZ_XMLNS_SRU_v1_response "http://www.loc.gov/*"
341
342 YAZ_EXPORT
343 int yaz_srw_check_content_type(Z_HTTP_Response *hres);
344
345 YAZ_EXPORT
346 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
347                                      Z_SRW_diagnostic **diag,
348                                      int *num, ODR odr);
349
350 YAZ_EXPORT
351 void yaz_mk_sru_surrogate(ODR o, Z_SRW_record *record, int pos,
352                           int code, const char *details);
353
354 /** \brief encode SRU database for HTTP path
355     \param out memory handle for resulting encoded database string
356     \param db source database
357     \returns encoded database path (includes leading /)
358 */
359 YAZ_EXPORT
360 char *yaz_encode_sru_dbpath_odr(ODR out, const char *db);
361
362 /** \brief encode SRU database for HTTP path
363     \param dst destination buffer (should be at least strlen(db) +2 in size)
364     \param db source database
365
366     The resulting database (dst) includes a leading /
367 */
368 YAZ_EXPORT
369 void yaz_encode_sru_dbpath_buf(char *dst, const char *db);
370
371 YAZ_END_CDECL
372
373 #endif
374 /*
375  * Local variables:
376  * c-basic-offset: 4
377  * c-file-style: "Stroustrup"
378  * indent-tabs-mode: nil
379  * End:
380  * vim: shiftwidth=4 tabstop=8 expandtab
381  */
382