Skip to content

Chunked responses and env support #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 61 additions & 9 deletions git-http-backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ type Service struct {
}

type Config struct {
ProjectRoot string
GitBinPath string
UploadPack bool
ReceivePack bool
AuthPassEnvVar string
AuthUserEnvVar string
DefaultEnv string
ProjectRoot string
GitBinPath string
UploadPack bool
ReceivePack bool
}

type HandlerReq struct {
Expand All @@ -38,10 +41,13 @@ type HandlerReq struct {
}

var config Config = Config{
ProjectRoot: "/tmp",
GitBinPath: "/usr/bin/git",
UploadPack: true,
ReceivePack: true,
AuthPassEnvVar: "",
AuthUserEnvVar: "",
DefaultEnv: "",
ProjectRoot: "/tmp",
GitBinPath: "/usr/bin/git",
UploadPack: true,
ReceivePack: true,
}

var services = map[string]Service{
Expand All @@ -63,6 +69,9 @@ var (
)

func init() {
flag.StringVar(&config.AuthPassEnvVar, "auth_pass_env_var", config.AuthPassEnvVar, "set an env var to provide the basic auth pass as")
flag.StringVar(&config.AuthUserEnvVar, "auth_user_env_var", config.AuthUserEnvVar, "set an env var to provide the basic auth user as")
flag.StringVar(&config.DefaultEnv, "default_env", config.DefaultEnv, "set the default env")
flag.StringVar(&config.ProjectRoot, "project_root", config.ProjectRoot, "set project root")
flag.StringVar(&config.GitBinPath, "git_bin_path", config.GitBinPath, "set git bin path")
flag.StringVar(&address, "server_address", address, "set server address")
Expand Down Expand Up @@ -117,15 +126,35 @@ func serviceRpc(hr HandlerReq) {
}

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

env := []string{}

if config.DefaultEnv != "" {
env = append(env, config.DefaultEnv)
}

user, password, authok := r.BasicAuth()
if authok {
if config.AuthUserEnvVar != "" {
env = append(env, fmt.Sprintf("%s=%s", config.AuthUserEnvVar, user))
}
if config.AuthPassEnvVar != "" {
env = append(env, fmt.Sprintf("%s=%s", config.AuthPassEnvVar, password))
}
}

args := []string{rpc, "--stateless-rpc", dir}
cmd := exec.Command(config.GitBinPath, args...)
version := r.Header.Get("Git-Protocol")
if len(version) != 0 {
cmd.Env = append(os.Environ(), fmt.Sprintf("GIT_PROTOCOL=%s", version))
}
cmd.Dir = dir
cmd.Env = env
in, err := cmd.StdinPipe()
if err != nil {
log.Print(err)
Expand All @@ -151,7 +180,30 @@ func serviceRpc(hr HandlerReq) {
}
io.Copy(in, reader)
in.Close()
io.Copy(w, stdout)

flusher, ok := w.(http.Flusher)
if !ok {
panic("expected http.ResponseWriter to be an http.Flusher")
}

p := make([]byte, 1024)
for {
n_read, err := stdout.Read(p)
if err == io.EOF {
break
}
n_write, err := w.Write(p[:n_read])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if n_read != n_write {
fmt.Printf("failed to write data: %d read, %d written\n", n_read, n_write)
os.Exit(1)
}
flusher.Flush()
}

cmd.Wait()
}

Expand Down