X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Furl.c;h=5e699d98b7b9d052679a68561837b3a3bcb87e5b;hp=97e8247b5e9b42f755c79a6b499d55f3a939aa06;hb=9afc8581574131c6d3ecea36c85740c07714318c;hpb=6a149fd3f0dc901448205e1511e434054a51bfa6 diff --git a/src/url.c b/src/url.c index 97e8247..5e699d9 100644 --- a/src/url.c +++ b/src/url.c @@ -13,11 +13,14 @@ #include #include #include +#include struct yaz_url { ODR odr_in; ODR odr_out; char *proxy; + int max_redirects; + WRBUF w_error; }; yaz_url_t yaz_url_create(void) @@ -26,6 +29,8 @@ yaz_url_t yaz_url_create(void) p->odr_in = odr_createmem(ODR_DECODE); p->odr_out = odr_createmem(ODR_ENCODE); p->proxy = 0; + p->max_redirects = 10; + p->w_error = wrbuf_alloc(); return p; } @@ -36,6 +41,7 @@ void yaz_url_destroy(yaz_url_t p) odr_destroy(p->odr_in); odr_destroy(p->odr_out); xfree(p->proxy); + wrbuf_destroy(p->w_error); xfree(p); } } @@ -48,6 +54,11 @@ void yaz_url_set_proxy(yaz_url_t p, const char *proxy) p->proxy = xstrdup(proxy); } +void yaz_url_set_max_redirects(yaz_url_t p, int num) +{ + p->max_redirects = num; +} + static void extract_user_pass(NMEM nmem, const char *uri, char **uri_lean, char **http_user, @@ -86,6 +97,16 @@ static void extract_user_pass(NMEM nmem, *uri_lean = nmem_strdup(nmem, uri); } +const char *yaz_url_get_error(yaz_url_t p) +{ + return wrbuf_cstr(p->w_error); +} + +static void log_warn(yaz_url_t p) +{ + yaz_log(YLOG_WARN, "yaz_url: %s", wrbuf_cstr(p->w_error)); +} + Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri, const char *method, Z_HTTP_Header *user_headers, @@ -94,6 +115,7 @@ Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri, Z_HTTP_Response *res = 0; int number_of_redirects = 0; + wrbuf_rewind(p->w_error); while (1) { void *add; @@ -137,17 +159,21 @@ Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri, } if (!z_GDU(p->odr_out, &gdu, 0, 0)) { - yaz_log(YLOG_WARN, "Can not encode HTTP request URL:%s", uri); + wrbuf_printf(p->w_error, "Can not encode HTTP request for URL %s", + uri); + log_warn(p); return 0; } conn = cs_create_host_proxy(uri_lean, 1, &add, p->proxy); if (!conn) { - yaz_log(YLOG_WARN, "Could not resolve URL: %s", uri); + wrbuf_printf(p->w_error, "Can not resolve URL %s", uri); + log_warn(p); } else if (cs_connect(conn, add) < 0) { - yaz_log(YLOG_WARN, "Can not connect to URL: %s", uri); + wrbuf_printf(p->w_error, "Can not connect to URL %s", uri); + log_warn(p); } else { @@ -155,7 +181,10 @@ Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri, char *buf = odr_getbuf(p->odr_out, &len, 0); if (cs_put(conn, buf, len) < 0) - yaz_log(YLOG_WARN, "cs_put failed URL: %s", uri); + { + wrbuf_printf(p->w_error, "cs_put fail for URL %s", uri); + log_warn(p); + } else { char *netbuffer = 0; @@ -163,7 +192,8 @@ Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri, int cs_res = cs_get(conn, &netbuffer, &netlen); if (cs_res <= 0) { - yaz_log(YLOG_WARN, "cs_get failed URL: %s", uri); + wrbuf_printf(p->w_error, "cs_get failed for URL %s", uri); + log_warn(p); } else { @@ -172,8 +202,9 @@ Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri, if (!z_GDU(p->odr_in, &gdu, 0, 0) || gdu->which != Z_GDU_HTTP_Response) { - yaz_log(YLOG_WARN, "HTTP decoding failed " - "URL:%s", uri); + wrbuf_printf(p->w_error, "HTTP decoding fail for " + "URL %s", uri); + log_warn(p); } else { @@ -189,7 +220,7 @@ Z_HTTP_Response *yaz_url_exec(yaz_url_t p, const char *uri, break; code = res->code; location = z_HTTP_header_lookup(res->headers, "Location"); - if (++number_of_redirects < 10 && + if (++number_of_redirects <= p->max_redirects && location && (code == 301 || code == 302 || code == 307)) { odr_reset(p->odr_out);