From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.0 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 408BE20281; Tue, 23 May 2017 23:17:56 +0000 (UTC) Date: Tue, 23 May 2017 23:17:56 +0000 From: Eric Wong To: =?utf-8?B?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason Cc: meta@public-inbox.org Subject: [PATCH] searchview: retry queries if uri_unescape-able Message-ID: <20170523231756.GA20232@dcvr> References: <20170523184310.GB9543@dcvr> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20170523184310.GB9543@dcvr> List-Id: > Ævar Arnfjörð Bjarmason wrote: > > I don't know if it breaks anything else but calling uri_unescape() > > would work for these sort of msgids, but then of course searching for > > %2b wouldn't work, hrm... So it's now trying the query as-is first, then retrying the unescaped query if there's no results: ------8<------ Subject: [PATCH] searchview: retry queries if uri_unescape-able MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is possible to have double-escaped queries when copy and pasting into browsers, so try to help users work around this common error by automatically retrying after unescaping once. Of course, we must inform the user when doing this results in success, in case they really meant to search for a double-escaped term which resulted in nothing. Reported-by: Ævar Arnfjörð Bjarmason https://public-inbox.org/meta/CACBZZX5Gnow08r=0A1J_kt3a=zpGyMfvsqu8nAN7kacNnDm+dg@mail.gmail.com/ --- lib/PublicInbox/SearchView.pm | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm index 42bc648..f92790f 100644 --- a/lib/PublicInbox/SearchView.pm +++ b/lib/PublicInbox/SearchView.pm @@ -5,11 +5,12 @@ package PublicInbox::SearchView; use strict; use warnings; +use URI::Escape qw(uri_unescape uri_escape); use PublicInbox::SearchMsg; use PublicInbox::Hval qw/ascii_html/; use PublicInbox::View; use PublicInbox::WwwAtomStream; -use PublicInbox::MID qw(mid2path mid_mime mid_clean mid_escape); +use PublicInbox::MID qw(mid2path mid_mime mid_clean mid_escape MID_ESC); use PublicInbox::MIME; require PublicInbox::Git; require PublicInbox::SearchThread; @@ -29,19 +30,27 @@ sub sres_top_html { mset => 1, relevance => $q->{r}, }; - my ($mset, $total); + my ($mset, $total, $err, $cb); +retry: eval { $mset = $ctx->{srch}->query($q->{'q'}, $opts); $total = $mset->get_matches_estimated; }; - my $err = $@; + $err = $@; ctx_prepare($q, $ctx); - my $cb; if ($err) { $code = 400; $ctx->{-html_tip} = '
'.err_txt($ctx, $err).'

'; $cb = *noop; } elsif ($total == 0) { + if (defined($ctx->{-uxs_retried})) { + # undo retry damage: + $q->{'q'} = $ctx->{-uxs_retried}; + } elsif (index($q->{'q'}, '%') >= 0) { + $ctx->{-uxs_retried} = $q->{'q'}; + $q->{'q'} = uri_unescape($q->{'q'}); + goto retry; + } $code = 404; $ctx->{-html_tip} = "
\n[No results found]

"; $cb = *noop; @@ -49,7 +58,7 @@ sub sres_top_html { my $x = $q->{x}; return adump($_[0], $mset, $q, $ctx) if $x eq 'A'; - $ctx->{-html_tip} = search_nav_top($mset, $q) . "\n\n"; + $ctx->{-html_tip} = search_nav_top($mset, $q, $ctx) . "\n\n"; if ($x eq 't') { $cb = mset_thread($ctx, $mset, $q); } else { @@ -113,9 +122,22 @@ sub err_txt { } sub search_nav_top { - my ($mset, $q) = @_; + my ($mset, $q, $ctx) = @_; + + my $rv = '
';
+	my $initial_q = $ctx->{-uxs_retried};
+	if (defined $initial_q) {
+		my $rewritten = $q->{'q'};
+		utf8::decode($initial_q);
+		utf8::decode($rewritten);
+		$initial_q = ascii_html($initial_q);
+		$rewritten = ascii_html($rewritten);
+		$rv .= " Warning: Initial query:\n $initial_q\n";
+		$rv .= " returned no results, used:\n";
+		$rv .= " $rewritten\n instead\n\n";
+	}
 
-	my $rv = "
Search results ordered by [";
+	$rv .= 'Search results ordered by [';
 	if ($q->{r}) {
 		my $d = $q->qs_html(r => 0);
 		$rv .= qq{date|relevance};
-- 
(deployed on public-inbox.org and all .onions)