+#if YAZ_HAVE_XML2
+static int send_gdu(Z_GDU *gdu)
+{
+ if (z_GDU(out, &gdu, 0, 0))
+ {
+ /* encode OK */
+ char *buf_out;
+ int len_out;
+ int r;
+ if (apdu_file)
+ {
+ if (!z_GDU(print, &gdu, 0, 0))
+ printf("Failed to print outgoing SRU package\n");
+ odr_reset(print);
+ }
+ buf_out = odr_getbuf(out, &len_out, 0);
+
+ /* we don't odr_reset(out), since we may need the buffer again */
+
+ do_hex_dump(buf_out, len_out);
+
+ r = cs_put(conn, buf_out, len_out);
+
+ if (r >= 0)
+ return 2;
+ }
+ return 0;
+}
+
+static int send_srw_host_path(Z_SRW_PDU *sr, const char *host_port,
+ char *path)
+{
+ const char *charset = negotiationCharset;
+ Z_GDU *gdu;
+
+ gdu = z_get_HTTP_Request_host_path(out, host_port, path);
+
+ if (auth)
+ {
+ if (auth->which == Z_IdAuthentication_open)
+ {
+ char **darray;
+ int num;
+ nmem_strsplit(out->mem, "/", auth->u.open, &darray, &num);
+ if (num >= 1)
+ sr->username = darray[0];
+ if (num >= 2)
+ sr->password = darray[1];
+ }
+ else if (auth->which == Z_IdAuthentication_idPass)
+ {
+ sr->username = auth->u.idPass->userId;
+ sr->password = auth->u.idPass->password;
+ }
+ }
+
+ if (!yaz_matchstr(sru_method, "get"))
+ {
+ yaz_sru_get_encode(gdu->u.HTTP_Request, sr, out, charset);
+ }
+ else if (!yaz_matchstr(sru_method, "post"))
+ {
+ yaz_sru_post_encode(gdu->u.HTTP_Request, sr, out, charset);
+ }
+ else if (!yaz_matchstr(sru_method, "soap"))
+ {
+ yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, out, charset);
+ }
+ else if (!yaz_matchstr(sru_method, "solr"))
+ {
+ yaz_solr_encode_request(gdu->u.HTTP_Request, sr, out, charset);
+ }
+
+ return send_gdu(gdu);
+}
+
+static int send_srw(Z_SRW_PDU *sr)
+{
+ char *path = yaz_encode_sru_dbpath_odr(out, databaseNames[0]);
+ return send_srw_host_path(sr, cur_host, path);
+}
+
+static int send_SRW_redirect(const char *uri, Z_HTTP_Response *cookie_hres)
+{
+ const char *username = 0;
+ const char *password = 0;
+ struct Z_HTTP_Header *h;
+ char *combined_cookies = 0;
+ int combined_cookies_len = 0;
+ Z_GDU *gdu = get_HTTP_Request_url(out, uri);
+
+ gdu->u.HTTP_Request->method = odr_strdup(out, "GET");
+ z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers, "Accept",
+ "text/xml");
+
+ for (h = cookie_hres->headers; h; h = h->next)
+ {
+ if (!strcmp(h->name, "Set-Cookie"))
+ {
+ char *cp;
+
+ if (!(cp = strchr(h->value, ';')))
+ cp = h->value + strlen(h->value);
+ if (cp - h->value >= 1)
+ {
+ combined_cookies = xrealloc(combined_cookies, combined_cookies_len + cp - h->value + 3);
+ memcpy(combined_cookies+combined_cookies_len, h->value, cp - h->value);
+ combined_cookies[combined_cookies_len + cp - h->value] = '\0';
+ strcat(combined_cookies,"; ");
+ combined_cookies_len = strlen(combined_cookies);
+ }
+ }
+ }
+ if (combined_cookies_len)
+ {
+ z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers, "Cookie", combined_cookies);
+ xfree(combined_cookies);
+ }
+
+ if (auth)
+ {
+ if (auth->which == Z_IdAuthentication_open)
+ {
+ char **darray;
+ int num;
+ nmem_strsplit(out->mem, "/", auth->u.open, &darray, &num);
+ if (num >= 1)
+ username = darray[0];
+ if (num >= 2)
+ password = darray[1];
+ }
+ else if (auth->which == Z_IdAuthentication_idPass)
+ {
+ username = auth->u.idPass->userId;
+ password = auth->u.idPass->password;
+ }
+ }
+
+ if (username && password)
+ {
+ z_HTTP_header_add_basic_auth(out, &gdu->u.HTTP_Request->headers,
+ username, password);
+ }
+
+ return send_gdu(gdu);
+}
+#endif
+
+#if YAZ_HAVE_XML2
+static char *encode_SRW_term(ODR o, const char *q)
+{
+ const char *in_charset = "ISO-8859-1";
+ WRBUF w = wrbuf_alloc();
+ yaz_iconv_t cd;
+ char *res;
+ if (outputCharset)
+ in_charset = outputCharset;
+ cd = yaz_iconv_open("UTF-8", in_charset);
+ if (!cd)
+ {
+ wrbuf_destroy(w);
+ return odr_strdup(o, q);
+ }
+ wrbuf_iconv_write(w, cd, q, strlen(q));
+ if (wrbuf_len(w))
+ res = odr_strdup(o, wrbuf_cstr(w));
+ else
+ res = odr_strdup(o, q);
+ yaz_iconv_close(cd);
+ wrbuf_destroy(w);
+ return res;
+}
+
+
+static int send_SRW_scanRequest(const char *arg, Odr_int *pos, int num)
+{
+ Z_SRW_PDU *sr = 0;
+
+ /* regular requestse .. */
+ sr = yaz_srw_get_pdu(out, Z_SRW_scan_request, sru_version);
+
+ switch (queryType)
+ {
+ case QueryType_CQL:
+ sr->u.scan_request->queryType = "cql";
+ sr->u.scan_request->scanClause = encode_SRW_term(out, arg);
+ break;
+ case QueryType_Prefix:
+ sr->u.scan_request->queryType = "pqf";
+ sr->u.scan_request->scanClause = encode_SRW_term(out, arg);
+ break;
+ default:
+ printf("Only CQL and PQF supported in SRW\n");
+ return 0;
+ }
+ sr->u.scan_request->responsePosition = pos;
+ sr->u.scan_request->maximumTerms = odr_intdup(out, num);
+ return send_srw(sr);
+}
+
+static int send_SRW_searchRequest(const char *arg)
+{
+ Z_SRW_PDU *sr = 0;
+
+ if (!srw_sr)
+ {
+ assert(srw_sr_odr_out == 0);
+ srw_sr_odr_out = odr_createmem(ODR_ENCODE);
+ }
+ odr_reset(srw_sr_odr_out);
+
+ setno = 1;
+
+ /* save this for later .. when fetching individual records */
+ srw_sr = yaz_srw_get_pdu(srw_sr_odr_out, Z_SRW_searchRetrieve_request,
+ sru_version);
+
+ /* regular request .. */
+ sr = yaz_srw_get_pdu(out, Z_SRW_searchRetrieve_request, sru_version);
+
+ switch (queryType)
+ {
+ case QueryType_CQL:
+ srw_sr->u.request->queryType = "cql";
+ srw_sr->u.request->query = encode_SRW_term(srw_sr_odr_out, arg);
+
+ sr->u.request->queryType = "cql";
+ sr->u.request->query = encode_SRW_term(srw_sr_odr_out, arg);
+ break;
+ case QueryType_Prefix:
+ srw_sr->u.request->queryType = "pqf";
+ srw_sr->u.request->query = encode_SRW_term(srw_sr_odr_out, arg);
+
+ sr->u.request->queryType = "pqf";
+ sr->u.request->query = encode_SRW_term(srw_sr_odr_out, arg);
+ break;
+ default:
+ printf("Only CQL and PQF supported in SRW\n");
+ return 0;
+ }
+ if (*sru_recordPacking)
+ sr->u.request->recordPacking = sru_recordPacking;
+ sru_maximumRecords = 0;
+ sr->u.request->maximumRecords = odr_intdup(out, 0);
+ sr->u.request->facetList = facet_list;
+ if (record_schema)
+ sr->u.request->recordSchema = record_schema;
+ if (recordsyntax_size == 1 && !yaz_matchstr(recordsyntax_list[0], "xml"))
+ sr->u.request->recordPacking = "xml";
+ return send_srw(sr);
+}
+#endif
+
+static void query_charset_convert(Z_RPNQuery *q)
+{
+ if (queryCharset && outputCharset)
+ {
+ yaz_iconv_t cd = yaz_iconv_open(queryCharset, outputCharset);
+ if (!cd)
+ {
+ printf("Conversion from %s to %s unsupported\n",
+ outputCharset, queryCharset);
+ return;
+ }
+ yaz_query_charset_convert_rpnquery(q, out, cd);
+ yaz_iconv_close(cd);
+ }
+}
+
+static int send_Z3950_searchRequest(const char *arg)