Commit 1c60885c authored by Jacob Vosmaer's avatar Jacob Vosmaer

Set content-length when sending git blobs

This gives the HTTP client receiving the blob a way to detect some
transmission failures.
parent ab7a52c5
......@@ -7,6 +7,7 @@ import (
"io"
"log"
"net/http"
"strings"
)
type blob struct{ senddata.Prefix }
......@@ -23,10 +24,16 @@ func (b *blob) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
log.Printf("SendBlob: sending %q for %q", params.BlobId, r.URL.Path)
sizeOutput, err := gitCommand("", "git", "--git-dir="+params.RepoPath, "cat-file", "-s", params.BlobId).Output()
if err != nil {
helper.Fail500(w, fmt.Errorf("SendBlob: get blob size: %v", err))
return
}
gitShowCmd := gitCommand("", "git", "--git-dir="+params.RepoPath, "cat-file", "blob", params.BlobId)
stdout, err := gitShowCmd.StdoutPipe()
if err != nil {
helper.Fail500(w, fmt.Errorf("SendBlob: git stdout: %v", err))
helper.Fail500(w, fmt.Errorf("SendBlob: git cat-file stdout: %v", err))
return
}
if err := gitShowCmd.Start(); err != nil {
......@@ -35,8 +42,7 @@ func (b *blob) Inject(w http.ResponseWriter, r *http.Request, sendData string) {
}
defer helper.CleanUpProcessGroup(gitShowCmd)
// Ignore incorrect Content-Length that may have been set by Rails
w.Header().Del("Content-Length")
w.Header().Set("Content-Length", strings.TrimSpace(string(sizeOutput)))
if _, err := io.Copy(w, stdout); err != nil {
helper.LogError(fmt.Errorf("SendBlob: copy git cat-file stdout: %v", err))
return
......
......@@ -590,6 +590,9 @@ func TestGetGitBlob(t *testing.T) {
if len(body) != blobLength {
t.Fatalf("Expected body of %d bytes, got %d", blobLength, len(body))
}
if cl := resp.Header.Get("Content-Length"); cl != fmt.Sprintf("%d", blobLength) {
t.Fatalf("Expected Content-Length %v, got %q", blobLength, cl)
}
if !strings.HasPrefix(string(body), "The MIT License (MIT)") {
t.Fatalf("Expected MIT license, got %q", body)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment