Commit ed976a2f authored by Jacob Vosmaer's avatar Jacob Vosmaer

Use regular expressions to match routes

parent 5e28545e
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"regexp"
"strings" "strings"
) )
...@@ -23,7 +24,7 @@ type gitHandler struct { ...@@ -23,7 +24,7 @@ type gitHandler struct {
type gitService struct { type gitService struct {
method string method string
suffix string regex *regexp.Regexp
handleFunc func(w http.ResponseWriter, r *gitRequest, rpc string) handleFunc func(w http.ResponseWriter, r *gitRequest, rpc string)
rpc string rpc string
} }
...@@ -51,14 +52,14 @@ type gitRequest struct { ...@@ -51,14 +52,14 @@ type gitRequest struct {
// Routing table // Routing table
var gitServices = [...]gitService{ var gitServices = [...]gitService{
gitService{"GET", "/info/refs", handleGetInfoRefs, ""}, gitService{"GET", regexp.MustCompile(`/info/refs\z`), handleGetInfoRefs, ""},
gitService{"POST", "/git-upload-pack", handlePostRPC, "git-upload-pack"}, gitService{"POST", regexp.MustCompile(`/git-upload-pack\z`), handlePostRPC, "git-upload-pack"},
gitService{"POST", "/git-receive-pack", handlePostRPC, "git-receive-pack"}, gitService{"POST", regexp.MustCompile(`/git-receive-pack\z`), handlePostRPC, "git-receive-pack"},
gitService{"GET", "/repository/archive", handleGetArchive, "tar.gz"}, gitService{"GET", regexp.MustCompile(`/repository/archive\z`), handleGetArchive, "tar.gz"},
gitService{"GET", "/repository/archive.zip", handleGetArchive, "zip"}, gitService{"GET", regexp.MustCompile(`/repository/archive.zip\z`), handleGetArchive, "zip"},
gitService{"GET", "/repository/archive.tar", handleGetArchive, "tar"}, gitService{"GET", regexp.MustCompile(`/repository/archive.tar\z`), handleGetArchive, "tar"},
gitService{"GET", "/repository/archive.tar.gz", handleGetArchive, "tar.gz"}, gitService{"GET", regexp.MustCompile(`/repository/archive.tar.gz\z`), handleGetArchive, "tar.gz"},
gitService{"GET", "/repository/archive.tar.bz2", handleGetArchive, "tar.bz2"}, gitService{"GET", regexp.MustCompile(`/repository/archive.tar.bz2\z`), handleGetArchive, "tar.bz2"},
} }
func newGitHandler(authBackend string, authTransport http.RoundTripper) *gitHandler { func newGitHandler(authBackend string, authTransport http.RoundTripper) *gitHandler {
...@@ -73,7 +74,7 @@ func (h *gitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -73,7 +74,7 @@ func (h *gitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Look for a matching Git service // Look for a matching Git service
foundService := false foundService := false
for _, g = range gitServices { for _, g = range gitServices {
if r.Method == g.method && strings.HasSuffix(r.URL.Path, g.suffix) { if r.Method == g.method && g.regex.MatchString(r.URL.Path) {
foundService = true foundService = true
break break
} }
......
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