2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: srwutil.c,v 1.53 2006-10-27 11:22:09 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 void yaz_add_sru_update_diagnostic(ODR o, Z_SRW_diagnostic **d,
333 int *num, int code, const char *addinfo)
337 sprintf(uri, "info:srw/diagnostic/12/%d", code);
338 yaz_add_srw_diagnostic_uri(o, d, num, uri, 0, addinfo);
342 static void grab_charset(ODR o, const char *content_type, char **charset)
346 const char *charset_p = 0;
347 if (content_type && (charset_p = strstr(content_type, "; charset=")))
351 while (i < 20 && charset_p[i] &&
352 !strchr("; \n\r", charset_p[i]))
354 *charset = (char*) odr_malloc(o, i+1);
355 memcpy(*charset, charset_p, i);
356 (*charset)[i] = '\0';
361 int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
362 Z_SOAP **soap_package, ODR decode, char **charset)
364 if (!strcmp(hreq->method, "POST"))
366 const char *content_type = z_HTTP_header_lookup(hreq->headers,
369 (!yaz_strcmp_del("text/xml", content_type, "; ") ||
370 !yaz_strcmp_del("application/soap+xml", content_type, "; ") ||
371 !yaz_strcmp_del("text/plain", content_type, "; ")))
373 char *db = "Default";
374 const char *p0 = hreq->path, *p1;
377 static Z_SOAP_Handler soap_handlers[4] = {
379 {"http://www.loc.gov/zing/srw/", 0,
380 (Z_SOAP_fun) yaz_srw_codec},
381 {"http://www.loc.gov/zing/srw/v1.0/", 0,
382 (Z_SOAP_fun) yaz_srw_codec},
383 {"http://www.loc.gov/zing/srw/update/", 0,
384 (Z_SOAP_fun) yaz_ucp_codec},
391 p1 = strchr(p0, '?');
393 p1 = p0 + strlen(p0);
396 db = (char*) odr_malloc(decode, p1 - p0 + 1);
397 memcpy (db, p0, p1 - p0);
401 grab_charset(decode, content_type, charset);
403 ret = z_soap_codec(decode, soap_package,
404 &hreq->content_buf, &hreq->content_len,
406 if (ret == 0 && (*soap_package)->which == Z_SOAP_generic)
408 *srw_pdu = (Z_SRW_PDU*) (*soap_package)->u.generic->p;
410 if ((*srw_pdu)->which == Z_SRW_searchRetrieve_request &&
411 (*srw_pdu)->u.request->database == 0)
412 (*srw_pdu)->u.request->database = db;
414 if ((*srw_pdu)->which == Z_SRW_explain_request &&
415 (*srw_pdu)->u.explain_request->database == 0)
416 (*srw_pdu)->u.explain_request->database = db;
418 if ((*srw_pdu)->which == Z_SRW_scan_request &&
419 (*srw_pdu)->u.scan_request->database == 0)
420 (*srw_pdu)->u.scan_request->database = db;
422 if ((*srw_pdu)->which == Z_SRW_update_request &&
423 (*srw_pdu)->u.update_request->database == 0)
424 (*srw_pdu)->u.update_request->database = db;
434 static int yaz_sru_decode_integer(ODR odr, const char *pname,
435 const char *valstr, int **valp,
436 Z_SRW_diagnostic **diag, int *num_diag,
442 if (sscanf(valstr, "%d", &ival) != 1)
444 yaz_add_srw_diagnostic(odr, diag, num_diag,
445 YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname);
448 if (min_value >= 0 && ival < min_value)
450 yaz_add_srw_diagnostic(odr, diag, num_diag,
451 YAZ_SRW_UNSUPP_PARAMETER_VALUE, pname);
454 *valp = odr_intdup(odr, ival);
459 http://www.loc.gov/z3950/agency/zing/srw/service.html
461 int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
462 Z_SOAP **soap_package, ODR decode, char **charset,
463 Z_SRW_diagnostic **diag, int *num_diag)
466 static Z_SOAP_Handler soap_handlers[2] = {
467 {"http://www.loc.gov/zing/srw/", 0,
468 (Z_SOAP_fun) yaz_srw_codec},
472 const char *content_type = z_HTTP_header_lookup(hreq->headers,
476 SRU GET: ignore content type.
477 SRU POST: we support "application/x-www-form-urlencoded";
478 not "multipart/form-data" .
480 if (!strcmp(hreq->method, "GET")
482 (!strcmp(hreq->method, "POST") && content_type &&
483 !yaz_strcmp_del("application/x-www-form-urlencoded",
484 content_type, "; ")))
486 char *db = "Default";
487 const char *p0 = hreq->path, *p1;
489 const char *operation = 0;
496 char *stylesheet = 0;
497 char *scanClause = 0;
498 char *pScanClause = 0;
499 char *recordXPath = 0;
500 char *recordSchema = 0;
501 char *recordPacking = "xml"; /* xml packing is default for SRU */
502 char *maximumRecords = 0;
503 char *startRecord = 0;
504 char *maximumTerms = 0;
505 char *responsePosition = 0;
506 char *extraRequestData = 0;
511 grab_charset(decode, content_type, charset);
512 if (charset && *charset == 0 && !strcmp(hreq->method, "GET"))
517 p1 = strchr(p0, '?');
519 p1 = p0 + strlen(p0);
522 db = (char*) odr_malloc(decode, p1 - p0 + 1);
523 memcpy (db, p0, p1 - p0);
526 if (!strcmp(hreq->method, "POST"))
527 p1 = hreq->content_buf;
528 yaz_uri_array(p1, decode, &uri_name, &uri_val);
533 for (i = 0; uri_name[i]; i++)
535 char *n = uri_name[i];
536 char *v = uri_val[i];
537 if (!strcmp(n, "query"))
539 else if (!strcmp(n, "x-pquery"))
541 else if (!strcmp(n, "x-username"))
543 else if (!strcmp(n, "x-password"))
545 else if (!strcmp(n, "operation"))
547 else if (!strcmp(n, "stylesheet"))
549 else if (!strcmp(n, "sortKeys"))
551 else if (!strcmp(n, "recordXPath"))
553 else if (!strcmp(n, "recordSchema"))
555 else if (!strcmp(n, "recordPacking"))
557 else if (!strcmp(n, "version"))
559 else if (!strcmp(n, "scanClause"))
561 else if (!strcmp(n, "x-pScanClause"))
563 else if (!strcmp(n, "maximumRecords"))
565 else if (!strcmp(n, "startRecord"))
567 else if (!strcmp(n, "maximumTerms"))
569 else if (!strcmp(n, "responsePosition"))
570 responsePosition = v;
571 else if (!strcmp(n, "extraRequestData"))
572 extraRequestData = v;
574 yaz_add_srw_diagnostic(decode, diag, num_diag,
575 YAZ_SRW_UNSUPP_PARAMETER, n);
581 yaz_add_srw_diagnostic(
582 decode, diag, num_diag,
583 YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "version");
586 if (strcmp(version, "1.1"))
587 yaz_add_srw_diagnostic(decode, diag, num_diag,
588 YAZ_SRW_UNSUPP_VERSION, "1.1");
592 yaz_add_srw_diagnostic(
593 decode, diag, num_diag,
594 YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "operation");
595 operation = "explain";
597 if (!strcmp(operation, "searchRetrieve"))
599 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_searchRetrieve_request);
601 sr->srw_version = version;
603 yaz_srw_decodeauth(sr, hreq, username, password, decode);
606 sr->u.request->query_type = Z_SRW_query_type_cql;
607 sr->u.request->query.cql = query;
611 sr->u.request->query_type = Z_SRW_query_type_pqf;
612 sr->u.request->query.pqf = pQuery;
615 yaz_add_srw_diagnostic(
616 decode, diag, num_diag,
617 YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "query");
621 sr->u.request->sort_type = Z_SRW_sort_type_sort;
622 sr->u.request->sort.sortKeys = sortKeys;
624 sr->u.request->recordXPath = recordXPath;
625 sr->u.request->recordSchema = recordSchema;
626 sr->u.request->recordPacking = recordPacking;
627 sr->u.request->stylesheet = stylesheet;
629 yaz_sru_decode_integer(decode, "maximumRecords", maximumRecords,
630 &sr->u.request->maximumRecords,
633 yaz_sru_decode_integer(decode, "startRecord", startRecord,
634 &sr->u.request->startRecord,
637 sr->u.request->database = db;
639 (*soap_package) = odr_malloc(decode, sizeof(**soap_package));
640 (*soap_package)->which = Z_SOAP_generic;
642 (*soap_package)->u.generic =
643 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
645 (*soap_package)->u.generic->p = sr;
646 (*soap_package)->u.generic->ns = soap_handlers[0].ns;
647 (*soap_package)->u.generic->no = 0;
649 (*soap_package)->ns = "SRU";
653 else if (!strcmp(operation, "explain"))
655 /* Transfer SRU explain parameters to common struct */
656 /* http://www.loc.gov/z3950/agency/zing/srw/explain.html */
657 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
659 sr->srw_version = version;
660 yaz_srw_decodeauth(sr, hreq, username, password, decode);
662 sr->u.explain_request->recordPacking = recordPacking;
663 sr->u.explain_request->database = db;
665 sr->u.explain_request->stylesheet = stylesheet;
667 (*soap_package) = odr_malloc(decode, sizeof(**soap_package));
668 (*soap_package)->which = Z_SOAP_generic;
670 (*soap_package)->u.generic =
671 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
673 (*soap_package)->u.generic->p = sr;
674 (*soap_package)->u.generic->ns = soap_handlers[0].ns;
675 (*soap_package)->u.generic->no = 0;
677 (*soap_package)->ns = "SRU";
681 else if (!strcmp(operation, "scan"))
683 /* Transfer SRU scan parameters to common struct */
684 /* http://www.loc.gov/z3950/agency/zing/srw/scan.html */
685 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_scan_request);
687 sr->srw_version = version;
689 yaz_srw_decodeauth(sr, hreq, username, password, decode);
693 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
694 sr->u.scan_request->scanClause.cql = scanClause;
696 else if (pScanClause)
698 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
699 sr->u.scan_request->scanClause.pqf = pScanClause;
702 yaz_add_srw_diagnostic(
703 decode, diag, num_diag,
704 YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED, "scanClause");
705 sr->u.scan_request->database = db;
707 yaz_sru_decode_integer(decode, "maximumTerms",
709 &sr->u.scan_request->maximumTerms,
712 yaz_sru_decode_integer(decode, "responsePosition",
714 &sr->u.scan_request->responsePosition,
717 sr->u.scan_request->stylesheet = stylesheet;
719 (*soap_package) = odr_malloc(decode, sizeof(**soap_package));
720 (*soap_package)->which = Z_SOAP_generic;
722 (*soap_package)->u.generic =
723 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
725 (*soap_package)->u.generic->p = sr;
726 (*soap_package)->u.generic->ns = soap_handlers[0].ns;
727 (*soap_package)->u.generic->no = 0;
729 (*soap_package)->ns = "SRU";
735 /* unsupported operation ... */
736 /* Act as if we received a explain request and throw diagnostic. */
738 Z_SRW_PDU *sr = yaz_srw_get(decode, Z_SRW_explain_request);
740 sr->srw_version = version;
742 sr->u.explain_request->recordPacking = recordPacking;
743 sr->u.explain_request->database = db;
745 sr->u.explain_request->stylesheet = stylesheet;
747 (*soap_package) = odr_malloc(decode, sizeof(**soap_package));
748 (*soap_package)->which = Z_SOAP_generic;
750 (*soap_package)->u.generic =
751 odr_malloc(decode, sizeof(*(*soap_package)->u.generic));
753 (*soap_package)->u.generic->p = sr;
754 (*soap_package)->u.generic->ns = soap_handlers[0].ns;
755 (*soap_package)->u.generic->no = 0;
757 (*soap_package)->ns = "SRU";
759 yaz_add_srw_diagnostic(decode, diag, num_diag,
760 YAZ_SRW_UNSUPP_OPERATION, operation);
769 Z_SRW_extra_record *yaz_srw_get_extra_record(ODR o)
771 Z_SRW_extra_record *res = (Z_SRW_extra_record *)
772 odr_malloc(o, sizeof(*res));
774 res->recordReviewCode = 0;
775 res->recordReviewNote = 0;
777 res->nonDupRecordId = 0;
778 res->recordLockStatus = 0;
779 res->recordOldVersion = 0;
783 Z_SRW_PDU *yaz_srw_get_core_v_1_1(ODR o)
785 Z_SRW_PDU *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(*p));
786 p->srw_version = odr_strdup(o, "1.1");
793 Z_SRW_PDU *yaz_srw_get(ODR o, int which)
795 Z_SRW_PDU *sr = yaz_srw_get_core_v_1_1(o);
799 case Z_SRW_searchRetrieve_request:
800 sr->u.request = (Z_SRW_searchRetrieveRequest *)
801 odr_malloc(o, sizeof(*sr->u.request));
802 sr->u.request->query_type = Z_SRW_query_type_cql;
803 sr->u.request->query.cql = 0;
804 sr->u.request->sort_type = Z_SRW_sort_type_none;
805 sr->u.request->sort.none = 0;
806 sr->u.request->startRecord = 0;
807 sr->u.request->maximumRecords = 0;
808 sr->u.request->recordSchema = 0;
809 sr->u.request->recordPacking = 0;
810 sr->u.request->recordXPath = 0;
811 sr->u.request->database = 0;
812 sr->u.request->resultSetTTL = 0;
813 sr->u.request->stylesheet = 0;
815 case Z_SRW_searchRetrieve_response:
816 sr->u.response = (Z_SRW_searchRetrieveResponse *)
817 odr_malloc(o, sizeof(*sr->u.response));
818 sr->u.response->numberOfRecords = 0;
819 sr->u.response->resultSetId = 0;
820 sr->u.response->resultSetIdleTime = 0;
821 sr->u.response->records = 0;
822 sr->u.response->num_records = 0;
823 sr->u.response->diagnostics = 0;
824 sr->u.response->num_diagnostics = 0;
825 sr->u.response->nextRecordPosition = 0;
826 sr->u.response->extra_records = 0;
828 case Z_SRW_explain_request:
829 sr->u.explain_request = (Z_SRW_explainRequest *)
830 odr_malloc(o, sizeof(*sr->u.explain_request));
831 sr->u.explain_request->recordPacking = 0;
832 sr->u.explain_request->database = 0;
833 sr->u.explain_request->stylesheet = 0;
835 case Z_SRW_explain_response:
836 sr->u.explain_response = (Z_SRW_explainResponse *)
837 odr_malloc(o, sizeof(*sr->u.explain_response));
838 sr->u.explain_response->record.recordData_buf = 0;
839 sr->u.explain_response->record.recordData_len = 0;
840 sr->u.explain_response->record.recordSchema = 0;
841 sr->u.explain_response->record.recordPosition = 0;
842 sr->u.explain_response->record.recordPacking =
843 Z_SRW_recordPacking_string;
844 sr->u.explain_response->diagnostics = 0;
845 sr->u.explain_response->num_diagnostics = 0;
846 sr->u.explain_response->extra_record = 0;
848 case Z_SRW_scan_request:
849 sr->u.scan_request = (Z_SRW_scanRequest *)
850 odr_malloc(o, sizeof(*sr->u.scan_request));
851 sr->u.scan_request->database = 0;
852 sr->u.scan_request->stylesheet = 0;
853 sr->u.scan_request->maximumTerms = 0;
854 sr->u.scan_request->responsePosition = 0;
855 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
856 sr->u.scan_request->scanClause.cql = 0;
858 case Z_SRW_scan_response:
859 sr->u.scan_response = (Z_SRW_scanResponse *)
860 odr_malloc(o, sizeof(*sr->u.scan_response));
861 sr->u.scan_response->terms = 0;
862 sr->u.scan_response->num_terms = 0;
863 sr->u.scan_response->diagnostics = 0;
864 sr->u.scan_response->num_diagnostics = 0;
866 case Z_SRW_update_request:
867 sr->u.update_request = (Z_SRW_updateRequest *)
868 odr_malloc(o, sizeof(*sr->u.update_request));
869 sr->u.update_request->database = 0;
870 sr->u.update_request->stylesheet = 0;
871 sr->u.update_request->record.recordSchema = 0;
872 sr->u.update_request->record.recordPacking = Z_SRW_recordPacking_XML;
873 sr->u.update_request->recordId = 0;
874 sr->u.update_request->recordVersion = 0;
875 sr->u.update_request->recordOldVersion = 0;
876 sr->u.update_request->record.recordData_buf = 0;
877 sr->u.update_request->record.recordData_len = 0;
878 sr->u.update_request->extra_record = 0;
879 sr->u.update_request->extraRequestData = 0;
880 sr->u.request->database = 0;
882 case Z_SRW_update_response:
883 sr->u.update_response = (Z_SRW_updateResponse *)
884 odr_malloc(o, sizeof(*sr->u.update_response));
885 sr->u.update_response->operationStatus = 0;
886 sr->u.update_response->recordId = 0;
887 sr->u.update_response->recordVersion = 0;
888 sr->u.update_response->recordChecksum = 0;
889 sr->u.update_response->record.recordData_buf = 0;
890 sr->u.update_response->record.recordData_len = 0;
891 sr->u.update_response->record.recordSchema = 0;
892 sr->u.update_response->record.recordPacking =
893 Z_SRW_recordPacking_XML;
894 sr->u.update_response->extra_record = 0;
895 sr->u.update_response->extraResponseData = 0;
896 sr->u.update_response->diagnostics = 0;
897 sr->u.update_response->num_diagnostics = 0;
903 static int srw_bib1_map[] = {
913 108, 10, /* Malformed query : Syntax error */
943 100, 1, /* bad map */
991 205, 1, /* bad map */
992 206, 1, /* bad map */
994 208, 1, /* bad map */
1005 218, 1, /* bad map */
1006 219, 1, /* bad map */
1007 220, 1, /* bad map */
1008 221, 1, /* bad map */
1010 223, 1, /* bad map */
1011 224, 1, /* bad map */
1012 225, 1, /* bad map */
1013 226, 1, /* bad map */
1015 228, 1, /* bad map */
1020 233, 1, /* bad map */
1021 234, 1, /* bad map */
1027 240, 1, /* bad map */
1028 241, 1, /* bad map */
1030 243, 1, /* bad map */
1035 1001, 1, /* bad map */
1036 1002, 1, /* bad map */
1037 1003, 1, /* bad map */
1038 1004, 1, /* bad map */
1039 1005, 1, /* bad map */
1040 1006, 1, /* bad map */
1072 int yaz_diag_bib1_to_srw (int code)
1074 const int *p = srw_bib1_map;
1084 int yaz_diag_srw_to_bib1(int code)
1086 const int *p = srw_bib1_map;
1096 static void add_val_int(ODR o, char **name, char **value, int *i,
1097 char *a_name, int *val)
1102 value[*i] = odr_malloc(o, 30);
1103 sprintf(value[*i], "%d", *val);
1108 static void add_val_str(ODR o, char **name, char **value, int *i,
1109 char *a_name, char *val)
1119 static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode,
1120 char **name, char **value)
1123 add_val_str(encode, name, value, &i, "version", srw_pdu->srw_version);
1124 name[i] = "operation";
1125 switch(srw_pdu->which)
1127 case Z_SRW_searchRetrieve_request:
1128 value[i++] = "searchRetrieve";
1129 switch(srw_pdu->u.request->query_type)
1131 case Z_SRW_query_type_cql:
1132 add_val_str(encode, name, value, &i, "query",
1133 srw_pdu->u.request->query.cql);
1135 case Z_SRW_query_type_pqf:
1136 add_val_str(encode, name, value, &i, "x-pquery",
1137 srw_pdu->u.request->query.pqf);
1139 case Z_SRW_query_type_xcql:
1140 add_val_str(encode, name, value, &i, "x-cql",
1141 srw_pdu->u.request->query.xcql);
1144 switch(srw_pdu->u.request->sort_type)
1146 case Z_SRW_sort_type_none:
1148 case Z_SRW_sort_type_sort:
1149 add_val_str(encode, name, value, &i, "sortKeys",
1150 srw_pdu->u.request->sort.sortKeys);
1153 add_val_int(encode, name, value, &i, "startRecord",
1154 srw_pdu->u.request->startRecord);
1155 add_val_int(encode, name, value, &i, "maximumRecords",
1156 srw_pdu->u.request->maximumRecords);
1157 add_val_str(encode, name, value, &i, "recordSchema",
1158 srw_pdu->u.request->recordSchema);
1159 add_val_str(encode, name, value, &i, "recordPacking",
1160 srw_pdu->u.request->recordPacking);
1161 add_val_str(encode, name, value, &i, "recordXPath",
1162 srw_pdu->u.request->recordXPath);
1163 add_val_str(encode, name, value, &i, "stylesheet",
1164 srw_pdu->u.request->stylesheet);
1165 add_val_int(encode, name, value, &i, "resultSetTTL",
1166 srw_pdu->u.request->resultSetTTL);
1168 case Z_SRW_explain_request:
1169 value[i++] = "explain";
1170 add_val_str(encode, name, value, &i, "stylesheet",
1171 srw_pdu->u.explain_request->stylesheet);
1173 case Z_SRW_scan_request:
1174 value[i++] = "scan";
1176 switch(srw_pdu->u.scan_request->query_type)
1178 case Z_SRW_query_type_cql:
1179 add_val_str(encode, name, value, &i, "scanClause",
1180 srw_pdu->u.scan_request->scanClause.cql);
1182 case Z_SRW_query_type_pqf:
1183 add_val_str(encode, name, value, &i, "x-pScanClause",
1184 srw_pdu->u.scan_request->scanClause.pqf);
1186 case Z_SRW_query_type_xcql:
1187 add_val_str(encode, name, value, &i, "x-cqlScanClause",
1188 srw_pdu->u.scan_request->scanClause.xcql);
1191 add_val_int(encode, name, value, &i, "responsePosition",
1192 srw_pdu->u.scan_request->responsePosition);
1193 add_val_int(encode, name, value, &i, "maximumTerms",
1194 srw_pdu->u.scan_request->maximumTerms);
1195 add_val_str(encode, name, value, &i, "stylesheet",
1196 srw_pdu->u.scan_request->stylesheet);
1198 case Z_SRW_update_request:
1199 value[i++] = "update";
1208 int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1209 ODR encode, const char *charset)
1211 char *name[30], *value[30]; /* definite upper limit for SRU params */
1215 if (yaz_get_sru_parms(srw_pdu, encode, name, value))
1217 yaz_array_to_uri_ex(&uri_args, encode, name, value, srw_pdu->extra_args);
1219 hreq->method = "GET";
1221 path = odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4
1222 +(srw_pdu->extra_args ? strlen(srw_pdu->extra_args) : 0)
1224 sprintf(path, "%s?%s", hreq->path, uri_args);
1227 z_HTTP_header_add_content_type(encode, &hreq->headers,
1228 "text/xml", charset);
1232 int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1233 ODR encode, const char *charset)
1235 char *name[30], *value[30]; /* definite upper limit for SRU params */
1238 if (yaz_get_sru_parms(srw_pdu, encode, name, value))
1241 yaz_array_to_uri_ex(&uri_args, encode, name, value, srw_pdu->extra_args);
1243 hreq->method = "POST";
1245 hreq->content_buf = uri_args;
1246 hreq->content_len = strlen(uri_args);
1248 z_HTTP_header_add_content_type(encode, &hreq->headers,
1249 "application/x-www-form-urlencoded",
1254 int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
1255 ODR odr, const char *charset)
1257 Z_SOAP_Handler handlers[2] = {
1259 {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec},
1263 Z_SOAP *p = (Z_SOAP*) odr_malloc(odr, sizeof(*p));
1264 z_HTTP_header_add_content_type(odr,
1266 "text/xml", charset);
1268 z_HTTP_header_add(odr, &hreq->headers,
1269 "SOAPAction", "\"\"");
1270 p->which = Z_SOAP_generic;
1271 p->u.generic = (Z_SOAP_Generic *) odr_malloc(odr, sizeof(*p->u.generic));
1272 p->u.generic->no = 0;
1273 p->u.generic->ns = 0;
1274 p->u.generic->p = srw_pdu;
1275 p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
1277 return z_soap_codec_enc(odr, &p,
1279 &hreq->content_len, handlers,
1286 * indent-tabs-mode: nil
1288 * vim: shiftwidth=4 tabstop=8 expandtab