Plack-Debugger
view release on metacpan or search on metacpan
lib/Plack/Middleware/Debugger/Injector.pm view on Meta::CPAN
# specifically handle this one
# just in case ...
return $resp;
}
sub handle_html_content_type {
my ($self, $env, $resp) = @_;
# content to be inserted ...
my $content = $self->get_content_to_insert( $env );
# if the response is not a streaming one ...
if ( (scalar @$resp) == 3 && ref $resp->[2] eq 'ARRAY' ) {
# adjust Content-Length if we have it ...
if ( my $content_length = Plack::Util::header_get( $resp->[1], 'Content-Length' ) ) {
Plack::Util::header_set( $resp->[1], 'Content-Length', $content_length + length($content) );
}
# now inject our content before the closing
# body tag, makes the most sense to process
# the body in reverse since it will most
lib/Plack/Middleware/Debugger/Injector.pm view on Meta::CPAN
# skip if we don't have it
next unless $chunk =~ m!(?=</body>)!i;
# if we do have it, substitute and ...
$chunk =~ s!(?=</body>)!$content!i;
# break out of the loop, we are done
last;
}
return $resp;
}
# if we have streaming response, just do what is sensible
else {
# NOTE:
# Plack will remove the Content-Length header
# if it has a streaming response, so there is
# no need to worry about that at all.
# - SL
return sub {
my $chunk = shift;
return unless defined $chunk;
$chunk =~ s!(?=</body>)!$content!i;
return $chunk;
};
}
}
lib/Plack/Middleware/Debugger/Injector.pm view on Meta::CPAN
=item C<text/html>, C<application/xhtml>, etc.
Given a content-type that looks like HTML, this will PSGI responses in
the most sensible way possible.
If we detect a PSGI response which is an array of strings, we will process
it (in reverse) looking for the closing C<<body>> tag and inject the
content accordingly.
All other PSGI responses will be handled as if they are streaming
responses, in which case we simple return a C<CODE> reference that will
process the stream and if a closing C<<body>> tag is found, inject
accordingly.
=item C<application/json>
While we have a specific handler for this content-type, we do not do
anything but just let it pass through. This is handled in this way
specifically in case someone decides it is sensible to inject some
kind of data into a JSON response.
( run in 0.233 second using v1.01-cache-2.11-cpan-4d50c553e7e )