+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;
+ char *cp;
+
+ 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";
+
+ cp = strchr(hreq->path, '#');
+ if (cp)
+ *cp = '\0';
+
+ path = (char *)
+ odr_malloc(encode, strlen(hreq->path) + strlen(uri_args) + 4);