Subject: | $response->content(\&coderef) |
Many thanks for the wonderful module!
With this patch (see rt.cpan.org) to HTTP/Proxy.pm 0.13, request filters can use coderefs in short-circuiting responses, as with HTTP::Daemon. This makes it easy to serve a large response locally without reading it all into memory. It may also allow one to reuse code that relies on the HTTP::Daemon feature.
One could argue that the problem is in HTTP::Daemon. A better solution might be to split HTTP::Daemon::send_response into header and body portions so that users would have less need to duplicate its logic.
Best regards,
-John
--- /usr/local/share/perl/5.6.1/HTTP/Proxy.pm Tue Mar 2 19:15:02 2004
+++ /home/jtobey/lib/perl/HTTP/Proxy.pm Sat Feb 12 12:13:28 2005
@@ -621,7 +621,18 @@
if ( !$sent ) {
($last, $chunked) = $self->_send_response_headers( $served );
my $content = $response->content;
- if ($chunked) {
+ if (ref($content) eq "CODE") {
+ while (1) {
+ my $chunk = &$content();
+ last unless defined($chunk) && length($chunk);
+ if ($chunked) {
+ printf $conn "%x$CRLF%s$CRLF", length($chunk), $chunk;
+ } else {
+ print $conn $chunk;
+ }
+ }
+ print $conn "0$CRLF$CRLF" if $chunked; # no trailers either
+ } elsif ($chunked) {
printf $conn "%x$CRLF%s$CRLF", length($content), $content
if length($content); # the filter may leave nothing
print $conn "0$CRLF$CRLF";