Skip to content

Commit e07559d

Browse files
committed
Make favicon optional and disable /ping by default
1 parent 3f4b9a9 commit e07559d

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

lib/AnyEvent/HTTP/Server.pm

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,17 @@ sub new {
9191
$self->can("handle_request")
9292
and croak "It's a new version of ".__PACKAGE__.". For old version use `legacy' branch, or better make some minor patches to support new version";
9393

94-
$self->set_favicon( exists $self->{favicon} ? do {
95-
open my $f, '<:raw', $self->{favicon} or die "Can't open favicon: $!";
96-
local $/;
97-
<$f>;
98-
} : $ico ) if !exists $self->{favicon} or $self->{favicon};
94+
if (!exists $self->{favicon}) {
95+
$self->{favicon} = \$ico;
96+
};
97+
if (!ref $self->{favicon}) {
98+
$self->{favicon} = \do {
99+
open my $f, '<:raw', $self->{favicon} or die "Can't open favicon: $!";
100+
local $/;
101+
<$f>;
102+
};
103+
}
104+
$self->set_favicon( ${ $self->{favicon} } );
99105

100106
return $self;
101107
}
@@ -107,8 +113,13 @@ sub DESTROY { $_[0]->destroy };
107113

108114
sub set_favicon {
109115
my $self = shift;
110-
my $icondata = shift;
111-
$self->{ico} = "HTTP/1.1 200 OK${LF}Connection:close${LF}Content-Type:image/x-icon${LF}Content-Length:".length($icondata)."${LF}${LF}".$icondata;
116+
if (shift) {
117+
my $icondata = shift;
118+
$self->{ico} = "HTTP/1.1 200 OK${LF}Connection:close${LF}Content-Type:image/x-icon${LF}Content-Length:".length($icondata)."${LF}${LF}".$icondata;
119+
}
120+
else {
121+
delete $self->{ico};
122+
}
112123
}
113124

114125
sub listen:method {
@@ -369,19 +380,21 @@ sub incoming {
369380

370381
$self->{total_requests}++;
371382

372-
if ( $method eq "GET" and $uri =~ m{^/favicon\.ico( \Z | \? )}sox ) {
383+
if ( $self->{ico} and $method eq "GET" and $uri =~ m{^/favicon\.ico( \Z | \? )}sox ) {
373384
$write->(\$self->{ico});
374385
$write->(\undef) if lc $h{connecton} =~ /^close\b/;
375386
$self->{active_requests}--;
376387
$ixx = $pos + $h{'content-length'};
377-
} elsif ( $method eq "GET" and $uri =~ m{^/ping( \Z | \? )}sox ) {
388+
}
389+
elsif ( $self->{ping} and $method eq "GET" and $uri =~ m{^/ping( \Z | \? )}sox ) {
378390
my ( $header_str, $content ) = ref $self->{ping_sub} eq 'CODE' ? $self->{ping_sub}->() : ('200 OK', 'Pong');
379391
my $str = "HTTP/1.1 $header_str${LF}Connection:close${LF}Content-Type:text/plain${LF}Content-Length:".length($content)."${LF}${LF}".$content;
380392
$write->(\$str);
381393
$write->(\undef) if lc $h{connecton} =~ /^close\b/;
382394
$self->{active_requests}--;
383395
$ixx = $pos + $h{'content-length'};
384-
} else {
396+
}
397+
else {
385398
#warn "Create request object";
386399
$req = $self->{request}->new(
387400
method => $method,

0 commit comments

Comments
 (0)