user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 13/13] www_stream: add response wrapper sub
Date: Thu, 30 Jun 2016 09:21:43 +0000	[thread overview]
Message-ID: <20160630092143.31651-14-e@80x24.org> (raw)
In-Reply-To: <20160630092143.31651-1-e@80x24.org>

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       | 3 +--
 lib/PublicInbox/SearchView.pm | 4 +---
 lib/PublicInbox/View.pm       | 8 +++-----
 lib/PublicInbox/WWW.pm        | 3 +--
 lib/PublicInbox/WwwStream.pm  | 6 ++++++
 t/view.t                      | 3 ++-
 6 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index c16c417..2f141c4 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -48,7 +48,7 @@ sub new_html {
 	}
 	$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;
@@ -58,7 +58,6 @@ sub new_html {
 		}
 		undef;
 	});
-	[ 200, ['Content-Type', 'text/html; charset=UTF-8'], $res ]
 }
 
 # private subs
diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm
index 4af6cad..30a310c 100644
--- a/lib/PublicInbox/SearchView.pm
+++ b/lib/PublicInbox/SearchView.pm
@@ -55,9 +55,7 @@ sub sres_top_html {
 			$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
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index b4f80d1..27dd155 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -27,7 +27,7 @@ sub msg_html {
 	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, '') .
@@ -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);
-	my $body = PublicInbox::WwwStream->new($ctx, sub {
+	PublicInbox::WwwStream->response($ctx, 200, sub {
 		return unless $ctx;
 		while (@q) {
 			$level = shift @q;
@@ -297,7 +297,6 @@ sub stream_thread ($$) {
 		$ctx = undef;
 		$ret;
 	});
-	[ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
 }
 
 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;
-	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));
@@ -352,7 +351,6 @@ sub thread_html {
 		$msgs = undef;
 		'</pre>'.$skel;
 	});
-	[ 200, ['Content-Type', 'text/html; charset=UTF-8'], $body ];
 }
 
 sub multipart_text_as_html {
diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm
index cbd3142..c4509bd 100644
--- a/lib/PublicInbox/WWW.pm
+++ b/lib/PublicInbox/WWW.pm
@@ -236,8 +236,7 @@ sub get_mid_html {
 	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/
diff --git a/lib/PublicInbox/WwwStream.pm b/lib/PublicInbox/WwwStream.pm
index d2bf318..6de1b31 100644
--- a/lib/PublicInbox/WwwStream.pm
+++ b/lib/PublicInbox/WwwStream.pm
@@ -14,6 +14,12 @@ sub new {
 	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};
diff --git a/t/view.t b/t/view.t
index 8a898fe..4fdd151 100644
--- a/t/view.t
+++ b/t/view.t
@@ -34,7 +34,8 @@ sub msg_html ($) {
 	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;
 	}
-- 
EW


      parent reply	other threads:[~2016-06-30  9:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-30  9:21 [PATCH 0/13] www: hybrid flat+thread conversation view Eric Wong
2016-06-30  9:21 ` [PATCH 01/13] www: implement " Eric Wong
2016-06-30  9:21 ` [PATCH 02/13] www: use WwwStream for dumping thread and search views Eric Wong
2016-06-30  9:21 ` [PATCH 03/13] view: show thread context in the thread-aware flat view Eric Wong
2016-06-30  9:21 ` [PATCH 04/13] view: merge $state hash with existing $ctx Eric Wong
2016-06-30  9:21 ` [PATCH 05/13] feed: add $INBOX/new.html endpoint Eric Wong
2016-06-30  9:21 ` [PATCH 06/13] view: tweak thread/index header slightly Eric Wong
2016-06-30  9:21 ` [PATCH 07/13] view: show more nearby messages in flat thread view Eric Wong
2016-06-30  9:21 ` [PATCH 08/13] www: reinstate old thread view as an option Eric Wong
2016-06-30  9:21 ` [PATCH 09/13] view: fix up some HTML injection via Message-ID vectors Eric Wong
2016-06-30 19:03   ` [PATCH 14/13] view: fix permalink and raw links at the top Eric Wong
2016-06-30  9:21 ` [PATCH 10/13] view: default to flat/hybrid thread display Eric Wong
2016-06-30  9:21 ` [PATCH 11/13] view: show thread size when linking to summary Eric Wong
2016-06-30  9:21 ` [PATCH 12/13] view: fixup bad reference to new_msgid Eric Wong
2016-06-30  9:21 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160630092143.31651-14-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).