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] githttpbackend: support Last-Modified and If-Modified-Since
Date: Sat,  4 May 2019 22:23:35 +0000	[thread overview]
Message-ID: <20190504222335.10286-1-e@80x24.org> (raw)

This can be used to optimized frequent polling over dumb HTTP.
I will try making git support dumb polling, one day:

https://public-inbox.org/git/20190502085055.34kkll2deowat6il@dcvr/
---
 lib/PublicInbox/GitHTTPBackend.pm | 14 ++++++++++++--
 t/httpd.t                         | 28 ++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/lib/PublicInbox/GitHTTPBackend.pm b/lib/PublicInbox/GitHTTPBackend.pm
index 57944a0..c69b693 100644
--- a/lib/PublicInbox/GitHTTPBackend.pm
+++ b/lib/PublicInbox/GitHTTPBackend.pm
@@ -84,9 +84,19 @@ sub static_result ($$$$) {
 	my ($env, $h, $f, $type) = @_;
 	return r(404) unless -f $f && -r _; # just in case it's a FIFO :P
 
-	# TODO: If-Modified-Since and Last-Modified?
+	my $mtime = (stat(_))[9];
+	my $last_mod = time2str($mtime);
+	if (my $ims = $env->{HTTP_IF_MODIFIED_SINCE}) {
+		# RFC2616 14.25 allows exact date comparisons
+		return [304, [], [] ] if $ims eq $last_mod;
+	}
+
 	open my $in, '<', $f or return r(404);
 	my $size = -s $in;
+	if ($mtime != (stat(_))[9]) {
+		# unlikely, not impossible for mtime to change before fstat
+		$last_mod = time2str((stat(_))[9]);
+	}
 	my $len = $size;
 	my $code = 200;
 	push @$h, 'Content-Type', $type;
@@ -97,7 +107,7 @@ sub static_result ($$$$) {
 			return [ 416, $h, [] ];
 		}
 	}
-	push @$h, 'Content-Length', $len;
+	push @$h, 'Content-Length', $len, 'Last-Modified', $last_mod;
 	my $n = 65536;
 	[ $code, $h, Plack::Util::inline_object(close => sub { close $in },
 		getline => sub {
diff --git a/t/httpd.t b/t/httpd.t
index 44df164..21daeb1 100644
--- a/t/httpd.t
+++ b/t/httpd.t
@@ -13,6 +13,7 @@ use File::Temp qw/tempdir/;
 use Cwd qw/getcwd/;
 use IO::Socket;
 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
+use PublicInbox::Spawn qw(which);
 require './t/common.perl';
 
 # FIXME: too much setup
@@ -93,6 +94,33 @@ EOF
 			"http://$host:$port/$group", "$tmpdir/dumb.git"),
 		0, 'clone successful');
 
+	my $curl = which('curl');
+	SKIP: {
+		skip('curl missing', 6) unless defined $curl;
+		my $ldesc = "$tmpdir/dumb.git/description";
+		my $rdesc = "$maindir/description";
+		utime 12, 34, $rdesc;
+
+		# initial retrieval
+		is(system(qw(curl -sSfR -z), $ldesc, '-o', $ldesc,
+				"http://$host:$port/$group/description"),
+			0, 'initial curl retrieval OK');
+		is((stat($ldesc))[9], (stat($rdesc))[9], 'curl set mtime');
+
+		# no modification
+		is(system(qw(curl -sSfR -z), $ldesc, '-o', "$ldesc.x",
+				"http://$host:$port/$group/description"),
+			0, 'curl 304 OK');
+		ok(!-f "$ldesc.x", 'no retrieval on 304');
+
+		# modification detected
+		utime 56, 78, $rdesc;
+		is(system(qw(curl -sSfR -z), $ldesc, '-o', $ldesc,
+				"http://$host:$port/$group/description"),
+			0, 'curl updated file OK');
+		is((stat($ldesc))[9], (stat($rdesc))[9], 'curl updates mtime');
+	};
+
 	ok(kill('TERM', $pid), 'killed httpd');
 	$pid = undef;
 	waitpid(-1, 0);
-- 
EW


                 reply	other threads:[~2019-05-04 22:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20190504222335.10286-1-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).