From a12a16151aa33dfd5e7627d4bfc2dc1673a7bf8e Mon Sep 17 00:00:00 2001 From: Jay Satiro Date: Thu, 23 Jul 2020 03:16:14 -0400 Subject: [PATCH] url: fix CURLU and location following Prior to this change if the user set a URL handle (CURLOPT_CURLU) it was incorrectly used for the location follow, resulting in infinite requests to the original location. Reported-by: sspiri@users.noreply.github.com Fixes https://github.com/curl/curl/issues/5709 Closes https://github.com/curl/curl/pull/5713 --- lib/url.c | 5 +-- tests/data/Makefile.inc | 2 +- tests/data/test1567 | 73 ++++++++++++++++++++++++++++++++++++++ tests/libtest/Makefile.inc | 5 ++- tests/libtest/lib1567.c | 52 +++++++++++++++++++++++++++ 5 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 tests/data/test1567 create mode 100644 tests/libtest/lib1567.c diff --git a/lib/url.c b/lib/url.c index caeb429a50..a98aab27f2 100644 --- a/lib/url.c +++ b/lib/url.c @@ -1836,11 +1836,12 @@ static CURLcode parseurlandfillconn(struct Curl_easy *data, CURLU *uh; CURLUcode uc; char *hostname; + bool use_set_uh = (data->set.uh && !data->state.this_is_a_follow); up_free(data); /* cleanup previous leftovers first */ /* parse the URL */ - if(data->set.uh) { + if(use_set_uh) { uh = data->state.uh = curl_url_dup(data->set.uh); } else { @@ -1863,7 +1864,7 @@ static CURLcode parseurlandfillconn(struct Curl_easy *data, data->change.url_alloc = TRUE; } - if(!data->set.uh) { + if(!use_set_uh) { char *newurl; uc = curl_url_set(uh, CURLUPART_URL, data->change.url, CURLU_GUESS_SCHEME | diff --git a/tests/data/Makefile.inc b/tests/data/Makefile.inc index e5dfbfde4a..3752502ef5 100644 --- a/tests/data/Makefile.inc +++ b/tests/data/Makefile.inc @@ -189,7 +189,7 @@ test1540 test1541 \ \ test1550 test1551 test1552 test1553 test1554 test1555 test1556 test1557 \ test1558 test1559 test1560 test1561 test1562 test1563 test1564 test1565 \ -test1566 \ +test1566 test1567 \ \ test1590 test1591 test1592 test1593 test1594 test1595 test1596 \ \ diff --git a/tests/data/test1567 b/tests/data/test1567 new file mode 100644 index 0000000000..088ebf4971 --- /dev/null +++ b/tests/data/test1567 @@ -0,0 +1,73 @@ + + + +HTTP +URL API +CURLOPT_CURLU +CURLOPT_FOLLOWLOCATION + + + +# Server-side + + +HTTP/1.1 302 OK +Content-Length: 6 +Location: /15670002 + +-foo- + + +HTTP/1.1 200 OK +Content-Length: 11 + +redirected + + +redirected +redirected + + + +# Client-side + + +http + +# tool is what to use instead of 'curl' + +lib1567 + + + +re-run redirected transfer without setting CURLU URL again + + +http://%HOSTIP:%HTTPPORT/1567 + + + + + +^User-Agent:.* + + +GET /1567 HTTP/1.1 +Host: %HOSTIP:%HTTPPORT +Accept: */* + +GET /15670002 HTTP/1.1 +Host: %HOSTIP:%HTTPPORT +Accept: */* + +GET /1567 HTTP/1.1 +Host: %HOSTIP:%HTTPPORT +Accept: */* + +GET /15670002 HTTP/1.1 +Host: %HOSTIP:%HTTPPORT +Accept: */* + + + + diff --git a/tests/libtest/Makefile.inc b/tests/libtest/Makefile.inc index 8ad7dc4e60..2c9c66539b 100644 --- a/tests/libtest/Makefile.inc +++ b/tests/libtest/Makefile.inc @@ -56,7 +56,7 @@ noinst_PROGRAMS = chkhostname libauthretry libntlmconnect \ lib1534 lib1535 lib1536 lib1537 lib1538 lib1539 \ lib1540 lib1541 \ lib1550 lib1551 lib1552 lib1553 lib1554 lib1555 lib1556 lib1557 \ - lib1558 lib1559 lib1560 lib1564 lib1565 \ + lib1558 lib1559 lib1560 lib1564 lib1565 lib1567 \ lib1591 lib1592 lib1593 lib1594 lib1596 \ lib1900 lib1905 lib1906 lib1907 lib1908 lib1910 \ lib2033 lib3010 @@ -604,6 +604,9 @@ lib1565_SOURCES = lib1565.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS) lib1565_LDADD = $(TESTUTIL_LIBS) lib1565_CPPFLAGS = $(AM_CPPFLAGS) +lib1567_SOURCES = lib1567.c $(SUPPORTFILES) +lib1567_CPPFLAGS = $(AM_CPPFLAGS) + lib1591_SOURCES = lib1591.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS) lib1591_LDADD = $(TESTUTIL_LIBS) lib1591_CPPFLAGS = $(AM_CPPFLAGS) -DLIB1591 diff --git a/tests/libtest/lib1567.c b/tests/libtest/lib1567.c new file mode 100644 index 0000000000..30fd7c3840 --- /dev/null +++ b/tests/libtest/lib1567.c @@ -0,0 +1,52 @@ +/*************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * Copyright (C) 1998 - 2017, Daniel Stenberg, , et al. + * + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at https://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ***************************************************************************/ +#include "test.h" + +#include "memdebug.h" + +#include + +int test(char *URL) +{ + CURL *curl; + CURLcode res = CURLE_OK; + + global_init(CURL_GLOBAL_ALL); + curl = curl_easy_init(); + if(curl) { + CURLU *u = curl_url(); + if(u) { + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); + curl_url_set(u, CURLUPART_URL, URL, 0); + curl_easy_setopt(curl, CURLOPT_CURLU, u); + res = curl_easy_perform(curl); + + fprintf(stderr, "****************************** Do it again\n"); + res = curl_easy_perform(curl); + curl_url_cleanup(u); + } + curl_easy_cleanup(curl); + } + curl_global_cleanup(); + return (int)res; +} -- 2.47.3