Skip to content

Commit 5a58016

Browse files
Jody McIntyrejosegonzalez
authored andcommitted
Use chunked encoding in push response
1 parent 5f54a8f commit 5a58016

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

git-http-backend.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ func serviceRpc(hr HandlerReq) {
126126
}
127127

128128
w.Header().Set("Content-Type", fmt.Sprintf("application/x-git-%s-result", rpc))
129+
w.Header().Set("Connection", "Keep-Alive")
130+
w.Header().Set("Transfer-Encoding", "chunked")
131+
w.Header().Set("X-Content-Type-Options", "nosniff")
129132
w.WriteHeader(http.StatusOK)
130133

131134
env := []string{}
@@ -177,7 +180,30 @@ func serviceRpc(hr HandlerReq) {
177180
}
178181
io.Copy(in, reader)
179182
in.Close()
180-
io.Copy(w, stdout)
183+
184+
flusher, ok := w.(http.Flusher)
185+
if !ok {
186+
panic("expected http.ResponseWriter to be an http.Flusher")
187+
}
188+
189+
p := make([]byte, 1024)
190+
for {
191+
n_read, err := stdout.Read(p)
192+
if err == io.EOF {
193+
break
194+
}
195+
n_write, err := w.Write(p[:n_read])
196+
if err != nil {
197+
fmt.Println(err)
198+
os.Exit(1)
199+
}
200+
if n_read != n_write {
201+
fmt.Printf("failed to write data: %d read, %d written\n", n_read, n_write)
202+
os.Exit(1)
203+
}
204+
flusher.Flush()
205+
}
206+
181207
cmd.Wait()
182208
}
183209

0 commit comments

Comments
 (0)