Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HttpClient] CurlHttpClient HEAD request hangs #34102

Closed
rscipien opened this issue Oct 24, 2019 · 2 comments
Closed

[HttpClient] CurlHttpClient HEAD request hangs #34102

rscipien opened this issue Oct 24, 2019 · 2 comments

Comments

@rscipien
Copy link

rscipien commented Oct 24, 2019

Symfony version(s) affected: 4.3.3

Description
Use HttpClientInterface::request with $method param set to HEAD
Depending on installed curl version request hangs.
Not working with curl

curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3
Release-Date: 2018-01-24
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL 

How to reproduce

    /**
    * @var HttpClientInterface
    */
    private $client;
    
    /**
     * Constructor.
     *
     * @param HttpClientInterface $client
     */
    public function __construct(HttpClientInterface $client)
    {
        $this->client = $client;
    }

   public function test()
   {
       $this->client->request('HEAD', 'some/url');
   }

Possible Solution
Change code in Symfony\Component\HttpClient\CurlHttpClient::request. For HEAD http metohd use $curlopts[CURLOPT_NOBODY] = 1;

if ('POST' === $method) {
            // Use CURLOPT_POST to have browser-like POST-to-GET redirects for 301, 302 and 303
            $curlopts[CURLOPT_POST] = true;
//        } elseif ('HEAD' === $method) {
//            $curlopts[CURLOPT_NOBODY] = 1;
        } else {
            $curlopts[CURLOPT_CUSTOMREQUEST] = $method;
        }

Additional context

https://curl.haxx.se/docs/manpage.html#-X
https://stackoverflow.com/a/770200

This option only changes the actual word used in the HTTP request, it does not alter the way curl behaves. So for example if you want to make a proper HEAD request, using -X HEAD will not suffice. You need to use the -I, --head option. 
@rscipien rscipien changed the title CurlHttpClient HEAD request hangs [HttpClient] CurlHttpClient HEAD request hangs Oct 24, 2019
@nicolas-grekas
Copy link
Member

Your patch works for me, would you like to submit a PR?

@rscipien
Copy link
Author

Yes sure

@nicolas-grekas nicolas-grekas added this to the 4.3 milestone Oct 25, 2019
@fabpot fabpot closed this as completed Oct 25, 2019
fabpot added a commit that referenced this issue Oct 25, 2019
… (nicolas-grekas)

This PR was merged into the 4.3 branch.

Discussion
----------

[HttpClient] ignore the body of responses to HEAD requests

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #34102
| License       | MIT
| Doc PR        | -

Commits
-------

0fc371e [HttpClient] ignore the body of responses to HEAD requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants