+
+Z_SRW_extra_record *yaz_srw_get_extra_record(ODR o)
+{
+ Z_SRW_extra_record *res = (Z_SRW_extra_record *)
+ odr_malloc(o, sizeof(*res));
+
+ res->extraRecordData_buf = 0;
+ res->extraRecordData_len = 0;
+ res->recordIdentifier = 0;
+ return res;
+}
+
+
+Z_SRW_record *yaz_srw_get_records(ODR o, int n)
+{
+ Z_SRW_record *res = (Z_SRW_record *) odr_malloc(o, n * sizeof(*res));
+ int i;
+
+ for (i = 0; i<n; i++)
+ {
+ res[i].recordSchema = 0;
+ res[i].recordPacking = Z_SRW_recordPacking_string;
+ res[i].recordData_buf = 0;
+ res[i].recordData_len = 0;
+ res[i].recordPosition = 0;
+ }
+ return res;
+}
+
+Z_SRW_record *yaz_srw_get_record(ODR o)
+{
+ return yaz_srw_get_records(o, 1);
+}
+
+static Z_SRW_PDU *yaz_srw_get_core_ver(ODR o, const char *version)
+{
+ Z_SRW_PDU *p = (Z_SRW_PDU *) odr_malloc(o, sizeof(*p));
+ p->srw_version = odr_strdup(o, version);
+ p->username = 0;
+ p->password = 0;
+ p->extra_args = 0;
+ p->extraResponseData_buf = 0;
+ p->extraResponseData_len = 0;
+ return p;
+}
+
+Z_SRW_PDU *yaz_srw_get_core_v_2_0(ODR o)
+{
+ return yaz_srw_get_core_ver(o, "2.0");
+}
+
+Z_SRW_PDU *yaz_srw_get(ODR o, int which)
+{
+ return yaz_srw_get_pdu(o, which, "2.0");
+}
+
+Z_SRW_PDU *yaz_srw_get_pdu(ODR o, int which, const char *version)
+{
+ Z_SRW_PDU *sr = yaz_srw_get_core_ver(o, version);
+
+ sr->which = which;
+ switch(which)
+ {
+ case Z_SRW_searchRetrieve_request:
+ sr->u.request = (Z_SRW_searchRetrieveRequest *)
+ odr_malloc(o, sizeof(*sr->u.request));
+ sr->u.request->queryType = "cql";
+ sr->u.request->query = 0;
+ sr->u.request->sort_type = Z_SRW_sort_type_none;
+ sr->u.request->sort.none = 0;
+ sr->u.request->startRecord = 0;
+ sr->u.request->maximumRecords = 0;
+ sr->u.request->recordSchema = 0;
+ sr->u.request->recordPacking = 0;
+ sr->u.request->packing = 0;
+ sr->u.request->recordXPath = 0;
+ sr->u.request->database = 0;
+ sr->u.request->resultSetTTL = 0;
+ sr->u.request->stylesheet = 0;
+ sr->u.request->facetList = 0;
+ break;
+ case Z_SRW_searchRetrieve_response:
+ sr->u.response = (Z_SRW_searchRetrieveResponse *)
+ odr_malloc(o, sizeof(*sr->u.response));
+ sr->u.response->numberOfRecords = 0;
+ sr->u.response->resultCountPrecision = 0;
+ sr->u.response->resultSetId = 0;
+ sr->u.response->resultSetIdleTime = 0;
+ sr->u.response->records = 0;
+ sr->u.response->num_records = 0;
+ sr->u.response->diagnostics = 0;
+ sr->u.response->num_diagnostics = 0;
+ sr->u.response->nextRecordPosition = 0;
+ sr->u.response->extra_records = 0;
+ sr->u.response->facetList = 0;
+ sr->u.response->suggestions = 0;
+ break;
+ case Z_SRW_explain_request:
+ sr->u.explain_request = (Z_SRW_explainRequest *)
+ odr_malloc(o, sizeof(*sr->u.explain_request));
+ sr->u.explain_request->recordPacking = 0;
+ sr->u.explain_request->packing = 0;
+ sr->u.explain_request->database = 0;
+ sr->u.explain_request->stylesheet = 0;
+ break;
+ case Z_SRW_explain_response:
+ sr->u.explain_response = (Z_SRW_explainResponse *)
+ odr_malloc(o, sizeof(*sr->u.explain_response));
+ sr->u.explain_response->record.recordData_buf = 0;
+ sr->u.explain_response->record.recordData_len = 0;
+ sr->u.explain_response->record.recordSchema = 0;
+ sr->u.explain_response->record.recordPosition = 0;
+ sr->u.explain_response->record.recordPacking =
+ Z_SRW_recordPacking_string;
+ sr->u.explain_response->diagnostics = 0;
+ sr->u.explain_response->num_diagnostics = 0;
+ sr->u.explain_response->extra_record = 0;
+ break;
+ case Z_SRW_scan_request:
+ sr->u.scan_request = (Z_SRW_scanRequest *)
+ odr_malloc(o, sizeof(*sr->u.scan_request));
+ sr->u.scan_request->database = 0;
+ sr->u.scan_request->stylesheet = 0;
+ sr->u.scan_request->maximumTerms = 0;
+ sr->u.scan_request->responsePosition = 0;
+ sr->u.scan_request->queryType = "cql";
+ sr->u.scan_request->scanClause = 0;
+ break;
+ case Z_SRW_scan_response:
+ sr->u.scan_response = (Z_SRW_scanResponse *)
+ odr_malloc(o, sizeof(*sr->u.scan_response));
+ sr->u.scan_response->terms = 0;
+ sr->u.scan_response->num_terms = 0;
+ sr->u.scan_response->diagnostics = 0;
+ sr->u.scan_response->num_diagnostics = 0;
+ break;
+ case Z_SRW_update_request:
+ sr->u.update_request = (Z_SRW_updateRequest *)
+ odr_malloc(o, sizeof(*sr->u.update_request));
+ sr->u.update_request->database = 0;
+ sr->u.update_request->stylesheet = 0;
+ sr->u.update_request->record = 0;
+ sr->u.update_request->recordId = 0;
+ sr->u.update_request->recordVersions = 0;
+ sr->u.update_request->num_recordVersions = 0;
+ sr->u.update_request->extra_record = 0;
+ sr->u.update_request->extraRequestData_buf = 0;
+ sr->u.update_request->extraRequestData_len = 0;
+ sr->u.request->database = 0;
+ break;
+ case Z_SRW_update_response:
+ sr->u.update_response = (Z_SRW_updateResponse *)
+ odr_malloc(o, sizeof(*sr->u.update_response));
+ sr->u.update_response->operationStatus = 0;
+ sr->u.update_response->recordId = 0;
+ sr->u.update_response->recordVersions = 0;
+ sr->u.update_response->num_recordVersions = 0;
+ sr->u.update_response->record = 0;
+ sr->u.update_response->extra_record = 0;
+ sr->u.update_response->extraResponseData_buf = 0;
+ sr->u.update_response->extraResponseData_len = 0;
+ sr->u.update_response->diagnostics = 0;
+ sr->u.update_response->num_diagnostics = 0;
+ }
+ return sr;
+}
+
+void yaz_add_name_value_int(ODR o, char **name, char **value, int *i,
+ char *a_name, Odr_int *val)
+{
+ if (val)
+ {
+ name[*i] = a_name;
+ value[*i] = (char *) odr_malloc(o, 40);
+ sprintf(value[*i], ODR_INT_PRINTF, *val);
+ (*i)++;
+ }
+}
+
+void yaz_add_name_value_str(ODR o, char **name, char **value, int *i,
+ char *a_name, char *val)
+{
+ if (val)
+ {
+ name[*i] = a_name;
+ value[*i] = val;
+ (*i)++;
+ }
+}
+
+static int yaz_get_sru_parms(const Z_SRW_PDU *srw_pdu, ODR encode,
+ char **name, char **value, int max_names)
+{
+ int version2 = strcmp(srw_pdu->srw_version, "2.") > 0;
+ int i = 0;
+ char *queryType;
+ yaz_add_name_value_str(encode, name, value, &i, "version",
+ srw_pdu->srw_version);
+ name[i] = "operation";
+ switch (srw_pdu->which)
+ {
+ case Z_SRW_searchRetrieve_request:
+ value[i++] = "searchRetrieve";
+ queryType = srw_pdu->u.request->queryType;
+ if (version2)
+ {
+ if (queryType && strcmp(queryType, "cql"))
+ yaz_add_name_value_str(encode, name, value, &i, "queryType",
+ queryType);
+ yaz_add_name_value_str(encode, name, value, &i, "query",
+ srw_pdu->u.request->query);
+ }
+ else
+ {
+ if (!strcmp(queryType, "cql"))
+ {
+ yaz_add_name_value_str(encode, name, value, &i, "query",
+ srw_pdu->u.request->query);
+ }
+ else if (!strcmp(queryType, "pqf"))
+ {
+ yaz_add_name_value_str(encode, name, value, &i, "x-pquery",
+ srw_pdu->u.request->query);
+ }
+ else if (!strcmp(queryType, "xcql"))
+ {
+ yaz_add_name_value_str(encode, name, value, &i, "x-cql",
+ srw_pdu->u.request->query);
+ }
+ }
+ switch (srw_pdu->u.request->sort_type)
+ {
+ case Z_SRW_sort_type_none:
+ break;
+ case Z_SRW_sort_type_sort:
+ yaz_add_name_value_str(encode, name, value, &i, "sortKeys",
+ srw_pdu->u.request->sort.sortKeys);
+ break;
+ }
+ yaz_add_name_value_int(encode, name, value, &i, "startRecord",
+ srw_pdu->u.request->startRecord);
+ yaz_add_name_value_int(encode, name, value, &i, "maximumRecords",
+ srw_pdu->u.request->maximumRecords);
+ yaz_add_name_value_str(encode, name, value, &i, "recordSchema",
+ srw_pdu->u.request->recordSchema);
+ if (version2)
+ {
+ yaz_add_name_value_str(encode, name, value, &i, "recordXMLEscaping",
+ srw_pdu->u.request->recordPacking);
+ yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
+ srw_pdu->u.request->packing);
+ }
+ else
+ yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
+ srw_pdu->u.request->recordPacking);
+ yaz_add_name_value_str(encode, name, value, &i, "recordXPath",
+ srw_pdu->u.request->recordXPath);
+ yaz_add_name_value_str(encode, name, value, &i, "stylesheet",
+ srw_pdu->u.request->stylesheet);
+ yaz_add_name_value_int(encode, name, value, &i, "resultSetTTL",
+ srw_pdu->u.request->resultSetTTL);
+ {
+ const char *facetLimit = 0;
+ const char *facetStart = 0;
+ const char *facetSort = 0;
+ yaz_sru_facet_request(encode, &srw_pdu->u.request->facetList,
+ &facetLimit, &facetStart, &facetSort);
+ yaz_add_name_value_str(encode, name, value, &i, "facetLimit",
+ (char *) facetLimit);
+ yaz_add_name_value_str(encode, name, value, &i, "facetStart",
+ (char *) facetStart);
+ yaz_add_name_value_str(encode, name, value, &i, "facetSort",
+ (char *) facetSort);
+ }
+ break;
+ case Z_SRW_explain_request:
+ value[i++] = "explain";
+
+ if (version2)
+ {
+ yaz_add_name_value_str(encode, name, value, &i, "recordXMLEscaping",
+ srw_pdu->u.explain_request->recordPacking);
+ yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
+ srw_pdu->u.explain_request->packing);
+ }
+ else
+ yaz_add_name_value_str(encode, name, value, &i, "recordPacking",
+ srw_pdu->u.explain_request->recordPacking);
+ yaz_add_name_value_str(encode, name, value, &i, "stylesheet",
+ srw_pdu->u.explain_request->stylesheet);
+ break;
+ case Z_SRW_scan_request:
+ value[i++] = "scan";
+ queryType = srw_pdu->u.scan_request->queryType;
+ if (version2)
+ {
+ if (queryType && strcmp(queryType, "cql"))
+ yaz_add_name_value_str(encode, name, value, &i, "queryType",
+ queryType);
+ yaz_add_name_value_str(encode, name, value, &i, "scanClause",
+ srw_pdu->u.scan_request->scanClause);
+ }
+ else
+ {
+ if (!queryType || !strcmp(queryType, "cql"))
+ yaz_add_name_value_str(encode, name, value, &i, "scanClause",
+ srw_pdu->u.scan_request->scanClause);
+ else if (!strcmp(queryType, "pqf"))
+ yaz_add_name_value_str(encode, name, value, &i, "x-pScanClause",
+ srw_pdu->u.scan_request->scanClause);
+ else if (!strcmp(queryType, "xcql"))
+ yaz_add_name_value_str(encode, name, value, &i,
+ "x-cqlScanClause",
+ srw_pdu->u.scan_request->scanClause);
+ }
+ yaz_add_name_value_int(encode, name, value, &i, "responsePosition",
+ srw_pdu->u.scan_request->responsePosition);
+ yaz_add_name_value_int(encode, name, value, &i, "maximumTerms",
+ srw_pdu->u.scan_request->maximumTerms);
+ yaz_add_name_value_str(encode, name, value, &i, "stylesheet",
+ srw_pdu->u.scan_request->stylesheet);
+ break;
+ case Z_SRW_update_request:
+ value[i++] = "update";
+ break;
+ default:
+ return -1;
+ }
+ if (srw_pdu->extra_args)
+ {
+ Z_SRW_extra_arg *ea = srw_pdu->extra_args;
+ for (; ea && i < max_names-1; ea = ea->next)
+ {
+ name[i] = ea->name;
+ value[i] = ea->value;
+ i++;
+ }
+ }
+ name[i++] = 0;
+
+ return 0;
+}
+
+int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
+ ODR encode, const char *charset)
+{
+ char *name[MAX_SRU_PARAMETERS], *value[MAX_SRU_PARAMETERS]; /* definite upper limit for SRU params */
+ char *uri_args;
+ char *path;
+
+ z_HTTP_header_add_basic_auth(encode, &hreq->headers,
+ srw_pdu->username, srw_pdu->password);
+ if (yaz_get_sru_parms(srw_pdu, encode, name, value, MAX_SRU_PARAMETERS))
+ return -1;
+ yaz_array_to_uri(&uri_args, encode, name, value);
+
+ hreq->method = "GET";
+
+ path = (char *)
+ odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4);
+
+ sprintf(path, "%s?%s", hreq->path, uri_args);
+ hreq->path = path;
+
+ z_HTTP_header_add_content_type(encode, &hreq->headers,
+ "text/xml", charset);
+ return 0;
+}
+
+int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
+ ODR encode, const char *charset)
+{
+ char *name[MAX_SRU_PARAMETERS], *value[MAX_SRU_PARAMETERS]; /* definite upper limit for SRU params */
+ char *uri_args;
+
+ z_HTTP_header_add_basic_auth(encode, &hreq->headers,
+ srw_pdu->username, srw_pdu->password);
+ if (yaz_get_sru_parms(srw_pdu, encode, name, value, MAX_SRU_PARAMETERS))
+ return -1;
+
+ yaz_array_to_uri(&uri_args, encode, name, value);
+
+ hreq->method = "POST";
+
+ hreq->content_buf = uri_args;
+ hreq->content_len = strlen(uri_args);
+
+ z_HTTP_header_add_content_type(encode, &hreq->headers,
+ "application/x-www-form-urlencoded",
+ charset);
+ return 0;
+}
+
+int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
+ ODR odr, const char *charset)
+{
+ Z_SOAP_Handler handlers[3] = {
+#if YAZ_HAVE_XML2
+ {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
+ {YAZ_XMLNS_UPDATE_v0_9, 0, (Z_SOAP_fun) yaz_ucp_codec},
+#endif
+ {0, 0, 0}
+ };
+ Z_SOAP *p = (Z_SOAP*) odr_malloc(odr, sizeof(*p));
+
+ z_HTTP_header_add_basic_auth(odr, &hreq->headers,
+ srw_pdu->username, srw_pdu->password);
+ z_HTTP_header_add_content_type(odr,
+ &hreq->headers,
+ "text/xml", charset);
+
+ z_HTTP_header_add(odr, &hreq->headers,
+ "SOAPAction", "\"\"");
+ p->which = Z_SOAP_generic;
+ p->u.generic = (Z_SOAP_Generic *) odr_malloc(odr, sizeof(*p->u.generic));
+ p->u.generic->no = 0;
+ p->u.generic->ns = 0;
+ p->u.generic->p = srw_pdu;
+ p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
+
+#if YAZ_HAVE_XML2
+ if (srw_pdu->which == Z_SRW_update_request ||
+ srw_pdu->which == Z_SRW_update_response)
+ p->u.generic->no = 1; /* second handler */
+#endif
+ return z_soap_codec_enc(odr, &p,
+ &hreq->content_buf,
+ &hreq->content_len, handlers,
+ charset);
+}
+
+Z_SRW_recordVersion *yaz_srw_get_record_versions(ODR odr, int num)
+{
+ Z_SRW_recordVersion *ver
+ = (Z_SRW_recordVersion *) odr_malloc(odr,num * sizeof(*ver));
+ int i;
+ for (i = 0; i < num; ++i)
+ {
+ ver[i].versionType = 0;
+ ver[i].versionValue = 0;
+ }
+ return ver;
+}
+
+const char *yaz_srw_pack_to_str(int pack)
+{
+ switch(pack)
+ {
+ case Z_SRW_recordPacking_string:
+ return "string";
+ case Z_SRW_recordPacking_XML:
+ return "xml";
+ case Z_SRW_recordPacking_URL:
+ return "url";
+ }
+ return 0;
+}
+
+int yaz_srw_str_to_pack(const char *str)
+{
+ if (!yaz_matchstr(str, "string"))
+ return Z_SRW_recordPacking_string;
+ if (!yaz_matchstr(str, "xml"))
+ return Z_SRW_recordPacking_XML;
+ if (!yaz_matchstr(str, "url"))
+ return Z_SRW_recordPacking_URL;
+ return -1;
+}
+
+void yaz_encode_sru_extra(Z_SRW_PDU *sr, ODR odr, const char *extra_args)
+{
+ if (extra_args)
+ {
+ char **name;
+ char **val;
+ Z_SRW_extra_arg **ea = &sr->extra_args;
+ yaz_uri_to_array(extra_args, odr, &name, &val);
+
+ while (*name)
+ {
+ *ea = (Z_SRW_extra_arg *) odr_malloc(odr, sizeof(**ea));
+ (*ea)->name = *name;
+ (*ea)->value = *val;
+ ea = &(*ea)->next;
+ val++;
+ name++;
+ }
+ *ea = 0;
+ }
+}
+
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+