user/dev discussion of public-inbox itself
 help / color / 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	[flat|nested] only message in thread

only message in thread, back to index

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

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror http://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.org/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox