mje has asked for the wisdom of the Perl Monks concerning the following question:
I've been experimenting trying to get the time down for some HTTP code which was using LWP. HTTP::Lite was faster then HTTP::GHTTP was faster again and WWW::Curl seemed to be the fastest. However when posting I seem to get a mysterious persistent header string even though I've cleared it. Here is example code:
use strict; use warnings; use WWW::Curl::Easy; use WWW::Curl::Form; use Encode; my $hdr; my $curl = WWW::Curl::Easy->new(); $curl->setopt(CURLOPT_NOPROGRESS, 1); # shut off the built-in progress + meter $curl->setopt(CURLOPT_HEADER, 0); # don't include hdr in body $curl->setopt(CURLOPT_ENCODING, 'gzip'); $curl->setopt(CURLOPT_PROXY, ''); # no proxy open my $fh, ">", \$hdr; $curl->setopt(CURLOPT_WRITEHEADER, $fh); $curl->setopt(CURLOPT_URL, 'http://xxx.yyy.zzz:82/vx'); post(); print "FIRST HEADER $hdr\n\n"; $hdr = ''; sleep 2; post(); print "SECOND HEADER $hdr\n"; sub post { my $form = WWW::Curl::Form->new; $curl->setopt(CURLOPT_HTTPHEADER, ['Expect:','']); $form->formadd('method', 'login'); $form->formadd('username', 'xxx'); $form->formadd('password', 'xxx'); $curl->setopt(CURLOPT_HTTPPOST, $form); my $response_body; $curl->setopt(CURLOPT_WRITEDATA,\$response_body); my $retcode = $curl->perform; if ($retcode != 0) { die "post_request failed (", $retcode, " - ", $curl->errbuf, ")"; } my $decoded_content = decode ('utf-8', $response_body); }
which outputs
FIRST HEADER HTTP/1.1 200 OK Server: nginx/1.0.4 Date: Tue, 28 Jun 2011 17:57:39 GMT Content-Type: application/json; charset=UTF-8 Connection: keep-alive Set-Cookie: XXX=A6C8DD1FCAEFF6A4E040007F010036E6%3Abcffbfc621258ed7c1f +4b8fba d5e515230912a769433656c984bad7a0ceb73ec; path=/; expires=Wed, 29-Jun-2 +011 17:57: 39 GMT Content-Length: 68 SECOND HEADER HTTP/1.1 200 OK Server: nginx/1.0.4 Date: Tue, 28 Jun 2011 17:57:39 GMT Content-Type: application/json; charset=UTF-8 Connection: keep-alive Set-Cookie: XXX=A6C8DD1FCAEFF6A4E040007F010036E6%3Abcffbfc621258ed7c1f +4b8fba d5e515230912a769433656c984bad7a0ceb73ec; path=/; expires=Wed, 29-Jun-2 +011 17:57: 39 GMT Content-Length: 68 HTTP/1.1 200 OK Server: nginx/1.0.4 Date: Tue, 28 Jun 2011 17:57:41 GMT Content-Type: application/json; charset=UTF-8 Connection: keep-alive Set-Cookie: XXX=A6C8AF9C5885B010E040007F010033A2%3A88dd9574a4b678c1e55 +e0fd63 ec1c2fbebc77c7cfd456473df25dc22db26ab90; path=/; expires=Wed, 29-Jun-2 +011 17:57: 41 GMT Content-Length: 68
even though $hdr is cleared between post calls. Notice the data in $hdr in the second case contains the identical data from the first post - see the times. The more times you post the longer $hdr gets like it has stored every header result ever made. I'm quite prepared to find I've done something wrong but I find this strange. Any ideas?
Sorry I cannot provide a URL for the exact service but it is not visible on the net. It is basically a starman service which allows you to login to an API.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: WWW::Curl mysterious header values persisting
by Corion (Patriarch) on Jun 28, 2011 at 18:23 UTC | |
by mje (Curate) on Jun 28, 2011 at 18:29 UTC | |
by mje (Curate) on Jun 28, 2011 at 18:33 UTC |