Como faço cURL usar keepalive a partir da linha de comando?

30

Estou tentando verificar se as conexões persistentes de HTTP estão sendo usadas durante a comunicação com um servidor da web do Tomcat que estou executando. Atualmente, posso recuperar um recurso em meu servidor de um navegador (por exemplo, Chrome) e verificar usando o netstat que a conexão está estabelecida:

# visit http://server:8080/path/to/resource in Chrome
[server:/tmp]$ netstat -a
...
tcp        0      0 server.mydomain:webcache client.mydomain:55502 ESTABLISHED

No entanto, se eu usar o curl, nunca vejo a conexão no servidor no netstat.

[client:/tmp]$ curl --keepalive-time 60 --keepalive http://server:8080/path/to/resource
...

[server:/tmp]$ netstat -a
# no connection exists for client.mydomain

Eu também tentei usar o seguinte comando curl:

curl -H "Keep-Alive: 60" -H "Connection: keep-alive" http://server:8080/path/to/resource

Aqui está a versão curl da minha máquina cliente:

[server:/tmp]$ curl -V
curl 7.19.5 (x86_64-unknown-linux-gnu) libcurl/7.19.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5 libssh2/1.1
Protocols: tftp ftp telnet dict http file https ftps scp sftp
Features: IDN IPv6 Largefile NTLM SSL libz

Como obtenho o curl para usar uma conexão persistente / keepalive? Eu pesquisei bastante sobre o assunto, mas sem sucesso. Deve-se notar que eu também usei links na máquina cliente para recuperar o recurso, e isso faz me dar uma conexão ESTABLISHED no servidor.

Deixe-me saber se preciso fornecer mais informações.

    
por Rob Hruska 08.11.2010 / 17:03

4 respostas

36
O

curl já usa o keepalive por padrão.

Como exemplo:

curl -v http://www.google.com http://www.google.com

Produz o seguinte:

* About to connect() to www.google.com port 80 (#0)
*   Trying 74.125.39.99... connected
* Connected to www.google.com (74.125.39.99) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15
> Host: www.google.com
> Accept: */*
> 
< HTTP/1.1 302 Found
< Location: http://www.google.ch/
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Set-Cookie: PREF=ID=0dd153a227433b2f:FF=0:TM=1289232886:LM=1289232886:S=VoXSLP8XWvjzNcFj; expires=Wed, 07-Nov-2012 16:14:46 GMT; path=/; domain=.google.com
< Set-Cookie: NID=40=sOJuv6mxhQgqXkVEOzBwpUFU3YLPQYf4HRcySE1veCBV5cPtP3OiLPKqvRxL10VLiFETGz7cu25pD_EoUq1f_CkNwOna-xRcFFsCokiFqIbGPrb6DmUO7XhcpMYOt3dB; expires=Tue, 10-May-2011 16:14:46 GMT; path=/; domain=.google.com; HttpOnly
< Date: Mon, 08 Nov 2010 16:14:46 GMT
< Server: gws
< Content-Length: 218
< X-XSS-Protection: 1; mode=block
< 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.ch/">here</A>.
</BODY></HTML>
* Connection #0 to host www.google.com left intact
* Re-using existing connection! (#0) with host www.google.com
* Connected to www.google.com (74.125.39.99) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15
> Host: www.google.com
> Accept: */*
> 
< HTTP/1.1 302 Found
< Location: http://www.google.ch/
< Cache-Control: private
< Content-Type: text/html; charset=UTF-8
< Set-Cookie: PREF=ID=8b531815cdfef717:FF=0:TM=1289232886:LM=1289232886:S=ifbAe1QBX915QGHr; expires=Wed, 07-Nov-2012 16:14:46 GMT; path=/; domain=.google.com
< Set-Cookie: NID=40=Rk86FyMCV3LzorQ1Ph8g1TV3f-h41NA-9fP6l7G-441pLEiciG9k8L4faOGC0VI6a8RafpukiDvaNvJqy8wExED9-Irzs7VdUQYwI8bCF2Kc2ivskb6KDRDkWzMxW_xG; expires=Tue, 10-May-2011 16:14:46 GMT; path=/; domain=.google.com; HttpOnly
< Date: Mon, 08 Nov 2010 16:14:46 GMT
< Server: gws
< Content-Length: 218
< X-XSS-Protection: 1; mode=block
< 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.ch/">here</A>.
</BODY></HTML>
* Connection #0 to host www.google.com left intact
* Closing connection #0

Este snippet:

* Connection #0 to host www.google.com left intact
* Re-using existing connection! (#0) with host www.google.com

Indica que reutilizou a mesma conexão.

Use a mesma invocação de " curl -v http://my.server/url1 http://my.server/url2 " no seu servidor e verifique se você vê a mesma mensagem.

Considere usar o tcpdump em vez do netstat para ver como os pacotes são manipulados. O netstat só lhe dará um vislumbre momentâneo do que está acontecendo, ao passo que com o tcpdump você verá todos os pacotes envolvidos. Outra opção é o Wireshark.

    
por 08.11.2010 / 17:18
6

Se o seu servidor permitir 'KeepAlive On', você pode usar o telnet para manter uma conexão persistente assim:

$ while :;do echo -e "GET / HTTP/1.1\nhost: $YOUR_VIRTUAL_HOSTNAME\n\n";sleep 1;done|telnet $YOUR_SERVERS_IP 80
    
por 16.05.2013 / 23:23
5

Uma forma de testar a conexão persistente HTTP / Keep-Alive é ver se a conexão TCP é reutilizada para conexões subsequentes.

Por exemplo. Eu tenho um arquivo contendo o link link repetido várias vezes.

A execução abaixo do comando abrirá o link várias vezes com a mesma conexão TCP.

curl -K /tmp/file

E durante este tempo, se você netstat você pode achar que a conexão TCP não muda e a mais antiga é resued (O socket permanece o mesmo).

$ sudo netstat -pnt|grep curl
tcp        0      0 106.51.85.118:48682     74.125.236.69:80        ESTABLISHED 9732/curl       
$ sudo netstat -pnt|grep curl
tcp        0      0 106.51.85.118:48682     74.125.236.69:80        ESTABLISHED 9732/curl       
$ sudo netstat -pnt|grep curl
tcp        0      0 106.51.85.118:48682     74.125.236.69:80        ESTABLISHED 9732/curl   

Mas quando pedimos ao cliente para usar o HTTP 1.0 que não suporta conexão HTTP persistente, o endereço do soquete muda

$ curl -0 -K /tmp/file

$ sudo netstat -pnt|grep curl
tcp        0      0 106.51.85.118:48817     74.125.236.69:80        ESTABLISHED 9765/curl       
$ sudo netstat -pnt|grep curl
tcp        0      0 106.51.85.118:48827     74.125.236.69:80        ESTABLISHED 9765/curl       
$ sudo netstat -pnt|grep curl
tcp        0     74 106.51.85.118:48838     74.125.236.69:80        ESTABLISHED 9765/curl       

disso podemos ter certeza de que a conexão TCP é reutilizada.

    
por 05.12.2013 / 14:36
2

- keepalive-time

man curl ... man ..: D

    
por 08.11.2010 / 17:16