From 94140e7ec2de8ecd8c6f3715121b0502f469c003 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 2 Jul 2013 10:34:18 +0200 Subject: [PATCH] Allow max HTTP redirects to be controlled YAZ-667 --- doc/yaz-url-man.xml | 9 +++++++++ include/yaz/url.h | 6 ++++++ src/url.c | 9 ++++++++- util/yaz-url.c | 6 +++++- 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/doc/yaz-url-man.xml b/doc/yaz-url-man.xml index 65cd4ef..c98c467 100644 --- a/doc/yaz-url-man.xml +++ b/doc/yaz-url-man.xml @@ -33,6 +33,7 @@ -m method -O fname -p fname + -R num -u user/password -x proxy url @@ -87,6 +88,14 @@ + -R num + + Sets maximum number of HTTP redirects to be followed. + A value of zero disables follow of HTTP redirects. + + + + -u user/password Specifies a user and a password to be uesd in HTTP diff --git a/include/yaz/url.h b/include/yaz/url.h index aa5fd4d..0deb4db 100644 --- a/include/yaz/url.h +++ b/include/yaz/url.h @@ -60,6 +60,12 @@ YAZ_EXPORT void yaz_url_destroy(yaz_url_t p); */ YAZ_EXPORT void yaz_url_set_proxy(yaz_url_t p, const char *proxy); +/** \brief sets maximum number of redirects + \param p handle + \param num maximum number of redirects +*/ +YAZ_EXPORT void yaz_url_set_max_redirects(yaz_url_t p, int num); + /** \brief executes the actual HTTP request (including redirects, etc) \param p handle \param uri URL diff --git a/src/url.c b/src/url.c index 97e8247..ff7ff81 100644 --- a/src/url.c +++ b/src/url.c @@ -18,6 +18,7 @@ struct yaz_url { ODR odr_in; ODR odr_out; char *proxy; + int max_redirects; }; yaz_url_t yaz_url_create(void) @@ -26,6 +27,7 @@ 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; return p; } @@ -48,6 +50,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, @@ -189,7 +196,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); diff --git a/util/yaz-url.c b/util/yaz-url.c index 498e2f8..54551dd 100644 --- a/util/yaz-url.c +++ b/util/yaz-url.c @@ -21,6 +21,7 @@ static void usage(void) printf(" -m method Sets HTTP method\n"); printf(" -O fname Writes HTTP content to file\n"); printf(" -p fname POSTs file at following url\n"); + printf(" -R num Set maximum number of HTTP redirects\n"); printf(" -u user/password Sets Basic HTTP auth\n"); printf(" -x proxy Sets HTTP proxy\n"); exit(1); @@ -72,7 +73,7 @@ int main(int argc, char **argv) int no_urls = 0; const char *outfname = 0; - while ((ret = options("hH:m:O:p:u:x:", argv, argc, &arg)) + while ((ret = options("h{help}H:m:O:p:R{max-redirs}:u:x:", argv, argc, &arg)) != YAZ_OPTIONS_EOF) { switch (ret) @@ -109,6 +110,9 @@ int main(int argc, char **argv) post_buf = get_file(arg, &post_len); method = "POST"; break; + case 'R': + yaz_url_set_max_redirects(p, atoi(arg)); + break; case 'u': if (strchr(arg, '/')) { -- 1.7.10.4