- z_HTTP_header_add_basic_auth(out, &gdu->u.HTTP_Request->headers,
- wrbuf_cstr(username),
- wrbuf_cstr(password));
- }
- z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers, "Accept",
- "application/xml");
- if (!z_GDU(out, &gdu, 0, 0))
- {
- yaz_log(YLOG_WARN, "Can not encode HTTP request URL:%s", uri);
- break;
- }
- void *add;
- COMSTACK conn = cs_create_host(uri, 1, &add);
- if (!conn)
- yaz_log(YLOG_WARN, "Bad address for URL:%s", uri);
- else if (cs_connect(conn, add) < 0)
- yaz_log(YLOG_WARN, "Can not connect to URL:%s", uri);
- else
- {
- int len;
- char *buf = odr_getbuf(out, &len, 0);
-
- if (cs_put(conn, buf, len) < 0)
- yaz_log(YLOG_WARN, "cs_put failed URL:%s", uri);
- else
- {
- char *netbuffer = 0;
- int netlen = 0;
- int cs_res = cs_get(conn, &netbuffer, &netlen);
- if (cs_res <= 0)
- {
- yaz_log(YLOG_WARN, "cs_get failed URL:%s", uri);
- }
- else
- {
- Z_GDU *gdu;
- odr_setbuf(in, netbuffer, cs_res, 0);
- if (!z_GDU(in, &gdu, 0, 0)
- || gdu->which != Z_GDU_HTTP_Response)
- {
- yaz_log(YLOG_WARN, "HTTP decoding failed "
- "URL:%s", uri);
- }
- else
- {
- res = gdu->u.HTTP_Response;
- }
- }
- xfree(netbuffer);
- }
- cs_close(conn);
- }
- if (!res)
- break; // ERROR
- *code = res->code;
- location = z_HTTP_header_lookup(res->headers, "Location");
- if (++number_of_redirects < 10 &&
- location && (*code == 301 || *code == 302 || *code == 307))
- {
- odr_reset(out);
- uri = odr_strdup(out, location);
- odr_reset(in);
- }
- else
- {
- result = wrbuf_alloc();
- wrbuf_write(result, res->content_buf, res->content_len);
- break;