no
warnings;
our
$VERSION
=
'0.66'
;
Hide Show 63 lines of Pod
our
$cookies_lifetime
= 3600*24*31;
sub
new {
my
(
$self
) =
@_
;
$self
->start_timer;
$self
->{buffer} = HTTP::Server::EV::Buffer->new({
fd
=>
$self
->{fd} });
$self
->{stdout_guard} = [];
(
$self
->{headers}{SCRIPT_NAME},
$self
->{headers}{QUERY_STRING} ) =(
split
/\?/,
$self
->{headers}{REQUEST_URI});
$self
->{headers}{DOCUMENT_URI} =
$self
->{headers}{SCRIPT_NAME};
$self
->{headers}{REMOTE_ADDR} =
$self
->{headers}{
'HTTP_X-REAL-IP'
}
if
(
$HTTP::Server::EV::backend
&&
$self
->{headers}{
'HTTP_X-REAL-IP'
});
$self
->{headers}{CONTENT_TYPE} =
$self
->{headers}{
'HTTP_CONTENT-TYPE'
};
$self
->{headers}{CONTENT_LENGTH} =
$self
->{headers}{
'HTTP_CONTENT-LENGTH'
};
my
@pairs
=
split
(/[;&]/,
$self
->{headers}{QUERY_STRING},1024);
foreach
(
@pairs
) {
my
(
$name
,
$data
) =
split
/=/,
$self
->urldecode(
$_
);
Encode::_utf8_on(
$name
);
Encode::_utf8_on(
$data
);
$self
->{get}{
$name
} =
$data
;
$self
->{get_a}{
$name
}=[]
unless
$self
->{get_a}{
$name
};
push
@{
$self
->{get_a}{
$name
}},
$data
;
}
if
(
$self
->{REQUEST_BODY}){
my
@pairs
=
split
(/[;&]/,
$self
->{REQUEST_BODY},1024);
foreach
(
@pairs
) {
my
(
$name
,
$data
) =
split
/=/,
$self
->urldecode(
$_
);
Encode::_utf8_on(
$name
);
Encode::_utf8_on(
$data
);
$self
->{post}{
$name
} =
$data
;
$self
->{post_a}{
$name
}=[]
unless
$self
->{post_a}{
$name
};
push
@{
$self
->{post_a}{
$name
}},
$data
;
}
}
@pairs
=
split
(/; /,
$self
->{headers}{HTTP_COOKIE},100);
foreach
(
@pairs
) {
my
(
$name
,
$data
) =
split
/=/,
$self
->urldecode(
$_
);
Encode::_utf8_on(
$name
);
Encode::_utf8_on(
$data
);
$self
->{cookies}{
$name
} =
$data
;
}
return
$self
;
}
Hide Show 6 lines of Pod
sub
next
{
$_
[0]->{received} ?
goto
(NEXT_REQ) :
$_
[0]-> drop};
Hide Show 6 lines of Pod
sub
fd {
$_
[0]->{fd} }
Hide Show 7 lines of Pod
sub
fh {
croak
'Can`t get fh of closed socket!'
unless
$_
[0]->{buffer};
$_
[0]->{buffer}{fh}
}
Hide Show 6 lines of Pod
sub
buffer {
croak
'Can`t get buffered handle from closed socket!'
unless
$_
[0]->{buffer};
tie
(
$_
[0]->{buf_fh},
'HTTP::Server::EV::Buffer'
,
$_
[0]->{buffer})
unless
$_
[0]->{buf_fh};
return
$_
[0]->{buf_fh};
}
Hide Show 19 lines of Pod
sub
attach {
croak
'Can`t attach closed socket!'
unless
$_
[0]->{buffer};
push
@{
$_
[0]->{stdout_guard}}, HTTP::Server::EV::BufTie->new(
$_
[1],
$_
[0]->{buffer});
}
Hide Show 6 lines of Pod
sub
copy {
croak
'Can`t attach closed socket!'
unless
$_
[0]->{buffer};
open
(
$_
[1],
'>&'
,
$_
[0]->{fd} ) or croak
'Can`t attach socket handle'
;
binmode
$_
[1];
}
Hide Show 6 lines of Pod
sub
print
{
shift
->{buffer}->
print
(
@_
)};
Hide Show 6 lines of Pod
sub
flush {
$_
[0]->{buffer}->flush(
$_
[1])};
sub
flush_wait {
$_
[0]->{buffer}->flush_wait(
$_
[1])};
Hide Show 6 lines of Pod
sub
close
{
delete
$_
[0]->{buffer};
$_
[0]->{stdout_guard} = [];
};
Hide Show 10 lines of Pod
sub
start_timer {
shift
->{timer}=[gettimeofday] };
sub
flush_timer {
return
tv_interval(
shift
->{timer}) };
sub
get {
return
$_
[0]->{get_a}{
$_
[1]} ?
$_
[0]->{get_a}{
$_
[1]} : [] ;}
sub
post {
return
$_
[0]->{post_a}{
$_
[1]} ?
$_
[0]->{post_a}{
$_
[1]} : [] ;}
sub
file {
return
$_
[0]->{file_a}{
$_
[1]} ?
$_
[0]->{file_a}{
$_
[1]} : [] ;}
sub
param {
if
(
wantarray
){
return
( (@{
$_
[0]->{get_a}{
$_
[1]} || []}) ? @{
$_
[0]->{get_a}{
$_
[1]}} : @{
$_
[0]->{post_a}{
$_
[1]} || []} );
}
else
{
return
$_
[0]->{get}{
$_
[1]} ||
$_
[0]->{post}{
$_
[1]};
}
}
Hide Show 6 lines of Pod
sub
set_cookies {
my
(
$self
,
$cookies
,
$lifetime
)=
@_
;
my
(
$name
,
$value
);
my
@days
=
qw(Sun Mon Tue Wed Thu Fri Sat)
;
my
@months
=
qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
;
my
(
$sec
,
$min
,
$hour
,
$mday
,
$mon
,
$year
,
$wday
)=
gmtime
(
time
+ (
defined
(
$lifetime
) ?
$lifetime
:
$HTTP::Server::EV::CGI::cookies_lifetime
) );
my
$date
=
sprintf
(
"%s, %02d-%s-%04d %02d:%02d:%02d GMT"
,
$days
[
$wday
],
$mday
,
$months
[
$mon
],
$year
+1900,
$hour
,
$min
,
$sec
);
$self
->{cookiesbuffer}.=
"Set-Cookie: $name=$value; path=/; expires=$date;\r\n"
while
((
$name
,
$value
)=
each
%{
$cookies
});
};
Hide Show 26 lines of Pod
sub
header {
my
(
$self
,
$params
)=
@_
;
croak
'Can`t print headers to closed socket!'
unless
$_
[0]->{buffer};
my
$headers
=
'HTTP/1.1 '
.(
$params
->{
'STATUS'
} ?
delete
(
$params
->{
'STATUS'
}) :
'200 OK'
).
"\r\n"
;
$headers
.=
'Server: '
.(
$params
->{
'Server'
} ?
delete
(
$params
->{
'Server'
}) :
'Perl HTTP::Server::EV'
).
"\r\n"
;
$headers
.=
$self
->{cookiesbuffer};
$headers
.=
'Content-Type: '
.(
$params
->{
'Content-Type'
} ?
delete
(
$params
->{
'Content-Type'
}) :
'text/html'
).
"\r\n"
;
$headers
.=
$_
.
': '
.
$params
->{
$_
}.
"\r\n"
for
(
keys
%{
$params
});
$self
->{buffer}->
print
(
$headers
.
"\r\n"
);
}
Hide Show 6 lines of Pod
sub
urldecode {
my
(
$output
,
$is_utf
) = HTTP::Server::EV::url_decode(
$_
[1]);
$output
= decode(
$is_utf
?
'utf8'
:
'cp1251'
,
$output
);
Encode::_utf8_on(
$output
);
$output
;
};
Hide Show 18 lines of Pod
sub
stop { HTTP::Server::EV::stop_req(
$_
[0]->{stack_pos}); 1}
sub
start {
my
$fd
= HTTP::Server::EV::start_req(
$_
[0]->{stack_pos});
EV::feed_fd_event(
$fd
, EV::READ)
if
defined
$fd
;
1;
}
sub
drop {
HTTP::Server::EV::drop_req(
$_
[0]->{stack_pos});
EV::feed_fd_event(
$_
[0]->{fd}, EV::READ);
}
sub
DESTROY {
HTTP::Server::EV::start_listen(
$_
[0]->{parent_listener}{ptr})
unless
$_
[0]->{parent_listener}{stopped};
}
1;