Skip to content

curl_easy_nextheader returns null on all subsequent requests after having followed a redirect #9424

Closed
@charonn0

Description

@charonn0

I did this

I'm calling curl_easy_nextheader in a loop to count the number of requests in the previous transfer. This works until a transfer involves HTTP redirection. After the redirected request completes, curl_easy_nextheader will work as expected. However, on all subsequent requests curl_easy_nextheader will return null if the request parameter is greater than -1.

I expected the following

That curl_easy_nextheader would not be affected by previous transfers.

curl/libcurl version

libcurl/7.85.0 OpenSSL/3.0.5 (Schannel) zlib/1.2.12 brotli/1.0.9 zstd/1.5.2 libidn2/2.3.3 libssh2/1.10.0 nghttp2/1.49.0 ngtcp2/0.8.0 nghttp3/0.7.0 libgsasl/1.10.0

operating system

Windows 10 build 19044.1889

Activity

bagder

bagder commented on Sep 4, 2022

@bagder
Member

Thanks a lot for your report!

Do you perhaps have a small stand-alone program we can run from our ends against a public URL to trigger the problem?

charonn0

charonn0 commented on Sep 7, 2022

@charonn0
ContributorAuthor

My knowledge of C is limited, but I cobbled together something. I get linker errors when I try to run it, so I'm not 100% sure it's correct.

#include "curl.h"
 
int main(void)
{
  CURL *curl;
  CURLcode res;
 
  curl = curl_easy_init();
  if(curl) {

    /*perform a request that involves redirection*/
    curl_easy_setopt(curl, CURLOPT_URL, "https://nghttp2.org/httpbin/redirect/1");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    res = curl_easy_perform(curl);
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

    /*count the number of requests by reading the first header of each request. Works as expected. */
    struct curl_header *h;
    int count = 0;
    int origins = (CURLH_HEADER|CURLH_TRAILER|CURLH_CONNECT|CURLH_1XX|CURLH_PSEUDO);
    while((h = curl_easy_nextheader(curl, origins, count, NULL))) {
        count++;
    }
    printf("count =%u\n",count ); /* count=2 */

    /*perform another request.*/
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com");
    res = curl_easy_perform(curl);
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

    /*count the number of requests again. Always get NULL back. */
    count = 0;
    while((h = curl_easy_nextheader(curl, origins, count, NULL))) {
        count++;
    }
    printf("count =%u\n",count );/* count=0 */
    curl_easy_cleanup(curl);
  }
  return 0;
}
bagder

bagder commented on Sep 7, 2022

@bagder
Member

Excellent, I can reproduce what you have reported with this.

self-assigned this
on Sep 7, 2022
added a commit that references this issue on Sep 7, 2022
9cd2b02
added a commit that references this issue on Sep 9, 2022
b6ca549

1 remaining item

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    Participants

    @bagder@charonn0

    Issue actions

      curl_easy_nextheader returns null on all subsequent requests after having followed a redirect · Issue #9424 · curl/curl