unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
* [PATCH] githttpbackend: support Last-Modified and If-Modified-Since
@ 2019-05-04 22:23 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2019-05-04 22:23 UTC (permalink / raw)
  To: meta

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2019-05-04 22:23 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-05-04 22:23 [PATCH] githttpbackend: support Last-Modified and If-Modified-Since Eric Wong

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).