… | |
… | |
46 | use AnyEvent::Util (); |
46 | use AnyEvent::Util (); |
47 | use AnyEvent::Handle (); |
47 | use AnyEvent::Handle (); |
48 | |
48 | |
49 | use base Exporter::; |
49 | use base Exporter::; |
50 | |
50 | |
51 | our $VERSION = '2.02'; |
51 | our $VERSION = '2.04'; |
52 | |
52 | |
53 | our @EXPORT = qw(http_get http_post http_head http_request); |
53 | our @EXPORT = qw(http_get http_post http_head http_request); |
54 | |
54 | |
55 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
55 | our $USERAGENT = "Mozilla/5.0 (compatible; U; AnyEvent-HTTP/$VERSION; +http://software.schmorp.de/pkg/AnyEvent)"; |
56 | our $MAX_RECURSE = 10; |
56 | our $MAX_RECURSE = 10; |
… | |
… | |
179 | |
179 | |
180 | Default timeout is 5 minutes. |
180 | Default timeout is 5 minutes. |
181 | |
181 | |
182 | =item proxy => [$host, $port[, $scheme]] or undef |
182 | =item proxy => [$host, $port[, $scheme]] or undef |
183 | |
183 | |
184 | Use the given http proxy for all requests. If not specified, then the |
184 | Use the given http proxy for all requests, or no proxy if C<undef> is |
185 | default proxy (as specified by C<$ENV{http_proxy}>) is used. |
185 | used. |
186 | |
186 | |
187 | C<$scheme> must be either missing or must be C<http> for HTTP. |
187 | C<$scheme> must be either missing or must be C<http> for HTTP. |
|
|
188 | |
|
|
189 | If not specified, then the default proxy is used (see |
|
|
190 | C<AnyEvent::HTTP::set_proxy>). |
188 | |
191 | |
189 | =item body => $string |
192 | =item body => $string |
190 | |
193 | |
191 | The request body, usually empty. Will be sent as-is (future versions of |
194 | The request body, usually empty. Will be sent as-is (future versions of |
192 | this module might offer more options). |
195 | this module might offer more options). |
… | |
… | |
529 | while ( |
532 | while ( |
530 | m{ |
533 | m{ |
531 | \G\s* |
534 | \G\s* |
532 | (?: |
535 | (?: |
533 | expires \s*=\s* ([A-Z][a-z][a-z]+,\ [^,;]+) |
536 | expires \s*=\s* ([A-Z][a-z][a-z]+,\ [^,;]+) |
534 | | ([^=;,[:space:]]+) (?: \s*=\s* (?: "((?:[^\\"]+|\\.)*)" | ([^=;,[:space:]]*) ) )? |
537 | | ([^=;,[:space:]]+) (?: \s*=\s* (?: "((?:[^\\"]+|\\.)*)" | ([^;,[:space:]]*) ) )? |
535 | ) |
538 | ) |
536 | }gcxsi |
539 | }gcxsi |
537 | ) { |
540 | ) { |
538 | my $name = $2; |
541 | my $name = $2; |
539 | my $value = $4; |
542 | my $value = $4; |
… | |
… | |
709 | my $recurse = exists $arg{recurse} ? delete $arg{recurse} : $MAX_RECURSE; |
712 | my $recurse = exists $arg{recurse} ? delete $arg{recurse} : $MAX_RECURSE; |
710 | |
713 | |
711 | return $cb->(undef, { @pseudo, Status => 599, Reason => "Too many redirections" }) |
714 | return $cb->(undef, { @pseudo, Status => 599, Reason => "Too many redirections" }) |
712 | if $recurse < 0; |
715 | if $recurse < 0; |
713 | |
716 | |
714 | my $proxy = $arg{proxy} || $PROXY; |
717 | my $proxy = exists $arg{proxy} ? $arg{proxy} : $PROXY; |
715 | my $timeout = $arg{timeout} || $TIMEOUT; |
718 | my $timeout = $arg{timeout} || $TIMEOUT; |
716 | |
719 | |
717 | my ($uscheme, $uauthority, $upath, $query, undef) = # ignore fragment |
720 | my ($uscheme, $uauthority, $upath, $query, undef) = # ignore fragment |
718 | $url =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:(\?[^#]*))?(?:#(.*))?|; |
721 | $url =~ m|^([^:]+):(?://([^/?#]*))?([^?#]*)(?:(\?[^#]*))?(?:#(.*))?$|; |
719 | |
722 | |
720 | $uscheme = lc $uscheme; |
723 | $uscheme = lc $uscheme; |
721 | |
724 | |
722 | my $uport = $uscheme eq "http" ? 80 |
725 | my $uport = $uscheme eq "http" ? 80 |
723 | : $uscheme eq "https" ? 443 |
726 | : $uscheme eq "https" ? 443 |
… | |
… | |
1172 | Sets the default proxy server to use. The proxy-url must begin with a |
1175 | Sets the default proxy server to use. The proxy-url must begin with a |
1173 | string of the form C<http://host:port>, croaks otherwise. |
1176 | string of the form C<http://host:port>, croaks otherwise. |
1174 | |
1177 | |
1175 | To clear an already-set proxy, use C<undef>. |
1178 | To clear an already-set proxy, use C<undef>. |
1176 | |
1179 | |
|
|
1180 | When AnyEvent::HTTP is laoded for the first time it will query the |
|
|
1181 | default proxy from the operating system, currently by looking at |
|
|
1182 | C<$ENV{http_proxy>}. |
|
|
1183 | |
1177 | =item AnyEvent::HTTP::cookie_jar_expire $jar[, $session_end] |
1184 | =item AnyEvent::HTTP::cookie_jar_expire $jar[, $session_end] |
1178 | |
1185 | |
1179 | Remove all cookies from the cookie jar that have been expired. If |
1186 | Remove all cookies from the cookie jar that have been expired. If |
1180 | C<$session_end> is given and true, then additionally remove all session |
1187 | C<$session_end> is given and true, then additionally remove all session |
1181 | cookies. |
1188 | cookies. |
… | |
… | |
1333 | snippets. |
1340 | snippets. |
1334 | |
1341 | |
1335 | =head2 HTTP/1.1 FILE DOWNLOAD |
1342 | =head2 HTTP/1.1 FILE DOWNLOAD |
1336 | |
1343 | |
1337 | Downloading files with HTTP can be quite tricky, especially when something |
1344 | Downloading files with HTTP can be quite tricky, especially when something |
1338 | goes wrong and you want tor esume. |
1345 | goes wrong and you want to resume. |
1339 | |
1346 | |
1340 | Here is a function that initiates and resumes a download. It uses the |
1347 | Here is a function that initiates and resumes a download. It uses the |
1341 | last modified time to check for file content changes, and works with many |
1348 | last modified time to check for file content changes, and works with many |
1342 | HTTP/1.0 servers as well, and usually falls back to a complete re-download |
1349 | HTTP/1.0 servers as well, and usually falls back to a complete re-download |
1343 | on older servers. |
1350 | on older servers. |