$POE::Component::Client::HTTP::RequestFactory::VERSION
=
'0.949'
;
FCT_AGENT
=> 0,
FCT_STREAMING
=> 1,
FCT_MAXSIZE
=> 2,
FCT_PROTOCOL
=> 3,
FCT_COOKIEJAR
=> 4,
FCT_FROM
=> 5,
FCT_NOPROXY
=> 6,
FCT_HTTP_PROXY
=> 7,
FCT_FOLLOWREDIRECTS
=> 8,
FCT_TIMEOUT
=> 9,
};
use
constant
DEFAULT_BLOCK_SIZE
=> 4096;
sub
new {
my
(
$class
,
$params
) =
@_
;
croak __PACKAGE__ .
"expects its arguments in a hashref"
unless
(!
defined
(
$params
) or
ref
(
$params
) eq
'HASH'
);
my
$agent
=
delete
$params
->{Agent};
$agent
= []
unless
defined
$agent
;
$agent
= [
$agent
]
unless
ref
(
$agent
);
unless
(
ref
(
$agent
) eq
"ARRAY"
) {
croak
"Agent must be a scalar or a reference to a list of agent strings"
;
}
my
$v
=
$POE::Component::Client::HTTP::VERSION
;
$v
=
"0.000"
unless
defined
$v
;
push
(
@$agent
,
sprintf
(
'POE-Component-Client-HTTP/%s (perl; N; POE; en; rv:%f)'
,
$v
,
$v
)
)
unless
@$agent
;
my
$max_size
=
delete
$params
->{MaxSize};
my
$streaming
=
delete
$params
->{Streaming};
my
$protocol
=
delete
$params
->{Protocol};
$protocol
=
'HTTP/1.1'
unless
defined
$protocol
and
length
$protocol
;
my
$cookie_jar
=
delete
$params
->{CookieJar};
my
$from
=
delete
$params
->{From};
my
$no_proxy
=
delete
$params
->{NoProxy};
my
$proxy
=
delete
$params
->{Proxy};
my
$follow_redirects
=
delete
$params
->{FollowRedirects} || 0;
my
$timeout
=
delete
$params
->{Timeout};
$proxy
=
$ENV
{HTTP_PROXY} ||
$ENV
{http_proxy}
unless
defined
$proxy
;
$no_proxy
=
$ENV
{NO_PROXY} ||
$ENV
{no_proxy}
unless
defined
$no_proxy
;
$class
->parse_proxy(
$proxy
)
if
defined
$proxy
;
if
(
defined
$no_proxy
) {
unless
(
ref
(
$no_proxy
) eq
'ARRAY'
) {
$no_proxy
= [
split
(/\s*\,\s*/,
$no_proxy
) ];
}
}
$timeout
= 180
unless
(
defined
$timeout
and
$timeout
> 0);
my
$self
= [
$agent
,
$streaming
,
$max_size
,
$protocol
,
$cookie_jar
,
$from
,
$no_proxy
,
$proxy
,
$follow_redirects
,
$timeout
,
];
return
bless
$self
,
$class
;
}
sub
timeout {
my
(
$self
,
$timeout
) =
@_
;
if
(
defined
$timeout
) {
$self
->[FCT_TIMEOUT] =
$timeout
;
}
return
$self
->[FCT_TIMEOUT];
}
sub
is_streaming {
my
(
$self
) =
@_
;
DEBUG and
warn
(
"FCT: this is "
. (
$self
->[FCT_STREAMING] ?
""
:
"not "
)
.
"streaming"
);
return
$self
->[FCT_STREAMING];
}
sub
agent {
my
(
$self
) =
@_
;
return
$self
->[FCT_AGENT]->[
rand
@{
$self
->[FCT_AGENT]}];
}
sub
from {
my
(
$self
) =
@_
;
if
(
defined
$self
->[FCT_FROM] and
length
$self
->[FCT_FROM]) {
return
$self
->[FCT_FROM];
}
return
undef
;
}
sub
create_request {
my
(
$self
,
$http_request
,
$response_event
,
$tag
,
$progress_event
,
$proxy_override
,
$sender
) =
@_
;
$http_request
->protocol(
$self
->[FCT_PROTOCOL] )
unless
(
defined
$http_request
->protocol()
and
length
$http_request
->protocol()
);
unless
(
defined
$http_request
->user_agent()) {
$http_request
->user_agent(
$self
->agent);
}
if
(
defined
$self
->from) {
my
$req_from
=
$http_request
->from();
unless
(
defined
$req_from
and
length
$req_from
) {
$http_request
->from(
$self
->from );
}
}
if
(
length
(
$http_request
->content()) and
!
ref
(
$http_request
->content()) and
!
$http_request
->content_length()
) {
$http_request
->content_length(
length
(
$http_request
->content()));
}
my
(
$last_request
,
$postback
);
if
(
ref
(
$response_event
) eq
'POE::Component::Client::HTTP::Request'
) {
$last_request
=
$response_event
;
$postback
=
$last_request
->postback;
}
else
{
$postback
=
$sender
->postback(
$response_event
,
$http_request
,
$tag
);
}
my
$progress_postback
;
if
(
defined
$progress_event
) {
if
(
ref
$progress_event
) {
$progress_postback
=
$progress_event
;
}
else
{
$progress_postback
=
$sender
->postback(
$progress_event
,
$http_request
,
$tag
);
}
}
if
(
defined
$self
->[FCT_COOKIEJAR]) {
$self
->[FCT_COOKIEJAR]->add_cookie_header(
$http_request
);
}
my
$proxy
=
$proxy_override
;
if
(
$http_request
->uri->scheme() eq
"http"
) {
$proxy
||=
$self
->[FCT_HTTP_PROXY];
}
if
(
defined
$proxy
) {
my
$host
=
$http_request
->uri->host;
undef
$proxy
if
(
!
defined
(
$host
) or
_in_no_proxy (
$host
,
$self
->[FCT_NOPROXY])
);
}
my
$request
= POE::Component::Client::HTTP::Request->new (
Request
=>
$http_request
,
Proxy
=>
$proxy
,
Postback
=>
$postback
,
Progress
=>
$progress_postback
,
Factory
=>
$self
,
);
if
(
defined
$last_request
) {
$request
->does_redirect(
$last_request
);
}
return
$request
;
}
sub
_in_no_proxy {
my
(
$host
,
$no_proxy
) =
@_
;
foreach
my
$no_proxy_domain
(
@$no_proxy
) {
return
1
if
$host
=~ /\Q
$no_proxy_domain
\E$/i;
}
return
0;
}
sub
max_response_size {
my
(
$self
) =
@_
;
return
$self
->[FCT_MAXSIZE];
}
sub
block_size {
my
(
$self
) =
@_
;
my
$block_size
=
$self
->[FCT_STREAMING] || DEFAULT_BLOCK_SIZE;
$block_size
= DEFAULT_BLOCK_SIZE
if
$block_size
< 1;
return
$block_size
;
}
sub
frob_cookies {
my
(
$self
,
$response
) =
@_
;
if
(
defined
$self
->[FCT_COOKIEJAR]) {
$self
->[FCT_COOKIEJAR] ->extract_cookies(
$response
);
}
}
sub
max_redirect_count {
my
(
$self
,
$count
) =
@_
;
if
(
defined
$count
) {
$self
->[FCT_FOLLOWREDIRECTS] =
$count
;
}
return
$self
->[FCT_FOLLOWREDIRECTS];
}
sub
parse_proxy {
my
$proxy
=
$_
[1];
if
(
ref
(
$proxy
) eq
'ARRAY'
) {
croak
"Proxy must contain [HOST,PORT]"
unless
@$proxy
== 2;
$proxy
= [
$proxy
];
}
else
{
my
@proxies
=
split
/\s*\,\s*/,
$proxy
;
foreach
(
@proxies
) {
s/^http:\/+//;
s/\/+$//;
croak
"Proxy must contain host:port"
unless
/^(.+):(\d+)$/;
$_
= [ $1, $2 ];
}
if
(
@proxies
) {
$proxy
= \
@proxies
;
}
else
{
undef
$proxy
;
}
}
$_
[1] =
$proxy
;
}
1;