Commit be49aebf authored by Kamil Trzcinski's avatar Kamil Trzcinski

Remove RPC

parent 359219c1
......@@ -14,15 +14,31 @@ import (
"os/exec"
"path"
"time"
"path/filepath"
"errors"
)
func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
var format string
switch filepath.Base(r.URL.Path) {
case "archive.zip":
format = "zip"
case "archive.tar":
format = "tar"
case "archive", "archive.tar.gz":
format = "tar.gz"
case "archive.tar.bz2":
format = "tar.bz2"
default:
fail500(w, "handleGetArchive", errors.New("invalid archive format"))
}
archiveFilename := path.Base(r.ArchivePath)
if cachedArchive, err := os.Open(r.ArchivePath); err == nil {
defer cachedArchive.Close()
log.Printf("Serving cached file %q", r.ArchivePath)
setArchiveHeaders(w, r.rpc, archiveFilename)
setArchiveHeaders(w, format, archiveFilename)
// Even if somebody deleted the cachedArchive from disk since we opened
// the file, Unix file semantics guarantee we can still read from the
// open file in this process.
......@@ -41,7 +57,7 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
defer tempFile.Close()
defer os.Remove(tempFile.Name())
compressCmd, archiveFormat := parseArchiveFormat(r.rpc)
compressCmd, archiveFormat := parseArchiveFormat(format)
archiveCmd := gitCommand("", "git", "--git-dir="+r.RepoPath, "archive", "--format="+archiveFormat, "--prefix="+r.ArchivePrefix+"/", r.CommitId)
archiveStdout, err := archiveCmd.StdoutPipe()
......@@ -82,7 +98,7 @@ func handleGetArchive(w http.ResponseWriter, r *gitRequest) {
archiveReader := io.TeeReader(stdout, tempFile)
// Start writing the response
setArchiveHeaders(w, r.rpc, archiveFilename)
setArchiveHeaders(w, format, archiveFilename)
w.WriteHeader(200) // Don't bother with HTTP 500 from this point on, just return
if _, err := io.Copy(w, archiveReader); err != nil {
logContext("handleGetArchive read from subprocess", err)
......
......@@ -10,6 +10,7 @@ import (
"io"
"net/http"
"strings"
"path/filepath"
)
func handleGetInfoRefs(w http.ResponseWriter, r *gitRequest) {
......@@ -60,6 +61,14 @@ func handlePostRPC(w http.ResponseWriter, r *gitRequest) {
var body io.ReadCloser
var err error
// Get Git action from URL
action := filepath.Base(r.URL.Path)
if !(action == "git-upload-pack" || action == "git-receive-pack") {
// The 'dumb' Git HTTP protocol is not supported
fail500(w, "handlePostRPC", err)
return
}
// The client request body may have been gzipped.
if r.Header.Get("Content-Encoding") == "gzip" {
body, err = gzip.NewReader(r.Body)
......@@ -73,7 +82,7 @@ func handlePostRPC(w http.ResponseWriter, r *gitRequest) {
defer body.Close()
// Prepare our Git subprocess
cmd := gitCommand(r.GL_ID, "git", subCommand(r.rpc), "--stateless-rpc", r.RepoPath)
cmd := gitCommand(r.GL_ID, "git", subCommand(action), "--stateless-rpc", r.RepoPath)
stdout, err := cmd.StdoutPipe()
if err != nil {
fail500(w, "handlePostRPC", err)
......@@ -108,7 +117,7 @@ func handlePostRPC(w http.ResponseWriter, r *gitRequest) {
body.Close()
// Start writing the response
w.Header().Add("Content-Type", fmt.Sprintf("application/x-%s-result", r.rpc))
w.Header().Add("Content-Type", fmt.Sprintf("application/x-%s-result", action))
w.Header().Add("Cache-Control", "no-cache")
w.WriteHeader(200) // Don't bother with HTTP 500 from this point on, just return
......
......@@ -26,7 +26,6 @@ type gitService struct {
method string
regex *regexp.Regexp
handleFunc serviceHandleFunc
rpc string
}
type authorizationResponse struct {
......@@ -53,20 +52,19 @@ type gitRequest struct {
*http.Request
authorizationResponse
u *upstream
rpc string
}
// Routing table
var gitServices = [...]gitService{
gitService{"GET", regexp.MustCompile(`/info/refs\z`), repoPreAuthorizeHandler(handleGetInfoRefs), ""},
gitService{"POST", regexp.MustCompile(`/git-upload-pack\z`), repoPreAuthorizeHandler(handlePostRPC), "git-upload-pack"},
gitService{"POST", regexp.MustCompile(`/git-receive-pack\z`), repoPreAuthorizeHandler(handlePostRPC), "git-receive-pack"},
gitService{"GET", regexp.MustCompile(`/repository/archive\z`), repoPreAuthorizeHandler(handleGetArchive), "tar.gz"},
gitService{"GET", regexp.MustCompile(`/repository/archive.zip\z`), repoPreAuthorizeHandler(handleGetArchive), "zip"},
gitService{"GET", regexp.MustCompile(`/repository/archive.tar\z`), repoPreAuthorizeHandler(handleGetArchive), "tar"},
gitService{"GET", regexp.MustCompile(`/repository/archive.tar.gz\z`), repoPreAuthorizeHandler(handleGetArchive), "tar.gz"},
gitService{"GET", regexp.MustCompile(`/repository/archive.tar.bz2\z`), repoPreAuthorizeHandler(handleGetArchive), "tar.bz2"},
gitService{"GET", regexp.MustCompile(`/uploads/`), handleSendFile, ""},
gitService{"GET", regexp.MustCompile(`/info/refs\z`), repoPreAuthorizeHandler(handleGetInfoRefs)},
gitService{"POST", regexp.MustCompile(`/git-upload-pack\z`), repoPreAuthorizeHandler(handlePostRPC)},
gitService{"POST", regexp.MustCompile(`/git-receive-pack\z`), repoPreAuthorizeHandler(handlePostRPC)},
gitService{"GET", regexp.MustCompile(`/repository/archive\z`), repoPreAuthorizeHandler(handleGetArchive)},
gitService{"GET", regexp.MustCompile(`/repository/archive.zip\z`), repoPreAuthorizeHandler(handleGetArchive)},
gitService{"GET", regexp.MustCompile(`/repository/archive.tar\z`), repoPreAuthorizeHandler(handleGetArchive)},
gitService{"GET", regexp.MustCompile(`/repository/archive.tar.gz\z`), repoPreAuthorizeHandler(handleGetArchive)},
gitService{"GET", regexp.MustCompile(`/repository/archive.tar.bz2\z`), repoPreAuthorizeHandler(handleGetArchive)},
gitService{"GET", regexp.MustCompile(`/uploads/`), handleSendFile},
}
func newUpstream(authBackend string, authTransport http.RoundTripper) *upstream {
......@@ -96,7 +94,6 @@ func (u *upstream) ServeHTTP(w http.ResponseWriter, r *http.Request) {
request := gitRequest{
Request: r,
u: u,
rpc: g.rpc,
}
g.handleFunc(w, &request)
......
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