2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: srwutil.c,v 1.52 2006-10-24 09:18:34 adam Exp $
9 * \brief Implements SRW/SRU utilities.
14 #include <yaz/yaz-iconv.h>
16 static int hex_digit (int ch)
18 if (ch >= '0' && ch <= '9')
20 else if (ch >= 'a' && ch <= 'f')
22 else if (ch >= 'A' && ch <= 'F')
27 void encode_uri_char(char *dst, char ch)
31 else if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
32 (ch >= '0' && ch <= '9'))
40 sprintf(dst+1, "%02X", (unsigned char ) ch);
44 static void yaz_array_to_uri_ex(char **path, ODR o, char **name, char **value,
45 const char *extra_args)
47 size_t i, szp = 0, sz = extra_args ? 1+strlen(extra_args) : 1;
48 for(i = 0; name[i]; i++)
49 sz += strlen(name[i]) + 3 + strlen(value[i]) * 3;
50 *path = odr_malloc(o, sz);
52 for(i = 0; name[i]; i++)
57 ilen = strlen(name[i]);
58 memcpy(*path+szp, name[i], ilen);
61 for (j = 0; value[i][j]; j++)
65 encode_uri_char(vstr, value[i][j]);
67 memcpy(*path+szp, vstr, vlen);
75 memcpy(*path + szp, extra_args, strlen(extra_args));
76 szp += strlen(extra_args);
81 void yaz_array_to_uri(char **path, ODR o, char **name, char **value)
83 yaz_array_to_uri_ex(path, o, name, value, 0);
86 int yaz_uri_array(const char *path, ODR o, char ***name, char ***val)
96 while ((cp = strchr(cp, '&')))
101 *name = odr_malloc(o, no * sizeof(char*));
102 *val = odr_malloc(o, no * sizeof(char*));
104 for (no = 0; *path; no++)
106 const char *p1 = strchr(path, '=');
112 (*name)[no] = odr_malloc(o, (p1-path)+1);
113 memcpy((*name)[no], path, p1-path);
114 (*name)[no][p1-path] = '\0';
117 p1 = strchr(path, '&');
119 p1 = strlen(path) + path;
120 (*val)[no] = ret = odr_malloc(o, p1 - path + 1);
121 while (*path && *path != '&')
128 else if (*path == '%' && path[1] && path[2])
130 ret[i++] = hex_digit (path[1])*16 + hex_digit (path[2]);
146 char *yaz_uri_val(const char *path, const char *name, ODR o)
148 size_t nlen = strlen(name);
152 while (path && *path)
154 const char *p1 = strchr(path, '=');
157 if ((size_t)(p1 - path) == nlen && !memcmp(path, name, nlen))
163 p1 = strchr(path, '&');
165 p1 = strlen(path) + path;
166 ret = (char *) odr_malloc(o, p1 - path + 1);
167 while (*path && *path != '&')
174 else if (*path == '%' && path[1] && path[2])
176 ret[i++] = hex_digit (path[1])*16 + hex_digit (path[2]);
185 path = strchr(p1, '&');
192 static int yaz_base64decode(const char *in, char *out)
194 const char *map = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
195 "abcdefghijklmnopqrstuvwxyz0123456789+/";
197 int len = strlen(in);
204 if (!(p = strchr(map, in[0])))
208 if (!(p = strchr(map, in[1])))
212 *(out++) = i0 << 2 | i1 >> 4;
216 if (!(p = strchr(map, in[2])))
220 *(out++) = i1 << 4 | i2 >> 2;
224 if (!(p = strchr(map, in[3])))
228 *(out++) = i2 << 6 | i3;
239 * Look for authentication tokens in HTTP Basic parameters or in x-username/x-password
240 * parameters. Added by SH.
242 static void yaz_srw_decodeauth(Z_SRW_PDU *sr, Z_HTTP_Request *hreq,
243 char *username, char *password, ODR decode)
245 const char *basic = z_HTTP_header_lookup(hreq->headers, "Authorization");
248 sr->username = username;
250 sr->password = password;
255 char ubuf[256] = "", pbuf[256] = "", *p;
256 if (strncmp(basic, "Basic ", 6))
260 if (!len || len > 256)
262 olen = yaz_base64decode(basic, out);
263 /* Format of out should be username:password at this point */
265 if ((p = strchr(ubuf, ':'))) {
271 sr->username = odr_strdup(decode, ubuf);
273 sr->password = odr_strdup(decode, pbuf);
277 void yaz_uri_val_int(const char *path, const char *name, ODR o, int **intp)
279 const char *v = yaz_uri_val(path, name, o);
281 *intp = odr_intdup(o, atoi(v));
284 void yaz_mk_srw_diagnostic(ODR o, Z_SRW_diagnostic *d,
285 const char *uri, const char *message,
288 d->uri = odr_strdup(o, uri);
290 d->message = odr_strdup(o, message);
294 d->details = odr_strdup(o, details);
299 void yaz_mk_std_diagnostic(ODR o, Z_SRW_diagnostic *d,
300 int code, const char *details)
304 sprintf(uri, "info:srw/diagnostic/1/%d", code);
305 yaz_mk_srw_diagnostic(o, d, uri, 0, details);
308 void yaz_add_srw_diagnostic_uri(ODR o, Z_SRW_diagnostic **d,
309 int *num, const char *uri,
310 const char *message, const char *details)
312 Z_SRW_diagnostic *d_new;
313 d_new = (Z_SRW_diagnostic *) odr_malloc (o, (*num + 1)* sizeof(**d));
315 memcpy (d_new, *d, *num *sizeof(**d));
318 yaz_mk_srw_diagnostic(o, *d + *num, uri, message, details);
322 void yaz_add_srw_diagnostic(ODR o, Z_SRW_diagnostic **d,
323 int *num, int code, const char *addinfo)
327 sprintf(uri, "info:srw/diagnostic/1/%d", code);
328 yaz_add_srw_diagnostic_uri(o, d, num, uri, 0, addinfo);
332 static void grab_charset(ODR o, const char *content_type, char **charset)
336 const char *charset_p = 0;
337 if (content_type && (charset_p = strstr(content_type, "; charset=")))
341 while (i < 20 && charset_p[i] &&
342 !strchr("; \n\r", charset_p[i]))
344 *charset = (char*) odr_malloc(o, i+1);
345 memcpy(*charset, charset_p, i);
346 (*charset)[i] = '\0';
351 int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
352 Z_SOAP **soap_package, ODR decode, char **charset)
354 if (!strcmp(hreq->method, "POST"))
356 const char *content_type = z_HTTP_header_lookup(hreq->headers,
359 (!yaz_strcmp_del("text/xml", content_type, "; ") ||
360 !yaz_strcmp_del("application/soap+xml", content_type, "; ") ||
361 !yaz_strcmp_del("text/plain", content_type, "; ")))
363 char *db = "Default";
364 const char *p0 = hreq->path, *p1;
367 static Z_SOAP_Handler soap_handlers[4] = {
369 {"http://www.loc.gov/zing/srw/", 0,
370 (Z_SOAP_fun) yaz_srw_codec},
371 {"http://www.loc.gov/zing/srw/v1.0/", 0,
372 (Z_SOAP_fun) yaz_srw_codec},
373 {"http://www.loc.gov/zing/srw/update/", 0,
374 (Z_SOAP_fun) yaz_ucp_codec},
381 p1 = strchr(p0, '?');
383 p1 = p0 + strlen(p0);
386 db = (char*) odr_malloc(decode, p1 - p0 + 1);
387 memcpy (db, p0, p1 - p0);
391 grab_charset(decode, content_type, charset);
393 ret = z_soap_codec(decode, soap_package,
394 &hreq->content_buf, &hreq->content_len,
396 if (ret == 0 && (*soap_package)->which == Z_SOAP_generic)
398 *srw_pdu = (Z_SRW_PDU*) (*soap_package)->u.generic->p;
400 if ((*srw_pdu)->which == Z_SRW_searchRetrieve_request &&
401 (*srw_pdu)->u.request->database == 0)
402 (*srw_pdu)->u.request->database = db;
404 if ((*srw_pdu)->which == Z_SRW_explain_request &&
405 (*srw_pdu)->u.explain_request->database == 0)
406 (*srw_pdu)->u.explain_request->database = db;
408 if ((*srw_pdu)->which == Z_SRW_scan_request &&
409 (*srw_pdu)->u.scan_request->database == 0)
410 (*srw_pdu)->u.scan_request->database = db;
412 if ((*srw_pdu)->which == Z_SRW_update_request &&
413 (*srw_pdu)->u.update_request->database == 0)
414 (*srw_pdu)->u.update_request->database = db;
424 static int yaz_sru_decode_integer(ODR odr, const char *pname,
425 const char *valstr, int **valp,
426 Z_SRW_diagnostic **diag, int *num_diag,
432 if (sscanf(valstr, "%d", &ival) != 1)
434 yaz_add_srw_diagnostic(odr, diag, num_diag,
435 YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname);
438 if (min_value >= 0 && ival < min_value)
440 yaz_add_srw_diagnostic(odr, diag, num_diag,
441 YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname);
444 *valp = odr_intdup(odr, ival);
449 http://www.loc.gov/z3950/agency/zing/srw/service.html
451 int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
452 Z_SOAP **soap_package, ODR decode, char **charset,
453 Z_SRW_diagnostic **diag, int *num_diag)
456 static Z_SOAP_Handler soap_handlers[2] = {
457 {"http://www.loc.gov/zing/srw/", 0,
458 (Z_SOAP_fun) yaz_srw_codec},
462 const char *content_type = z_HTTP_header_lookup(hreq->headers,
466 SRU GET: ignore content type.
467 SRU POST: we support "application/x-www-form-urlencoded";
468 not "multipart/form-data" .
470 if (!strcmp(hreq->method, "GET")
472 (!strcmp(hreq->method, "POST") && content_type &&
473 !yaz_strcmp_del("application/x-www-form-urlencoded",
474 content_type, "; ")))
476 char *db = "Default";
477 const char *p0 = hreq->path, *p1;
479 const char *operation = 0;
486 char *stylesheet = 0;
487 char *scanClause = 0;
488 char *pScanClause = 0;
489 char *recordXPath = 0;
490 char *recordSchema = 0;
491 char *recordPacking = "xml"; /* xml packing is default for SRU */
492 char *maximumRecords = 0;
493 char *startRecord = 0;
494 char *maximumTerms = 0;
495 char *responsePosition = 0;
496 char *extraRequestData = 0;
501 grab_charset(decode, content_type, charset);
502 if (charset && *charset == 0 && !strcmp(hreq->method, "GET"))
507 p1 = strchr(p0, '?');
509 p1 = p0 + strlen(p0);
512 db = (char*) odr_malloc(decode, p1 - p0 + 1);
513 memcpy (db, p0, p1 - p0);
516 if (!strcmp(hreq->method, "POST"))
517 p1 = hreq->content_buf;
518 yaz_uri_array(p1, decode, &uri_name, &uri_val);
523 for (i = 0; uri_name[i]; i++)
525 char *n = uri_name[i];
526 char *v = uri_val[i];
527 if (!strcmp(n, "query"))
529 else if (!strcmp(n, "x-pquery"))
531 else if (!strcmp(n, "x-username"))
533 else if (!strcmp(n, "x-password"))
535 else if (!strcmp(n, "operation"))
537 else if (!strcmp(n, "stylesheet"))
539 else if (!strcmp(n, "sortKeys"))
541 else if (!strcmp(n, "recordXPath"))
543 else if (!strcmp(n, "recordSchema"))
545 else if (!strcmp(n, "recordPacking"))
547 else if (!strcmp(n, "version"))
549 else if (!strcmp(n, "scanClause"))
551 else if (!strcmp(n, "x-pScanClause"))
553 else if (!strcmp(n, "maximumRecords"))
555 else if (!strcmp(n, "startRecord"))
557 else if (!strcmp(n, "maximumTerms"))
559 else if (!strcmp(n, "responsePosition"))
560 responsePosition = v;
561 else if (!strcmp(n, "extraRequestData"))
562 extraRequestData = v;
564 yaz_add_srw_diagnostic(decode, diag, num_diag,
565 YAZ_SRW_UNSUPP_PARAMETER, n);
571 yaz_add_srw_diagnostic(
572 decode, diag, num_diag,
573 YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "version");
576 if (strcmp(version, "1.1"))
577 yaz_add_srw_diagnostic(decode, diag, num_diag,
578 YAZ_SRW_UNSUPP_VERSION, "1.1");
582 yaz_add_srw_diagnostic(
583 decode, diag, num_diag,
584 YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "operation");
585 operation = "explain";
587 if (!strcmp(operation, "searchRetrieve"))
589 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_searchRetrieve_request);
591 sr->srw_version = version;
593 yaz_srw_decodeauth(sr, hreq, username, password, decode);
596 sr->u.request->query_type = Z_SRW_query_type_cql;
597 sr->u.request->query.cql = query;
601 sr->u.request->query_type = Z_SRW_query_type_pqf;
602 sr->u.request->query.pqf = pQuery;
605 yaz_add_srw_diagnostic(
606 decode, diag, num_diag,
607 YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "query");
611 sr->u.request->sort_type = Z_SRW_sort_type_sort;
612 sr->u.request->sort.sortKeys = sortKeys;
614 sr->u.request->recordXPath = recordXPath;
615 sr->u.request->recordSchema = recordSchema;
616 sr->u.request->recordPacking = recordPacking;
617 sr->u.request->stylesheet = stylesheet;
619 yaz_sru_decode_integer(decode, "maximumRecords", maximumRecords,
620 &sr->u.request->maximumRecords,
623 yaz_sru_decode_integer(decode, "startRecord", startRecord,
624 &sr->u.request->startRecord,
627 sr->u.request->database = db;
629 (*soap_package) = odr_malloc(decode, sizeof(**soap_package));
630 (*soap_package)->which = Z_SOAP_generic;
632 (*soap_package)->u.generic =
633 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
635 (*soap_package)->u.generic->p = sr;
636 (*soap_package)->u.generic->ns = soap_handlers[0].ns;
637 (*soap_package)->u.generic->no = 0;
639 (*soap_package)->ns = "SRU";
643 else if (!strcmp(operation, "explain"))
645 /* Transfer SRU explain parameters to common struct */
646 /* http://www.loc.gov/z3950/agency/zing/srw/explain.html */
647 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
649 sr->srw_version = version;
650 yaz_srw_decodeauth(sr, hreq, username, password, decode);
652 sr->u.explain_request->recordPacking = recordPacking;
653 sr->u.explain_request->database = db;
655 sr->u.explain_request->stylesheet = stylesheet;
657 (*soap_package) = odr_malloc(decode, sizeof(**soap_package));
658 (*soap_package)->which = Z_SOAP_generic;
660 (*soap_package)->u.generic =
661 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
663 (*soap_package)->u.generic->p = sr;
664 (*soap_package)->u.generic->ns = soap_handlers[0].ns;
665 (*soap_package)->u.generic->no = 0;
667 (*soap_package)->ns = "SRU";
671 else if (!strcmp(operation, "scan"))
673 /* Transfer SRU scan parameters to common struct */
674 /* http://www.loc.gov/z3950/agency/zing/srw/scan.html */
675 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_scan_request);
677 sr->srw_version = version;
679 yaz_srw_decodeauth(sr, hreq, username, password, decode);
683 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
684 sr->u.scan_request->scanClause.cql = scanClause;
686 else if (pScanClause)
688 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
689 sr->u.scan_request->scanClause.pqf = pScanClause;
692 yaz_add_srw_diagnostic(
693 decode, diag, num_diag,
694 YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "scanClause");
695 sr->u.scan_request->database = db;
697 yaz_sru_decode_integer(decode, "maximumTerms",
699 &sr->u.scan_request->maximumTerms,
702 yaz_sru_decode_integer(decode, "responsePosition",
704 &sr->u.scan_request->responsePosition,
707 sr->u.scan_request->stylesheet = stylesheet;
709 (*soap_package) = odr_malloc(decode, sizeof(**soap_package));
710 (*soap_package)->which = Z_SOAP_generic;
712 (*soap_package)->u.generic =
713 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
715 (*soap_package)->u.generic->p = sr;
716 (*soap_package)->u.generic->ns = soap_handlers[0].ns;
717 (*soap_package)->u.generic->no = 0;
719 (*soap_package)->ns = "SRU";
725 /* unsupported operation ... */
726 /* Act as if we received a explain request and throw diagnostic. */
728 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
730 sr->srw_version = version;
732 sr->u.explain_request->recordPacking = recordPacking;
733 sr->u.explain_request->database = db;
735 sr->u.explain_request->stylesheet = stylesheet;
737 (*soap_package) = odr_malloc(decode, sizeof(**soap_package));
738 (*soap_package)->which = Z_SOAP_generic;
740 (*soap_package)->u.generic =
741 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
743 (*soap_package)->u.generic->p = sr;
744 (*soap_package)->u.generic->ns = soap_handlers[0].ns;
745 (*soap_package)->u.generic->no = 0;
747 (*soap_package)->ns = "SRU";
749 yaz_add_srw_diagnostic(decode, diag, num_diag,
750 YAZ_SRW_UNSUPP_OPERATION, operation);
759 Z_SRW_extra_record *yaz_srw_get_extra_record(ODR o)
761 Z_SRW_extra_record *res = (Z_SRW_extra_record *)
762 odr_malloc(o, sizeof(*res));
764 res->recordReviewCode = 0;
765 res->recordReviewNote = 0;
767 res->nonDupRecordId = 0;
768 res->recordLockStatus = 0;
769 res->recordOldVersion = 0;
773 Z_SRW_PDU *yaz_srw_get_core_v_1_1(ODR o)
775 Z_SRW_PDU *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(*p));
776 p->srw_version = odr_strdup(o, "1.1");
783 Z_SRW_PDU *yaz_srw_get(ODR o, int which)
785 Z_SRW_PDU *sr = yaz_srw_get_core_v_1_1(o);
789 case Z_SRW_searchRetrieve_request:
790 sr->u.request = (Z_SRW_searchRetrieveRequest *)
791 odr_malloc(o, sizeof(*sr->u.request));
792 sr->u.request->query_type = Z_SRW_query_type_cql;
793 sr->u.request->query.cql = 0;
794 sr->u.request->sort_type = Z_SRW_sort_type_none;
795 sr->u.request->sort.none = 0;
796 sr->u.request->startRecord = 0;
797 sr->u.request->maximumRecords = 0;
798 sr->u.request->recordSchema = 0;
799 sr->u.request->recordPacking = 0;
800 sr->u.request->recordXPath = 0;
801 sr->u.request->database = 0;
802 sr->u.request->resultSetTTL = 0;
803 sr->u.request->stylesheet = 0;
805 case Z_SRW_searchRetrieve_response:
806 sr->u.response = (Z_SRW_searchRetrieveResponse *)
807 odr_malloc(o, sizeof(*sr->u.response));
808 sr->u.response->numberOfRecords = 0;
809 sr->u.response->resultSetId = 0;
810 sr->u.response->resultSetIdleTime = 0;
811 sr->u.response->records = 0;
812 sr->u.response->num_records = 0;
813 sr->u.response->diagnostics = 0;
814 sr->u.response->num_diagnostics = 0;
815 sr->u.response->nextRecordPosition = 0;
816 sr->u.response->extra_records = 0;
818 case Z_SRW_explain_request:
819 sr->u.explain_request = (Z_SRW_explainRequest *)
820 odr_malloc(o, sizeof(*sr->u.explain_request));
821 sr->u.explain_request->recordPacking = 0;
822 sr->u.explain_request->database = 0;
823 sr->u.explain_request->stylesheet = 0;
825 case Z_SRW_explain_response:
826 sr->u.explain_response = (Z_SRW_explainResponse *)
827 odr_malloc(o, sizeof(*sr->u.explain_response));
828 sr->u.explain_response->record.recordData_buf = 0;
829 sr->u.explain_response->record.recordData_len = 0;
830 sr->u.explain_response->record.recordSchema = 0;
831 sr->u.explain_response->record.recordPosition = 0;
832 sr->u.explain_response->record.recordPacking =
833 Z_SRW_recordPacking_string;
834 sr->u.explain_response->diagnostics = 0;
835 sr->u.explain_response->num_diagnostics = 0;
836 sr->u.explain_response->extra_record = 0;
838 case Z_SRW_scan_request:
839 sr->u.scan_request = (Z_SRW_scanRequest *)
840 odr_malloc(o, sizeof(*sr->u.scan_request));
841 sr->u.scan_request->database = 0;
842 sr->u.scan_request->stylesheet = 0;
843 sr->u.scan_request->maximumTerms = 0;
844 sr->u.scan_request->responsePosition = 0;
845 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
846 sr->u.scan_request->scanClause.cql = 0;
848 case Z_SRW_scan_response:
849 sr->u.scan_response = (Z_SRW_scanResponse *)
850 odr_malloc(o, sizeof(*sr->u.scan_response));
851 sr->u.scan_response->terms = 0;
852 sr->u.scan_response->num_terms = 0;
853 sr->u.scan_response->diagnostics = 0;
854 sr->u.scan_response->num_diagnostics = 0;
856 case Z_SRW_update_request:
857 sr->u.update_request = (Z_SRW_updateRequest *)
858 odr_malloc(o, sizeof(*sr->u.update_request));
859 sr->u.update_request->database = 0;
860 sr->u.update_request->stylesheet = 0;
861 sr->u.update_request->record.recordSchema = 0;
862 sr->u.update_request->record.recordPacking = Z_SRW_recordPacking_XML;
863 sr->u.update_request->recordId = 0;
864 sr->u.update_request->recordVersion = 0;
865 sr->u.update_request->recordOldVersion = 0;
866 sr->u.update_request->record.recordData_buf = 0;
867 sr->u.update_request->record.recordData_len = 0;
868 sr->u.update_request->extra_record = 0;
869 sr->u.update_request->extraRequestData = 0;
870 sr->u.request->database = 0;
872 case Z_SRW_update_response:
873 sr->u.update_response = (Z_SRW_updateResponse *)
874 odr_malloc(o, sizeof(*sr->u.update_response));
875 sr->u.update_response->operationStatus = 0;
876 sr->u.update_response->recordId = 0;
877 sr->u.update_response->recordVersion = 0;
878 sr->u.update_response->recordChecksum = 0;
879 sr->u.update_response->record.recordData_buf = 0;
880 sr->u.update_response->record.recordData_len = 0;
881 sr->u.update_response->record.recordSchema = 0;
882 sr->u.update_response->record.recordPacking =
883 Z_SRW_recordPacking_XML;
884 sr->u.update_response->extra_record = 0;
885 sr->u.update_response->extraResponseData = 0;
886 sr->u.update_response->diagnostics = 0;
887 sr->u.update_response->num_diagnostics = 0;
893 static int srw_bib1_map[] = {
903 108, 10, /* Malformed query : Syntax error */
933 100, 1, /* bad map */
981 205, 1, /* bad map */
982 206, 1, /* bad map */
984 208, 1, /* bad map */
995 218, 1, /* bad map */
996 219, 1, /* bad map */
997 220, 1, /* bad map */
998 221, 1, /* bad map */
1000 223, 1, /* bad map */
1001 224, 1, /* bad map */
1002 225, 1, /* bad map */
1003 226, 1, /* bad map */
1005 228, 1, /* bad map */
1010 233, 1, /* bad map */
1011 234, 1, /* bad map */
1017 240, 1, /* bad map */
1018 241, 1, /* bad map */
1020 243, 1, /* bad map */
1025 1001, 1, /* bad map */
1026 1002, 1, /* bad map */
1027 1003, 1, /* bad map */
1028 1004, 1, /* bad map */
1029 1005, 1, /* bad map */
1030 1006, 1, /* bad map */
1062 int yaz_diag_bib1_to_srw (int code)
1064 const int *p = srw_bib1_map;
1074 int yaz_diag_srw_to_bib1(int code)
1076 const int *p = srw_bib1_map;
1086 static void add_val_int(ODR o, char **name, char **value, int *i,
1087 char *a_name, int *val)
1092 value[*i] = odr_malloc(o, 30);
1093 sprintf(value[*i], "%d", *val);
1098 static void add_val_str(ODR o, char **name, char **value, int *i,
1099 char *a_name, char *val)
1109 static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode,
1110 char **name, char **value)
1113 add_val_str(encode, name, value, &i, "version", srw_pdu->srw_version);
1114 name[i] = "operation";
1115 switch(srw_pdu->which)
1117 case Z_SRW_searchRetrieve_request:
1118 value[i++] = "searchRetrieve";
1119 switch(srw_pdu->u.request->query_type)
1121 case Z_SRW_query_type_cql:
1122 add_val_str(encode, name, value, &i, "query",
1123 srw_pdu->u.request->query.cql);
1125 case Z_SRW_query_type_pqf:
1126 add_val_str(encode, name, value, &i, "x-pquery",
1127 srw_pdu->u.request->query.pqf);
1129 case Z_SRW_query_type_xcql:
1130 add_val_str(encode, name, value, &i, "x-cql",
1131 srw_pdu->u.request->query.xcql);
1134 switch(srw_pdu->u.request->sort_type)
1136 case Z_SRW_sort_type_none:
1138 case Z_SRW_sort_type_sort:
1139 add_val_str(encode, name, value, &i, "sortKeys",
1140 srw_pdu->u.request->sort.sortKeys);
1143 add_val_int(encode, name, value, &i, "startRecord",
1144 srw_pdu->u.request->startRecord);
1145 add_val_int(encode, name, value, &i, "maximumRecords",
1146 srw_pdu->u.request->maximumRecords);
1147 add_val_str(encode, name, value, &i, "recordSchema",
1148 srw_pdu->u.request->recordSchema);
1149 add_val_str(encode, name, value, &i, "recordPacking",
1150 srw_pdu->u.request->recordPacking);
1151 add_val_str(encode, name, value, &i, "recordXPath",
1152 srw_pdu->u.request->recordXPath);
1153 add_val_str(encode, name, value, &i, "stylesheet",
1154 srw_pdu->u.request->stylesheet);
1155 add_val_int(encode, name, value, &i, "resultSetTTL",
1156 srw_pdu->u.request->resultSetTTL);
1158 case Z_SRW_explain_request:
1159 value[i++] = "explain";
1160 add_val_str(encode, name, value, &i, "stylesheet",
1161 srw_pdu->u.explain_request->stylesheet);
1163 case Z_SRW_scan_request:
1164 value[i++] = "scan";
1166 switch(srw_pdu->u.scan_request->query_type)
1168 case Z_SRW_query_type_cql:
1169 add_val_str(encode, name, value, &i, "scanClause",
1170 srw_pdu->u.scan_request->scanClause.cql);
1172 case Z_SRW_query_type_pqf:
1173 add_val_str(encode, name, value, &i, "x-pScanClause",
1174 srw_pdu->u.scan_request->scanClause.pqf);
1176 case Z_SRW_query_type_xcql:
1177 add_val_str(encode, name, value, &i, "x-cqlScanClause",
1178 srw_pdu->u.scan_request->scanClause.xcql);
1181 add_val_int(encode, name, value, &i, "responsePosition",
1182 srw_pdu->u.scan_request->responsePosition);
1183 add_val_int(encode, name, value, &i, "maximumTerms",
1184 srw_pdu->u.scan_request->maximumTerms);
1185 add_val_str(encode, name, value, &i, "stylesheet",
1186 srw_pdu->u.scan_request->stylesheet);
1188 case Z_SRW_update_request:
1189 value[i++] = "update";
1198 int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1199 ODR encode, const char *charset)
1201 char *name[30], *value[30]; /* definite upper limit for SRU params */
1205 if (yaz_get_sru_parms(srw_pdu, encode, name, value))
1207 yaz_array_to_uri_ex(&uri_args, encode, name, value, srw_pdu->extra_args);
1209 hreq->method = "GET";
1211 path = odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4
1212 +(srw_pdu->extra_args ? strlen(srw_pdu->extra_args) : 0)
1214 sprintf(path, "%s?%s", hreq->path, uri_args);
1217 z_HTTP_header_add_content_type(encode, &hreq->headers,
1218 "text/xml", charset);
1222 int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1223 ODR encode, const char *charset)
1225 char *name[30], *value[30]; /* definite upper limit for SRU params */
1228 if (yaz_get_sru_parms(srw_pdu, encode, name, value))
1231 yaz_array_to_uri_ex(&uri_args, encode, name, value, srw_pdu->extra_args);
1233 hreq->method = "POST";
1235 hreq->content_buf = uri_args;
1236 hreq->content_len = strlen(uri_args);
1238 z_HTTP_header_add_content_type(encode, &hreq->headers,
1239 "application/x-www-form-urlencoded",
1244 int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1245 ODR odr, const char *charset)
1247 Z_SOAP_Handler handlers[2] = {
1249 {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec},
1253 Z_SOAP *p = (Z_SOAP*) odr_malloc(odr, sizeof(*p));
1254 z_HTTP_header_add_content_type(odr,
1256 "text/xml", charset);
1258 z_HTTP_header_add(odr, &hreq->headers,
1259 "SOAPAction", "\"\"");
1260 p->which = Z_SOAP_generic;
1261 p->u.generic = (Z_SOAP_Generic *) odr_malloc(odr, sizeof(*p->u.generic));
1262 p->u.generic->no = 0;
1263 p->u.generic->ns = 0;
1264 p->u.generic->p = srw_pdu;
1265 p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
1267 return z_soap_codec_enc(odr, &p,
1269 &hreq->content_len, handlers,
1276 * indent-tabs-mode: nil
1278 * vim: shiftwidth=4 tabstop=8 expandtab