shithub: riscv

Download patch

ref: 8bd5be7c707b979caa277637cadf356b78c2edb5
parent: 2e47badb88312c5c045a8042dc2ef80148e5ab47
author: Michael Forney <mforney@mforney.org>
date: Wed Mar 16 21:41:09 EDT 2022

git/fetch: improve detection of dumb http protocol

If the server only supports the dumb protocol, the first 4 bytes of
response will be the initial part of the hash of the first ref.

The http-protocol documentation says that we should fall back to the
dumb protocol when we don't see a content-type of
application/x-$servicename-advertisement.  Check this before
attempting to read a smart git packet.

--- a/sys/src/cmd/git/proto.c
+++ b/sys/src/cmd/git/proto.c
@@ -220,14 +220,27 @@
 issmarthttp(Conn *c, char *direction)
 {
 	char buf[Pktmax+1], svc[128];
-	int n;
+	int fd, n;
 
+	if((fd = webopen(c, "contenttype", OREAD)) == -1)
+		return -1;
+	n = readn(fd, buf, sizeof(buf) - 1);
+	close(fd);
+	if(n == -1)
+		return -1;
+	buf[n] = '\0';
+	snprint(svc, sizeof(svc), "application/x-git-%s-pack-advertisement", direction);
+	if(strcmp(svc, buf) != 0){
+		werrstr("dumb http protocol not supported");
+		return -1;
+	}
+
 	if((n = readpkt(c, buf, sizeof(buf))) == -1)
 		sysfatal("http read: %r");
 	buf[n] = 0;
 	snprint(svc, sizeof(svc), "# service=git-%s-pack\n", direction);
 	if(strncmp(svc, buf, n) != 0){
-		werrstr("dumb http protocol not supported");
+		werrstr("invalid initial packet line");
 		return -1;
 	}
 	if(readpkt(c, buf, sizeof(buf)) != 0){