At the moment if you pass in 'max_redirects' => 0 in the array of options to the HTTP request, then cURL will return an error on any redirect, this is because CURLOPT_FOLLOWLOCATION is unconditionally set to TRUE, but then CURLOPT_MAXREDIRS will get set to 0, thus, if cURL encounters a redirect, instead of returning the response, it returns an error.

Test case:


$response = chr_curl_http_request('https://httpstat.us/307', array(
  'max_redirects' => 0,
));
print_r($response);

Actual response

stdClass Object
(
    [data] => 
    [errno] => 47
    [error] => Maximum (0) redirects followed
    [code] => 47
)

Expected

stdClass Object
(
    [data] => 307 Temporary Redirect
    [errno] => 0
    [redirect_url] => https://httpstat.us/307
    [request] => GET /307 HTTP/1.1
User-Agent: Drupal (+http://drupal.org/)
Accept: */*
Host: httpstat.us:443


    [protocol] => HTTP/1.1
    [status_message] => Temporary Redirect
    [headers] => Array
        (
            [cache-control] => private
            [content-length] => 22
            [content-type] => text/plain; charset=utf-8
            [location] => https://httpstat.us
            [server] => Microsoft-IIS/10.0
            [x-aspnetmvc-version] => 5.1
            [access-control-allow-origin] => *
            [x-aspnet-version] => 4.0.30319
            [x-powered-by] => ASP.NET
            [set-cookie] => ARRAffinity=3ade9fb19a6116ff5d90eba50cf35f1c1c09a59a7a509280d24030596e423ef2;Path=/;HttpOnly;Domain=httpstat.us:443
            [date] => Fri, 08 Feb 2019 10:06:10 GMT
        )

    [code] => 307
    [curl_info] => Array
        (
            [url] => https://httpstat.us/307
            [content_type] => text/plain; charset=utf-8
            [http_code] => 307
            [header_size] => 455
            [request_size] => 99
            [filetime] => -1
            [ssl_verify_result] => 0
            [redirect_count] => 0
            [total_time] => 0.636043
            [namelookup_time] => 0.028996
            [connect_time] => 0.17835
            [pretransfer_time] => 0.48396
            [size_upload] => 0
            [size_download] => 22
            [speed_download] => 34
            [speed_upload] => 0
            [download_content_length] => 22
            [upload_content_length] => 0
            [starttransfer_time] => 0.635992
            [redirect_time] => 0
            [redirect_url] => https://httpstat.us
            [primary_ip] => 23.99.0.12
            [certinfo] => Array
                (
                )

            [primary_port] => 443
            [local_ip] => 172.16.221.128
            [local_port] => 56171
            [request_header] => GET /307 HTTP/1.1
User-Agent: Drupal (+http://drupal.org/)
Accept: */*
Host: httpstat.us:443


        )

)

Doing the same request with drupal_http_request returns the 307 redirect, not an error.

CommentFileSizeAuthor
#4 chr-3031559-max_redirects_zero.patch665 bytessteven jones

Comments

Steven Jones created an issue. See original summary.

steven jones’s picture

Title: max_redirects = 0 is not respected » max_redirects => 0 does not function
steven jones’s picture

Given that this module actually handles the HTTP redirect in result processing, and because of #1699868: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set maybe CURLOPT_FOLLOWLOCATION should be FALSE always?

steven jones’s picture

Status: Active » Needs review
StatusFileSize
new665 bytes

However, maybe this is a less invasive patch, in that it only sets CURLOPT_FOLLOWLOCATION TRUE if max_redirects is non-empty.