simplegit

git: forward Content-Encoding/Git-Protocol to http-backend

A shallow fetch's upload-pack request grows by one boundary line per
sync and crosses 1KB after enough of them; git's HTTP transport then
gzips it (remote-curl.c: 1024 < rpc->len). ServeBackend built the CGI
env by hand and forwarded only Content-Type/Length, so http-backend
parsed the gzip bytes as pkt-lines and returned an empty body
("bad line length character"), stalling shallow mirrors with "the
remote end hung up unexpectedly".

Forward Content-Encoding (-> HTTP_CONTENT_ENCODING) and Git-Protocol
(-> HTTP_GIT_PROTOCOL, which also enables protocol v2), the two
headers http-backend reads as CGI vars. Regression test fails on the
old code with the exact production error.

f4d054b6dac98f495f4f0b189bd0c073c94c639b
cjosie <administrator@josie-c.com> · 2026-10-16T18:12 · browse files at this commit

parents: bc57e91

diff --git a/internal/git/backend.go b/internal/git/backend.go
index 2fcf6dd..e51c666 100644
--- a/internal/git/backend.go
+++ b/internal/git/backend.go
@@ -41,6 +41,20 @@ func ServeBackend(projectRoot, remoteUser, service string, req *http.Request, w
 		"LC_ALL=C",
 		"PATH=" + os.Getenv("PATH"),
 	}
+	// http-backend reads two request headers as CGI HTTP_* variables:
+	// Content-Encoding, so it gunzips a compressed request body (git's HTTP
+	// transport gzips any upload-pack request over 1KB, which a shallow
+	// fetch's growing boundary list reaches after enough syncs), and
+	// Git-Protocol, which selects protocol v2. Without Content-Encoding the
+	// CGI parses gzip bytes as pkt-lines and the fetch dies as "bad line
+	// length character"; the response is empty and git reports the remote
+	// hung up.
+	if enc := req.Header.Get("Content-Encoding"); enc != "" {
+		env = append(env, "HTTP_CONTENT_ENCODING="+enc)
+	}
+	if proto := req.Header.Get("Git-Protocol"); proto != "" {
+		env = append(env, "HTTP_GIT_PROTOCOL="+proto)
+	}
 	if remoteUser != "" {
 		env = append(env, "REMOTE_USER="+remoteUser)
 	}
diff --git a/internal/git/backend_test.go b/internal/git/backend_test.go
index 55246ef..c4da170 100644
--- a/internal/git/backend_test.go
+++ b/internal/git/backend_test.go
@@ -1,6 +1,9 @@
 package git
 
 import (
+	"bytes"
+	"compress/gzip"
+	"fmt"
 	"net/http/httptest"
 	"os"
 	"path/filepath"
@@ -35,6 +38,56 @@ func TestServeBackendAdvertisement(t *testing.T) {
 	}
 }
 
+// pktLine frames payload as a git pkt-line (4 hex length bytes + payload).
+func pktLine(payload string) string {
+	return fmt.Sprintf("%04x%s", len(payload)+4, payload)
+}
+
+// TestServeBackendDecompressesGzipRequest guards the Content-Encoding
+// forwarding fix: git's HTTP transport gzips an upload-pack request once it
+// exceeds 1KB (remote-curl.c), which a shallow fetch's accumulated boundary
+// list reaches after enough syncs. ServeBackend used to drop Content-Encoding,
+// so http-backend parsed the gzip bytes as pkt-lines and returned an empty
+// body ("bad line length character" in the log). A shallow mirror then stopped
+// syncing with "the remote end hung up unexpectedly".
+func TestServeBackendDecompressesGzipRequest(t *testing.T) {
+	root := t.TempDir()
+	repoPath := filepath.Join(root, "josie", "demo.git")
+	if err := InitBare(repoPath, "main"); err != nil {
+		t.Fatalf("InitBare: %v", err)
+	}
+	seedBare(t, repoPath)
+	tip, err := ResolveCommit(repoPath, "main")
+	if err != nil {
+		t.Fatalf("ResolveCommit: %v", err)
+	}
+
+	body := pktLine("want "+tip+" multi_ack_detailed no-done side-band-64k thin-pack ofs-delta\n") +
+		"0000" + pktLine("done\n")
+	var gz bytes.Buffer
+	zw := gzip.NewWriter(&gz)
+	if _, err := zw.Write([]byte(body)); err != nil {
+		t.Fatalf("gzip: %v", err)
+	}
+	if err := zw.Close(); err != nil {
+		t.Fatalf("gzip close: %v", err)
+	}
+
+	req := httptest.NewRequest("POST", "/josie/demo.git/git-upload-pack", &gz)
+	req.Header.Set("Content-Type", "application/x-git-upload-pack-request")
+	req.Header.Set("Content-Encoding", "gzip")
+	rec := httptest.NewRecorder()
+	if err := ServeBackend(root, "", "git-upload-pack", req, rec); err != nil {
+		t.Fatalf("ServeBackend: %v", err)
+	}
+	if rec.Code != 200 {
+		t.Fatalf("status = %d, want 200 (body %q)", rec.Code, rec.Body)
+	}
+	if rec.Body.Len() == 0 {
+		t.Fatal("empty response: gzipped upload-pack request was not decompressed")
+	}
+}
+
 func TestServeBackendUnknownRepo(t *testing.T) {
 	root := t.TempDir()
 	if err := os.MkdirAll(filepath.Join(root, "josie"), 0o755); err != nil {