about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-05-23 07:19:45 +0000
committerEric Wong <e@80x24.org>2016-05-23 07:19:54 +0000
commitbb38f0fcce73904e68d4cde8f9d85e9da2e93013 (patch)
tree598772755723af5a178131349140f0fd128fd777 /lib
parent347c6ee595c37d4e2214cb297811f154a41c452f (diff)
downloadpublic-inbox-bb38f0fcce73904e68d4cde8f9d85e9da2e93013.tar.gz
Since PSGI does not require Transfer-Encoding: chunked or
Content-Length, we cannot expect random apps we host to chunk
their responses.

Thus, to improve interoperability, chunk at the HTTP layer like
other PSGI servers do.  I'm chosing a more syscall-intensive method
(via multiple send(...MSG_MORE) for now to reduce copy + packet
overhead.
Diffstat (limited to 'lib')
-rw-r--r--lib/PublicInbox/HTTP.pm39
1 files changed, 31 insertions, 8 deletions
diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 480800bd..77b178c0 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -180,12 +180,19 @@ sub response_header_write {
 
         my $conn = $env->{HTTP_CONNECTION} || '';
         my $term = defined($len) || $chunked;
-        my $alive = $term &&
-                        (($proto eq 'HTTP/1.1' && $conn !~ /\bclose\b/i) ||
-                         ($conn =~ /\bkeep-alive\b/i));
-
-        $h .= 'Connection: ' . ($alive ? 'keep-alive' : 'close');
-        $h .= "\r\nDate: " . http_date() . "\r\n\r\n";
+        my $prot_persist = ($proto eq 'HTTP/1.1') && ($conn !~ /\bclose\b/i);
+        my $alive;
+        if (!$term && $prot_persist) { # auto-chunk
+                $chunked = $alive = 2;
+                $h .= "Transfer-Encoding: chunked\r\n";
+                # no need for "Connection: keep-alive" with HTTP/1.1
+        } elsif ($term && ($prot_persist || ($conn =~ /\bkeep-alive\b/i))) {
+                $alive = 1;
+                $h .= "Connection: keep-alive\r\n";
+        } else {
+                $h .= "Connection: close\r\n";
+        }
+        $h .= 'Date: ' . http_date() . "\r\n\r\n";
 
         if (($len || $chunked) && $env->{REQUEST_METHOD} ne 'HEAD') {
                 more($self, $h);
@@ -195,13 +202,29 @@ sub response_header_write {
         $alive;
 }
 
+# middlewares such as Deflater may write empty strings
+sub chunked_wcb ($) {
+        my ($self) = @_;
+        sub {
+                return if $_[0] eq '';
+                more($self, sprintf("%x\r\n", bytes::length($_[0])));
+                more($self, $_[0]);
+                $self->write("\r\n");
+        }
+}
+
+sub identity_wcb ($) {
+        my ($self) = @_;
+        sub { $self->write(\($_[0])) if $_[0] ne '' }
+}
+
 sub response_write {
         my ($self, $env, $res) = @_;
         my $alive = response_header_write($self, $env, $res);
 
-        # middlewares such as Deflater may write empty strings
-        my $write = sub { $self->write(\($_[0])) if $_[0] ne '' };
+        my $write = $alive == 2 ? chunked_wcb($self) : identity_wcb($self);
         my $close = sub {
+                $self->write("0\r\n\r\n") if $alive == 2;
                 if ($alive) {
                         $self->event_write; # watch for readability if done
                 } else {