Commit 23a16410 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Suppress upload-pack Wait() error on shallow clone

parent c3d62d2b
......@@ -5,11 +5,13 @@ In this file we handle the Git 'smart HTTP' protocol
package git
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
......@@ -101,6 +103,8 @@ func handleGetInfoRefs(w http.ResponseWriter, r *http.Request, a *api.Response)
func handlePostRPC(w http.ResponseWriter, r *http.Request, a *api.Response) {
var err error
var body io.Reader
var isShallowClone bool
// Get Git action from URL
action := filepath.Base(r.URL.Path)
......@@ -110,6 +114,25 @@ func handlePostRPC(w http.ResponseWriter, r *http.Request, a *api.Response) {
return
}
if action == "git-upload-pack" {
buffer := &bytes.Buffer{}
if _, err := io.Copy(buffer, r.Body); err != nil {
helper.Fail500(w, r, &copyError{fmt.Errorf("handlePostRPC: buffer git-upload-pack body: %v")})
return
}
isShallowClone, err = scanDeepen(bytes.NewReader(buffer.Bytes()))
body = buffer
if err != nil {
// Do not pass on the error: our failure to parse the
// request body should not abort the request.
helper.LogError(r, fmt.Errorf("parseBody: %v", err))
}
} else {
body = r.Body
}
// Prepare our Git subprocess
cmd := gitCommand(a.GL_ID, "git", subCommand(action), "--stateless-rpc", a.RepoPath)
stdout, err := cmd.StdoutPipe()
......@@ -131,7 +154,7 @@ func handlePostRPC(w http.ResponseWriter, r *http.Request, a *api.Response) {
defer helper.CleanUpProcessGroup(cmd) // Ensure brute force subprocess clean-up
// Write the client request body to Git's standard input
if _, err := io.Copy(stdin, r.Body); err != nil {
if _, err := io.Copy(stdin, body); err != nil {
helper.Fail500(w, r, fmt.Errorf("handlePostRPC: write to %v: %v", cmd.Args, err))
return
}
......@@ -155,22 +178,17 @@ func handlePostRPC(w http.ResponseWriter, r *http.Request, a *api.Response) {
)
return
}
if err := cmd.Wait(); err != nil {
if err := cmd.Wait(); err != nil && !(isExitError(err) && isShallowClone) {
helper.LogError(r, fmt.Errorf("handlePostRPC: wait for %v: %v", cmd.Args, err))
return
}
}
func subCommand(rpc string) string {
return strings.TrimPrefix(rpc, "git-")
func isExitError(err error) bool {
_, ok := err.(*exec.ExitError)
return ok
}
func pktLine(w io.Writer, s string) error {
_, err := fmt.Fprintf(w, "%04x%s", len(s)+4, s)
return err
}
func pktFlush(w io.Writer) error {
_, err := fmt.Fprint(w, "0000")
return err
func subCommand(rpc string) string {
return strings.TrimPrefix(rpc, "git-")
}
package git
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
)
func pktLine(w io.Writer, s string) error {
_, err := fmt.Fprintf(w, "%04x%s", len(s)+4, s)
return err
}
func pktFlush(w io.Writer) error {
_, err := fmt.Fprint(w, "0000")
return err
}
func scanDeepen(body io.Reader) (bool, error) {
hasDeepen := false
scanner := bufio.NewScanner(body)
scanner.Split(pktLineSplitter)
for scanner.Scan() {
if bytes.HasPrefix(scanner.Bytes(), []byte("deepen")) {
hasDeepen = true
break
}
}
return hasDeepen, scanner.Err()
}
func pktLineSplitter(data []byte, atEOF bool) (advance int, token []byte, err error) {
if len(data) < 4 {
if atEOF && len(data) > 0 {
return 0, nil, fmt.Errorf("pktLineSplitter: incomplete length prefix on %q", data)
}
return 0, nil, nil // want more data
}
if bytes.HasPrefix(data, []byte("0000")) {
// special case: "0000" terminator packet: return empty token
return 4, data[4:4], nil
}
// We have at least 4 bytes available so we can decode the 4-hex digit
// length prefix of the packet line.
pktLength64, err := strconv.ParseInt(string(data[:4]), 16, 0)
if err != nil {
return 0, nil, fmt.Errorf("pktLineSplitter: decode length: %v", err)
}
pktLength := int(pktLength64)
if pktLength < 0 {
return 0, nil, fmt.Errorf("pktLineSplitter: invalid length: %d", pktLength)
}
if len(data) < pktLength {
if atEOF {
return 0, nil, fmt.Errorf("pktLineSplitter: less than %d bytes in input %q", pktLength, data)
}
return 0, nil, nil // want more data
}
return pktLength, data[4:pktLength], nil
}
package git
import (
"bytes"
"testing"
)
func TestSuccessfulScanDeepen(t *testing.T) {
examples := []struct {
input string
output bool
}{
{"000dsomething000cdeepen 10000", true},
{"000dsomething0000000cdeepen 1", true},
{"000dsomething0000", false},
}
for _, example := range examples {
ok, err := scanDeepen(bytes.NewReader([]byte(example.input)))
if err != nil {
t.Fatalf("error scanning %q: %v", example.input, err)
}
if ok != example.output {
t.Fatalf("scanDeepen %q: expected %v, got %v", example.input, example.output, ok)
}
}
}
func TestFailedScanDeepen(t *testing.T) {
examples := []string{
"invalid data",
"deepen",
"000cdeepen",
}
for _, example := range examples {
ok, err := scanDeepen(bytes.NewReader([]byte(example)))
if err == nil {
t.Fatalf("expected error scanning %q", example)
}
t.Log(err)
if ok == true {
t.Fatalf("scanDeepen %q: expected result to be false, got true", example)
}
}
}
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