]> Sergey Matveev's repositories - public-inbox.git/commitdiff
www_stream: add response wrapper sub
authorEric Wong <e@80x24.org>
Thu, 30 Jun 2016 09:13:21 +0000 (09:13 +0000)
committerEric Wong <e@80x24.org>
Thu, 30 Jun 2016 09:13:21 +0000 (09:13 +0000)
This encapsulates an entire PSGI response array, hopefully
making it easier to generate responses and avoid typos when
setting the Content-Type.

lib/PublicInbox/Feed.pm
lib/PublicInbox/SearchView.pm
lib/PublicInbox/View.pm
lib/PublicInbox/WWW.pm
lib/PublicInbox/WwwStream.pm
t/view.t

index c16c417a099e9e6b88bdc94e5f9897e37293380a..2f141c4413b9473e0f4ee3cdaaaa692633a95428 100644 (file)
@@ -48,7 +48,7 @@ sub new_html {
        }
        $ctx->{-html_tip} = '<pre>';
        $ctx->{-upfx} = '';
        }
        $ctx->{-html_tip} = '<pre>';
        $ctx->{-upfx} = '';
-       my $res = PublicInbox::WwwStream->new($ctx, sub {
+       PublicInbox::WwwStream->response($ctx, 200, sub {
                while (my $path = shift @paths) {
                        my $m = do_cat_mail($ctx->{-inbox}, $path) or next;
                        my $more = scalar @paths;
                while (my $path = shift @paths) {
                        my $m = do_cat_mail($ctx->{-inbox}, $path) or next;
                        my $more = scalar @paths;
@@ -58,7 +58,6 @@ sub new_html {
                }
                undef;
        });
                }
                undef;
        });
-       [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $res ]
 }
 
 # private subs
 }
 
 # private subs
index 4af6cadbaf0e901b0a571912f5d53d4d1623d8c2..30a310c9f58ceede52857d3b07d23668eaedcf6b 100644 (file)
@@ -55,9 +55,7 @@ sub sres_top_html {
                        $cb = mset_summary($ctx, $mset, $q);
                }
        }
                        $cb = mset_summary($ctx, $mset, $q);
                }
        }
-
-       [ $code, ['Content-Type', 'text/html; charset=UTF-8'],
-               PublicInbox::WwwStream->new($ctx, $cb) ];
+       PublicInbox::WwwStream->response($ctx, $code, $cb);
 }
 
 # display non-threaded search results similar to what users expect from
 }
 
 # display non-threaded search results similar to what users expect from
index b4f80d1d470bb991be73372d3bc36312f01c4128..27dd15541a34eed0e4b9c0bd6eaf45f5c8013274 100644 (file)
@@ -27,7 +27,7 @@ sub msg_html {
        my ($ctx, $mime, $footer) = @_;
        my $hdr = $mime->header_obj;
        my $tip = _msg_html_prepare($hdr, $ctx);
        my ($ctx, $mime, $footer) = @_;
        my $hdr = $mime->header_obj;
        my $tip = _msg_html_prepare($hdr, $ctx);
-       PublicInbox::WwwStream->new($ctx, sub {
+       PublicInbox::WwwStream->response($ctx, 200, sub {
                my ($nr, undef) = @_;
                if ($nr == 1) {
                        $tip . multipart_text_as_html($mime, '') .
                my ($nr, undef) = @_;
                if ($nr == 1) {
                        $tip . multipart_text_as_html($mime, '') .
@@ -278,7 +278,7 @@ sub stream_thread ($$) {
        $mime = Email::MIME->new($mime);
        $ctx->{-title_html} = ascii_html($mime->header('Subject'));
        $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
        $mime = Email::MIME->new($mime);
        $ctx->{-title_html} = ascii_html($mime->header('Subject'));
        $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
-       my $body = PublicInbox::WwwStream->new($ctx, sub {
+       PublicInbox::WwwStream->response($ctx, 200, sub {
                return unless $ctx;
                while (@q) {
                        $level = shift @q;
                return unless $ctx;
                while (@q) {
                        $level = shift @q;
@@ -297,7 +297,6 @@ sub stream_thread ($$) {
                $ctx = undef;
                $ret;
        });
                $ctx = undef;
                $ret;
        });
-       [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
 }
 
 sub thread_html {
 }
 
 sub thread_html {
@@ -339,7 +338,7 @@ sub thread_html {
        $ctx->{-title_html} = ascii_html($mime->header('Subject'));
        $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
        $mime = undef;
        $ctx->{-title_html} = ascii_html($mime->header('Subject'));
        $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
        $mime = undef;
-       my $body = PublicInbox::WwwStream->new($ctx, sub {
+       PublicInbox::WwwStream->response($ctx, 200, sub {
                return unless $msgs;
                while ($mime = shift @$msgs) {
                        $mid = mid_clean(mid_mime($mime));
                return unless $msgs;
                while ($mime = shift @$msgs) {
                        $mid = mid_clean(mid_mime($mime));
@@ -352,7 +351,6 @@ sub thread_html {
                $msgs = undef;
                '</pre>'.$skel;
        });
                $msgs = undef;
                '</pre>'.$skel;
        });
-       [ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
 }
 
 sub multipart_text_as_html {
 }
 
 sub multipart_text_as_html {
index cbd3142d1c62c256766ac0fe842486f1c164364a..c4509bd95649ae2f8caa31c26600ef5b1bb0d2bf 100644 (file)
@@ -236,8 +236,7 @@ sub get_mid_html {
        require Email::MIME;
        my $mime = Email::MIME->new($x);
        searcher($ctx);
        require Email::MIME;
        my $mime = Email::MIME->new($x);
        searcher($ctx);
-       [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
-         PublicInbox::View::msg_html($ctx, $mime, $foot) ];
+       PublicInbox::View::msg_html($ctx, $mime, $foot);
 }
 
 # /$INBOX/$MESSAGE_ID/t/
 }
 
 # /$INBOX/$MESSAGE_ID/t/
index d2bf318bed49a6027e68f7a696a91de1ff29de61..6de1b310da360bbbee5bcbd580b7d552bc2b8298 100644 (file)
@@ -14,6 +14,12 @@ sub new {
        bless { nr => 0, cb => $cb, ctx => $ctx }, $class;
 }
 
        bless { nr => 0, cb => $cb, ctx => $ctx }, $class;
 }
 
+sub response {
+       my ($class, $ctx, $code, $cb) = @_;
+       [ $code, [ 'Content-Type', 'text/html; charset=UTF-8' ],
+         $class->new($ctx, $cb) ]
+}
+
 sub _html_top ($) {
        my ($self) = @_;
        my $ctx = $self->{ctx};
 sub _html_top ($) {
        my ($self) = @_;
        my $ctx = $self->{ctx};
index 8a898febc3f983433ad3d78c692fafbfa669fe87..4fdd15180cc45983630c6f2aa4f2b34f519f4704 100644 (file)
--- a/t/view.t
+++ b/t/view.t
@@ -34,7 +34,8 @@ sub msg_html ($) {
        my ($mime) = @_;
 
        my $s = '';
        my ($mime) = @_;
 
        my $s = '';
-       my $body = PublicInbox::View::msg_html($ctx, $mime);
+       my $r = PublicInbox::View::msg_html($ctx, $mime);
+       my $body = $r->[2];
        while (defined(my $buf = $body->getline)) {
                $s .= $buf;
        }
        while (defined(my $buf = $body->getline)) {
                $s .= $buf;
        }